参考资料:
coursera课程第四周前半部分及编程作业: Convolutional Neural Networks
-
主要原理
训练一个网络,输出一个128维向量,这些向量要满足,相同人之间距离相差很小,不同人之间距离相差很大,这个需要用到The Triplet Loss
-
The Triplet Loss
用来实现相同人之间距离相差很小,不同人之间距离相差很大,第一张图片叫anchor,也就是基准的那个人,然后是同一个人的另外一张图片Positive,然后是另外一个人的一张图片Negetive。
实现
# GRADED FUNCTION: triplet_loss
def triplet_loss(y_true, y_pred, alpha = 0.2):
"""
Implementation of the triplet loss as defined by formula (3)
Arguments:
y_true -- true labels, required when you define a loss in Keras, you don't need it in this function.
y_pred -- python list containing three objects:
anchor -- the encodings for the anchor images, of shape (None, 128)
positive -- the encodings for the positive images, of shape (None, 128)
negative -- the encodings for the negative images, of shape (None, 128)
Returns:
loss -- real number, value of the loss
"""
anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
### START CODE HERE ### (≈ 4 lines)
# Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1
pos_dist = tf.reduce_sum(tf.square(anchor - positive),axis=1)
# Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1
neg_dist = tf.reduce_sum(tf.square(anchor - negative),axis=1)
# Step 3: subtract the two previous distances and add alpha.
basic_loss = pos_dist - neg_dist + alpha
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss = tf.reduce_sum(tf.maximum(basic_loss,0))
### END CODE HERE ###
return loss
验证
训练好模型后,我们提前在数据库中放好某人对应的编码,要验证某张图片是否是某个人,那就计算这张图片的编码,然后跟数据库的编码计算距离是否小于阈值即可。识别
训练好模型后,我们提前在数据库中放好某人对应的编码,要验证某张图片对应的人是否在数据库中,轮询数据库中每个人,找到最近的距离,如果最近的距离小于阈值,那么就是,否则不是。改进
1、每个人在数据库中存放多张图片,然后比对的时候可以对比多张
2、将照相的框只限制为人脸大小,减少无关像素进来