Android+TensorFlow™
A beginner’s guide to Machine Learning on the Edge
@alex_dennis - Alex Dennis
How about
“Not Banana”?
First things first, show me the model
It’s all about the data
What is a Tensor?
● Basically Data
● Numbers that represent data
● A typed multi-dimensional array (from the docs)
Training your deep net
● Network Topology: CNN, RNN, LSTM, …
● Cost function
● Hyperparameters: Learning rate, Stride Length, Batch Size, Training Iterations, etc.
● Learning Algorithms: Gradient Descent, Stochastic Gradient Descent, Mini Batch Gradient
Descent, Adagrad, AdaDelta, etc.
● Evaluation techniques: Accuracy, Precision, Recall, F1 Score, etc.
● It can get complicated...
Don’t train on all your data
● Keep some, say 20% for testing to prevent overfitting
● If you are fiddling with your training model, maybe even take some out for cross validation
Alternatives
● Use an open source model
● Retrain an open source model for your specific needs
Retraining Inception v3 to recognize Bananas
● Android code labs - TensorFlow for Poets 1 & 2
● Retrained Inception V3 with my own data and labels
○ Inception has over 1,000 labels
○ I just have needed 2
○ Transfer learning
Optimize that model for mobile
● TensorFlow for poets 2
● Optimize the operations in the graph
● Make it fast, Make it small
Demo
Steps to greatness
● Load the TensorFlow graph file that you trained into TensorFlowInferenceInterface
● Transform your data into the shape of your input tensor
● Initiate an TensorFlow session with TensorFlowInferenceInterface
● Read results from output tensor back into your Android app and update UI or whatever else you
want to do
Loading the Graph
● Use the TensorFlowInferenceInterface class from TensorFlow Android library
● Uses AssetManager
● 1.3.1-alpha allows you to pass in an InputStream
○ HTTP
○ openRawResource
○ etc.
Transforming your data
● Need to convert into the shape of the input tensor
○ Crop
○ Preprocess
○ etc.
Run the data through the Graph
● Background thread
● TensorFlowInferenceInterface::feed
● TensorFlowInferenceInterface::run
● TensorFlowInferenceInterface::fetch
Reading the output of the graph
● Convert output into data structures appropriate for your application
Not Banana
● Borrows heavily from TFClassify Android sample
● Written in Kotlin
● https://github.com/alexdennis/not-banana-app
References
● How HBO’s Silivon Valley built “Not Hotdog”... -
https://medium.com/@timanglade/how-hbos-silicon-valley-built-not-hotdog-with-mobile-tensorfl
ow-keras-react-native-ef03260747f3
● TensorFlow Getting Started - https://www.tensorflow.org/get_started/get_started
● Deep Learning Series on Coursera - https://www.coursera.org/specializations/deep-learning
● Machine Learning on Coursera - https://www.coursera.org/learn/machine-learning
● Open Source TensorFlow models - https://www.youtube.com/watch?v=9ziVGkt8Gg4
● TensorFlow for Poets Android codelab -
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0
Easy? Right? Questions?

Android+TensorFlow

  • 1.
    Android+TensorFlow™ A beginner’s guideto Machine Learning on the Edge @alex_dennis - Alex Dennis
  • 3.
  • 4.
    First things first,show me the model
  • 5.
  • 6.
    What is aTensor? ● Basically Data ● Numbers that represent data ● A typed multi-dimensional array (from the docs)
  • 7.
    Training your deepnet ● Network Topology: CNN, RNN, LSTM, … ● Cost function ● Hyperparameters: Learning rate, Stride Length, Batch Size, Training Iterations, etc. ● Learning Algorithms: Gradient Descent, Stochastic Gradient Descent, Mini Batch Gradient Descent, Adagrad, AdaDelta, etc. ● Evaluation techniques: Accuracy, Precision, Recall, F1 Score, etc. ● It can get complicated...
  • 8.
    Don’t train onall your data ● Keep some, say 20% for testing to prevent overfitting ● If you are fiddling with your training model, maybe even take some out for cross validation
  • 9.
    Alternatives ● Use anopen source model ● Retrain an open source model for your specific needs
  • 10.
    Retraining Inception v3to recognize Bananas ● Android code labs - TensorFlow for Poets 1 & 2 ● Retrained Inception V3 with my own data and labels ○ Inception has over 1,000 labels ○ I just have needed 2 ○ Transfer learning
  • 11.
    Optimize that modelfor mobile ● TensorFlow for poets 2 ● Optimize the operations in the graph ● Make it fast, Make it small
  • 12.
  • 13.
    Steps to greatness ●Load the TensorFlow graph file that you trained into TensorFlowInferenceInterface ● Transform your data into the shape of your input tensor ● Initiate an TensorFlow session with TensorFlowInferenceInterface ● Read results from output tensor back into your Android app and update UI or whatever else you want to do
  • 14.
    Loading the Graph ●Use the TensorFlowInferenceInterface class from TensorFlow Android library ● Uses AssetManager ● 1.3.1-alpha allows you to pass in an InputStream ○ HTTP ○ openRawResource ○ etc.
  • 15.
    Transforming your data ●Need to convert into the shape of the input tensor ○ Crop ○ Preprocess ○ etc.
  • 16.
    Run the datathrough the Graph ● Background thread ● TensorFlowInferenceInterface::feed ● TensorFlowInferenceInterface::run ● TensorFlowInferenceInterface::fetch
  • 17.
    Reading the outputof the graph ● Convert output into data structures appropriate for your application
  • 18.
    Not Banana ● Borrowsheavily from TFClassify Android sample ● Written in Kotlin ● https://github.com/alexdennis/not-banana-app
  • 19.
    References ● How HBO’sSilivon Valley built “Not Hotdog”... - https://medium.com/@timanglade/how-hbos-silicon-valley-built-not-hotdog-with-mobile-tensorfl ow-keras-react-native-ef03260747f3 ● TensorFlow Getting Started - https://www.tensorflow.org/get_started/get_started ● Deep Learning Series on Coursera - https://www.coursera.org/specializations/deep-learning ● Machine Learning on Coursera - https://www.coursera.org/learn/machine-learning ● Open Source TensorFlow models - https://www.youtube.com/watch?v=9ziVGkt8Gg4 ● TensorFlow for Poets Android codelab - https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0
  • 20.