SlideShare a Scribd company logo
1 of 45
Download to read offline
Machine Learning Models

on Mobile Devices
Lars Gregori
Why Machine Learning
Models on Mobile Devices?
•Privacy

•Availability

•Speed
What Models?
•Image

•Text

•…
What Models (CoreML)?
https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml
What Models (CoreML)?
convolutional
Caffe

Keras
random forests scikit-learn

XGBoost
multiclass classification
logic regression
https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml
How?
•CoreML

•TensorFlow Lite

•Other e.g. Caffe2Go
CoreML
•Apple

•WWDC 2017

•iOS, macOS, watchOS, tvOS
Application
Vision NaturalLanguage GameplayKit
CoreML
Accelerate und BNNS MetalPerformanceShaders
HardwareCPU GPU
CoreML
BNNS - Basic neural network subroutines

https://developer.apple.com/documentation/accelerate/bnns
TensorFlow Lite
•Google

•Based on TensorFlow Mobile

•Android, iOS and Raspberry Pi
TensorFlow Lite
.tflite fileAndroid App
Java API
C++ API
Interpreter
Android Neural Network API
Operators
.tflite fileiOS App
C++ API
Interpreter
Operators
DEMO
Object Detection
95.1% African elephant, Loxodonta africana

4.2% tusker

0.4% Indian elephant, Elephas maximus

0.0% moped

0.0% warthog
Inception V3
•Object detection (96.5% top-5)

•Google Brain, arXiv:1512.00567 

•1000 classes; ILSVRC2012*
ILSVRC2012 - ImageNet Large Scale Visual Recognition Challenge 2012
Inception V3 and CoreML
https://developer.apple.com/machine-learning/build-run-models/
Inception V3 and CoreML
Inception V3 and CoreML
override func viewDidLoad() {
super.viewDidLoad()
let model = try! VNCoreMLModel(for: Inceptionv3().model)
let request = VNCoreMLRequest(model: model,
completionHandler: resHandler)
self.requests.append(request)
}
Inceptionv3().model
resHandler
self.requests
Inception V3 and CoreML
func resHandler(request: VNRequest, _: Error?) {
let results = request.results
as! [VNClassificationObservation]
let percent = Int(results[0].confidence * 100)
let identifier = results[0].identifier
resultLabel.text = "(percent)% (identifier)"
}
resHandler
results
results
results
Inception V3 and CoreML
Inception V3 and CoreML
@IBAction func predict(_ sender: UIButton) {
selectedImage.image = sender.currentImage
let image = sender.currentImage!.cgImage!
let handler = VNImageRequestHandler(cgImage: image,
options: [:])
try? handler.perform(self.requests)
}
@IBAction
sender.currentImage!.cgImage!
handler.preform
handler
image =
image
self.requests
Inception V3 and TF Lite
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/models.md
Inception V3 and TF Lite
String tffile = “inception_v3.tflite”;
protected void onCreate(Bundle savedInstanceState) {
tflite = new Interpreter(loadModelFile(tffile));
button1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
predict(context, R.drawable.image1);
Interpreter
setOnClickListener
predict
Inception V3 and TF Lite
private void predict(Context context, int imgId) {
Bitmap bitmap = BitmapFactory.decodeResource(...,
imgId, ...);
image.setImageBitmap(bitmap);
ByteBuffer imgBuf = convertBitmapToByteBuffer(bitmap);
float[][] labelProb = new float[1][labels.size()];
tflite.run(imgBuf, labelProb);

>>>
predict
imgBuf
imgBuf
labelProb
labelProbtflite.run
Inception V3 and TF Lite
String label = "";
Float percent = -1.0f;
for(int index = 0; index < labels.size(); index++) {
if (labelProb[0][index] > percent) {
percent = labelProb[0][index];
label = labels.get(index);
}
labelProb
labelProb
index
index
index
index
labels
labels
Image Retraining
37.7% strawberry

11.9% ice cream, icecream

7.9% pineapple, ananas

2.8% ear, spike, capitulum

2.6% trifle
Image Retraining
•Transfer learning

•Already trained model

•New classification layer on top
https://www.tensorflow.org/hub/tutorials/image_retraining
Example
XOR
XOR
•0 xor 0 = 0

•0 xor 1 = 1

•1 xor 0 = 1

•1 xor 1 = 0
XOR Keras Model
training_data = np.array([[0,0], [0,1], [1,0], [1,1]])
target_data = np.array([ [0], [1], [1], [0]])
model = Sequential()
model.add(Dense(8, input_dim=2, activation=sigmoid))
model.add(Dense(1, activation=sigmoid))
model.compile(loss=mean_squared_error, optimizer=SGD(lr=1.0))
model.fit(training_data, target_data, epochs=2000)
>> Jupyter Notebook
Jupyter Notebook: https://github.com/choas/Machine_Learning_Models_In_Apps/blob/master/XOR/models/XOR.ipynb

SGD - stochastic gradient descent - https://en.wikipedia.org/wiki/Stochastic_gradient_descent

Sigmoid - https://en.wikipedia.org/wiki/Sigmoid_function
XOR Keras Model
print model.predict(training_data)
[[0.03998361]
[0.9534271 ]
[0.9549079 ]
[0.05220655]]
Save Model
•CoreML Tools (Keras, …)

•tfcoreml (Tensorflow)

•TensorFlow Lite Optimizing Converter
(TOCO)
DEMO
XOR
XOR Example
XOR and CoreML
override func viewDidLoad() {
super.viewDidLoad()
out00.text = "(predict(0, 0))"
out01.text = "(predict(0, 1))"
out10.text = "(predict(1, 0))"
out11.text = "(predict(1, 1))"
}
predict(0, 0)
predict(0, 1)
predict(1, 0)
predict(1, 1)
XOR and CoreML
func predict(_ a: NSNumber, _ b: NSNumber) -> NSNumber {
let input_data = try! MLMultiArray(shape:[2], …)
input_data[0] = a
input_data[1] = b
let xor_input = xorInput(input: input_data)
let prediction = try! xor().prediction(input: xor_input)
return prediction.result[0]
}
predict
input_data
input_data
input_data
input_data
xor().predictionprediction
prediction.result[0]
xorInput
XOR and TF Lite
protected void onCreate(Bundle savedInstanceState) {
// …
tflite = new Interpreter(loadModelFile("xor.tflite"));
result00.setText(prediction(0, 0));
result01.setText(prediction(0, 1));
result10.setText(prediction(1, 0));
result11.setText(prediction(1, 1));
}
tflite
prediction(0, 0)
prediction(0, 1)
prediction(1, 0)
prediction(1, 1)
XOR and TF Lite
private String prediction(int a, int b) {
float[][] in = new float[][]{{a, b}};
float[][] out = new float[][]{{0}};
tflite.run(in, out);
return String.valueOf(out[0][0]);
}
prediction
in
in
out
out
out[0][0]
tflite.run(in, out)
Quantization
Quantization
•A discrete set of values.

•TensorFlow Lite: 8-bits

•CoreML 2: Weight Quantization
Quantization - Inception V3
https://ai.googleblog.com/2016/08/improving-inception-and-image.html
Quantization - Inception V3
Convolution
https://ai.googleblog.com/2016/08/improving-inception-and-image.html
DEMO
Quantization
97% original Inception V3

97% 16-bit linear

98% 8-bit linear

2% 4-bit linear (pillow is wrong)
Summary
Summary
•Why

•How

•What will you build?
Thank you
@choas

More Related Content

Similar to Machine Learning Models on Mobile Devices

Build, Train & Deploy Machine Learning Models at Scale
Build, Train & Deploy Machine Learning Models at ScaleBuild, Train & Deploy Machine Learning Models at Scale
Build, Train & Deploy Machine Learning Models at ScaleAmazon Web Services
 
Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2Sarah Stemmler
 
From Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMakerFrom Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMakerAmazon Web Services
 
Build, train, and deploy machine learning models at scale
Build, train, and deploy machine learning models at scaleBuild, train, and deploy machine learning models at scale
Build, train, and deploy machine learning models at scaleAmazon Web Services
 
Amazon SageMaker (December 2018)
Amazon SageMaker (December 2018)Amazon SageMaker (December 2018)
Amazon SageMaker (December 2018)Julien SIMON
 
Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...
Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...
Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...Codiax
 
Distributed Deep Learning on AWS with Apache MXNet
Distributed Deep Learning on AWS with Apache MXNetDistributed Deep Learning on AWS with Apache MXNet
Distributed Deep Learning on AWS with Apache MXNetAmazon Web Services
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to pythonActiveState
 
DeepLearning and Advanced Machine Learning on IoT
DeepLearning and Advanced Machine Learning on IoTDeepLearning and Advanced Machine Learning on IoT
DeepLearning and Advanced Machine Learning on IoTRomeo Kienzler
 
Build, train, and deploy Machine Learning models at scale (May 2018)
Build, train, and deploy Machine Learning models at scale (May 2018)Build, train, and deploy Machine Learning models at scale (May 2018)
Build, train, and deploy Machine Learning models at scale (May 2018)Julien SIMON
 
AWS re:Invent 2018 - AIM401 - Deep Learning using Tensorflow
AWS re:Invent 2018 - AIM401 - Deep Learning using TensorflowAWS re:Invent 2018 - AIM401 - Deep Learning using Tensorflow
AWS re:Invent 2018 - AIM401 - Deep Learning using TensorflowJulien SIMON
 
Designing Artificial Intelligence
Designing Artificial IntelligenceDesigning Artificial Intelligence
Designing Artificial IntelligenceDavid Chou
 
[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...
[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...
[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...Amazon Web Services
 
SigOpt at GTC - Reducing operational barriers to optimization
SigOpt at GTC - Reducing operational barriers to optimizationSigOpt at GTC - Reducing operational barriers to optimization
SigOpt at GTC - Reducing operational barriers to optimizationSigOpt
 
Optimizing training on Apache MXNet (January 2018)
Optimizing training on Apache MXNet (January 2018)Optimizing training on Apache MXNet (January 2018)
Optimizing training on Apache MXNet (January 2018)Julien SIMON
 
Optimizing training on Apache MXNet
Optimizing training on Apache MXNetOptimizing training on Apache MXNet
Optimizing training on Apache MXNetAmazon Web Services
 
AWS re:Invent 2018 - ENT321 - SageMaker Workshop
AWS re:Invent 2018 - ENT321 - SageMaker WorkshopAWS re:Invent 2018 - ENT321 - SageMaker Workshop
AWS re:Invent 2018 - ENT321 - SageMaker WorkshopJulien SIMON
 
Building an ML Platform with Ray and MLflow
Building an ML Platform with Ray and MLflowBuilding an ML Platform with Ray and MLflow
Building an ML Platform with Ray and MLflowDatabricks
 

Similar to Machine Learning Models on Mobile Devices (20)

Build, Train & Deploy Machine Learning Models at Scale
Build, Train & Deploy Machine Learning Models at ScaleBuild, Train & Deploy Machine Learning Models at Scale
Build, Train & Deploy Machine Learning Models at Scale
 
Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2Machine Learning with TensorFlow 2
Machine Learning with TensorFlow 2
 
From Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMakerFrom Notebook to production with Amazon SageMaker
From Notebook to production with Amazon SageMaker
 
Build, train, and deploy machine learning models at scale
Build, train, and deploy machine learning models at scaleBuild, train, and deploy machine learning models at scale
Build, train, and deploy machine learning models at scale
 
Amazon SageMaker (December 2018)
Amazon SageMaker (December 2018)Amazon SageMaker (December 2018)
Amazon SageMaker (December 2018)
 
Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...
Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...
Julien Simon, Principal Technical Evangelist at Amazon - Machine Learning: Fr...
 
Introduction to ML.NET
Introduction to ML.NETIntroduction to ML.NET
Introduction to ML.NET
 
Distributed Deep Learning on AWS with Apache MXNet
Distributed Deep Learning on AWS with Apache MXNetDistributed Deep Learning on AWS with Apache MXNet
Distributed Deep Learning on AWS with Apache MXNet
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
 
DeepLearning and Advanced Machine Learning on IoT
DeepLearning and Advanced Machine Learning on IoTDeepLearning and Advanced Machine Learning on IoT
DeepLearning and Advanced Machine Learning on IoT
 
Build, train, and deploy Machine Learning models at scale (May 2018)
Build, train, and deploy Machine Learning models at scale (May 2018)Build, train, and deploy Machine Learning models at scale (May 2018)
Build, train, and deploy Machine Learning models at scale (May 2018)
 
AWS re:Invent 2018 - AIM401 - Deep Learning using Tensorflow
AWS re:Invent 2018 - AIM401 - Deep Learning using TensorflowAWS re:Invent 2018 - AIM401 - Deep Learning using Tensorflow
AWS re:Invent 2018 - AIM401 - Deep Learning using Tensorflow
 
Designing Artificial Intelligence
Designing Artificial IntelligenceDesigning Artificial Intelligence
Designing Artificial Intelligence
 
[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...
[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...
[REPEAT] Deep Learning Applications Using TensorFlow (AIM401-R) - AWS re:Inve...
 
SigOpt at GTC - Reducing operational barriers to optimization
SigOpt at GTC - Reducing operational barriers to optimizationSigOpt at GTC - Reducing operational barriers to optimization
SigOpt at GTC - Reducing operational barriers to optimization
 
Optimizing training on Apache MXNet (January 2018)
Optimizing training on Apache MXNet (January 2018)Optimizing training on Apache MXNet (January 2018)
Optimizing training on Apache MXNet (January 2018)
 
Optimizing training on Apache MXNet
Optimizing training on Apache MXNetOptimizing training on Apache MXNet
Optimizing training on Apache MXNet
 
AWS re:Invent 2018 - ENT321 - SageMaker Workshop
AWS re:Invent 2018 - ENT321 - SageMaker WorkshopAWS re:Invent 2018 - ENT321 - SageMaker Workshop
AWS re:Invent 2018 - ENT321 - SageMaker Workshop
 
MXNet Workshop
MXNet WorkshopMXNet Workshop
MXNet Workshop
 
Building an ML Platform with Ray and MLflow
Building an ML Platform with Ray and MLflowBuilding an ML Platform with Ray and MLflow
Building an ML Platform with Ray and MLflow
 

More from Lars Gregori

BYOM - Bring Your Own Model
BYOM - Bring Your Own ModelBYOM - Bring Your Own Model
BYOM - Bring Your Own ModelLars Gregori
 
uTensor - embedded devices and machine learning models
uTensor - embedded devices and machine learning modelsuTensor - embedded devices and machine learning models
uTensor - embedded devices and machine learning modelsLars Gregori
 
SAP Leonardo Machine Learning
SAP Leonardo Machine LearningSAP Leonardo Machine Learning
SAP Leonardo Machine LearningLars Gregori
 
Minecraft and reinforcement learning
Minecraft and reinforcement learningMinecraft and reinforcement learning
Minecraft and reinforcement learningLars Gregori
 
Minecraft and Reinforcement Learning
Minecraft and Reinforcement LearningMinecraft and Reinforcement Learning
Minecraft and Reinforcement LearningLars Gregori
 
IoT protocolls - smart washing machine
IoT protocolls - smart washing machineIoT protocolls - smart washing machine
IoT protocolls - smart washing machineLars Gregori
 
[DE] AI und Minecraft
[DE] AI und Minecraft[DE] AI und Minecraft
[DE] AI und MinecraftLars Gregori
 
Minecraft and Reinforcement Learning
Minecraft and Reinforcement LearningMinecraft and Reinforcement Learning
Minecraft and Reinforcement LearningLars Gregori
 
[DE] IoT Protokolle
[DE] IoT Protokolle[DE] IoT Protokolle
[DE] IoT ProtokolleLars Gregori
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile deviceLars Gregori
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile deviceLars Gregori
 
[German] Boards für das IoT-Prototyping
[German] Boards für das IoT-Prototyping[German] Boards für das IoT-Prototyping
[German] Boards für das IoT-PrototypingLars Gregori
 
IoT, APIs und Microservices - alles unter Node-RED
IoT, APIs und Microservices - alles unter Node-REDIoT, APIs und Microservices - alles unter Node-RED
IoT, APIs und Microservices - alles unter Node-REDLars Gregori
 
Web Bluetooth - Next Generation Bluetooth?
Web Bluetooth - Next Generation Bluetooth?   Web Bluetooth - Next Generation Bluetooth?
Web Bluetooth - Next Generation Bluetooth? Lars Gregori
 
Embedded Rust – Rust on IoT devices
Embedded Rust – Rust on IoT devicesEmbedded Rust – Rust on IoT devices
Embedded Rust – Rust on IoT devicesLars Gregori
 
Embedded Rust on IoT devices
Embedded Rust on IoT devicesEmbedded Rust on IoT devices
Embedded Rust on IoT devicesLars Gregori
 
IoT mit Rust programmieren
IoT mit Rust programmierenIoT mit Rust programmieren
IoT mit Rust programmierenLars Gregori
 
Boards for the IoT-Prototyping
Boards for the IoT-PrototypingBoards for the IoT-Prototyping
Boards for the IoT-PrototypingLars Gregori
 
Groß steuert klein - Wie lässt sich ein Arduino steuern?
Groß steuert klein - Wie lässt sich ein Arduino steuern?Groß steuert klein - Wie lässt sich ein Arduino steuern?
Groß steuert klein - Wie lässt sich ein Arduino steuern?Lars Gregori
 

More from Lars Gregori (20)

BYOM - Bring Your Own Model
BYOM - Bring Your Own ModelBYOM - Bring Your Own Model
BYOM - Bring Your Own Model
 
uTensor - embedded devices and machine learning models
uTensor - embedded devices and machine learning modelsuTensor - embedded devices and machine learning models
uTensor - embedded devices and machine learning models
 
SAP Leonardo Machine Learning
SAP Leonardo Machine LearningSAP Leonardo Machine Learning
SAP Leonardo Machine Learning
 
Minecraft and reinforcement learning
Minecraft and reinforcement learningMinecraft and reinforcement learning
Minecraft and reinforcement learning
 
Minecraft and Reinforcement Learning
Minecraft and Reinforcement LearningMinecraft and Reinforcement Learning
Minecraft and Reinforcement Learning
 
IoT protocolls - smart washing machine
IoT protocolls - smart washing machineIoT protocolls - smart washing machine
IoT protocolls - smart washing machine
 
[DE] AI und Minecraft
[DE] AI und Minecraft[DE] AI und Minecraft
[DE] AI und Minecraft
 
Minecraft and Reinforcement Learning
Minecraft and Reinforcement LearningMinecraft and Reinforcement Learning
Minecraft and Reinforcement Learning
 
[DE] IoT Protokolle
[DE] IoT Protokolle[DE] IoT Protokolle
[DE] IoT Protokolle
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile device
 
Using a trained model on your mobile device
Using a trained model on your mobile deviceUsing a trained model on your mobile device
Using a trained model on your mobile device
 
AI and Minecraft
AI and MinecraftAI and Minecraft
AI and Minecraft
 
[German] Boards für das IoT-Prototyping
[German] Boards für das IoT-Prototyping[German] Boards für das IoT-Prototyping
[German] Boards für das IoT-Prototyping
 
IoT, APIs und Microservices - alles unter Node-RED
IoT, APIs und Microservices - alles unter Node-REDIoT, APIs und Microservices - alles unter Node-RED
IoT, APIs und Microservices - alles unter Node-RED
 
Web Bluetooth - Next Generation Bluetooth?
Web Bluetooth - Next Generation Bluetooth?   Web Bluetooth - Next Generation Bluetooth?
Web Bluetooth - Next Generation Bluetooth?
 
Embedded Rust – Rust on IoT devices
Embedded Rust – Rust on IoT devicesEmbedded Rust – Rust on IoT devices
Embedded Rust – Rust on IoT devices
 
Embedded Rust on IoT devices
Embedded Rust on IoT devicesEmbedded Rust on IoT devices
Embedded Rust on IoT devices
 
IoT mit Rust programmieren
IoT mit Rust programmierenIoT mit Rust programmieren
IoT mit Rust programmieren
 
Boards for the IoT-Prototyping
Boards for the IoT-PrototypingBoards for the IoT-Prototyping
Boards for the IoT-Prototyping
 
Groß steuert klein - Wie lässt sich ein Arduino steuern?
Groß steuert klein - Wie lässt sich ein Arduino steuern?Groß steuert klein - Wie lässt sich ein Arduino steuern?
Groß steuert klein - Wie lässt sich ein Arduino steuern?
 

Recently uploaded

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 

Machine Learning Models on Mobile Devices

  • 1. Machine Learning Models
 on Mobile Devices Lars Gregori
  • 2. Why Machine Learning Models on Mobile Devices? •Privacy •Availability •Speed
  • 5. What Models (CoreML)? convolutional Caffe
 Keras random forests scikit-learn
 XGBoost multiclass classification logic regression https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml
  • 8. Application Vision NaturalLanguage GameplayKit CoreML Accelerate und BNNS MetalPerformanceShaders HardwareCPU GPU CoreML BNNS - Basic neural network subroutines https://developer.apple.com/documentation/accelerate/bnns
  • 9. TensorFlow Lite •Google •Based on TensorFlow Mobile •Android, iOS and Raspberry Pi
  • 10. TensorFlow Lite .tflite fileAndroid App Java API C++ API Interpreter Android Neural Network API Operators .tflite fileiOS App C++ API Interpreter Operators
  • 12. 95.1% African elephant, Loxodonta africana 4.2% tusker 0.4% Indian elephant, Elephas maximus 0.0% moped 0.0% warthog
  • 13. Inception V3 •Object detection (96.5% top-5) •Google Brain, arXiv:1512.00567 •1000 classes; ILSVRC2012* ILSVRC2012 - ImageNet Large Scale Visual Recognition Challenge 2012
  • 14. Inception V3 and CoreML https://developer.apple.com/machine-learning/build-run-models/
  • 16. Inception V3 and CoreML override func viewDidLoad() { super.viewDidLoad() let model = try! VNCoreMLModel(for: Inceptionv3().model) let request = VNCoreMLRequest(model: model, completionHandler: resHandler) self.requests.append(request) } Inceptionv3().model resHandler self.requests
  • 17. Inception V3 and CoreML func resHandler(request: VNRequest, _: Error?) { let results = request.results as! [VNClassificationObservation] let percent = Int(results[0].confidence * 100) let identifier = results[0].identifier resultLabel.text = "(percent)% (identifier)" } resHandler results results results
  • 19. Inception V3 and CoreML @IBAction func predict(_ sender: UIButton) { selectedImage.image = sender.currentImage let image = sender.currentImage!.cgImage! let handler = VNImageRequestHandler(cgImage: image, options: [:]) try? handler.perform(self.requests) } @IBAction sender.currentImage!.cgImage! handler.preform handler image = image self.requests
  • 20. Inception V3 and TF Lite https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/models.md
  • 21. Inception V3 and TF Lite String tffile = “inception_v3.tflite”; protected void onCreate(Bundle savedInstanceState) { tflite = new Interpreter(loadModelFile(tffile)); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { predict(context, R.drawable.image1); Interpreter setOnClickListener predict
  • 22. Inception V3 and TF Lite private void predict(Context context, int imgId) { Bitmap bitmap = BitmapFactory.decodeResource(..., imgId, ...); image.setImageBitmap(bitmap); ByteBuffer imgBuf = convertBitmapToByteBuffer(bitmap); float[][] labelProb = new float[1][labels.size()]; tflite.run(imgBuf, labelProb);
 >>> predict imgBuf imgBuf labelProb labelProbtflite.run
  • 23. Inception V3 and TF Lite String label = ""; Float percent = -1.0f; for(int index = 0; index < labels.size(); index++) { if (labelProb[0][index] > percent) { percent = labelProb[0][index]; label = labels.get(index); } labelProb labelProb index index index index labels labels
  • 24. Image Retraining 37.7% strawberry 11.9% ice cream, icecream 7.9% pineapple, ananas 2.8% ear, spike, capitulum 2.6% trifle
  • 25. Image Retraining •Transfer learning •Already trained model •New classification layer on top https://www.tensorflow.org/hub/tutorials/image_retraining
  • 27. XOR •0 xor 0 = 0 •0 xor 1 = 1 •1 xor 0 = 1 •1 xor 1 = 0
  • 28. XOR Keras Model training_data = np.array([[0,0], [0,1], [1,0], [1,1]]) target_data = np.array([ [0], [1], [1], [0]]) model = Sequential() model.add(Dense(8, input_dim=2, activation=sigmoid)) model.add(Dense(1, activation=sigmoid)) model.compile(loss=mean_squared_error, optimizer=SGD(lr=1.0)) model.fit(training_data, target_data, epochs=2000) >> Jupyter Notebook Jupyter Notebook: https://github.com/choas/Machine_Learning_Models_In_Apps/blob/master/XOR/models/XOR.ipynb SGD - stochastic gradient descent - https://en.wikipedia.org/wiki/Stochastic_gradient_descent Sigmoid - https://en.wikipedia.org/wiki/Sigmoid_function
  • 29. XOR Keras Model print model.predict(training_data) [[0.03998361] [0.9534271 ] [0.9549079 ] [0.05220655]]
  • 30. Save Model •CoreML Tools (Keras, …) •tfcoreml (Tensorflow) •TensorFlow Lite Optimizing Converter (TOCO)
  • 33. XOR and CoreML override func viewDidLoad() { super.viewDidLoad() out00.text = "(predict(0, 0))" out01.text = "(predict(0, 1))" out10.text = "(predict(1, 0))" out11.text = "(predict(1, 1))" } predict(0, 0) predict(0, 1) predict(1, 0) predict(1, 1)
  • 34. XOR and CoreML func predict(_ a: NSNumber, _ b: NSNumber) -> NSNumber { let input_data = try! MLMultiArray(shape:[2], …) input_data[0] = a input_data[1] = b let xor_input = xorInput(input: input_data) let prediction = try! xor().prediction(input: xor_input) return prediction.result[0] } predict input_data input_data input_data input_data xor().predictionprediction prediction.result[0] xorInput
  • 35. XOR and TF Lite protected void onCreate(Bundle savedInstanceState) { // … tflite = new Interpreter(loadModelFile("xor.tflite")); result00.setText(prediction(0, 0)); result01.setText(prediction(0, 1)); result10.setText(prediction(1, 0)); result11.setText(prediction(1, 1)); } tflite prediction(0, 0) prediction(0, 1) prediction(1, 0) prediction(1, 1)
  • 36. XOR and TF Lite private String prediction(int a, int b) { float[][] in = new float[][]{{a, b}}; float[][] out = new float[][]{{0}}; tflite.run(in, out); return String.valueOf(out[0][0]); } prediction in in out out out[0][0] tflite.run(in, out)
  • 38. Quantization •A discrete set of values. •TensorFlow Lite: 8-bits •CoreML 2: Weight Quantization
  • 39. Quantization - Inception V3 https://ai.googleblog.com/2016/08/improving-inception-and-image.html
  • 40. Quantization - Inception V3 Convolution https://ai.googleblog.com/2016/08/improving-inception-and-image.html
  • 42. 97% original Inception V3 97% 16-bit linear 98% 8-bit linear 2% 4-bit linear (pillow is wrong)