How to run TensorFlow Inference on Android Things for example classifying images?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 22:06:00

问题


Is TensorFlow supported on Android Things? Can TensorFlow Android samples be ported to run on Android Things? If so, what’s the simplest way to do it?


回答1:


The short answer: Yes, you can indeed run TensorFlow on embedded devices running Android Things such as Raspberry Pi 3. We have ported the TensorFlow image classification app to Android Things. It is available at: https://github.com/androidthings/sample-tensorflow-imageclassifier.

The long answer: In order to run TensorFlow you first need to build model graph and run training against large input dataset. Once that’s done, you run inference based on the trained model to make intelligent decisions. The first part in training is resources hungry and intensive, typically done beforehand while the inference part is relatively lightweight. It is the inference part that has been ported to run on devices running Android Things.

The following summarizes what you need to do when integrating TensorFlow into your Android Things project:

  • First, add the TensorFlow Android Inference library file into your project: Tensorflow-Android-Inference-alpha-debug.aar

  • Next, add a dependency in your app’s build.gradle file like this: compile(name: 'Tensorflow-Android-Inference-alpha-debug', ext: 'aar')

To utilize the TensorFlow Android Inference library, you instantiate an org.tensorflow.contrib.android.TensorFlowInferenceInterface instance in order to expose the following methods:

  • initializeTensorFlow: initialize TensorFlow object using a model graph as input
  • fillNodeFloat: copy input data into TensorFlow input array
  • runInference: run inference and save results in TensorFlow output array
  • readNodeFloat: read from TensorFlow output array and save into your own array

Check out the implementation in TensorFlowImageClassifier.java of the image classifier sample to see how you can pass inputs to TensorFlow, run inference, and then extract output labels from TensorFlow. For example in our image classification sample, our app can detect what breeds of dogs when shown an image of a dog.

The model graph in the sample is built using Google Inception V3 TensorFlow model with a training set of 1.2 million images from ImageNet. If you like to build your own model graph, be sure to update the model file, the label file, and input/output names accordingly in your classifier implementation.

In short the approach outlined above using TensorFlow Android Inference library as a gradle dependency offers a quick and easy way to add TensorFlow core functionalities into any Android projects, even complex projects such as the TensorFlow Android samples that have native code and require NDK integration. This approach also extends host platforms to those that currently do not have full Bazel support like Windows.



来源:https://stackoverflow.com/questions/42520805/how-to-run-tensorflow-inference-on-android-things-for-example-classifying-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!