{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Deploy the Pretrained Model on Android\n**Author**: [Tomohiro Kato](https://tkat0.github.io/)\n\nThis is an example of using Relay to compile a keras model and deploy it on Android device.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nimport numpy as np\nfrom PIL import Image\nimport keras\nfrom keras.applications.mobilenet_v2 import MobileNetV2\nimport tvm\nfrom tvm import te\nimport tvm.relay as relay\nfrom tvm import rpc\nfrom tvm.contrib import utils, ndk, graph_executor as runtime\nfrom tvm.contrib.download import download_testdata" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup Environment\nSince there are many required packages for Android, it is recommended to use the official Docker Image.\n\nFirst, to build and run Docker Image, we can run the following command.\n\n```bash\ngit clone --recursive https://github.com/apache/tvm tvm\ncd tvm\ndocker build -t tvm.demo_android -f docker/Dockerfile.demo_android ./docker\ndocker run --pid=host -h tvm -v $PWD:/workspace \\\n -w /workspace -p 9190:9190 --name tvm -it tvm.demo_android bash\n```\nYou are now inside the container. The cloned TVM directory is mounted on /workspace.\nAt this time, mount the 9190 port used by RPC described later.\n\n
Please execute the following steps in the container.\n We can execute :code:`docker exec -it tvm bash` to open a new terminal in the container.
At this time, don't forget to [create a standalone toolchain](https://github.com/apache/tvm/tree/main/apps/android_rpc#architecture-and-android-standalone-toolchain) .\n\n for example\n\n```bash\n$ANDROID_NDK_HOME/build/tools/make-standalone-toolchain.sh \\\n --platform=android-24 --use-llvm --arch=arm64 --install-dir=/opt/android-toolchain-arm64\nexport TVM_NDK_CC=/opt/android-toolchain-arm64/bin/aarch64-linux-android-g++