Machine Learning
YAIA!
Yet Another Intro (for Android)
How did we get here?
But what is Machine Learning?
● Subset of AI
● Narrow intelligence
● Highly interdisciplinary
● Let the process find without being explicitly
programmed
○ But… Find what? A Model
● Your model is as good as the data you use
Programming vs Teaching
What can you do with ML?
Examples
● Self-driving cars
● Auto color images
● Translation
● Text generation
Examples
● Games
● Health
problems
detection
● Recommendation
● IoT
Where are we going?
● Better algorithms with less data
● AutoML
● Better integration with mobile
● Augmentation of your skills
○ Collaboration
● New roles: Machine trainers
● Terminator is not coming but look at these papers:
○ Pix2code: Generating Code from a Graphical User Interface Screenshot
○ DeepCoder: Learning to Write Programs
○ A deep learning model for estimating story points
Machine Learning YAIA!
2nd part
The Mobile Case
● Limited size and processing power
○ Yet we have GPUs!
● Forget about training
○ By now…
● Smaller models yet almost equally capable
● Sacrifice accuracy by speed/size
Architectures. Cloud-based
Classifier model
Data
Trained
model
Query
service
Trainer
Architectures. Model baked-in
Data
Trained
model
Trainer
Classifier model
Query
Architectures. All in the phone?
Data
Trainer
Classifier model
Architectures. Federated Learning
A basic typical simple DIY example
● Using MNIST
○ Toy dataset with handwritten digits
● Our app will classify user’s input
● We’ll use a Convolutional Neural Network
○ This is state-of-art :)
● Tensorflow
Steps
1. Explore the dataset
2. Curate the dataset (if required)
3. Preprocess the input
4. Determine the train/test datasets
5. Choose your architecture
6. Define the hyperparameters
7. Train the model
8. Evaluate the model
9. Export the model
10. Use the exported model in the Android app with Tensorflow
Explore the dataset
Preprocess the input
● The image can be flattened from matrix to a vector
○ Tensorflow is already doing this for you in the sample dataset!
● But important to remember as we will have to replicate the preprocessing in the client
Choose your architecture
● We’ll go with a Convolutional Neural Network
○ Very good at finding patterns in images
● We could have used a neural network with one hidden layer
● Lost with the terms?
○ This is expected!
○ So many new terms and definitions and vocabulary
Convolutional Neural Network
A Convolutional Neural Network
def deepnn(x):
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32, name='dropout_prob')
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2, name='output_node')
return y_conv, keep_prob
Build the Deep Network
# Create the model
x = tf.placeholder(tf.float32, [None, 784], name='input')
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
# Build the graph for the deep net
y_conv, keep_prob = deepnn(x)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Train your Dragon
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(10000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
# Create Saver
saver = tf.train.Saver()
# Save variables to .ckpt file
saver.save(sess, "./runs/trained_variables.ckpt")
Test if it flights
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
How good was that?
step 0, training accuracy 0.18
step 100, training accuracy 0.76
step 200, training accuracy 0.94
step 300, training accuracy 0.92
step 400, training accuracy 0.86
step 500, training accuracy 0.94
step 600, training accuracy 0.96
step 700, training accuracy 0.9
…
step 9800, training accuracy 0.96
step 9900, training accuracy 1
test accuracy 0.9919
Export the model
Trainer
Protobuf format
Process flow
Digit
Classifier
Using Tensorflow in Android
compile 'org.tensorflow:tensorflow-android:1.2.0'
Gradle dependencies
Using Tensorflow in Android
inferenceInterface = new TensorFlowInferenceInterface(assetManager, MODEL_FILENAME);
// Note: The size of the input tensor includes the batch size!!
int inputSize = batchSize * imageSize * imageSize;
float[] inputTensor = getInputTensor(rawImage, inputSize);
float[] outputs = new float[batchSize * numClasses];
// Feed the inputs to the model
inferenceInterface.feed(sharedConfig.getInputNodeName(), inputTensor, inputSize);
inferenceInterface.feed("dropout_prob", new float[]{1.0f});
inferenceInterface.run(modelDefinition.getOutputNames(), true);
inferenceInterface.fetch(modelDefinition.getOutputName(), outputs);
int result = argmax(outputs, numClasses);
Results
Digit 0 predicted with a 3.46 %
Digit 1 predicted with a 7.15 %
Digit 2 predicted with a 5.55 %
Digit 3 predicted with a 8.61 %
Digit 4 predicted with a 5.77 %
Digit 5 predicted with a 48.16 %
Digit 6 predicted with a 15.97 %
Digit 7 predicted with a 1.43 %
Digit 8 predicted with a 2.51 %
Digit 9 predicted with a 3.50 %
Q&A
Extras & Director’s Cut
● https://github.com/mccorby/ML-Android-Presentation-Model
● https://github.com/mccorby/ML-LetterPredictor-Android
○ number_predictor branch
Do you want to start? Prepare for this
● Change your mind!
● Data processing
● Maths
○ Linear Algebra, Statistics
● Python, C++ and some Java (and R and Scala and…)
○ Most common libraries use Python for prototyping
● Patience
Types of Algorithms
Some Concepts (II)
● Type of learning
○ Supervised learning
○ Unsupervised learning
○ Reinforcement learning
● Train and infer
● Type of outcome
○ Classification
○ Regression
○ Anomaly detection
Supervised Learning
Android
iOS
iOS
iOS
Android
Android
Supervised training Predictive Model
feature label
Unsupervised Learning
Neural Networks
● Non linear
● Based on how the neurons work (more or less)
● As with the brain, neuron are activated
● Produce a result in the last layer
○ And funny intermediate results!
Some names
● Yann LeCun
● Andrew Ng
● Geoffrey Hinton
● Fei-Fei Li
● Nick Bostrom
● Ray Kurzweil
… and some software
● Low level Frameworks
○ Tensorflow, Theanos, Caffe2, PyTorch
● High-level frameworks
○ Keras, Deeplearning4J, scikit-learn
● Util libraries
○ numpy, pandas, CUDA
ML in Android

ML in Android

  • 1.
  • 3.
    How did weget here?
  • 4.
    But what isMachine Learning? ● Subset of AI ● Narrow intelligence ● Highly interdisciplinary ● Let the process find without being explicitly programmed ○ But… Find what? A Model ● Your model is as good as the data you use
  • 5.
  • 6.
    What can youdo with ML?
  • 7.
    Examples ● Self-driving cars ●Auto color images ● Translation ● Text generation
  • 8.
  • 9.
    Where are wegoing? ● Better algorithms with less data ● AutoML ● Better integration with mobile ● Augmentation of your skills ○ Collaboration ● New roles: Machine trainers ● Terminator is not coming but look at these papers: ○ Pix2code: Generating Code from a Graphical User Interface Screenshot ○ DeepCoder: Learning to Write Programs ○ A deep learning model for estimating story points
  • 10.
  • 11.
    The Mobile Case ●Limited size and processing power ○ Yet we have GPUs! ● Forget about training ○ By now… ● Smaller models yet almost equally capable ● Sacrifice accuracy by speed/size
  • 12.
  • 13.
  • 14.
    Architectures. All inthe phone? Data Trainer Classifier model
  • 15.
  • 16.
    A basic typicalsimple DIY example ● Using MNIST ○ Toy dataset with handwritten digits ● Our app will classify user’s input ● We’ll use a Convolutional Neural Network ○ This is state-of-art :) ● Tensorflow
  • 17.
    Steps 1. Explore thedataset 2. Curate the dataset (if required) 3. Preprocess the input 4. Determine the train/test datasets 5. Choose your architecture 6. Define the hyperparameters 7. Train the model 8. Evaluate the model 9. Export the model 10. Use the exported model in the Android app with Tensorflow
  • 18.
  • 19.
    Preprocess the input ●The image can be flattened from matrix to a vector ○ Tensorflow is already doing this for you in the sample dataset! ● But important to remember as we will have to replicate the preprocessing in the client
  • 20.
    Choose your architecture ●We’ll go with a Convolutional Neural Network ○ Very good at finding patterns in images ● We could have used a neural network with one hidden layer ● Lost with the terms? ○ This is expected! ○ So many new terms and definitions and vocabulary
  • 21.
  • 22.
    A Convolutional NeuralNetwork def deepnn(x): W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32, name='dropout_prob') h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.add(tf.matmul(h_fc1_drop, W_fc2), b_fc2, name='output_node') return y_conv, keep_prob
  • 23.
    Build the DeepNetwork # Create the model x = tf.placeholder(tf.float32, [None, 784], name='input') # Define loss and optimizer y_ = tf.placeholder(tf.float32, [None, 10]) # Build the graph for the deep net y_conv, keep_prob = deepnn(x) cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  • 24.
    Train your Dragon withtf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(10000): batch = mnist.train.next_batch(50) if i % 100 == 0: train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) print('step %d, training accuracy %g' % (i, train_accuracy)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) # Create Saver saver = tf.train.Saver() # Save variables to .ckpt file saver.save(sess, "./runs/trained_variables.ckpt")
  • 25.
    Test if itflights print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
  • 26.
    How good wasthat? step 0, training accuracy 0.18 step 100, training accuracy 0.76 step 200, training accuracy 0.94 step 300, training accuracy 0.92 step 400, training accuracy 0.86 step 500, training accuracy 0.94 step 600, training accuracy 0.96 step 700, training accuracy 0.9 … step 9800, training accuracy 0.96 step 9900, training accuracy 1 test accuracy 0.9919
  • 27.
  • 28.
  • 29.
    Using Tensorflow inAndroid compile 'org.tensorflow:tensorflow-android:1.2.0' Gradle dependencies
  • 30.
    Using Tensorflow inAndroid inferenceInterface = new TensorFlowInferenceInterface(assetManager, MODEL_FILENAME); // Note: The size of the input tensor includes the batch size!! int inputSize = batchSize * imageSize * imageSize; float[] inputTensor = getInputTensor(rawImage, inputSize); float[] outputs = new float[batchSize * numClasses]; // Feed the inputs to the model inferenceInterface.feed(sharedConfig.getInputNodeName(), inputTensor, inputSize); inferenceInterface.feed("dropout_prob", new float[]{1.0f}); inferenceInterface.run(modelDefinition.getOutputNames(), true); inferenceInterface.fetch(modelDefinition.getOutputName(), outputs); int result = argmax(outputs, numClasses);
  • 31.
    Results Digit 0 predictedwith a 3.46 % Digit 1 predicted with a 7.15 % Digit 2 predicted with a 5.55 % Digit 3 predicted with a 8.61 % Digit 4 predicted with a 5.77 % Digit 5 predicted with a 48.16 % Digit 6 predicted with a 15.97 % Digit 7 predicted with a 1.43 % Digit 8 predicted with a 2.51 % Digit 9 predicted with a 3.50 %
  • 32.
  • 33.
    Extras & Director’sCut ● https://github.com/mccorby/ML-Android-Presentation-Model ● https://github.com/mccorby/ML-LetterPredictor-Android ○ number_predictor branch
  • 34.
    Do you wantto start? Prepare for this ● Change your mind! ● Data processing ● Maths ○ Linear Algebra, Statistics ● Python, C++ and some Java (and R and Scala and…) ○ Most common libraries use Python for prototyping ● Patience
  • 35.
  • 36.
    Some Concepts (II) ●Type of learning ○ Supervised learning ○ Unsupervised learning ○ Reinforcement learning ● Train and infer ● Type of outcome ○ Classification ○ Regression ○ Anomaly detection
  • 37.
  • 38.
  • 39.
    Neural Networks ● Nonlinear ● Based on how the neurons work (more or less) ● As with the brain, neuron are activated ● Produce a result in the last layer ○ And funny intermediate results!
  • 40.
    Some names ● YannLeCun ● Andrew Ng ● Geoffrey Hinton ● Fei-Fei Li ● Nick Bostrom ● Ray Kurzweil
  • 41.
    … and somesoftware ● Low level Frameworks ○ Tensorflow, Theanos, Caffe2, PyTorch ● High-level frameworks ○ Keras, Deeplearning4J, scikit-learn ● Util libraries ○ numpy, pandas, CUDA