This project demonstrates how to use TensorFlow Mobile on Android for handwritten digits classification from MNIST.
- Python 3.6, TensorFlow 1.8.0
- Android Studio 3.0, Gradle 4.1
Copy the mnist_optimized.pb
generated in Step 2 to /android/app/src/main/assets
, then build and run the app.
you can find it assets folder because it is already copied there
The Classifer creates a TensorFlowInferenceInterface from mnist_optimized.pb
. The TensorFlowInferenceInterface provides an interface for inference and performance summarization, which is included in the following library.
implementation "org.tensorflow:tensorflow-android:1.8.0"
Initialize the TensorFlowInferenceInterface.
private static final String MODEL_PATH = "file:///android_asset/mnist_optimized.pb";
c.inferenceInterface = new TensorFlowInferenceInterface(assetManager, modelpath);
The names of the input and output for the model come from our mnist.py training script
private static final String INPUT_NAME = "input";
private static final String OUTPUT_NAME = "output";
- To figure out the names of your input and output nodes is to import your TensorFlow model into TensorBoard and inspect it there.
We feed in the pixel data, run the classifier, then fetch the outputs. Those outputs are then sorted to get the one with the highest confidence (above a specified threshold), and shown to the user
- Copy the input data into TensorFlow.
inferenceInterface.feed(inputName, pixels, new long[]{inputSize * inputSize});
- Run the inference call.
inferenceInterface.run(outputNames);
- Copy the output Tensor back into the output array.
inferenceInterface.fetch(outputName, outputs);
- Find the best classifications.
for (int i = 0; i < outputs.length; ++i) {
<snip>
}
- I have took the pre-trained and pre-optimized model from nex3z
- The basic model architecture comes from tensorflow-mnist-tutorial.
- The official TensorFlow Mobile Android demo.
- The FingerPaint from Android API demo.