The mobile version of the PyTorch framework

There is no available binary distribution of PyTorch for mobile devices, so we need to build it from source code. We can do this in the same way as we compile its regular version but with additional CMake parameters to enable mobile mode. You also have to install the Android Native Development Kit (NDK), which includes an appropriate version of the C/C++ compiler and the Android native libraries that are required to build the application. The following code snippet shows how to use the command-line environment to check out PyTorch and build its Android mobile version:

cd /home/[USER]
git clone https://github.com/pytorch/pytorch.git
cd pytorch/
git checkout v1.2.0
git submodule update --init
export ANDROID_NDK=/home/[USER]/Android/Sdk/ndk/20.0.5594570
export ANDROID_ABI='armeabi-v7a'

/home/[USER]/pytorch/scripts/build_android.sh
-DBUILD_CAFFE2_MOBILE=OFF
-DBUILD_SHARED_LIBS=ON
-DCMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
-DPYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')

Here, we assumed that /home/[USER] is the user's home directory. The main requirement when it comes to building the mobile version of PyTorch is to declare the ANDROID_NDK environmental variable, which should point to the Android NDK installation directory. The simplest way to install Android development tools is to download the Android Studio IDE and use the SDK Manager tool from that. You can find the SDK Manager under the Tools | SDK Manager menu. You can use this manager to install appropriate Android SDK versions. You can install the corresponding NDKs by using the SDK Tools tab in the manager's window. You can also use this tab to install the CMake utility. The ANDROID_ABI environment variable can be used to specify the ARM CPU architecture's compatibility for the compiler to generate architecture-specific code. In this example, we used the armeabi-v7a architecture.

We used the build_android.sh script from the PyTorch source code distribution to build mobile PyTorch binaries. This script uses the CMake command internally, which is why it takes CMake parameter definitions as arguments. Notice that we passed the BUILD_CAFFE2_MOBILE=OFF parameter to disable building the mobile version of Caffe2, which is hard to use in the current version because the library is deprecated. The second important parameter we used was BUILD_SHARED_LIBS=ON, which enabled us to build shared libraries. We did this because the static versions of the mobile PyTorch 1.2 libraries can't be used now because of broken initialization functions. The other parameters that were configured were the Python installation paths for intermediate build code generation.

Now that we have the mobile PyTorch libraries, that is, libc10.so and libtorch.so, we can start developing the application. We are going to build a simple image classification application based on the ResNet-18 neural network architecture. This architecture has the smallest number of parameters within the ResNet networks family, so we can use it on devices with a low amount of memory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.147