20
Caffeでお手軽本格 ディープラーニング iOSアプリ TAKUYA (@noradaiko) potatotips #22

Caffeでお手軽本格ディープラーニングアプリ @potatotips

Embed Size (px)

Citation preview

Page 1: Caffeでお手軽本格ディープラーニングアプリ @potatotips

Caffeでお手軽本格 ディープラーニング

iOSアプリTAKUYA (@noradaiko)

potatotips #22

Page 2: Caffeでお手軽本格ディープラーニングアプリ @potatotips

松山 拓矢

• @noradaiko

• フリーランス4年目 (元Yahoo!)

• iOSアプリ,ウェブアプリなどを制作

• 機械学習に興味あり

• 絵も描きます

2

Page 3: Caffeでお手軽本格ディープラーニングアプリ @potatotips

め ん き き

麺利き

3

Page 4: Caffeでお手軽本格ディープラーニングアプリ @potatotips

4

麺利き

写真に基づいてラーメン屋を推薦するアプリ

入力

Page 5: Caffeでお手軽本格ディープラーニングアプリ @potatotips

5

ディープラーニング の技術を使用

+ =

Page 6: Caffeでお手軽本格ディープラーニングアプリ @potatotips

画像認識に強い 機械学習アルゴリズム

6

ディープラーニングとは..

Page 7: Caffeでお手軽本格ディープラーニングアプリ @potatotips

• 脳の神経構造を模倣したニューラルネットワークの一種

• 大量のデータから物体の特徴を自動的に学習

‣ これまでは特徴の抽出方法は人間ががんばって用意していた

7

!

Page 8: Caffeでお手軽本格ディープラーニングアプリ @potatotips

作ろう

ディープラーニングアプリ!

誰でも簡単に作れる方法をご紹介します

8

Page 9: Caffeでお手軽本格ディープラーニングアプリ @potatotips

Caffe ディープラーニング用 フレームワーク

• http://caffe.berkeleyvision.org/

• GPU演算(CUDA)で高速に学習できる

• すぐに試せる学習済みモデルもある

• Mac OSX対応

9

Page 10: Caffeでお手軽本格ディープラーニングアプリ @potatotips

Caffe for iOS上で動くCaffe!!!• https://github.com/aleph7/caffe

• Caffeのfork

• iOS上で識別処理が実用に耐える速度で動かせる ‣ iPhone5sで1~2秒

• サーバいらずで使える

• ただしXCode7 はまだ非対応

10

Page 11: Caffeでお手軽本格ディープラーニングアプリ @potatotips

Caffe for iOS サンプル作りました

• https://github.com/noradaiko/

caffe-ios-sample

• 単純な物体認識

• BLVC CaffeNet Modelを使用

11

Page 12: Caffeでお手軽本格ディープラーニングアプリ @potatotips

demo

Page 13: Caffeでお手軽本格ディープラーニングアプリ @potatotips

使用するデータ

• labels.txt: 識別結果を名前に変換するための一覧

• deploy.prototxt: ネットワーク定義

• mean.binaryproto: 平均画像

• bvlc_reference_caffenet.caffemodel: 学習済みデータ

13

Page 14: Caffeでお手軽本格ディープラーニングアプリ @potatotips

処理の流れ

1.識別対象の画像の読み込み

• 像の画像

2.Classifierクラスの初期化

• 4つのモデルデータへのファイルパスを指定

3.Classifierの実行

• 画像を指定して結果を取得

4.識別結果の出力

14

Page 15: Caffeでお手軽本格ディープラーニングアプリ @potatotips

UIImage* image = [UIImage imageNamed:@"sample.jpg"];

cv::Mat src_img, img; UIImageToMat(image, src_img);

cv::cvtColor(src_img, img, CV_RGBA2BGRA);

画像の読み込み• UIImageを読み込み

• cv::Mat形式に変換

• カラー配列をRGBAからBGRAに変換

Page 16: Caffeでお手軽本格ディープラーニングアプリ @potatotips

// ファイルパスをstring型に変換 string model_file_str = std::string([model_file UTF8String]); string label_file_str = std::string([label_file UTF8String]); string trained_file_str = std::string([trained_file UTF8String]); string mean_file_str = std::string([mean_file UTF8String]); Classifier classifier = Classifier(model_file_str, trained_file_str, mean_file_str, label_file_str);

Classifierの初期化• モデル定義、ラベル、学習済みモデル、平均画像のパスを取得

• 各ファイルパスをstd::stringに変換

• Classifierのインスタンスを作成

Page 17: Caffeでお手軽本格ディープラーニングアプリ @potatotips

// 識別の実行 std::vector<Prediction> result = classifier.Classify(img);

Classifierの実行

• 画像を指定するだけ!

Page 18: Caffeでお手軽本格ディープラーニングアプリ @potatotips

for (std::vector<Prediction>::iterator it = result.begin(); it != result.end(); ++it) {

NSString* label = [NSString stringWithUTF8String:it->first.c_str()];

NSNumber* probability = [NSNumber numberWithFloat:it->second];

NSLog(@"label: %@, prob: %@", label, probability); }

識別結果の出力• std:vector形式で複数の識別候補が得られる

• iteratorで回して各候補を取得

• it->first: ラベル、it->second: 確率

Page 19: Caffeでお手軽本格ディープラーニングアプリ @potatotips

まとめ

• Caffeを使えばオリジナルの学習モデルが作れる

• Caffe for iOSならサーバいらずで識別処理が出来る

• サンプルプロジェクトのご紹介

• オリジナルの学習モデルでアプリを作ろう!

19

Page 20: Caffeでお手軽本格ディープラーニングアプリ @potatotips

ご清聴ありがとうございました

20