WWDC2017で発表されたcoreMLにVGG16の学習モデルを実装して、最小の手順で物体認識アプリを作ってみましたのでやり方を共有いたします。何かのお役に立ちましたら幸いです。
前提
下記の準備は出来ているものとします。
・Xcode beta9
・iOS11 beta
・ビルドのための設定こもごも
サンプルコードの入手
こちらのページのResourcesから公開されたサンプルコードを入手しましょう。
https://developer.apple.com/videos/play/wwdc2017/506/
サンプルコードそのままでもMNIST(手書き数字認識)の学習モデルがセットされているので、紙に書いた数字を写真に撮れば数字認識ができます。でも、数字だけでも寂しいので学習モデルを入れ替えてみましょう。
学習済みモデルの入手
こちらのページの下の方からVGG16をダウンロードしましょう。
https://developer.apple.com/machine-learning/
VGG16に関して詳しくはこちら
画像から1000種類の物体を見分けることが出来ます。
http://aidiary.hatenablog.com/entry/20170104/1483535144
実装
まずVGG16学習済みモデルをプロジェクトにドラッグ・アンド・ドロップします。
ViewController.swiftのclassificationRequestにある学習モデルファイルをMNISTClassfierからVGG16に書き換えます。
let model = try VNCoreMLModel(for: VGG16().model)//MNISTClassfierからVGG16に書き換え
さらにimagePickerControllerを下記のように書き換えましょう。(乱暴ですが)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true)
classificationLabel.text = "Analyzing Image…"
correctedImageView.image = nil
guard let uiImage = info[UIImagePickerControllerOriginalImage] as? UIImage
else { fatalError("no image from image picker") }
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))
// Show the image in the UI.
imageView.image = uiImage
//読み込んだ画像をそのまま推論処理へ
let handler = VNImageRequestHandler(ciImage: inputImage)
do {
try handler.perform([classificationRequest])
} catch {
print(error)
}
/**
//MNIST用に紙の四隅を検知したり、色を白黒に落としたりの処理。今回とりあえずスキップ
// Run the rectangle detector, which upon completion runs the ML classifier.
let handler = VNImageRequestHandler(ciImage: ciImage, orientation: Int32(orientation.rawValue))
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([self.rectanglesRequest])
} catch {
print(error)
}
}
**/
}
以上で完成です。
所感
学習モデルさえ入れ替えればかなりお手軽に流行りの"人工知能アプリ"を作ることが出来ます。推論だけに特化して思い切り割り切ってきたなぁ!という印象です。私のような深い知識がない人間も簡単に実装できてしまうので、学習モデル作成の手法も今後更に一般化されるであろうことを考えれば、世の中に機械学習の恩恵があふれる日は思ったよりも近いのかもしれません。