Water's Home

Just another Life Style

0%

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

pip install tensorflow==1.7.1

Download Google Demo

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

Download TF_Files

wget http://download.tensorflow.org/example\_images/flower\_photos.tgz
tar -xvf flower_photos.tgz -C tf_files

Set ARCHITECTURE

IMAGE_SIZE=224
ARCHITECTURE=”mobilenet_0.50_${IMAGE_SIZE}”

Start TensorBoard

tensorboard –logdir tf_files/training_summaries &

Help : retraining script

python -m scripts.retrain -h

Train

python -m scripts.retrain \
–bottleneck_dir=tf_files/bottlenecks \
–how_many_training_steps=500 \
–model_dir=tf_files/models/ \
–summaries_dir=tf_files/training_summaries/“${ARCHITECTURE}” \
–output_graph=tf_files/retrained_graph.pb \
–output_labels=tf_files/retrained_labels.txt \
–architecture=”${ARCHITECTURE}” \
–image_dir=tf_files/flower_photos

Train with default 4,000 iterations

python -m scripts.retrain \
–bottleneck_dir=tf_files/bottlenecks \
–model_dir=tf_files/models/“${ARCHITECTURE}” \
–summaries_dir=tf_files/training_summaries/“${ARCHITECTURE}” \
–output_graph=tf_files/retrained_graph.pb \
–output_labels=tf_files/retrained_labels.txt \
–architecture=”${ARCHITECTURE}” \
–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

tf_files/retrained_graph.pb
tf_files/retrained_labels.txt

Test

(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

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

Evaluation time (1-image): 0.094s

daisy (score=0.99938)
sunflowers (score=0.00033)
dandelion (score=0.00028)
roses (score=0.00000)
tulips (score=0.00000)

Now, Prepare for Android

Install PILLOW

pip install PILLOW

Convert the model to TFLite format

IMAGE_SIZE=224
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

tf_files/optimized_graph.lite

Install AndroidStudio

unzip android-studio-ide-182.5199772-linux.zip
cd android-studio/
cd bin/
./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

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

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