Slide 1

Slide 1 text

Machine Learning for Android Developers Dan Jarvis

Slide 2

Slide 2 text

● Part 1 - Fundamentals ○ Context and example applications ○ Overview of training ● Part 2 - Building stuff ○ APIs and frameworks ○ Using a pre-trained TensorFlow model ○ Custom TensorFlow image classifier Agenda

Slide 3

Slide 3 text

Part 1 - Fundamentals

Slide 4

Slide 4 text

● 1997 - Deep Blue supercomputer beats chess world champion ● 2006 - Personal computer beats chess world champion ● 2011 - IBM Watson beats Jeopardy champions ● 2015 - ML image classifiers start to beat humans ● 2016 - AlphaGo beats Go Master Tipping Point

Slide 5

Slide 5 text

“I honestly can't think of any recent product development that Microsoft has been involved in that hasn't involved machine learning” “Everything we do now is influenced, one way or another, by machine learning.” - Peter Lee, Microsoft’s Director of Research (Feb 2015)

Slide 6

Slide 6 text

Jeff Dean, Google Senior Fellow - “Machine Learning - The Future Is Now” https://www.youtube.com/watch?v=UdHUDvxcH9o

Slide 7

Slide 7 text

Machine Learning Examples

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Neural Style ● Apply an artistic style to an existing image

Slide 13

Slide 13 text

Neural Style + = https://github.com/anishathalye/neural-style

Slide 14

Slide 14 text

Neural Style + = https://medium.com/@daj/how-does-prisma-work-f434273da92a

Slide 15

Slide 15 text

Sentiment Analysis ● Does author feel positively, neutrally, or negatively? ● Movie reviews, social media ● Order matters

Slide 16

Slide 16 text

http://nlp.stanford.edu:8080/sentiment/rntnDemo.html

Slide 17

Slide 17 text

TensorFlow Android Demos https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android

Slide 18

Slide 18 text

TensorFlow Android Demos https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android

Slide 19

Slide 19 text

Image Classification ● “TF Classify” ● Works offline! ● ~90MB model

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Machine Learning Overview

Slide 22

Slide 22 text

When To Use Machine Learning ● You cannot code the solution ● You cannot scale with humans ● You have training data

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

● Need lots of data ● Lots of excellent ones are online 1 - Get Data

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

● Subset of NIST ○ 60k for training ○ 10k for testing ● Normalized ● 20x20 pixels per character MNIST - http://yann.lecun.com/exdb/mnist/

Slide 27

Slide 27 text

● English lexical database ● Concepts = “Synsets” ○ >114k synsets ○ >80k are nouns ● No longer maintained WORDNET - http://wordnet.princeton.edu

Slide 28

Slide 28 text

● Images for some WORDNET ○ >14 million images ○ >21k synsets ● Annual competitions IMAGENET - http://imagenet.stanford.edu

Slide 29

Slide 29 text

Kaggle - https://www.kaggle.com

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

● Convert into appropriate formats ● Label the data (for supervised learning) ● Distort your data to generalize your model ○ Rotate, zoom in/out, brighten, crop … mirror? ○ Takes longer to train, so make a good model first 2 - Clean & Prepare Data

Slide 34

Slide 34 text

● Very computationally expensive (CPU -> GPU -> TPU) ● Only experts can configure training from scratch 3 - Train Model

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Underfitting and Overfitting

Slide 37

Slide 37 text

● Typically save some training data for testing ● Sometimes your accuracy is misleading! 4 - Test

Slide 38

Slide 38 text

● Iterate and increase accuracy 5 - Improve

Slide 39

Slide 39 text

Neural Networks

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

● Computer system modelled on the human brain Neural Networks http://home.agh.edu.pl/~vlsi/AI/intro/

Slide 42

Slide 42 text

● Computer system modelled on the human brain ● Nonlinear processing units extract features Neural Networks https://stackoverflow.com/a/32527774

Slide 43

Slide 43 text

● Computer system modelled on the human brain ● Nonlinear processing units extract features Neural Networks https://stackoverflow.com/a/32527774

Slide 44

Slide 44 text

https://blog.webkid.io/neural-networks-in-javascript/

Slide 45

Slide 45 text

Deep Learning https://blog.webkid.io/neural-networks-in-javascript/

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

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

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Underfitting and Overfitting

Slide 55

Slide 55 text

Types of Machine Learning

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

● Input data are not labelled ● Model deduces structures ● Common examples: ○ Clustering ○ Dimensionality Reduction 2 - Unsupervised

Slide 62

Slide 62 text

● Learn by using a Reward function 3 - Reinforcement

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Part 2 - Building Stuff

Slide 66

Slide 66 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 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Machine Learning Frameworks

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

● Training is done in Python or C++ ● Requires NDK ● Bazel is the build tool ● Tutorials lag platform ● Big model files (10MB to 90MB for image classifiers) ○ TensorFlow Lite to the rescue? TensorFlow Limitations

Slide 81

Slide 81 text

Using Pre-Trained TensorFlow Models on Android

Slide 82

Slide 82 text

https://github.com/miyosuda/TensorFlowAndroidMNIST

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Install Android NDK Add to your PATH Build native library from command line: cd jni-build make Building the Native Library

Slide 93

Slide 93 text

ndk-build Android NDK: WARNING:jni/Android.mk:tensorflow_mnist: non-system libraries in linker flags: jni/libs/armeabi-v7a/libprotos_all_cc.a jni/libs/armeabi-v7a/libprotobuf.a jni/libs/armeabi-v7a/libprotobuf_lite.a /Users/xgl413/Library/Android/android-ndk-r15b/sources/cxx-stl/gnu-libstdc++/4.9/libs/ armeabi-v7a/libgnustl_static.a /Users/xgl413/Library/Android/android-ndk-r15b/sources/cxx-stl/gnu-libstdc++/4.9/libs/ armeabi-v7a/libsupc++.a Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the Android NDK: current module make[1]: Entering directory `/development/TensorFlowAndroidMNIST/jni-build' [armeabi-v7a] Compile++ arm : tensorflow_mnist <= tensorflow_jni.cc [armeabi-v7a] Compile++ arm : tensorflow_mnist <= jni_utils.cc [armeabi-v7a] SharedLibrary : libtensorflow_mnist.so [armeabi-v7a] Install : libtensorflow_mnist.so => libs/armeabi-v7a/libtensorflow_mnist.so make[1]: Leaving directory `/development/TensorFlowAndroidMNIST/jni-build'

Slide 94

Slide 94 text

ndk-build Android NDK: WARNING:jni/Android.mk:tensorflow_mnist: non-system libraries in linker flags: jni/libs/armeabi-v7a/libprotos_all_cc.a jni/libs/armeabi-v7a/libprotobuf.a jni/libs/armeabi-v7a/libprotobuf_lite.a /Users/xgl413/Library/Android/android-ndk-r15b/sources/cxx-stl/gnu-libstdc++/4.9/libs/ armeabi-v7a/libgnustl_static.a /Users/xgl413/Library/Android/android-ndk-r15b/sources/cxx-stl/gnu-libstdc++/4.9/libs/ armeabi-v7a/libsupc++.a Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the Android NDK: current module make[1]: Entering directory `/development/TensorFlowAndroidMNIST/jni-build' [armeabi-v7a] Compile++ arm : tensorflow_mnist <= tensorflow_jni.cc [armeabi-v7a] Compile++ arm : tensorflow_mnist <= jni_utils.cc [armeabi-v7a] SharedLibrary : libtensorflow_mnist.so [armeabi-v7a] Install : libtensorflow_mnist.so => libs/armeabi-v7a/libtensorflow_mnist.so make[1]: Leaving directory `/development/TensorFlowAndroidMNIST/jni-build'

Slide 95

Slide 95 text

File Sizes Built: ● APK = 17MB Raw: ● libtensorflow_mnist.so = 14MB ● expert-graph.pb = 13MB

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Training TensorFlow Models For Beginners

Slide 98

Slide 98 text

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

Slide 99

Slide 99 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 100

Slide 100 text

2 - Find Training Script

Slide 101

Slide 101 text

2 - Find Training Script

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

2 - Download Training Script Download the scripts into our Docker TensorFlow container: curl -O https://raw.githubusercontent.com/miyosuda/TensorFlowAndr oidMNIST/master/trainer-script/expert.py curl -O https://raw.githubusercontent.com/miyosuda/TensorFlowAndr oidMNIST/master/trainer-script/input_data.py

Slide 104

Slide 104 text

2 - Run Training Script Run it: python expert.py

Slide 105

Slide 105 text

# python expert.py Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Extracting data/train-images-idx3-ubyte.gz Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes. Extracting data/train-labels-idx1-ubyte.gz Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes. Extracting data/t10k-images-idx3-ubyte.gz Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes. Extracting data/t10k-labels-idx1-ubyte.gz 2017-07-15 20:45:24.352372: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. ... step 0, training accuracy 0.14 step 100, training accuracy 0.86 step 200, training accuracy 0.96 step 300, training accuracy 0.88 step 400, training accuracy 1 step 500, training accuracy 0.94 ...

Slide 106

Slide 106 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

Slide 107

Slide 107 text

... step 10000, training accuracy 1 step 10100, training accuracy 1 step 10200, training accuracy 0.98 step 10300, training accuracy 1 step 10400, training accuracy 0.98 step 10500, training accuracy 1 step 10600, training accuracy 1 ...

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

... step 19500, training accuracy 1 step 19600, training accuracy 1 step 19700, training accuracy 1 step 19800, training accuracy 1 step 19900, training accuracy 1 Killed

Slide 115

Slide 115 text

Viewing TensorFlow Models In TensorBoard

Slide 116

Slide 116 text

https://blog.altoros.com/visualizing-tensorflow-graphs-with-tensorboard.html

Slide 117

Slide 117 text

https://blog.altoros.com/visualizing-tensorflow-graphs-with-tensorboard.html

Slide 118

Slide 118 text

Installing TensorBoard ● By default has a dependency on TensorFlow docker run -p 6006:6006 -it gcr.io/tensorflow/tensorflow:1.2.0 bash ● Standalone version available: https://github.com/dmlc/tensorboard pip install tensorboard

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

TensorBoard - Limitations ● No easy way to import a pretrained model :-( ○ import_pb_to_tensorboard.py in master, but doesn’t work ● Requires code into your training script ● Try to find training examples built for TensorBoard

Slide 121

Slide 121 text

Custom Image Classifier on Android https://medium.com/@daj

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

Building From Source https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/README.md ● Clone the repo ● Install ○ Bazel ○ Android NDK ○ Android SDK tools ● Update ./WORKSPACE

Slide 125

Slide 125 text

# Uncomment and update the paths in these entries to build the demo. android_sdk_repository( name = "androidsdk", api_level = 23, build_tools_version = " 25.0.2", # Replace with path to Android SDK on your system path = "/android", ) android_ndk_repository( name="androidndk", path="/android/android-ndk-r12b ", api_level= 21)

Slide 126

Slide 126 text

... ERROR: /Users/Dan/Code/tensorflow/WORKSPACE:8:1: Traceback (most recent call last): File "/Users/Dan/Code/tensorflow/WORKSPACE", line 8 check_version("0.4.2") File "/Users/Dan/Code/tensorflow/tensorflow/workspace.bzl", line 33, in check_version fail(" Current Bazel version is {}, e...)) Current Bazel version is 0.3.2-homebrew, expected at least 0.4.2 . ERROR: Error evaluating WORKSPACE file. ERROR: error loading package 'external': Package 'external' contains errors. INFO: Elapsed time: 0.214s

Slide 127

Slide 127 text

... ERROR: /Users/Dan/Code/tensorflow/WORKSPACE:8:1: Traceback (most recent call last): File "/Users/Dan/Code/tensorflow/WORKSPACE", line 8 check_version("0.4.2") File "/Users/Dan/Code/tensorflow/tensorflow/workspace.bzl", line 33, in check_version fail(" Current Bazel version is {}, e...)) Current Bazel version is 0.3.2-homebrew, expected at least 0.4.2 . ERROR: Error evaluating WORKSPACE file. ERROR: error loading package 'external': Package 'external' contains errors. INFO: Elapsed time: 0.214s

Slide 128

Slide 128 text

WARNING: /Users/Dan/Code/tensorflow/tensorflow/core/BUILD:826:12: in srcs attribute of cc_library rule //tensorflow/core:android_tensorflow_lib_lite: please do not import '//tensorflow/core/util/tensor_bundle:tensor_bundle.h' directly. You should either move the file to this package or depend on an appropriate rule there. ERROR: /Users/Dan/Code/tensorflow/WORKSPACE:11:1: no such package '@androidsdk//': Bazel requires Android build tools version 24.0.3 or newer, 23.0.1 was provided and referenced by '//external:android/sdk'. ERROR: Analysis of target '//tensorflow/examples/android:tensorflow_demo' failed; build aborted. INFO: Elapsed time: 28.826s

Slide 129

Slide 129 text

WARNING: /Users/Dan/Code/tensorflow/tensorflow/core/BUILD:826:12: in srcs attribute of cc_library rule //tensorflow/core:android_tensorflow_lib_lite: please do not import '//tensorflow/core/util/tensor_bundle:tensor_bundle.h' directly. You should either move the file to this package or depend on an appropriate rule there. ERROR: /Users/Dan/Code/tensorflow/WORKSPACE:11:1: no such package '@androidsdk//': Bazel requires Android build tools version 24.0.3 or newer, 23.0.1 was provided and referenced by '//external:android/sdk'. ERROR: Analysis of target '//tensorflow/examples/android:tensorflow_demo' failed; build aborted. INFO: Elapsed time: 28.826s

Slide 130

Slide 130 text

● Difficult/tedious ● Interferes with your development environment Problems

Slide 131

Slide 131 text

Solution 6.57 GB Docker container with everything in it: https://hub.docker.com/r/danjarvis/tensorflow-android/ Easy to use: docker run -it danjarvis/tensorflow-android:1.0.0

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

Building From Source Start our Docker container docker run -it danjarvis/tensorflow-android:1.0.0 From in the container bazel build -c opt --local_resources 4096,4.0,1.0 -j 1 //tensorflow/examples/android:tensorflow_demo

Slide 134

Slide 134 text

tensorflow/core/kernels/split_op.cc:159:64: warning: narrowing conversion of '(tensorflow::int64)split_dim_output_size' from 'tensorflow::int64 {aka long long int}' to 'int' inside { } [-Wnarrowing] Slow read: a 756175192-byte read from /root/.cache/bazel/_bazel_root/68a62076e91007a7908bc42a32e4cff9/execroot/tensorflow/ba zel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-opt/bin/tensorflow/core/ke rnels/libandroid_tensorflow_kernels.lo took 5883ms. Target //tensorflow/examples/android:tensorflow_demo up-to-date: bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jar bazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apk bazel-bin/tensorflow/examples/android/tensorflow_demo.apk INFO: Elapsed time: 1325.598s, Critical Path: 95.81s

Slide 135

Slide 135 text

tensorflow/core/kernels/split_op.cc:159:64: warning: narrowing conversion of '(tensorflow::int64)split_dim_output_size' from 'tensorflow::int64 {aka long long int}' to 'int' inside { } [-Wnarrowing] Slow read: a 756175192-byte read from /root/.cache/bazel/_bazel_root/68a62076e91007a7908bc42a32e4cff9/execroot/tensorflow/ba zel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-opt/bin/tensorflow/core/ke rnels/libandroid_tensorflow_kernels.lo took 5883ms. Target //tensorflow/examples/android:tensorflow_demo up-to-date: bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jar bazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apk bazel-bin/tensorflow/examples/android/tensorflow_demo.apk INFO: Elapsed time: 1325.598s, Critical Path: 95.81s

Slide 136

Slide 136 text

tensorflow/core/kernels/split_op.cc:159:64: warning: narrowing conversion of '(tensorflow::int64)split_dim_output_size' from 'tensorflow::int64 {aka long long int}' to 'int' inside { } [-Wnarrowing] Slow read: a 756175192-byte read from /root/.cache/bazel/_bazel_root/68a62076e91007a7908bc42a32e4cff9/execroot/tensorflow/ba zel-out/android-arm-linux-androideabi-4.9-v7a-gnu-libstdcpp-opt/bin/tensorflow/core/ke rnels/libandroid_tensorflow_kernels.lo took 5883ms. Target //tensorflow/examples/android:tensorflow_demo up-to-date: bazel-bin/tensorflow/examples/android/tensorflow_demo_deploy.jar bazel-bin/tensorflow/examples/android/tensorflow_demo_unsigned.apk bazel-bin/tensorflow/examples/android/tensorflow_demo.apk INFO: Elapsed time: 1325.598s, Critical Path: 95.81s

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

No content

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

Image Classifier Model - Inception-v3 ● https://github.com/tensorflow/models/tree/master/inception ● Trained on 1000 IMAGENET categories from 2012 https://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8

Slide 147

Slide 147 text

Transfer Learning ● https://www.tensorflow.org/tutorials/image_retraining ● Retraining a classifier is easy! https://medium.com/towards-data-science/transfer-learning-using-keras-d804b2e04ef8

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

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

Slide 152

Slide 152 text

docker run -it -v $HOME/tf_files:/tf_files danjarvis/tensorflow-android:1.0.0 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 153

Slide 153 text

... 2017–02–27 02:39:58.461678: Step 499: Train accuracy = 100.0% 2017–02–27 02:39:58.461841: Step 499: Cross entropy = 0.036179 2017–02–27 02:39:59.081111: Step 499: Validation accuracy = 99.0% (N=100) Final test accuracy = 97.2% (N=145) Converted 2 variables to const ops.

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

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

Slide 158

Slide 158 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 159

Slide 159 text

cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output _graph.pb Rebuilding From Source

Slide 160

Slide 160 text

cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output _graph.pb Rebuilding From Source

Slide 161

Slide 161 text

cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output _graph.pb cp /tf_files/retrained_labels.txt bazel-bin/tensorflow/examples/android/assets/imagenet_comp_g raph_label_strings.txt Rebuilding From Source

Slide 162

Slide 162 text

cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output _graph.pb cp /tf_files/retrained_labels.txt bazel-bin/tensorflow/examples/android/assets/imagenet_comp_g raph_label_strings.txt Rebuilding From Source

Slide 163

Slide 163 text

cp /tf_files/stripped_retrained_graph.pb bazel-bin/tensorflow/examples/android/assets/stripped_output _graph.pb cp /tf_files/retrained_labels.txt bazel-bin/tensorflow/examples/android/assets/imagenet_comp_g raph_label_strings.txt bazel build -c opt --local_resources 4096,4.0,1.0 -j 1 //tensorflow/examples/android:tensorflow_demo Rebuilding From Source

Slide 164

Slide 164 text

python label_image.py Command Line Testing

Slide 165

Slide 165 text

python label_image.py test_02_hotdogs.jpg Command Line Testing

Slide 166

Slide 166 text

python label_image.py test_02_hotdogs.jpg legs (score = 0.59678) hot dogs (score = 0.40322) Command Line Testing

Slide 167

Slide 167 text

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

Slide 168

Slide 168 text

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

Slide 169

Slide 169 text

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

Slide 170

Slide 170 text

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

Slide 171

Slide 171 text

Recap

Slide 172

Slide 172 text

From easiest to hardest: 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 5. Train your own model from scratch 6. Train your model dynamically on the device How To Use Machine Learning

Slide 173

Slide 173 text

● Blog posts: https://medium.com/@daj ● Docker container: https://hub.docker.com/r/danjarvis/ ● Legs or hot dogs images: https://github.com/daj/legs-or-hotdogs-images ● Slides: https://speakerdeck.com/daj Links

Slide 174

Slide 174 text

Thank You!