Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CoreMLではじめる機械学習

 CoreMLではじめる機械学習

Neural Networks on Keras ( TensorFlow backends )

naru-jpn

June 21, 2017
Tweet

More Decks by naru-jpn

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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.
  4. 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
  5. Environment - Tensorflow 1.1.0 (virtualenv) - Keras 1.2.2 - coremltools

    0.3.0 - Xcode 9.0 beta ※ Tensorflow, Keras ͸ coremltools ͷରԠόʔδϣϯͰ͋Δඞཁ͕͋ΔͷͰগ͠ݹ͍Ͱ͢ɻ
  6. Programs to train neural networks - mnist_mlp.py - mnist_cnn.py ※

    Keras ͷ࠷৽όʔδϣϯ΁ͷϦϯΫʹͳ͍ͬͯ·͕͢ɺ࣮ࡍ͸όʔδϣϯ 1.2.2 Λࢀর͠·͢ɻ https://github.com/fchollet/keras/tree/master/examples
  7. 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
  8. 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 Λѻ͏ҝͷίʔυ͕ࣗಈੜ੒͞ΕΔ
  9. 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) )