Kerasメモ(OpenFace)
Keras-OpenFaceを試してみる。
モデルを通すと、顔画像を128次元の特徴量に落とし込める。
類似度が計算できるので、本人確認や似ている人物の検索ができる。
学習済みのモデルは4種類ある。
Models and Accuracies - OpenFace
landmarkIndicesは2種類。
- INNER_EYES_AND_BOTTOM_LIP = [39, 42, 57] // 目の内側と下唇
- OUTER_EYES_AND_NOSE = [36, 45, 33] // 目の外側と鼻
landmarks(0始まり)
今回は「nn4.small2.v1」モデルを試してみる。
with CustomObjectScope({'tf': tf}): model = load_model('./model/nn4.small2.v1.h5')
model summary
InputLayer shape: (None, 96, 96, 3) OutputLayer shape: (None, 128) Total params: 3,743,280 Trainable params: 3,733,968 Non-trainable params: 9,312
ざっと見ると、レイヤを分岐(多出力)させて結合する(concatenate)、の繰り返しが7回。
異なる2つの画像をモデルに通し、それぞれの特徴量ベクトルが得られたとして、L2ノルム(ユークリッド距離)を計算することで、似ている/似てないが判定できる。
# emb1: image1, shape (1, 128) # emb2: image2, shape (1, 128) np.sqrt(np.sum(np.square(emb1 - emb2))) np.linalg.norm( emb1 - emb2) scipy.spatial.distance.euclidean(emb1, emb2)