その後のその後

iOSエンジニア 堤 修一のブログ github.com/shu223

ドラッグ&ドロップで機械学習のモデルがつくれる「Create ML」の使い方

iOS 12の気になる新機能のAPIを見ていくシリーズ。昨日はARKit 2の永続化・共有機能や3D物体検出機能について書きました。

本記事ではCreate MLについて。1

Create ML

Create MLは、Core MLのモデルを作成するためのmacOSの新フレームワークです。

f:id:shu223:20180606070639p:plain

昨日のState of the Unionにてデモがありましたが、なんと、学習用データが入ったフォルダをドラッグ&ドロップするだけで作成できます。

ちなみにmacOS 10.14 Mojaveです。

MLImageClassifierBuilder

まだMojaveにアップデートしていないので試せていない2のですが、丁寧なチュートリアル記事が出ていて、作業手順を図付きで確認できます。

PlaygroundsでMLImageClassifierBuildeを初期化してshowInLiveViewを呼ぶコードを書き、

import CreateMLUI

let builder = MLImageClassifierBuilder()
builder.showInLiveView()

実行するとGUIが表れ、そこにデータフォルダをドラッグ&ドロップすると学習が開始されます。

f:id:shu223:20180606071137p:plain

f:id:shu223:20180606071232p:plain

テスト(モデルの評価)もドラッグ&ドロップ。

f:id:shu223:20180606071320p:plain

いかがでしょうか。コードは正味3行書いただけ機械学習ができてしまいました。

Create MLでつくった.mlmodelフォーマットのモデルファイルを使ったアプリの実装方法はこちらの記事をどうぞ。

Core ML+Visionを用いた物体認識の最小実装 - Qiita

学習用データについて

ドラッグ&ドロップでできます」とはいえ、そのためのデータセットを用意する必要はあります。

どういうデータを用意するかですが、同チュートリアル記事によると、

  • ラベルごとに最低でも10枚の画像
  • ラベルごとに枚数のバランスをとること(チーターは10枚、ゾウは1000枚、みたいなことをしない)

Use at least 10 images per label for the training set, but more is always better. Also, balance the number of images for each label. For example, don’t use 10 images for Cheetah and 1000 images for Elephant.

  • JPEGPNG等、画像フォーマットはなんでもいい(UTIがpublic.imageに適合していれば)
  • サイズは揃ってなくてもOK
  • サイズもなんでもいいが、最低でも299x299ピクセルはあった方が良い

The images can be in any format whose uniform type identifer conforms to public.image. This includes common formats like JPEG and PNG. The images don’t have to be the same size as each other, nor do they have to be any particular size, although it’s best to use images that are at least 299x299 pixels.

あとは実際に推論を行うのと同じ状況で学習データも収集した方がいいとか、いろんな角度、ライティングの状況のデータがあった方がいい、ということが書かれています。

If possible, train with images collected in a way that’s similar to how images will be collected for prediction.

Provide images with variety. For example, use images that show animals from many different angles and in different lighting conditions. A classifier trained on nearly identical images for a given label tends to have poorer performance than one trained on a more diverse image set.

ラベルをフォルダ名にし、その配下にトレーニングデータ、テストデータを配置します。

Next, create a folder called Training Data, and another called Testing Data. In each folder, create subfolders using your labels as names. Then sort the images into the appropriate subfolders for each data set.

f:id:shu223:20180606071547p:plain

オープンなデータセット

ちなみに独自のデータセットを作成するのは非常に大変ですが、公開されているものもたくさんあります。

Mojaveを入れたら、このあたりのデータからいろいろなモデルをつくって試してみたいなと思っています。

こちらもどうぞ

shu223.hatenablog.com


  1. なお、本記事はNDAに配慮し、Xcode 10やiOS 12のスクショは使わず、公開情報のみで構成しています。

  2. High SierraXcode 10を入れて試してみましたが、import CreateMLUIでエラーになりました。