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

    View full-size slide

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

    View full-size slide

  3. What is Neural Networks?

    View full-size slide

  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

    View full-size slide

  5. What is Keras?

    View full-size slide

  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

    View full-size slide

  7. What is CoreML?

    View full-size slide

  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.

    View full-size slide

  9. CoreML
    Trained Model
    Application
    Keras
    Train
    coremltools

    View full-size slide

  10. What is coremltools?

    View full-size slide

  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

    View full-size slide

  12. CoreML
    Trained Model
    Application
    Keras
    Train
    coremltools

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  15. 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

    View full-size slide

  16. 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 Λѻ͏ҝͷίʔυ͕ࣗಈੜ੒͞ΕΔ

    View full-size slide

  17. 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)
    )

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. CoreML
    Trained Model
    Application
    Keras
    Train
    coremltools
    Recap

    View full-size slide

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

    View full-size slide

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

    View full-size slide