TensorFlow on Android (Refer to codelabs : tensorflow-for-poets)

The google demo is good, but there are some error when you follow it, so I copied and update it.

Install TensorFlow As Normal

  1. pip install tensorflow==1.7.1

Download Google Demo

  1. git clone https://github.com/googlecodelabs/tensorflow-for-poets-2
  2. cd tensorflow-for-poets-2

Download TF_Files

  1. wget http://download.tensorflow.org/example_images/flower_photos.tgz
  2. tar -xvf flower_photos.tgz -C tf_files

Set ARCHITECTURE

  1. IMAGE_SIZE=224
  2. ARCHITECTURE="mobilenet_0.50_${IMAGE_SIZE}"

Start TensorBoard

  1. tensorboard --logdir tf_files/training_summaries &

Help : retraining script

  1. python -m scripts.retrain -h

Train

  1. python -m scripts.retrain \
  2.   --bottleneck_dir=tf_files/bottlenecks \
  3.   --how_many_training_steps=500 \
  4.   --model_dir=tf_files/models/ \
  5.   --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \
  6.   --output_graph=tf_files/retrained_graph.pb \
  7.   --output_labels=tf_files/retrained_labels.txt \
  8.   --architecture="${ARCHITECTURE}" \
  9.   --image_dir=tf_files/flower_photos

Train with default 4,000 iterations

  1. python -m scripts.retrain \
  2.   --bottleneck_dir=tf_files/bottlenecks \
  3.   --model_dir=tf_files/models/"${ARCHITECTURE}" \
  4.   --summaries_dir=tf_files/training_summaries/"${ARCHITECTURE}" \
  5.   --output_graph=tf_files/retrained_graph.pb \
  6.   --output_labels=tf_files/retrained_labels.txt \
  7.   --architecture="${ARCHITECTURE}" \
  8.   --image_dir=tf_files/flower_photos

Train with Your Own Data

--image_dir= The root folder of the subdirectories which is used as label names by the classification script
This image is copied from google’s codelabs !

The Retrained Model

  1. tf_files/retrained_graph.pb
  2. tf_files/retrained_labels.txt

Test

  1. (venv) water@water-G11CD:~/tensorflow-for-poets-2$ python -m scripts.label_image     --graph=tf_files/retrained_graph.pb      --image=tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg
  2.  
  3. 2019-01-23 14:13:50.234939: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
  4.  
  5. Evaluation time (1-image): 0.094s
  6.  
  7. daisy (score=0.99938)
  8. sunflowers (score=0.00033)
  9. dandelion (score=0.00028)
  10. roses (score=0.00000)
  11. tulips (score=0.00000)

Now, Prepare for Android

Install PILLOW

  1. pip install PILLOW

Convert the model to TFLite format

  1. IMAGE_SIZE=224
  2. toco   --input_file=tf_files/retrained_graph.pb   --output_file=tf_files/optimized_graph.lite   --input_format=TENSORFLOW_GRAPHDEF   --output_format=TFLITE   --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3   --input_array=input   --output_array=final_result   --inference_type=FLOAT   --input_data_type=FLOAT

The TFLite Model

  1. tf_files/optimized_graph.lite

Install AndroidStudio

  1. unzip android-studio-ide-182.5199772-linux.zip 
  2. cd android-studio/
  3. cd bin/
  4. ./studio.sh

Maybe You Need to Set Proxy

If you use lantern, you can do it like this!

Open Project

Open an existing Android Studio project

Choose android/tflite

Gradle Sync, YES

Build, Make Project

  1. android/tflite/app/build/outputs/apk/debug/app-debug.apk

Test on Your Phone

Add your model files to the project, and then Make Project, Test Again

  1. cp tf_files/optimized_graph.lite android/tflite/app/src/main/assets/graph.lite
  2. cp tf_files/retrained_labels.txt android/tflite/app/src/main/assets/labels.txt