facenet源码注释

源码:https://github.com/davidsandberg/facenet

步骤:
1、配置环境(安装opencv)
所需的python模块
这里写图片描述
Clone the FaceNet repo:
git clone https://github.com/davidsandberg/facenet.git
2、下载训练好的网络模型
facenet提供了两个预训练模型,分别是基于CASIA-WebFace和MS-Celeb-1M人脸库训练的,链接为:https://github.com/davidsandberg/facenet#pre-trained-models
这里写图片描述
3、下载数据集LFW:http://vis-www.cs.umass.edu/lfw/lfw.tgz
4、评估模型在数据集的准确率:
测试运行使用validate_on_lfw:

python src/validate_on_lfw.py ~/datasets/lfw/lfw_mtcnnpy_160 ~/models/facenet/20170512-110547

mtcnn(处理人脸检测和人脸关键点定位问题)
一、基于mtcnn与facenet的人脸聚类

代码:facenet/contributed/cluster.py

代码:facenet/contributed/clustering.py实现了相似的功能,只是没有mtcnn进行检测这一步

1.使用mtcnn进行人脸检测并对齐与裁剪

2.对裁剪的人脸使用facenet进行embedding

3.对embedding的特征向量使用欧式距离进行聚类

二、基于mtcnn与facenet的人脸识别(输入单张图片判断这人是谁)

代码:facenet/contributed/predict.py

1.使用mtcnn进行人脸检测并对齐与裁剪

2.对裁剪的人脸使用facenet进行embedding

3.执行predict.py进行人脸识别(需要训练好的svm模型)

三.Exports the embeddings and labels of a directory of images as numpy arrays

代码:facenet/contributed/export_embeddings.py

1.需要对数据进行对齐与裁剪做为输入数据

2.输出embeddings.npy;labels.npy;label_strings.npy

四.Performs face alignment and calculates distance between the embeddings of images

代码:facenet/src/compare.py

1.使用mtcnn进行人脸检测并对齐与裁剪

2.对裁剪的人脸使用facenet进行embedding

3.计算输入图片的距离

train_tripletloss.py

构造计算图,prelogits为最后一层的输出
    #其中prelogits是最后一层的输出
    prelogits, _ = network.inference(image_batch, args.keep_probability, 
            phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size,
            weight_decay=args.weight_decay)
        # 对最后的输出进行标准化,得到该图像的embedding
        # embeddings = tf.nn.l2_normalize(输入向量, L2范化的维数(取0(列L2范化)或1(行L2范化)), 泛化的最小值边界, name='embeddings')
        embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings')
        # Split embeddings into anchor, positive and negative and calculate triplet loss
        # 将输出的embeddings分为anchor,正样本, 负样本三个部分
        anchor, positive, negative = tf.unstack(tf.reshape(embeddings, [-1,3,args.embedding_size]), 3, 1)
        #根据上面三个部分计算triplet-loss
        triplet_loss = facenet.triplet_loss(anchor, positive, negative, args.alpha)
        #定义优化方法,将指数衰减应用到学习率上 
        #tf.train.exponential_decay(learning_rate, global_, decay_steps, decay_rate, staircase=True/False)
        ##如果staircase=True,那就表明每decay_steps次更新学习速率,如果是False,那就是每一步都更新学习速率。
        learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step,
            args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the total losses
        # 加入正则化损失
        regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        # 整体的损失即为triplet-loss+正则损失
        total_loss = tf.add_n([triplet_loss] + regularization_losses, name='total_loss')

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        # 用上述定义的优化方法和loss进行优化
        train_op = facenet.train(total_loss, global_step, args.optimizer, 
            learning_rate, args.moving_average_decay, tf.global_variables())

参考:http://blog.csdn.net/qq_36673141/article/details/78958582

猜你喜欢

转载自blog.csdn.net/baidu_27643275/article/details/79256243