Slide 1

Slide 1 text

CoreMLͰ͸͡ΊΔػցֶश Neural Networks on Keras ( TensorFlow backends ) Timers inc. / Github: naru-jpn / Twitter: @naruchigi

Slide 2

Slide 2 text

CoreMLͰ͸͡ΊΔػցֶश Timers inc. / Github: naru-jpn / Twitter: @naruchigi Neural Networks on Keras ( TensorFlow backends )

Slide 3

Slide 3 text

What is Neural Networks?

Slide 4

Slide 4 text

One of machine learning models. - Neural networks - Tree ensembles - Support vector machines - Generalized linear models - … https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml

Slide 5

Slide 5 text

What is Keras?

Slide 6

Slide 6 text

Theano TensorFlow Keras Keras is a high-level neural networks API, written in Python and capable of running on top of either TensorFlow, CNTK or Theano. https://keras.io

Slide 7

Slide 7 text

What is CoreML?

Slide 8

Slide 8 text

Accelerate and BNNS Metal Performance Shaders CoreML BNNS : Basic Neural Network Subroutines https://developer.apple.com/documentation/coreml With Core ML, you can integrate trained machine learning models into your app. Core ML requires the Core ML model format.

Slide 9

Slide 9 text

CoreML Trained Model Application Keras Train coremltools

Slide 10

Slide 10 text

What is coremltools?

Slide 11

Slide 11 text

Convert existing models to .mlmodel format from popular machine learning tools including Keras, Caffe, scikit-learn, libsvm, and XGBoost. https://pypi.python.org/pypi/coremltools coremltools

Slide 12

Slide 12 text

CoreML Trained Model Application Keras Train coremltools

Slide 13

Slide 13 text

(Demo App)

Slide 14

Slide 14 text

Environment - Tensorflow 1.1.0 (virtualenv) - Keras 1.2.2 - coremltools 0.3.0 - Xcode 9.0 beta ※ Tensorflow, Keras ͸ coremltools ͷରԠόʔδϣϯͰ͋Δඞཁ͕͋ΔͷͰগ͠ݹ͍Ͱ͢ɻ

Slide 15

Slide 15 text

Programs to train neural networks - mnist_mlp.py - mnist_cnn.py ※ Keras ͷ࠷৽όʔδϣϯ΁ͷϦϯΫʹͳ͍ͬͯ·͕͢ɺ࣮ࡍ͸όʔδϣϯ 1.2.2 Λࢀর͠·͢ɻ https://github.com/fchollet/keras/tree/master/examples

Slide 16

Slide 16 text

Convert model with coremltools 1. Import coremltools import coremltools model = Sequential() …
 coreml_model = coremltools.converters.keras.convert(model) coreml_model.save("keras_mnist_mlp.mlmodel") 2. Convert model

Slide 17

Slide 17 text

Import model into Xcode project // 入力データ class keras_mnist_mlpInput : MLFeatureProvider { var input1: MLMultiArray // … } // 出力データ class keras_mnist_mlpOutput : MLFeatureProvider { var output1: MLMultiArray // … } // モデル @objc class keras_mnist_mlp:NSObject { var model: MLModel init(contentsOf url: URL) throws { self.model = try MLModel(contentsOf: url) } // … func prediction(input: keras_mnist_mlpInput) throws -> keras_mnist_mlpOutput { // … keras_mnist_mlp.mlmodel Λѻ͏ҝͷίʔυ͕ࣗಈੜ੒͞ΕΔ

Slide 18

Slide 18 text

Prepare model and input in code // モデルの作成 let model = keras_mnist_mlp() // 入力データの格納用変数 (入力は28*28の画像) let input = keras_mnist_mlpInput( input1: try! MLMultiArray(shape: [784], dataType: .double) )

Slide 19

Slide 19 text

Modify input value // 入力データの 0 番目の要素に 1.0 を代入 input.input1[0] = NSNumber(value: 1.0)

Slide 20

Slide 20 text

Make a prediction // モデルに入力データを渡して計算 let output = try model.prediction( input: self.input )

Slide 21

Slide 21 text

CoreML Trained Model Application Keras Train coremltools Recap

Slide 22

Slide 22 text

Demo App on Github https://github.com/naru-jpn/MLModelSample

Slide 23

Slide 23 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠