Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Applied TensorFlow for Android apps

C31286b6049746dfeb89d311aad9ef2b?s=47 Dan J
September 26, 2017

Applied TensorFlow for Android apps

The video of this talk is now available: https://www.youtube.com/watch?v=B6Xx67liMCk

The slides are for a talk I gave at Droidcon NYC 2017: http://droidcon.nyc/

------------------------------------

Find out how to use machine learning on Android!

This talk is a hands-on guide to using TensorFlow in your Android app.

We cover:
- A brief overview of machine learning.
- How to use machine learning in Android apps:
- Machine learning REST APIs.
- Using a pre-trained TensorFlow model.
- Inspecting TensorFlow models in TensorBoard.
- Training and transfer learning with TensorFlow.

C31286b6049746dfeb89d311aad9ef2b?s=128

Dan J

September 26, 2017
Tweet

Transcript

  1. Applied TensorFlow in Android apps Dan Jarvis @jarvisapps @daj

  2. Agenda • Machine learning basics • Building things

  3. Brief Overview of Machine Learning

  4. https://www.youtube.com/watch?v=fX7eQ9oRp-U

  5. 1. Supervised 2. Unsupervised 3. Reinforcement Types of Machine Learning

  6. • Input data are already labelled • Model trains by

    iterating 1 - Supervised
  7. • Input data are already labelled • Model trains by

    iterating • Common examples: • Regression 1 - Supervised
  8. • Input data are already labelled • Model trains by

    iterating • Common examples: • Regression 1 - Supervised New input value
  9. • Input data are already labelled • Model trains by

    iterating • Common examples: • Regression 1 - Supervised Prediction
  10. • Input data are already labelled • Model trains by

    iterating • Common examples: • Regression • Classification 1 - Supervised
  11. TensorFlow Android image classifier demo • Trained on 1000 image

    categories • Works offline!
  12. Label Predictions TensorFlow Android image classifier demo

  13. TensorFlow Android image classifier demo Confidence

  14. • Input data are not labelled • Model deduces structures

    2 - Unsupervised
  15. • Input data are not labelled • Model deduces structures

    • Common examples: • Clustering 2 - Unsupervised
  16. “Contrary to what appears to be a widely-held intuition, our

    experimental results reveal that it is possible to train a face detector without having to label images as containing a face or not.”
  17. “Contrary to what appears to be a widely-held intuition, our

    experimental results reveal that it is possible to train a face detector without having to label images as containing a face or not.” -Quoc V. Le, Marc’Aurelio Ranzato, Rajat Monga, Matthieu Devin, Kai Chen, Greg S. Corrado, Jeff Dean, Andrew Y. Ng (July 2012) https://arxiv.org/pdf/1112.6209.pdf
  18. • Learn by using a reward function 3 - Reinforcement

  19. Zero reward

  20. More reward

  21. Maximum reward

  22. https://www.youtube.com/watch?v=Lt-KLtkDlh8

  23. • Learn by using a reward function • Common examples:

    • Games 3 - Reinforcement http://karpathy.github.io/2016/05/31/rl/
  24. • Learn by using a reward function • Common examples:

    • Games 3 - Reinforcement https://nihit.github.io/resources/spaceinvaders.pdf
  25. 1. Supervised – labelled data 2. Unsupervised – data only

    3. Reinforcement – reward function Types of Machine Learning - Recap
  26. Training Overview https://upxacademy.com/introduction-machine-learning

  27. None
  28. None
  29. Why TensorFlow? • Excellent tutorials, tools and demos • Great

    developer engagement • Cross platform • Windows, Mac, Linux, Android, iOS, Android Things! • Built to scale
  30. • Training is done in Python or C++ • Bazel

    is sometimes used as a build tool • Using on Android requires NDK TensorFlow Basics
  31. • Tutorials lag platform • Big model files • 10MB

    to 90MB for image classifiers • Can be shrunk (quantization, remove unused ops) TensorFlow Gotchas
  32. How To Use Machine Learning

  33. How To Use Machine Learning 1. Cloud APIs (MLaaS)

  34. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model
  35. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model 3. Follow instructions to train a model
  36. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model 3. Follow instructions to train a model 4. Retraining a model
  37. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model 3. Follow instructions to train a model 4. Retraining a model 5. Train your own model from scratch
  38. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model 3. Follow instructions to train a model 4. Retraining a model 5. Train your own model from scratch 6. Train your model dynamically on the device
  39. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model 3. Follow instructions to train a model 4. Retraining a model 5. Train your own model from scratch 6. Train your model dynamically on the device
  40. (1) Use Machine Learning Cloud APIs

  41. Machine Learning APIs • Google Cloud https://cloud.google.com/products/machine-learning/ • IBM Watson

    https://www.ibm.com/watson/developercloud/discovery.html • Amazon AWS https://aws.amazon.com/machine-learning/ • Microsoft Azure https://docs.microsoft.com/en-us/rest/api/machinelearning/
  42. Google Cloud APIs • Machine Learning Engine • Job Search

    • Video • Vision • Speech • Natural Language • Translation
  43. Vision API https://cloud.google.com/vision/

  44. https://cloud.google.com/vision/

  45. None
  46. None
  47. Natural Language API https://cloud.google.com/natural-language/

  48. None
  49. None
  50. None
  51. (2) Using a Pre-trained Model

  52. https://github.com/MindorksOpenSource/AndroidTensorFlowMNISTExample

  53. None
  54. None
  55. https://github.com/MindorksOpenSource/AndroidTensorFlowMNISTExample

  56. • 814k handwritten character images • 128x128 pixels per character

    NIST
  57. • Subset of NIST • 60k for training • 10k

    for testing • Normalized MNIST - http://yann.lecun.com/exdb/mnist/
  58. https://github.com/MindorksOpenSource/AndroidTensorFlowMNISTExample

  59. https://jalammar.github.io/Supercharging-android-apps-using-tensorflow/

  60. None
  61. Interfacing With TensorFlow 1. Add dependency: compile 'org.tensorflow:tensorflow-android:+' 2. Use

    TensorFlowInferenceInterface to use your model
  62. buildscript { repositories { jcenter() } } dependencies { compile

    'org.tensorflow:tensorflow-android:1.2.0' }
  63. None
  64. None
  65. None
  66. None
  67. None
  68. 1. Init

  69. 1. Init 2. Feed input

  70. 1. Init 2. Feed input 3. Run inference

  71. 1. Init 2. Feed input 3. Run inference 4. Fetch

    output
  72. 1. Init 2. Feed input 3. Run inference 4. Fetch

    output 5. Close
  73. None
  74. inferenceInterface = new TensorFlowInferenceInterface( getAssets(), file:///android_asset/mnist_model_graph.pb);

  75. public TensorFlowInferenceInterface( AssetManager assetManager, String modelFile) { System.loadLibrary("tensorflow_inference"); InputStream file

    = assetManager.open(modelFile); this.loadGraph(file, new Graph()); }
  76. public TensorFlowInferenceInterface( AssetManager assetManager, String modelFile) { System.loadLibrary("tensorflow_inference"); InputStream file

    = assetManager.open(modelFile); this.loadGraph(file, new Graph()); }
  77. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  78. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  79. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  80. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  81. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  82. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  83. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  84. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  85. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  86. Viewing TensorFlow Models In TensorBoard

  87. None
  88. Run TensorBoard • Easy to run: tensorboard --logdir=/tmp/tensorflow_logdir • Open

    http://localhost:6006 in your browser
  89. Install TensorBoard Two easiest options: 1. Standalone pip install tensorboard

    2. TensorFlow Docker container docker run -p 6006:6006 -it gcr.io/tensorflow/tensorflow:1.2.0 bash
  90. Install TensorBoard Two easiest options: 1. Standalone pip install tensorboard

    2. TensorFlow Docker container docker run -p 6006:6006 -it gcr.io/tensorflow/tensorflow:1.2.0 bash
  91. Import Existing Model • Get import_pb_to_tensorboard.py from TensorFlow GitHub •

    Run it: python import_pb_to_tensorboard.py \ --model_dir /tmp/mnist_model_graph.pb \ --log_dir /tmp/tensorflow_logdir https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/import_pb_to_tensorboard.py
  92. Import Existing Model • Get import_pb_to_tensorboard.py from TensorFlow GitHub •

    Run it: python import_pb_to_tensorboard.py \ --model_dir /tmp/mnist_model_graph.pb \ --log_dir /tmp/tensorflow_logdir https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/import_pb_to_tensorboard.py
  93. $ python import_pb_to_tensorboard.py --model_dir /tmp/mnist_model_graph.pb --log_dir /tmp/tensorflow_logdir <snip> Model Imported.

    Visualize by running: tensorboard -- logdir=/tmp/tensorflow_logdir
  94. $ python import_pb_to_tensorboard.py --model_dir /tmp/mnist_model_graph.pb --log_dir /tmp/tensorflow_logdir <snip> Model Imported.

    Visualize by running: tensorboard -- logdir=/tmp/tensorflow_logdir
  95. None
  96. None
  97. None
  98. None
  99. None
  100. None
  101. None
  102. None
  103. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  104. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( inputName, pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  105. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  106. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{outputName}); // Copy the output data inferenceInterface.fetch(outputName, outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  107. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{"output"}); // Copy the output data inferenceInterface.fetch("output", outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  108. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{"output"}); // Copy the output data inferenceInterface.fetch("output", outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  109. // Output shape is [N, NUM_CLASSES], where N is the

    batch size int numClasses = (int) inferenceInterface.graph(). operation(outputName).output(0).shape().size(1); outputs = new float[numClasses];
  110. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{"output"}); // Copy the output data inferenceInterface.fetch("output", outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  111. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{inputSize * inputSize}); // Run the inference inferenceInterface.run(new String[]{"output"}); // Copy the output data inferenceInterface.fetch("output", outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  112. @Override public List<Recognition> recognizeImage(final float[] pixels) { // Copy the

    input data into TensorFlow inferenceInterface.feed( "input", pixels, new long[]{28 * 28}); // Run the inference inferenceInterface.run(new String[]{"output"}); // Copy the output data inferenceInterface.fetch("output", outputs); // Find the best classifications for (int i = 0; i < outputs.length; ++i) { <snip> } return recognitions; }
  113. https://github.com/daj/AndroidTensorFlowMNISTExample

  114. https://github.com/daj/AndroidTensorFlowMNISTExample 28x28 inputs 5 5 3 3

  115. None
  116. File Sizes Built: • APK = 32MB Internals: • tensorflow.aar

    = 19MB (compressed) • libtensorflow_mnist.so = 11MB to 17MB per architecture • mnist_model_graph.pb = 13MB
  117. Performance • Android build = <30 seconds • Classification =

    ~70ms first time, 10-20ms thereafter (Nexus 5)
  118. (3) Training TensorFlow Models For Beginners

  119. Training TensorFlow Models 1. Install TensorFlow 2. Find, download and

    run a training script
  120. 1 - Install TensorFlow Install Docker from www.docker.com Download and

    start the TensorFlow binary image: docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash
  121. None
  122. $ docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash

  123. $ docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash root@d3db3849abfa:/notebooks#

  124. $ docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash root@d3db3849abfa:/notebooks#

  125. $ docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash root@d3db3849abfa:/notebooks# $ docker ps

    CONTAINER ID IMAGE ... COMMAND d3db3849abfa gcr.io/tensorflow/tensorflow:1.2.0 ... "bash"
  126. $ docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash root@d3db3849abfa:/notebooks# $ docker ps

    CONTAINER ID IMAGE ... COMMAND d3db3849abfa gcr.io/tensorflow/tensorflow:1.2.0 ... "bash"
  127. Docker Gotchas • Changes are lost unless you commit them

    • Only certain local folders can be mounted (e.g. $HOME) • Weird workarounds to avoid out of memory errors • Tricky to clean up leftover containers and images
  128. 2(a) - Find Training Script

  129. 2(a) - Find Training Script

  130. None
  131. 2(b) - Download Training Script From in our Docker container:

    curl -O https://raw.githubusercontent.com/MindorksOpenSource/Andr oidTensorFlowMNISTExample/master/mnist.py
  132. 2(c) - Run Training Script python mnist.py

  133. # python mnist.py Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Extracting MNIST_data/train-images-idx3-ubyte.gz

    ...
  134. # python mnist.py Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Extracting MNIST_data/train-images-idx3-ubyte.gz

    ...
  135. # python mnist.py Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Extracting MNIST_data/train-images-idx3-ubyte.gz

    Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes. Extracting MNIST_data/train-labels-idx1-ubyte.gz Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes. Extracting MNIST_data/t10k-images-idx3-ubyte.gz Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes. Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
  136. ... Iter 1280, Minibatch Loss= 32370.832031, Training Accuracy= 0.21875 Iter

    2560, Minibatch Loss= 15398.544922, Training Accuracy= 0.43750 Iter 3840, Minibatch Loss= 10572.830078, Training Accuracy= 0.54688 Iter 5120, Minibatch Loss= 5175.708008, Training Accuracy= 0.72656 Iter 6400, Minibatch Loss= 4093.027100, Training Accuracy= 0.80469 Iter 7680, Minibatch Loss= 7343.525391, Training Accuracy= 0.73438 Iter 8960, Minibatch Loss= 2532.883789, Training Accuracy= 0.82812 Iter 10240, Minibatch Loss= 2786.300293, Training Accuracy= 0.79688 Iter 11520, Minibatch Loss= 2092.962158, Training Accuracy= 0.91406 Iter 12800, Minibatch Loss= 2265.324707, Training Accuracy= 0.85938 ...
  137. ... Iter 1280, Minibatch Loss= 32370.832031, Training Accuracy= 0.21875 Iter

    2560, Minibatch Loss= 15398.544922, Training Accuracy= 0.43750 Iter 3840, Minibatch Loss= 10572.830078, Training Accuracy= 0.54688 Iter 5120, Minibatch Loss= 5175.708008, Training Accuracy= 0.72656 Iter 6400, Minibatch Loss= 4093.027100, Training Accuracy= 0.80469 Iter 7680, Minibatch Loss= 7343.525391, Training Accuracy= 0.73438 Iter 8960, Minibatch Loss= 2532.883789, Training Accuracy= 0.82812 Iter 10240, Minibatch Loss= 2786.300293, Training Accuracy= 0.79688 Iter 11520, Minibatch Loss= 2092.962158, Training Accuracy= 0.91406 Iter 12800, Minibatch Loss= 2265.324707, Training Accuracy= 0.85938 ...
  138. ... Iter 98560, Minibatch Loss= 66.527161, Training Accuracy= 0.99219 Iter

    99840, Minibatch Loss= 513.522278, Training Accuracy= 0.95312 Iter 101120, Minibatch Loss= 552.358948, Training Accuracy= 0.96094 Iter 102400, Minibatch Loss= 825.438721, Training Accuracy= 0.93750 ...
  139. ... Iter 194560, Minibatch Loss= 80.143494, Training Accuracy= 0.98438 Iter

    195840, Minibatch Loss= 150.199615, Training Accuracy= 0.97656 Iter 197120, Minibatch Loss= 18.043427, Training Accuracy= 0.99219 Iter 198400, Minibatch Loss= 62.561752, Training Accuracy= 0.98438 Iter 199680, Minibatch Loss= 91.177673, Training Accuracy= 0.98438 Optimization Finished! Testing Accuracy: 0.988281 check accuracy 0.9719
  140. None
  141. None
  142. None
  143. None
  144. None
  145. None
  146. None
  147. (4) Retraining TensorFlow Models With Transfer Learning

  148. None
  149. None
  150. None
  151. None
  152. None
  153. Image Classifier Model - Inception-v3 • https://github.com/tensorflow/models/tree/master/inception • Trained on

    1000 IMAGENET categories from 2012 http://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8
  154. http://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8

  155. http://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8 https://devblogs.nvidia.com/parallelforall/deep-learning-nutshell-core-concepts/

  156. None
  157. Transfer Learning • https://www.tensorflow.org/tutorials/image_retraining • Retraining a classifier is easy!

    http://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8
  158. None
  159. None
  160. None
  161. None
  162. None
  163. docker run -it -v $HOME/tf_files:/tf_files danjarvis/tensorflow-android:1.0.0 Retraining The Model

  164. docker run -it -v $HOME/tf_files:/tf_files danjarvis/tensorflow-android:1.0.0 Retraining The Model

  165. docker run -it -v $HOME/tf_files:/tf_files danjarvis/tensorflow-android:1.0.0 Retraining The Model

  166. None
  167. cd /tensorflow Retraining The Model

  168. cd /tensorflow python tensorflow/examples/image_retraining/retrain.py --bottleneck_dir=/tf_files/bottlenecks --how_many_training_steps 500 --model_dir=/tf_files/inception --output_graph=/tf_files/retrained_graph.pb --output_labels=/tf_files/retrained_labels.txt

    --image_dir /tf_files/photos Retraining The Model
  169. ... 2017-09-04 20:38:03.254191: Step 499: Train accuracy = 100.0% 2017-09-04

    20:38:03.254272: Step 499: Cross entropy = 0.007678 2017-09-04 20:38:03.763952: Step 499: Validation accuracy = 100.0% (N=100) Final test accuracy = 91.7% (N=12) Converted 2 variables to const ops.
  170. None
  171. None
  172. None
  173. bazel build --local_resources 4096,4.0,1.0 -j 1 tensorflow/python/tools:strip_unused Prepare The Model

  174. bazel build --local_resources 4096,4.0,1.0 -j 1 tensorflow/python/tools:strip_unused Prepare The Model

  175. bazel build --local_resources 4096,4.0,1.0 -j 1 tensorflow/python/tools:strip_unused bazel-bin/tensorflow/python/tools/strip_unused --input_graph=/tf_files/retrained_graph.pb --output_graph=/tf_files/stripped_retrained_graph.pb

    --input_node_names="Mul" --output_node_names="final_result" --input_binary=true Prepare The Model
  176. bazel build --local_resources 4096,4.0,1.0 -j 1 tensorflow/python/tools:strip_unused bazel-bin/tensorflow/python/tools/strip_unused --input_graph=/tf_files/retrained_graph.pb --output_graph=/tf_files/stripped_retrained_graph.pb

    --input_node_names="Mul" --output_node_names="final_result" --input_binary=true Prepare The Model
  177. Testing The Model

  178. curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/ma ster/tensorflow/examples/image_retraining/label_image.py Testing The Model

  179. curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/ma ster/tensorflow/examples/image_retraining/label_image.py python label_image.py --image <input_image> --graph retrained_graph.pb

    --labels retrained_labels.txt Testing The Model
  180. curl -O https://raw.githubusercontent.com/tensorflow/tensorflow/ma ster/tensorflow/examples/image_retraining/label_image.py python label_image.py --image <input_image> --graph retrained_graph.pb

    --labels retrained_labels.txt Testing The Model
  181. hot dogs (score = 0.89646) legs (score = 0.10354)

  182. legs (score = 0.59678) hot dogs (score = 0.40322)

  183. legs (score = 0.91612) hot dogs (score = 0.08388)

  184. legs (score = 0.50959) hot dogs (score = 0.49041)

  185. Recap

  186. 1. Use machine learning cloud APIs 2. Embed pre-trained models

    3. Follow instructions to train a model 4. Use transfer learning to customize a pre-trained model How To Use Machine Learning
  187. Blog posts: https://medium.com/@daj Slides: https://speakerdeck.com/daj Legs or hot dogs images:

    https://github.com/daj/legs-or-hotdogs-images Docker container: https://hub.docker.com/r/danjarvis/ Links @jarvisapps @daj