Sie sind auf Seite 1von 93

#WWDC18

What’s New in Core ML


Part two

Aseem Wadhwa, Core ML


Sohaib Qureshi, Core ML

© 2018 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
Core ML
Part One Recap

Float weights Quantized weights


Model size

Performance

Customization 60 MB 15 MB

Part One Recap

Model size

Performance

Customization

Part One Recap

Model size

Performance

Customization

Part One Recap

Model size

Performance

Customization

Part Two
Agenda
Agenda

Core ML Tools ecosystem


Agenda

Core ML Tools ecosystem


Quantization utilities
Agenda

Core ML Tools ecosystem


Quantization utilities

Custom conversion

Core ML Tools ecosystem


Quantization utilities

Custom conversion
?
Download
https://developer.apple.com/machine-learning/
NEW

Create ML
Last Year

Core ML Tools

https://github.com/apple/coremltools
Last Year

Core ML Tools
Today

Core ML Tools
Today

Core ML Tools
TensorFlow Converter
https://github.com/tf-coreml/tf-coreml

In collaboration with Google


TensorFlow Converter
https://github.com/tf-coreml/tf-coreml

Support for custom layers


TensorFlow Converter
https://github.com/tf-coreml/tf-coreml

Support for Quantized TensorFlow models


(coming soon!)
TensorFlow Converter
https://github.com/onnx/onnx-coreml

In collaboration with Facebook and Prisma


TensorFlow Converter
https://github.com/onnx/onnx-coreml

Core ML Tools ecosystem


Quantization utilities

Custom conversion

Core ML Tools 2.0



Quantization utilities
Core ML Tools 2.0
https://github.com/apple/coremltools

Support for latest Core ML .mlmodel specification

Quantization utilities

Flexible shape utilities


Quantization Utilities

Core ML Tools

Float32 weights Quantize weights 1-8 bit weights

Post-training Quantization
Quantization
Peeking under the hood

W W W
Quantization
Peeking under the hood

W
Quantization
Peeking under the hood

W
Quantization
Peeking under the hood

0.14
0.15
0.35
W 0.26
0.34
0.87
0.13

Weight Quantization
Peeking under the hood

Min Max

Float weights
Weight Quantization
Peeking under the hood

Min Max

Float weights

Quantized weights
Linear
Three-bit example

Min Max

Float weights

Quantized weights
0 1 2 3 4 5 6 7
Linear
Three-bit example

Min Max

Float weights

scale*quantized + bias

Quantized weights
0 1 2 3 4 5 6 7
Lookup Table
Three-bit example

Min Max

Float weights

Quantized weights
0 1 2 3 4 5 6 7
Lookup Table
Three-bit example

Min Max

Float weights

lookupTable[quantized]

Quantized weights
0 1 2 3 4 5 6 7
Quantization Utilities

Core ML Tools

Float32 weights Quantize weights 1-8 bit weights


Quantization Utilities

Core ML Tools

Float32 weights Quantize weights 1-8 bit weights

Decide on precision and algorithm


Quantization Utilities

Core ML Tools

Float32 weights Quantize weights 1-8 bit weights

Decide on precision and algorithm

Let Core ML Tools work its magic!


Demo
Quantization in Core ML Tools
// Quantize model using KMeans Lookup Table
quantized_model = quantize_weights(model, 8, ‘kmeans’)

// Compare Quantized model with original


compare_model(model, quantized_model, ‘./sample_data/’)
// Quantize model using KMeans Lookup Table
quantized_model = quantize_weights(model, 8, ‘kmeans’)

// Compare Quantized model with original


compare_model(model, quantized_model, ‘./sample_data/’)
// Quantize model using KMeans Lookup Table
quantized_model = quantize_weights(model, 8, ‘kmeans’)

// Compare Quantized model with original


compare_model(model, quantized_model, ‘./sample_data/’)
Model Size Versus Agreement
Model Size Versus Agreement

Model 1

Agreement Model 2
Metric

Float32 One bit

Model Size
Model Size Versus Agreement

Model 1

Agreement Model 2
Metric

Float32 One bit

Model Size
Model Size Versus Agreement

Metrics
• Accuracy Model 1

• Signal-to-noise ratio Agreement Model 2


Metric
• Visual inspection

• Model-specific metrics

Float32 One bit

Model Size
32 bit - 6.7 MB 16 bit - 3.4 MB 8 bit (linear) - 1.7 MB

4 bit (linear) - 857 KB 3 bit (linear) - 647 KB 2 bit (linear) - 437 KB


Core ML tools ecosystem


Quantization utilities

Custom conversion

Custom Conversion

Aseem Wadhwa, Core ML


New Neural Network Layer

New Model Architecture


?
New Neural Network Layer Customization

New Model Architecture


Customization
New Neural Network Layer
Model Conversion

Converters: Simple API


Model Conversion

Converters: Simple API

import coremltools
coremltools.converters.keras.convert(keras_model)
Model Conversion

Converters: Simple API

import onnx_coreml
onnx_coreml.convert(onnx_model)
Model Conversion

Converters: Simple API

import tfcoreml
tfcoreml.convert(tf_model_path=tf_model_path,
mlmodel_path=mlmodel_path,
output_feature_names=['output:0'])
Model Conversion

Converters: Simple API

import tfcoreml
tfcoreml.convert(tf_model_path=tf_model_path,
mlmodel_path=mlmodel_path,
output_feature_names=[‘output:0'],
)
Model Conversion

Converters: Simple API

import tfcoreml
tfcoreml.convert(tf_model_path=tf_model_path,
mlmodel_path=mlmodel_path,
output_feature_names=[‘output:0'],
)

Use Custom Layers!


Custom Layer Examples


Image Classifier

Beach
Opening the Hood

Beach

Neural Network
Convertible to Core ML

ReLU ReLU
Convolution Convolution Convolution
Activation Activation

Supported by Core ML
Not convertible to Core ML

New New
Convolution Convolution Convolution
Activation Activation

Supported by Core ML

Not Supported by Core ML


Custom Layer

New New
Convolution Convolution Convolution
Activation Activation

Supported by Core ML

Custom Layer in Core ML Model


Image Classifier

Beach

With Custom Activation Layer


Image Classifier

Beach

With Custom Activation Layer


Image Classifier
A Simple Classifier

Classifier Net
6
Localization Net

sha1_base64="8ziVlcrM346ng4sDFlX68r05NSY=">AAAB7nicjVDLSgNBEOyNrxhfUY9eBoPgKeyKoMegF48RzAOSJcxOOsmQ2dllplcISz7CiwdFvPo93vwbJ4+DioIFDUVVN91dUaqkJd//8Aorq2vrG8XN0tb2zu5eef+gaZPMCGyIRCWmHXGLSmpskCSF7dQgjyOFrWh8PfNb92isTPQdTVIMYz7UciAFJye1ujRC4qVeuRJU/TnY36QCS9R75fduPxFZjJqE4tZ2Aj+lMOeGpFA4LXUziykXYz7EjqOax2jDfH7ulJ04pc8GiXGlic3VrxM5j62dxJHrjDmN7E9vJv7mdTIaXIa51GlGqMVi0SBTjBI2+531pUFBauIIF0a6W5kYccMFuYT+GULzrBr41eD2vFK7WsZRhCM4hlMI4AJqcAN1aICAMTzAEzx7qffovXivi9aCt5w5hG/w3j4B3uSPPw==</latexit>
sha1_base64="8ziVlcrM346ng4sDFlX68r05NSY=">AAAB7nicjVDLSgNBEOyNrxhfUY9eBoPgKeyKoMegF48RzAOSJcxOOsmQ2dllplcISz7CiwdFvPo93vwbJ4+DioIFDUVVN91dUaqkJd//8Aorq2vrG8XN0tb2zu5eef+gaZPMCGyIRCWmHXGLSmpskCSF7dQgjyOFrWh8PfNb92isTPQdTVIMYz7UciAFJye1ujRC4qVeuRJU/TnY36QCS9R75fduPxFZjJqE4tZ2Aj+lMOeGpFA4LXUziykXYz7EjqOax2jDfH7ulJ04pc8GiXGlic3VrxM5j62dxJHrjDmN7E9vJv7mdTIaXIa51GlGqMVi0SBTjBI2+531pUFBauIIF0a6W5kYccMFuYT+GULzrBr41eD2vFK7WsZRhCM4hlMI4AJqcAN1aICAMTzAEzx7qffovXivi9aCt5w5hG/w3j4B3uSPPw==</latexit><latexit
sha1_base64="8ziVlcrM346ng4sDFlX68r05NSY=">AAAB7nicjVDLSgNBEOyNrxhfUY9eBoPgKeyKoMegF48RzAOSJcxOOsmQ2dllplcISz7CiwdFvPo93vwbJ4+DioIFDUVVN91dUaqkJd//8Aorq2vrG8XN0tb2zu5eef+gaZPMCGyIRCWmHXGLSmpskCSF7dQgjyOFrWh8PfNb92isTPQdTVIMYz7UciAFJye1ujRC4qVeuRJU/TnY36QCS9R75fduPxFZjJqE4tZ2Aj+lMOeGpFA4LXUziykXYz7EjqOax2jDfH7ulJ04pc8GiXGlic3VrxM5j62dxJHrjDmN7E9vJv7mdTIaXIa51GlGqMVi0SBTjBI2+531pUFBauIIF0a6W5kYccMFuYT+GULzrBr41eD2vFK7WsZRhCM4hlMI4AJqcAN1aICAMTzAEzx7qffovXivi9aCt5w5hG/w3j4B3uSPPw==</latexit><latexit
sha1_base64="8ziVlcrM346ng4sDFlX68r05NSY=">AAAB7nicjVDLSgNBEOyNrxhfUY9eBoPgKeyKoMegF48RzAOSJcxOOsmQ2dllplcISz7CiwdFvPo93vwbJ4+DioIFDUVVN91dUaqkJd//8Aorq2vrG8XN0tb2zu5eef+gaZPMCGyIRCWmHXGLSmpskCSF7dQgjyOFrWh8PfNb92isTPQdTVIMYz7UciAFJye1ujRC4qVeuRJU/TnY36QCS9R75fduPxFZjJqE4tZ2Aj+lMOeGpFA4LXUziykXYz7EjqOax2jDfH7ulJ04pc8GiXGlic3VrxM5j62dxJHrjDmN7E9vJv7mdTIaXIa51GlGqMVi0SBTjBI2+531pUFBauIIF0a6W5kYccMFuYT+GULzrBr41eD2vFK7WsZRhCM4hlMI4AJqcAN1aICAMTzAEzx7qffovXivi9aCt5w5hG/w3j4B3uSPPw==</latexit><latexit
latexit
<

Spatial Transformer Network

Grid Sampler
(Custom layer)

Jaderberg, Max, Karen Simonyan, and Andrew Zisserman. "Spatial transformer networks." Advances in neural information processing systems. 2015.
Classifier Net
6

Demo

Conversion with Custom Layer


Custom Layer
(Parameters)

gridSampler.swift
Custom Layer

Layer in a Neural Network


Custom Model

New Model

Core ML Custom Model Core ML


Summary
Core ML Tools 2.0
Core ML Tools 2.0

Rich Core ML Tools ecosystem


Core ML Tools 2.0

Rich Core ML Tools ecosystem


Easy-to-use quantization utilities


Core ML Tools 2.0

Rich Core ML Tools ecosystem


Easy-to-use quantization utilities


Integrate new layers


More Information
https://developer.apple.com/wwdc18/709

Machine Learning Lab Technology Lab 2 Wednesday 4:00PM

Machine Learning Lab Technology Lab 12 Friday 2:00PM

Das könnte Ihnen auch gefallen