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. CoreMLͰ͸͡ΊΔػցֶश Neural Networks on Keras ( TensorFlow backends ) Timers

    inc. / Github: naru-jpn / Twitter: @naruchigi
  2. CoreMLͰ͸͡ΊΔػցֶश Timers inc. / Github: naru-jpn / Twitter: @naruchigi Neural

    Networks on Keras ( TensorFlow backends )
  3. What is Neural Networks?

  4. 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
  5. What is Keras?

  6. 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
  7. What is CoreML?

  8. 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.
  9. CoreML Trained Model Application Keras Train coremltools

  10. What is coremltools?

  11. 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
  12. CoreML Trained Model Application Keras Train coremltools

  13. (Demo App)

  14. Environment - Tensorflow 1.1.0 (virtualenv) - Keras 1.2.2 - coremltools

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

    Keras ͷ࠷৽όʔδϣϯ΁ͷϦϯΫʹͳ͍ͬͯ·͕͢ɺ࣮ࡍ͸όʔδϣϯ 1.2.2 Λࢀর͠·͢ɻ https://github.com/fchollet/keras/tree/master/examples
  16. 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
  17. 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 Λѻ͏ҝͷίʔυ͕ࣗಈੜ੒͞ΕΔ
  18. 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) )
  19. Modify input value // 入力データの 0 番目の要素に 1.0 を代入 input.input1[0]

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

    input: self.input )
  21. CoreML Trained Model Application Keras Train coremltools Recap

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

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