XcodeのplaygroundでiOSのCore MLモデルを作成する

Core MLのモデルはXcodeのplyagroundで作ることができます。Create MLアプリが登場したいまplaygroundの出番はなさそうですが一度やっておきたかったので試してみました。

公式ドキュメントはこちらです。

Xcodeからplaygroundを起動します。templeteを選択しますが注意点としてmacOSのテンプレートを選択する必要があります。

スクリーンショット 2020-07-25 14.59.07

playgroundが起動します。

スクリーンショット 2020-07-25 14.57.40

入力済みのコードを消して以下のコードを貼り付けます。

import CreateMLUI
let builder = MLImageClassifierBuilder()
builder.showInLiveView()

ここで早速洗礼が、

スクリーンショット 2020-07-25 15.02.14

'MLImageClassifierBuilder' was deprecated in macOS 10.15: This feature is no longer supported in this environment. You can complete this task in the Create ML app.

学習で利用するMLImageClassifierBuilderはmacOS10.15でdeprecatedになったそうです。今後使うことはなさそうですね。なお別のクラスとしてMLImageClassifierが提供されているので今後はそちらを使うと良さそうです。

実行すると右側にトレーニング用のUIが表示されます。

スクリーンショット 2020-07-25 15.28.36

Drop Images to Begin Trainingと書かれている場所にトレーニング用のフォルダーをドロップスルトトレーニングが始まります。

スクリーンショット 2020-07-25 15.30.19

トレーニングが完了するとテストができます。適当な画像をドロップすると推論の結果が表示されます。

スクリーンショット 2020-07-25 15.31.42

Testing Dataがあればフォルダ毎ドロップするとテストにかけることができます。

スクリーンショット 2020-07-25 15.34.06

以下はコンソールのログです。

Automatic split: 5% of data is being used for validation.
Extracting image features from training data.
Analyzing and extracting image features.
+------------------+--------------+------------------+
| Images Processed | Elapsed Time | Percent Complete |
+------------------+--------------+------------------+
| 1                | 6.38s        | 0.5%             |
| 2                | 6.50s        | 1%               |
| 3                | 6.61s        | 1.5%             |
| 4                | 6.71s        | 2%               |
| 5                | 6.79s        | 2.5%             |
| 10               | 7.18s        | 5.25%            |
| 50               | 10.29s       | 27%              |
| 100              | 14.16s       | 54%              |
| 150              | 18.01s       | 81%              |
| 185              | 20.70s       | 100%             |
+------------------+--------------+------------------+
Extracting image features from user validation data.
Analyzing and extracting image features.
+------------------+--------------+------------------+
| Images Processed | Elapsed Time | Percent Complete |
+------------------+--------------+------------------+
| 1                | 76.557ms     | 6.5%             |
| 2                | 151.015ms    | 13.25%           |
| 3                | 227.697ms    | 20%              |
| 4                | 304.375ms    | 26.5%            |
| 5                | 383.275ms    | 33.25%           |
| 10               | 781.768ms    | 66.5%            |
| 15               | 1.16s        | 100%             |
+------------------+--------------+------------------+
Beginning model training on processed features. 
Calibrating solver; this may take some time.
+-----------+--------------+-------------------+---------------------+
| Iteration | Elapsed Time | Training Accuracy | Validation Accuracy |
+-----------+--------------+-------------------+---------------------+
| 0         | 0.020647     | 0.967568          | 0.933333            |
| 1         | 0.062796     | 0.994595          | 1.000000            |
| 2         | 0.085703     | 1.000000          | 1.000000            |
| 3         | 0.109700     | 1.000000          | 1.000000            |
| 4         | 0.143965     | 1.000000          | 1.000000            |
| 9         | 0.306308     | 1.000000          | 1.000000            |
+-----------+--------------+-------------------+---------------------+
Extracting image features from evaluation data.
Analyzing and extracting image features.
+------------------+--------------+------------------+
| Images Processed | Elapsed Time | Percent Complete |
+------------------+--------------+------------------+
| 1                | 193.601ms    | 0.5%             |
| 2                | 380.277ms    | 1%               |
| 3                | 542.609ms    | 1.5%             |
| 4                | 718.155ms    | 2%               |
| 5                | 868.825ms    | 2.5%             |
| 10               | 1.61s        | 5%               |
| 25               | 4.11s        | 12.5%            |
| 50               | 7.83s        | 25%              |
| 75               | 11.52s       | 37.5%            |
| 100              | 15.21s       | 50%              |
| 125              | 18.98s       | 62.5%            |
| 150              | 22.93s       | 75%              |
| 175              | 26.72s       | 87.5%            |
| 200              | 30.55s       | 100%             |
+------------------+--------------+------------------+
----------------------------------
Number of examples: 200
Number of classes: 2
Accuracy: 97.00%

******CONFUSION MATRIX******
----------------------------------
True¥Pred Cat  Dog  
Cat       97   3    
Dog       3    97   

******PRECISION RECALL******
----------------------------------
Class Precision(%)   Recall(%)      
Cat   97.00          97.00          
Dog   97.00          97.00          

ImageClassifierの右にあるボタンを押すとSaveボタンが出てくるのでモデルを保存します。

スクリーンショット 2020-07-25 15.35.01

保存したモデルをアプリに組み込んで試してみます、アプリはこちらを使いました。

画像9

分類できているようです。




この記事が気に入ったらサポートをしてみませんか?