Slide 1

Slide 1 text

Applied TensorFlow in Android apps Dan Jarvis @jarvisapps @daj

Slide 2

Slide 2 text

Agenda • Machine learning basics • Building things

Slide 3

Slide 3 text

Brief Overview of Machine Learning

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

TensorFlow Android image classifier demo • Trained on 1000 image categories • Works offline!

Slide 12

Slide 12 text

Label Predictions TensorFlow Android image classifier demo

Slide 13

Slide 13 text

TensorFlow Android image classifier demo Confidence

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

• Input data are not labelled • Model deduces structures • Common examples: • Clustering 2 - Unsupervised

Slide 16

Slide 16 text

“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.”

Slide 17

Slide 17 text

“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

Slide 18

Slide 18 text

• Learn by using a reward function 3 - Reinforcement

Slide 19

Slide 19 text

Zero reward

Slide 20

Slide 20 text

More reward

Slide 21

Slide 21 text

Maximum reward

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

• Learn by using a reward function • Common examples: • Games 3 - Reinforcement https://nihit.github.io/resources/spaceinvaders.pdf

Slide 25

Slide 25 text

1. Supervised – labelled data 2. Unsupervised – data only 3. Reinforcement – reward function Types of Machine Learning - Recap

Slide 26

Slide 26 text

Training Overview https://upxacademy.com/introduction-machine-learning

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Why TensorFlow? • Excellent tutorials, tools and demos • Great developer engagement • Cross platform • Windows, Mac, Linux, Android, iOS, Android Things! • Built to scale

Slide 30

Slide 30 text

• Training is done in Python or C++ • Bazel is sometimes used as a build tool • Using on Android requires NDK TensorFlow Basics

Slide 31

Slide 31 text

• Tutorials lag platform • Big model files • 10MB to 90MB for image classifiers • Can be shrunk (quantization, remove unused ops) TensorFlow Gotchas

Slide 32

Slide 32 text

How To Use Machine Learning

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

(1) Use Machine Learning Cloud APIs

Slide 41

Slide 41 text

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/

Slide 42

Slide 42 text

Google Cloud APIs • Machine Learning Engine • Job Search • Video • Vision • Speech • Natural Language • Translation

Slide 43

Slide 43 text

Vision API https://cloud.google.com/vision/

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Natural Language API https://cloud.google.com/natural-language/

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

(2) Using a Pre-trained Model

Slide 52

Slide 52 text

https://github.com/MindorksOpenSource/AndroidTensorFlowMNISTExample

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

https://github.com/MindorksOpenSource/AndroidTensorFlowMNISTExample

Slide 56

Slide 56 text

• 814k handwritten character images • 128x128 pixels per character NIST

Slide 57

Slide 57 text

• Subset of NIST • 60k for training • 10k for testing • Normalized MNIST - http://yann.lecun.com/exdb/mnist/

Slide 58

Slide 58 text

https://github.com/MindorksOpenSource/AndroidTensorFlowMNISTExample

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Interfacing With TensorFlow 1. Add dependency: compile 'org.tensorflow:tensorflow-android:+' 2. Use TensorFlowInferenceInterface to use your model

Slide 62

Slide 62 text

buildscript { repositories { jcenter() } } dependencies { compile 'org.tensorflow:tensorflow-android:1.2.0' }

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

1. Init

Slide 69

Slide 69 text

1. Init 2. Feed input

Slide 70

Slide 70 text

1. Init 2. Feed input 3. Run inference

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

inferenceInterface = new TensorFlowInferenceInterface( getAssets(), file:///android_asset/mnist_model_graph.pb);

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

@Override public List 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) { } return recognitions; }

Slide 78

Slide 78 text

@Override public List 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) { } return recognitions; }

Slide 79

Slide 79 text

@Override public List 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) { } return recognitions; }

Slide 80

Slide 80 text

@Override public List 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) { } return recognitions; }

Slide 81

Slide 81 text

@Override public List 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) { } return recognitions; }

Slide 82

Slide 82 text

@Override public List 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) { } return recognitions; }

Slide 83

Slide 83 text

@Override public List 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) { } return recognitions; }

Slide 84

Slide 84 text

@Override public List 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) { } return recognitions; }

Slide 85

Slide 85 text

@Override public List 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) { } return recognitions; }

Slide 86

Slide 86 text

Viewing TensorFlow Models In TensorBoard

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Run TensorBoard • Easy to run: tensorboard --logdir=/tmp/tensorflow_logdir • Open http://localhost:6006 in your browser

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

@Override public List 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) { } return recognitions; }

Slide 104

Slide 104 text

@Override public List 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) { } return recognitions; }

Slide 105

Slide 105 text

@Override public List 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) { } return recognitions; }

Slide 106

Slide 106 text

@Override public List 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) { } return recognitions; }

Slide 107

Slide 107 text

@Override public List 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) { } return recognitions; }

Slide 108

Slide 108 text

@Override public List 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) { } return recognitions; }

Slide 109

Slide 109 text

// 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];

Slide 110

Slide 110 text

@Override public List 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) { } return recognitions; }

Slide 111

Slide 111 text

@Override public List 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) { } return recognitions; }

Slide 112

Slide 112 text

@Override public List 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) { } return recognitions; }

Slide 113

Slide 113 text

https://github.com/daj/AndroidTensorFlowMNISTExample

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

File Sizes Built: • APK = 32MB Internals: • tensorflow.aar = 19MB (compressed) • libtensorflow_mnist.so = 11MB to 17MB per architecture • mnist_model_graph.pb = 13MB

Slide 117

Slide 117 text

Performance • Android build = <30 seconds • Classification = ~70ms first time, 10-20ms thereafter (Nexus 5)

Slide 118

Slide 118 text

(3) Training TensorFlow Models For Beginners

Slide 119

Slide 119 text

Training TensorFlow Models 1. Install TensorFlow 2. Find, download and run a training script

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

$ docker run -it gcr.io/tensorflow/tensorflow:1.2.0 bash

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

$ 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"

Slide 126

Slide 126 text

$ 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"

Slide 127

Slide 127 text

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

Slide 128

Slide 128 text

2(a) - Find Training Script

Slide 129

Slide 129 text

2(a) - Find Training Script

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

2(b) - Download Training Script From in our Docker container: curl -O https://raw.githubusercontent.com/MindorksOpenSource/Andr oidTensorFlowMNISTExample/master/mnist.py

Slide 132

Slide 132 text

2(c) - Run Training Script python mnist.py

Slide 133

Slide 133 text

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

Slide 134

Slide 134 text

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

Slide 135

Slide 135 text

# 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

Slide 136

Slide 136 text

... 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 ...

Slide 137

Slide 137 text

... 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 ...

Slide 138

Slide 138 text

... 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 ...

Slide 139

Slide 139 text

... 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

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

(4) Retraining TensorFlow Models With Transfer Learning

Slide 148

Slide 148 text

No content

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

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

Slide 154

Slide 154 text

http://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8

Slide 155

Slide 155 text

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

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

No content

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

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

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

cd /tensorflow Retraining The Model

Slide 168

Slide 168 text

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

Slide 169

Slide 169 text

... 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.

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

No content

Slide 172

Slide 172 text

No content

Slide 173

Slide 173 text

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

Slide 174

Slide 174 text

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

Slide 175

Slide 175 text

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

Slide 176

Slide 176 text

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

Slide 177

Slide 177 text

Testing The Model

Slide 178

Slide 178 text

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

Slide 179

Slide 179 text

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

Slide 180

Slide 180 text

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

Slide 181

Slide 181 text

hot dogs (score = 0.89646) legs (score = 0.10354)

Slide 182

Slide 182 text

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

Slide 183

Slide 183 text

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

Slide 184

Slide 184 text

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

Slide 185

Slide 185 text

Recap

Slide 186

Slide 186 text

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

Slide 187

Slide 187 text

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