710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection...

118
#WWDC17 © 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Krishna Sridhar, Core ML Zach Nation, Core ML Core ML in Depth System Frameworks

Transcript of 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection...

Page 1: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

#WWDC17

© 2017 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Krishna Sridhar, Core ML Zach Nation, Core ML

•Core ML in Depth

System Frameworks

Page 2: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

The easiest way to integrate machine learning models into your app.

Page 3: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

The easiest way to integrate machine learning models into your app.

Page 4: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

The easiest way to integrate machine learning models into your app.

Page 5: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

The easiest way to integrate machine learning models into your app.

Page 6: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

The easiest way to integrate machine learning models into your app.

Page 7: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Think of models as code

Page 8: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Development Flow

Page 9: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode

Development Flow

Page 10: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode

Development Flow

Page 11: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode Your App

Development Flow

Page 12: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode Your App

Development Flow

Page 13: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Rose Confidence 95%

Page 14: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Rose Confidence 95%

let model = FlowerClassifier()

if let prediction = try? model.prediction(image: image) { return prediction.flowerType }

Page 15: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Rose Confidence 95%

let model = FlowerClassifier()

if let prediction = try? model.prediction(image: image) { return prediction.flowerType }

Page 16: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

This Session

Use Cases

Page 17: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

This Session

Hardware Optimized

Accelerate MPS

Core ML

GPUCPU

Use Cases

Page 18: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

This Session

Obtaining ModelsHardware Optimized

Accelerate MPS

Core ML

GPUCPU

Use Cases

Page 19: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

This Session

Obtaining ModelsHardware Optimized

Accelerate MPS

Core ML

GPUCPU

Use Cases

Page 20: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 21: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 22: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 23: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 24: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 25: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis

Personalization

Object Detection

Summarization

Style Transfer

Gesture Recognition

Music Tagging

Page 26: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis

Personalization

Object Detection

Summarization

Style Transfer

Gesture Recognition

Music Tagging

Page 27: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis

Personalization

Object Detection

Summarization

Style Transfer

Gesture Recognition

Music Tagging

Page 28: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis

Personalization

Object Detection

Summarization

Style Transfer

Gesture Recognition

Music Tagging

Page 29: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis

Personalization

Object Detection

Summarization

Style Transfer

Gesture Recognition

Music Tagging

Tree Ensembles

Convolution Neural Networks

Support Vector Machines

Generalized Linear Models

Feedforward Neural Networks

Recurrent Neural Networks

Page 30: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Convolution Neural Networks

Feedforward Neural Networks

Recurrent Neural Networks

Page 31: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Neural NetworkVectorize Map to

String Rose

Pipeline

Page 32: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Focus on code, not models!

Sentiment Analysis

Personalization

Object Detection

Summarization

Style Transfer

Gesture Recognition

Music Tagging

Page 33: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Numeric

Categories

Images

Arrays

Dictionaries

Models as Functions

“Rose”

Page 34: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Numeric and Categories developer.apple.com/machine-learning

Numeric

Categories

Images

Arrays

Dictionaries

Double, Int64

String, Int64

CVPixelBuffer

MLMultiArray

[String : Double], [Int64 : Double]

Page 35: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Images

Numeric

Categories

Images

Arrays

Dictionaries

Double, Int64

String, Int64

CVPixelBuffer

MLMultiArray

[String : Double], [Int64 : Double]

Page 36: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Multi-Dimensional Arrays

Numeric

Categories

Images

Arrays

Dictionaries

Double, Int64

String, Int64

CVPixelBuffer

MLMultiArray

[String : Double], [Int64 : Double]

Page 37: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Dictionaries

Numeric

Categories

Images

Arrays

Dictionaries

Double, Int64

String, Int64

CVPixelBuffer

MLMultiArray

[String : Double], [Int64 : Double]

Page 38: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

•Working with Text

Page 39: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 40: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

CoreML is awesome.I love using it

Page 41: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 42: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Today’s lunch wasterrible anddisappointing

Page 43: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis Application

Page 44: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis Application

Page 45: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis Application

!

!

!

Page 46: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sentiment Analysis Application

!

!

!

Page 47: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Using Other Formats

Word Counts

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

Page 48: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Using Other Formats

Word Counts

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

Page 49: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Using Other Formats

Word Counts

!

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

Page 50: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Word Counts

Use NLP to Process Text

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

Page 51: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Use Core MLUse NLP (NSLinguisticTagger)

Sentiment Analysis Application

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

!

Page 52: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 53: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Processing Text

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

func tokenizeAndCountWords(rawTxt: String) -> [String : Double] { // Tokenize using NSLinguisticTagger ... // Count occurrences of each token}

Page 54: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

!

Making Predictions

{ "Core" : 1, "ML" : 1, "is" : 1, "awesome" : 1, "I" : 1, "love" : 1, "using" : 1, "it" : 1,}

let model = SentimentAnalysis()if let prediction = try? model.prediction(wordCounts: counts) { print("Sentiment: \(prediction.sentiment)")}

Page 55: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Text Gotchas

Most models don’t work directly on raw text

!

Page 56: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 57: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Predictive Keyboard

I’m not sure Oliver will eat oysters, but he will

Love

Page 58: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Predictive Keyboard

I’m not sure Oliver will eat oysters, but he will

Love

Task - Next word prediction

Page 59: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 60: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Language Models

Page 61: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Shakespeare Keyboard

Page 62: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Shakespeare Keyboard

Current Word

Page 63: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Shakespeare Keyboard

Current Word Next Word Choices

State

Page 64: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Shakespeare Keyboard

Current Word Next Word Choices

State

Next Word

Page 65: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Shakespeare Keyboard

Current Word Next Word Choices

State

Next Word

Page 66: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Shakespeare Keyboard

Current Word Next Word Choices

State

Next Word

State

Page 67: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sample Code

if let output = try? model.prediction(input: input) { // Send the best 3 words to the user displayTopPredictions(output.nextWordProb) }

// Update state for next predictioninput.state = output.stateinput.currentWord = getNextWordFromUser()

Page 68: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sample Code

if let output = try? model.prediction(input: input) { // Send the best 3 words to the user displayTopPredictions(output.nextWordProb) }

// Update state for next predictioninput.state = output.stateinput.currentWord = getNextWordFromUser()

Page 69: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sample Code

if let output = try? model.prediction(input: input) { // Send the best 3 words to the user displayTopPredictions(output.nextWordProb) }

// Update state for next predictioninput.state = output.stateinput.currentWord = getNextWordFromUser()

Page 70: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Sample Code

if let output = try? model.prediction(input: input) { // Send the best 3 words to the user displayTopPredictions(output.nextWordProb) }

// Update state for next predictioninput.state = output.stateinput.currentWord = getNextWordFromUser()

Page 71: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Come to the Labs

Thursday

Friday

11:00–3:30

1:30–4:00

Page 72: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

This Session

Use Cases Obtaining ModelsHardware Optimized

Accelerate MPS

Core ML

GPUCPU

Page 73: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 74: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')
Page 75: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Built on Performance Primitives

MPS

NLP

Accelerate

Core ML

Vision

Your app

ML Performance Primitives

GPUCPUHardware

Page 76: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Optimized for Hardware

Runs on GPU

MPS

Page 77: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Optimized for Hardware

Runs on GPU

MPS

Runs on CPU

Accelerate

Page 78: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Optimized for Hardware

Page 79: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Optimized for Hardware

Runs on Core ML

Page 80: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Dog playing in a soccer field

Context Switching

Runs on GPU Runs on CPU

Page 81: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

This Session

Use Cases Obtaining ModelsHardware Optimized

Accelerate MPS

Core ML

GPUCPU

Page 82: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode Your App

Deploying Core ML Models

Page 83: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode Your App

Deploying Core ML Models

Where do models come from?

Page 84: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Example Models Task specific Core ML models

developer.apple.com/machine-learning

Page 85: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Popular ML libraries

Many models

Thriving communities

Tap into ML Community

Page 86: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Popular ML libraries

Many models

Thriving communities

Tap into ML Community

Page 87: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Core ML Tools

Page 88: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Open SourceCore ML Tools

Page 89: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode

Conversion Workflow

Your App

Page 90: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode

Conversion Workflow

Your AppModel Source

e.g. Caffe

Page 91: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode

Conversion Workflow

Your AppModel Source

e.g. Caffe

Page 92: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Getting Started

## Download and install python package > pip install coremltools

Page 93: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

What Is coremltools?

Converter Library

Core ML Specification

Core ML Bindings

Converters Convert from other formats

Page 94: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

What Is coremltools?

Converter Library

Core ML Specification

Core ML Bindings

Converters Convert from other formats

Build your own converter

Page 95: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

What Is coremltools?

Converter Library

Core ML Specification

Core ML Bindings

Converters Convert from other formats

Build your own converter

Write your own models

Page 96: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Compatible and Extensible

Compatible

Converter Library

Core ML Specification

Core ML Bindings

Converters

Page 97: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Compatible and Extensible

Converter Library

Core ML Specification

Core ML Bindings

Converters

Extensible

Page 98: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Core ML Model developer.apple.com/machine-learning

Single document

Encapsulates • Functional description (inputs outputs) • Trained parameters

Public format

Converter Library

Core ML Specification

Core ML Bindings

Converters

Page 99: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode Model View

Page 100: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Xcode Model View

Page 101: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Core ML Converters

Unified APIs to convert models from various formats to Core ML

Converter Library

Core ML Specification

Core ML Bindings

ConvertersModel Source

e.g. Caffe

In Xcode

Convert

Page 102: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Flower Classification in Caffe

.caffemodel .prototxt

74

Page 103: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Flower Classification in Caffe

.caffemodel .prototxt

74 “Rose”labels.txt

Page 104: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Needed for conversion to Core ML format

Flower Classification in Caffe

.caffemodel .prototxt

74 “Rose”labels.txt

Page 105: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

•Demo

Page 106: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Demo Summary

import coremltools

caffe_model = ('flowers.caffemodel','flowers.prototxt') model = coremltools.converters.caffe.convert( caffe_model, image_input_names = 'data', class_labels = 'labels.txt')

model.save('FlowerClassifier.mlmodel')

Page 107: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Demo Summary

import coremltools

keras_model = 'flowers.h5' model = coremltools.converters.keras.convert( keras_model, image_input_names = 'data', class_labels = 'labels.txt')

model.save('FlowerClassifier.mlmodel')

Page 108: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Pipelines

Supported Packages

Neural Networks Tree Ensembles Linear Models Support Vector Machines

Page 109: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Obtaining Models Summary

developer.apple.com/machine-learning

Convert

Page 110: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Summary

Page 111: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Summary

Easy integration of ML models

Page 112: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Summary

Easy integration of ML models

Rich datatype support

Page 113: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Summary

Easy integration of ML models

Rich datatype support

Hardware optimized

Page 114: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Summary

Easy integration of ML models

Rich datatype support

Hardware optimized

Compatible with popular formats

Page 115: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

More Informationhttps://developer.apple.com/wwdc17/710

Page 116: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Related Sessions

Introducing Core ML WWDC 2017

Vision Framework: Building on Core ML WWDC 2017

Natural Language Processing and your Apps WWDC 2017

Accelerate and Sparse Solvers Grand Ballroom A Thursday 10:00AM

Using Metal 2 for Compute Grand Ballroom A Thursday 4:10PM

Page 117: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')

Labs

Core ML and Natural Language Processing Lab Technology Lab D Thu 11:00AM-3:30PM

Core ML and Natural Language Processing Lab Technology Lab D Fri 1:50PM-4:00PM

Page 118: 710 CoreMLinDepth 04 D - Apple Inc. · Sentiment Analysis Personalization Object Detection Summarization Style Transfer Gesture Recognition ... ('flowers.caffemodel','flowers.prototxt')