More Related Content
Similar to 画像認識で物を見分ける (20)
画像認識で物を見分ける
- 2. 自己紹介
● 谷田和章 (たにだかずあき)
○ GitHub: slaypni
● ソフトウェアエンジニア (フリーランス&白ヤギコーポレーション )
○ 機械学習, 自然言語処理
○ Web, iOS, Android
● カメリオの機械学習アルゴリズムの開発
- 11. 5. 推定値と正解から結果を評価 (1/2)
● sklearn.metrics.confusion_matrix()
混同行列をテキストで返す
● 混同行列
行: 正解ラベル
列: 推定ラベル
それぞれ分類された数
print(
metrics.confusion_matrix(
labels[test_indices], y_pred
)
)
[[38 0 0 0 0 50 0 0 0 0]
[ 0 50 0 0 0 41 0 0 0 0]
[ 0 0 29 0 0 57 0 0 0 0]
[ 0 0 0 54 0 37 0 0 0 0]
[ 0 0 0 0 27 65 0 0 0 0]
[ 0 0 0 0 0 91 0 0 0 0]
[ 0 0 0 0 0 58 33 0 0 0]
[ 0 0 0 0 0 64 0 25 0 0]
[ 0 0 0 0 0 87 0 0 1 0]
[ 0 0 0 0 0 84 0 0 0 8]]
正解ラベル = “2”
推定ラベル:
“2”: 29個
“5”: 57個
- 12. 5. 推定値と正解から結果を評価 (2/2)
● sklearn.metrics.classification_report()
分類性能の主要な指標をテキストで返す
● support
正解ラベル数
● f1-score
recallとprecisionの調和平均。
0から1の値をとる。
とりあえず、これが1に近いほど良い。
● recall
実際に正であるデータのうち
正と推定されたものの割合。
● precision
正と推定したデータのうち
実際に正であるものの割合。
print(
metrics.classification_report(
labels[test_indices], y_pred
)
)
precision recall f1-score support
0 1.00 0.43 0.60 88
1 1.00 0.55 0.71 91
2 1.00 0.34 0.50 86
3 1.00 0.59 0.74 91
4 1.00 0.29 0.45 92
5 0.14 1.00 0.25 91
6 1.00 0.36 0.53 91
7 1.00 0.28 0.44 89
8 1.00 0.01 0.02 88
9 1.00 0.09 0.16 92
avg / total 0.91 0.40 0.44 899
- 15. 結果を評価 (1/2)
● 対角成分の値が大きくなっている
→ 正しく分類できている
print(
metrics.confusion_matrix(
labels[test_indices], y_pred
)
)
[[87 0 0 0 1 0 0 0 0 0]
[ 0 88 1 0 0 0 0 0 1 1]
[ 0 0 85 1 0 0 0 0 0 0]
[ 0 0 0 79 0 3 0 4 5 0]
[ 0 0 0 0 88 0 0 0 0 4]
[ 0 0 0 0 0 88 1 0 0 2]
[ 0 1 0 0 0 0 90 0 0 0]
[ 0 0 0 0 0 1 0 88 0 0]
[ 0 0 0 0 0 0 0 0 88 0]
[ 0 0 0 1 0 1 0 0 0 90]]
- 16. 結果を評価 (2/2)
● f1-scoreが大きくなっている
● precisionとrecallも共に向上
print(
metrics.classification_report(
labels[test_indices], y_pred
)
)
precision recall f1-score support
0 1.00 0.99 0.99 88
1 0.99 0.97 0.98 91
2 0.99 0.99 0.99 86
3 0.98 0.87 0.92 91
4 0.99 0.96 0.97 92
5 0.95 0.97 0.96 91
6 0.99 0.99 0.99 91
7 0.96 0.99 0.97 89
8 0.94 1.00 0.97 88
9 0.93 0.98 0.95 92
avg / total 0.97 0.97 0.97 899
- 18. 使用するデータ
● The Paris Dataset
○ パリの観光名所の写真、約 6400枚
○ ラベルは全11種類
● iter_data()
ラベルと画像を返すイテレータ
(内容は.ipynbファイルを参照)
for (label, data) in islice(iter_data(), 4):
plt.imshow(data)
plt.title('{} {}'.format(label, data.shape))
plt.axis('off')
684x1024ピクセルのフルカラー画像
- 21. BoVWの概要 <特徴抽出>
1. 画像を入力
2. キーポイントを選択
3. 各点で局所特徴量を計算
4. <準備>で選んだ代表特徴量のヒストグラム作成 (最も似てる局所特徴量をカウント )
5. 特徴ベクトルを出力 (各次元が代表特徴量の頻度 )
キーポイント選択 局所特徴量抽出 ヒストグラム
(特徴ベクトル)
画像
- 27. 分類性能を評価 (1/2)
X = np.array([get_feature(descriptors)
for descriptors
in data_descriptors])
clf = SVC()
clf.fit(
X[train_indices],
labels[train_indices]
)
y_pred = clf.predict(X[test_indices])
print(metrics.classification_report(
labels[test_indices], y_pred
))
print(metrics.confusion_matrix(
labels[test_indices], y_pred
))
[[ 0 59 0 0 0 0 0 0 0 0 0]
[ 0 145 0 0 0 0 0 0 0 0 0]
[ 0 99 0 0 0 0 0 0 0 0 0]
[ 0 76 0 0 0 0 0 0 0 0 0]
[ 0 119 0 0 0 0 0 0 0 0 0]
[ 0 36 0 0 0 0 0 0 0 0 0]
[ 0 60 0 0 0 0 0 0 0 0 0]
[ 0 63 0 0 0 0 0 0 0 0 0]
[ 0 26 0 0 0 0 0 0 0 0 0]
[ 0 75 0 0 0 0 0 0 0 0 0]
[ 0 141 0 0 0 0 0 0 0 0 0]]
precision recall f1-score support
defense 0.00 0.00 0.00 59
eiffel 0.16 1.00 0.28 145
invalides 0.00 0.00 0.00 99
louvre 0.00 0.00 0.00 76
moulinrouge 0.00 0.00 0.00 119
museedorsay 0.00 0.00 0.00 36
notredame 0.00 0.00 0.00 60
pantheon 0.00 0.00 0.00 63
pompidou 0.00 0.00 0.00 26
sacrecoeur 0.00 0.00 0.00 75
triomphe 0.00 0.00 0.00 141
avg / total 0.03 0.16 0.04 899
- 28. 分類性能を評価 (2/2)
params = {
'gamma': [10 ** i
for i in range(-3, 4)]
}
clf = grid_search.GridSearchCV(
SVC(),
params
)
clf.fit(
X[train_indices],
labels[train_indices]
)
y_pred = clf.predict(X[test_indices])
[[ 35 7 0 3 1 0 0 1 0 0 12]
[ 1 117 1 1 2 0 0 3 0 3 17]
[ 1 38 38 2 1 0 0 1 0 10 8]
[ 1 15 3 48 1 0 1 0 0 2 5]
[ 0 10 3 0 101 0 1 0 0 3 1]
[ 0 2 0 0 1 29 0 1 0 2 1]
[ 0 5 3 3 0 0 34 1 0 11 3]
[ 1 7 3 0 1 0 0 44 0 2 5]
[ 0 4 2 0 0 0 0 0 17 0 3]
[ 0 14 9 1 2 0 0 2 0 44 3]
[ 0 14 3 1 0 0 1 1 0 2 119]]
precision recall f1-score support
defense 0.90 0.59 0.71 59
eiffel 0.50 0.81 0.62 145
invalides 0.58 0.38 0.46 99
louvre 0.81 0.63 0.71 76
moulinrouge 0.92 0.85 0.88 119
museedorsay 1.00 0.81 0.89 36
notredame 0.92 0.57 0.70 60
pantheon 0.81 0.70 0.75 63
pompidou 1.00 0.65 0.79 26
sacrecoeur 0.56 0.59 0.57 75
triomphe 0.67 0.84 0.75 141
avg / total 0.73 0.70 0.70 899
- 30. (再掲) BoVWの概要
1. 画像を入力
2. キーポイントを選択
3. 各点で局所特徴量を計算
4. <準備>で選んだ代表特徴量のヒストグラム作成 (最も似てる局所特徴量をカウント )
5. 特徴ベクトルを出力 (各次元が代表特徴量の頻度 )
キーポイント選択 局所特徴量抽出 ヒストグラム
(特徴ベクトル)
画像
- 31. (再掲) 機械学習を適用する流れ
● やりたいこと: (未知)データのラベルを推定
● 事前に必要なもの: データと正解ラベル
実験の手順 (概要)
1. データを特徴ベクトルに変換
2. データを学習用とテスト用に分ける
3. 分類器を学習させる
4. テストデータのラベルを推定
5. 推定値と正解から結果を評価