之前做一个医学图像特征点标注的项目,就是先从人脸关键点识别开始入门。
这是Kagge上一个比赛:Facial Keypoints Detection,我们这里使用CNN。大体内容如下图,识别人脸的15个关键点,每个关键点用x和y表示,所以神经网络的输出个数是30。
'left_eye_center_x', 'left_eye_center_y',
'right_eye_center_x', 'right_eye_center_y',
'left_eye_inner_corner_x', 'left_eye_inner_corner_y',
'left_eye_outer_corner_x', 'left_eye_outer_corner_y',
'right_eye_inner_corner_x', 'right_eye_inner_corner_y',
'right_eye_outer_corner_x', 'right_eye_outer_corner_y',
'left_eyebrow_inner_end_x', 'left_eyebrow_inner_end_y',
'left_eyebrow_outer_end_x', 'left_eyebrow_outer_end_y',
'right_eyebrow_inner_end_x', 'right_eyebrow_inner_end_y',
'right_eyebrow_outer_end_x', 'right_eyebrow_outer_end_y',
'nose_tip_x', 'nose_tip_y',
'mouth_left_corner_x', 'mouth_left_corner_y',
'mouth_right_corner_x', 'mouth_right_corner_y',
'mouth_center_top_lip_x', 'mouth_center_top_lip_y',
'mouth_center_bottom_lip_x', 'mouth_center_bottom_lip_y'
这里我们使用一个CNN,结构图如下:
CNN的模型代码如下。工程完整代码链接。里面有我在自己电脑上训练了一个下午的权重保存文件,数据自行到kaggle官网上下载,比较大。
def prepare_model():
input = Input(shape=(96, 96, 1,))
conv_1 = Conv2d(16, (2, 2))(input)
batch_norm_1 = BatchNormalization()(conv_1)
conv_2 = Conv2d(32, (3, 3))(batch_norm_1)
batch_norm_2 = BatchNormalization()(conv_2)
conv_3 = Conv2d(64, (4, 4))(batch_norm_2)
avg_pool_1 = AveragePooling2D((2, 2))(conv_3)
batch_norm_3 = BatchNormalization()(avg_pool_1)
conv_128 = Conv2d(128, (4, 4))(batch_norm_2)
avg_pool_128 = AveragePooling2D((2, 2))(conv_3)
batch_norm_128 = BatchNormalization()(avg_pool_1)
conv_4 = Conv2d(64, (7, 7))(batch_norm_128)
avg_pool_1 = AveragePooling2D((2, 2))(conv_128)
batch_norm_4 = BatchNormalization()(avg_pool_128)
conv_5 = Conv2d(32, (7, 7))(batch_norm_4)
flat_1 = Flatten()(conv_5)
dense_1 = Dense(30)(flat_1)
outputs = Dense(30)(dense_1)
model = tf.keras.Model(input, dense_1)
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
return model