LoginSignup
17
10

More than 5 years have passed since last update.

iOS11のCoreMLでMNISTをやってみた

Last updated at Posted at 2017-09-17

これはなに?

iOS11で機械学習(CoreML)をやってみたい方向けの初学者用メモ。備忘録。

ここで押さえている内容

  • PythonでKerasを使って.mlmodelを作成し、iPhoneの手書き文字で数字認識
  • 写真から数字認識

内容はハックするだけで、特に説明はない。

ポイント

  • iPhoneがなくてもMacのシュミレータで試せる
  • Xcode9が必要
    • Apple Developの無料アカウントで落とせるGM版で作業は可能

スクリーンショット 2017-09-17 22.52.20.png

Xcodeのダウンロードはとても根気がいるので先に落としておくのがいい

PythonでKerasを使って.mlmodelを作成し、iPhoneの手書き文字で数字認識

COMPUTER VISION IN IOS – COREML+KERAS+MNISTの流れ通りにすることで.mlmodelが作れる

トラブルシューティング

  • Support exports for Apple's new mlmodel format in Keras 2 #7007

    coremltoolsをインストールするとv0.3.6(2017/9/16時点)が入る
    keras==2.0.4ではなくkeras==2.0.6を入れること

  • Xcodeで.mlmodelを認識
    Xcodeでmlmodelを取り込んだ際は、Target Membershipにチェックを入れること
    なかなか認識しなかったりはする
    スクリーンショット 2017-09-17 22.48.12.png

こんな感じで動作

|スクリーンショット 2017-09-16 19.35.42.png|スクリーンショット 2017-09-16 19.40.35.png|

処理としては、DrawView.swiftで28x28のグレースケール画像を作成して、CoreMLに放り込むだけ。
.mlmodelの使い方を見ておけばそれで良さそう。

写真から数字認識

AppleのVision Framework: Building on Core ML
のページ下部にある「Image Classification with Vision and CoreML」からダウンロードする

スクリーンショット 2017-09-17 22.55.36.png

  • Swift4の変更によるエラーが出るのでコードの修正が必要。下記はパッチ
@@ -43,13 +43,13 @@
         guard let ciImage = CIImage(image: uiImage)
             else { fatalError("can't create CIImage from UIImage") }
         let orientation = CGImagePropertyOrientation(uiImage.imageOrientation)
-        inputImage = ciImage.applyingOrientation(Int32(orientation.rawValue))
+        inputImage = ciImage.oriented(orientation)

         // Show the image in the UI.
         imageView.image = uiImage

         // Run the rectangle detector, which upon completion runs the ML classifier.
-        let handler = VNImageRequestHandler(ciImage: ciImage, orientation: Int32(orientation.rawValue))
+        let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation)
         DispatchQueue.global(qos: .userInteractive).async {
             do {
                 try handler.perform([self.rectanglesRequest])
@@ -105,18 +105,18 @@
         let bottomLeft = detectedRectangle.bottomLeft.scaled(to: imageSize)
         let bottomRight = detectedRectangle.bottomRight.scaled(to: imageSize)
         let correctedImage = inputImage
-            .cropping(to: boundingBox)
-            .applyingFilter("CIPerspectiveCorrection", withInputParameters: [
+            .cropped(to: boundingBox)
+            .applyingFilter("CIPerspectiveCorrection", parameters: [
                 "inputTopLeft": CIVector(cgPoint: topLeft),
                 "inputTopRight": CIVector(cgPoint: topRight),
                 "inputBottomLeft": CIVector(cgPoint: bottomLeft),
                 "inputBottomRight": CIVector(cgPoint: bottomRight)
             ])
-            .applyingFilter("CIColorControls", withInputParameters: [
+            .applyingFilter("CIColorControls", parameters: [
                 kCIInputSaturationKey: 0,
                 kCIInputContrastKey: 32
             ])
-            .applyingFilter("CIColorInvert", withInputParameters: nil)
+            .applyingFilter("CIColorInvert", parameters: [:])

         // Show the pre-processed image
         DispatchQueue.main.async {

シュミレータで見てみる

実行するとシュミレータではこんな感じで動く、
使っている画像はシュミレータのSafariで落としたもの

スクリーンショット 2017-09-17 22.04.05.png

DLページのPDFにあるとおり、Vision Frameworkが四角の部分を認識するので、加工を施し、CoreMLで認識の流れになっている

スクリーンショット 2017-09-17 22.05.33.png

Kerasで作ったモデルと比較

AppleからDL: 0.999993
Kerasで自作: 0.975051

Appleで落としたモデルのが高性能(ノ∀`)アチャー

スクリーンショット 2017-09-17 22.22.21x.png

Next Step

Apple Core MLページのサンプル

スクリーンショット 2017-09-18 7.50.00.png

写真や静止画から分類、シュミレータに入っていた写真を利用

AzureのCustom Vision Service

Custom Vision ServiceではCoreML用に学習結果の出力ができる
また、リアルタイムに推論するコードも公開している。

※ 試すにはiOS11の入ったiPhoneが必要

IMG_1533.PNG
サンプルではフルーツを推論する学習結果が利用されている。
画像は手近にフルーツがないので適当な評価結果。

所感

機械学習の難しいことを知らなくてもやってみれば出来る感半端ない。
ひととおり流れをやると「ふーん、なるほどよくわからん」くらいにはなる

17
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
10