Android TensorFlow MNIST Machine Learning Example
Forked from MindorksOpenSource/AndroidTensorFlowMNISTExample.
Changed things are:
Some deprecated or outdated function calls were changed.
Some things were removed or simplified.
How to build library files
Current libtensorflow_inference.so and libandroid_tensorflow_inference_java.jar were built with TensorFlow v1.0.1.
If you want to build them with the newer version of TensorFlow,
read this guide or do the following:
Build libtensorflow_inference.so
Clone the TensorFlow's repository and edit WORKSPACE file:
$ cd tensorflow
$ vi WORKSPACEEdit android_sdk_repository and android_ndk_repository section like this:
# Uncomment and update the paths in these entries to build the Android demo.
android_sdk_repository(
name = "androidsdk",
api_level = 23,
build_tools_version = "25.0.1",
# Replace with path to Android SDK on your system
path = "/Users/meinside/Documents/files/dev/android-sdk-macosx",
)
# Android NDK r12b is recommended (higher may cause issues with Bazel)
android_ndk_repository(
name="androidndk",
path="/Users/meinside/Downloads/android-ndk-r12b",
api_level=14) # This needs to be 14 or higher to compile TensorFlow.
Then run:
$ bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so \
--crosstool_top=//external:android/crosstool \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
--cpu=armeabi-v7aIf you need .so files for other platforms, replace --cpu=armeabi-v7a with the desired one:
--cpu=arm64--cpu=x86--cpu=x86_64- ...
Compiled file will be placed at bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so.
Copy it into app/src/main/jniLibs/{PLATFORM_NAME}/:
$ mkdir -p {APP_SRC_DIR}/app/src/main/jniLibs/arm64/
$ cp bazel-bin/tensorflow/contrib/android/libtensorflow_inference.so {APP_SRC_DIR}/app/src/main/jniLibs/arm64/Note: NDK version
At the time of writing, (2017-03-30)
both TensorFlow 1.0.0 and 1.0.1 fails to build libtensorflow_inference.so with latest NDK version(r13).
But I could build it with r12b.
Build libandroid_tensorflow_inference_java.jar
Run:
$ bazel build //tensorflow/contrib/android:android_tensorflow_inference_javathen copy bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar into app/libs/:
$ cp bazel-bin/tensorflow/contrib/android/libandroid_tensorflow_inference_java.jar {APP_SRC_DIR}/app/libs/How to train it yourself
Run mnist.py and overwrite generated mnist_model_graph.pb into app/src/main/assets/.
$ cd {APP_SRC_DIR}
$ python mnist.py
$ cp model/mnist_model_graph.pb app/src/main/assets/