This post documents my experience reproducing the results in blog https://heartbeat.fritz.ai/neural-networks-on-mobile-devices-with-tensorflow-lite-a-tutorial-85b41f53230c on an Android Phone. The tutorial is about developing an Andoid app for image classification using TensorFlow Lite.
My machine is Lenovo Ideapad Flex 5-1570 running Windows 10 Home.
-
Install Anaconda (Python 3.7 version) from Anaconda website (630MB).
-
Click Start Menu -> Anaconda Prompt to open terminal.
- In terminal, install TensorFlow by
pip install tensorflow
- In terminal, update NumPy to v1.6:
pip install numpy —upgrade
The numpy coming with TensorFlow is v1.15, which will cause error when training.
-
Install Git for Windows (if not yet).
- Git clone the code files.
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2
-
Download training images and unzip to the location designated in tutorial.
- Train mobinenet as instructed in tutorial.
python scripts\retrain.py --output_graph=tf_files\retrained_graph.pb --output_labels=tf_files\retrained_labels.txt--image_dir=tf_files\flower_photos --architecture=mobilenet_1.0_224 --summaries_dir tf_files\training_summaries\mobilenet_1.0_244
- Convert to TF Lite format
toco --graph_def_file=tf_files\retrained_graph.pb --output_file=tf_files\optimized_graph.lite --output_format=TFLITE --input_shape=1,224,224,3 --input_array=input --output_array=final_result --inference_type=FLOAT --inference_input_type=FLOAT --input_format=TENSORFLOW_GRAPHDEF
-
Download and install Android Studio (taking a while).
-
Follow instructions in tutorial to update the
graph.lite
andlabels.txt
files in folderandroid\tflite\app\src\main\assets
. -
Follow instructions in tutorial to open the project in
\tflite
folder. Fist time building the app, there will be a few errors when building the project. Just follow the error messages to delete a few lines. Build the APK file as instructed. - Connect a device (BLUE ADVANCE 5.5 HD in my case). Turn on the
Developer Options
by following instructions at https://developer.android.com/studio/debug/dev-options. Then I was able to run the demo app on the device.