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

Applied TensorFlow for Android apps

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.

Dan J

September 26, 2017
Tweet

More Decks by Dan J

Other Decks in Technology

Transcript

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

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

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

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

    iterating • Common examples: • Regression • Classification 1 - Supervised
  5. • Input data are not labelled • Model deduces structures

    • Common examples: • Clustering 2 - Unsupervised
  6. “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.”
  7. “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
  8. • Learn by using a reward function • Common examples:

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

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

    3. Reinforcement – reward function Types of Machine Learning - Recap
  11. Why TensorFlow? • Excellent tutorials, tools and demos • Great

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

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

    to 90MB for image classifiers • Can be shrunk (quantization, remove unused ops) TensorFlow Gotchas
  14. How To Use Machine Learning 1. Cloud APIs (MLaaS) 2.

    Use a pre-trained model 3. Follow instructions to train a model
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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/
  20. Google Cloud APIs • Machine Learning Engine • Job Search

    • Video • Vision • Speech • Natural Language • Translation
  21. • Subset of NIST • 60k for training • 10k

    for testing • Normalized MNIST - http://yann.lecun.com/exdb/mnist/
  22. buildscript { repositories { jcenter() } } dependencies { compile

    'org.tensorflow:tensorflow-android:1.2.0' }
  23. @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; }
  24. @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; }
  25. @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; }
  26. @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; }
  27. @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; }
  28. @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; }
  29. @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; }
  30. @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; }
  31. @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; }
  32. 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
  33. 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
  34. 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
  35. 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
  36. @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; }
  37. @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; }
  38. @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; }
  39. @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; }
  40. @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; }
  41. @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; }
  42. // 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];
  43. @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; }
  44. @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; }
  45. @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; }
  46. File Sizes Built: • APK = 32MB Internals: • tensorflow.aar

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

    ~70ms first time, 10-20ms thereafter (Nexus 5)
  48. 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
  49. $ 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"
  50. $ 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"
  51. 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
  52. 2(b) - Download Training Script From in our Docker container:

    curl -O https://raw.githubusercontent.com/MindorksOpenSource/Andr oidTensorFlowMNISTExample/master/mnist.py
  53. # 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
  54. ... 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 ...
  55. ... 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 ...
  56. ... 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 ...
  57. ... 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
  58. 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
  59. 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
  60. ... 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.
  61. 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
  62. 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