Skip to content

Latest commit

 

History

History
114 lines (88 loc) · 2.46 KB

File metadata and controls

114 lines (88 loc) · 2.46 KB

TFLite

  1. Setup the docker container for Tensorflow 1.6
sudo docker run -it -d -v `pwd`:/notebooks tensorflow/tensorflow:1.6.0 /bin/bash
  1. List the dockers
sudo docker ps
  1. Get into the docker container
sudo docker exec -it (container-id) /bin/bash
  1. Test the already existing model
python -m scripts.label_image \
  --graph=tf_files/retrained_graph.pb  \
  --image=tf_files/flower_photos/daisy/3475870145_685a19116d.jpg
  1. Optimize your model using toco(TensorFlow Lite Optimizing Converter)
IMAGE_SIZE=224
toco \
  --input_file=tf_files/retrained_graph.pb \
  --output_file=tf_files/optimized_graph.pb \
  --input_format=TENSORFLOW_GRAPHDEF \
  --output_format=TENSORFLOW_GRAPHDEF \
  --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 \
  --input_array=input \
  --output_array=final_result
  1. Retest the existing model
  python -m scripts.label_image \
  --graph=tf_files/retrained_graph.pb\
  --image=tf_files/flower_photos/daisy/3475870145_685a19116d.jpg
  1. Test the optimized model
  python -m scripts.label_image \
    --graph=tf_files/optimized_graph.pb \
    --image=tf_files/flower_photos/daisy/3475870145_685a19116d.jpg
  1. Convert the model to TensorflowLite 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_type=FLOAT
  1. Create a directory for your reference
  mkdir android_tf
  1. Copy the .lite file to the reference folder
  cp tf_files/optimized_graph.lite android_tf/
  1. Copy the labels file to the reference folder
  cp tf_files/retrained_labels.txt android_tf/
  1. Declare in case required
  declare USER=yourname
  1. Copy to your storage drive
  gsutil cp -R android_tf/* gs://flower_${USER}/android_tf/

In case of some issue with gsutil, reinstall google cloud sdk:

cd TensorflowStarter

export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"

echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list

curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

apt-get update

apt-get install google-cloud-sdk

gcloud init

https://docs.google.com/forms/d/1FSYrE9H7J6gTBzdzUyfAGGPAerKZcqqzu7hOADsmXjM