VGG Face Descriptor 是牛津大学VGG小组的工作,现在已经开源训练好的网络结构和模型参数,本文将基于此模型在caffe上使用自己的人脸数据微调,并进行特征提取与精确度验证。
数据传送门:CASIA WebFace
模型传送门:http://www.robots.ox.ac.uk/~vgg/software/vgg_face/

模型准备

1.从上面的网址中下载VGG-Face已经训练好的模型和网络结构文件,根据deploy.proto文件来修改得到train_val.prototxt文件,主要有:修改数据层的输入和最后全连接层以及损失层等,注意fc8层名称的修改,具体如下:

name: "VGG_FACE_16_layers"
layer {name: "data"type: "Data"top: "data"top: "label"include {phase: TRAIN}transform_param {mirror: truecrop_size: 224
#    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"}data_param {source: "vggface/webface_train_lmdb"batch_size: 32backend: LMDB}
}
layer {name: "data"type: "Data"top: "data"top: "label"include {phase: TEST}transform_param {mirror: falsecrop_size: 224
#    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"}data_param {source: "vggface/webface_val_lmdb"batch_size: 32backend: LMDB}
}
......
layer {bottom: "fc7"top: "fc8_s"name: "fc8_s"type: "InnerProduct"param {lr_mult: 10decay_mult: 1}param {lr_mult: 20decay_mult: 0}inner_product_param {num_output: 2031weight_filler {  type: "gaussian"std: 0.01  }  bias_filler {  type: "constant"  value: 0.1}  }
}
layer {name: "accuracy"type: "Accuracy"bottom: "fc8_s"bottom: "label"top: "accuracy"include {phase: TEST}
}
layer {name: "loss"type: "SoftmaxWithLoss"bottom: "fc8_s"bottom: "label"top: "loss"
}

模型训练

本次训练,我采用的是webface中人脸数量在50张以上的个人类别,总共有两千多个。按照caffe的工具转换成lmdb格式即可开始训练。

模型效果测试

模型训练结束之后,在lfw上验证实验效果。lfw数据对使用官方提供的txt文件,不过我觉得格式不太好,就自己用脚本进行了些许修改:
pairs.txt

Abel_Pacheco    1   4
Akhmed_Zakayev  1   3
Akhmed_Zakayev  2   3
Amber_Tamblyn   1   2
Anders_Fogh_Rasmussen   1   3
Anders_Fogh_Rasmussen   1   4

修改后,将一对图片的路径放在一起,属于同一个人为1,否则为0

Abel_Pacheco/Abel_Pacheco_0001.jpg  Abel_Pacheco/Abel_Pacheco_0004.jpg  1
Akhmed_Zakayev/Akhmed_Zakayev_0001.jpg  Akhmed_Zakayev/Akhmed_Zakayev_0003.jpg  1
Akhmed_Zakayev/Akhmed_Zakayev_0002.jpg  Akhmed_Zakayev/Akhmed_Zakayev_0003.jpg  1
Amber_Tamblyn/Amber_Tamblyn_0001.jpg    Amber_Tamblyn/Amber_Tamblyn_0002.jpg    1

脚本如下:

#!/usr/bin/python
#-*- coding: utf-8 -*-
'''
#Created on Thur Mar 2 10:17:38 2017
#Goal:parse pairs.txt of LFW database to label.txt for face #recognition test
#@author: wujiyang
'''
import sysdef get_all_images(filename):file = open(filename)lines = file.readlines()list = []for line in lines:line_split = line.strip('\n').split('\t')if(len(line_split)) == 3:line_split[-1] = line_split[-1].zfill(4)line_split[-2] = line_split[-2].zfill(4)if(len(line_split)) == 4:line_split[-1] = line_split[-1].zfill(4)line_split[-3] = line_split[-3].zfill(4)list.append(line_split)file.close()return listdef save2labelfile(list):file = open('label.txt', 'w')labellines = []for i in range(len(list)):if len(list[i]) == 3:labelline = list[i][0] + '/' + list[i][0] + '_' + list[i][1] + '.jpg' + '\t' + 'original' + '/' + list[i][0] + '/' + list[i][0] + '_' +list[i][2] + '.jpg' + '\t' + '1\n'labellines.append(labelline)elif len(list[i]) == 4:labelline = list[i][0] + '/' + list[i][0] + '_' + list[i][1] + '.jpg' + '\t' + 'original' + '/' + list[i][2] + '/' + list[i][2] + '_' + list[i][3] + '.jpg' + '\t' + '0\n'labellines.append(labelline)file.writelines(labellines)file.close()'''''
使用方法:执行脚本  python pair2label.py pairs.txt
'''
if __name__ == "__main__":if len(sys.argv) != 2:print "Format Error! Usuage: python %s pairs.txt" %(sys.argv[0])sys.exit()list = get_all_images("pairs.txt")save2labelfile(list)print "Done!"

特征提取与精确度验证

此脚本采用caffe的python接口进行特征提取与验证。

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 20 16:55:55 2015@author: wujiyang
@brief:在lfw数据库上验证训练好了的网络
"""
import math
import sklearn
import numpy as np
import matplotlib.pyplot as plt
import skimage
caffe_root = '/home/wujiyang/caffe/'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffeimport sklearn.metrics.pairwise as pw#模型初始化相关操作
def initilize():print 'model initilizing...'deployPrototxt = "./vgg-face-deploy.prototxt"modelFile = "./vgg-face.caffemodel"caffe.set_mode_gpu()caffe.set_device(0)net = caffe.Net(deployPrototxt, modelFile,caffe.TEST)return netdef read_imagelist(labelfile):'''@brief:从列表文件中,读取图像数据到矩阵文件中@param: labelfile 图像列表文件@return :4D 的矩阵'''file = open(labelfile)lines = file.readlines()test_num=len(lines)file.close()x = np.empty((test_num,3,224,224))y = np.empty((test_num,3,224,224))labels = []i = 0for line in lines:path = line.strip('\n').split('\t')#read left imagefilename = path[0]img = skimage.io.imread(filename,as_grey=False)image = skimage.transform.resize(img,(224,224))*255if image.ndim < 3:print 'gray:'+filenamex[i,0,:,:]=image[:,:]x[i,1,:,:]=image[:,:]x[i,2,:,:]=image[:,:]else:x[i,0,:,:]=image[:,:,0]x[i,1,:,:]=image[:,:,1]x[i,2,:,:]=image[:,:,2]#read right imagefilename = path[1]img = skimage.io.imread(filename,as_grey=False)image = skimage.transform.resize(img,(224,224))*255if image.ndim < 3:print 'gray:'+filenamey[i,0,:,:]=image[:,:]y[i,1,:,:]=image[:,:]y[i,2,:,:]=image[:,:]else:y[i,0,:,:]=image[:,:,0]y[i,1,:,:]=image[:,:,1]y[i,2,:,:]=image[:,:,2]#read labellabels.append(int(path[2]))i=i+1return x, y, labelsdef extractFeature(leftdata,rightdata):#提取左半部分的特征test_num=np.shape(leftdata)[0]#data 是输入层的名字out = net.forward_all(data = leftdata)                                                                    feature1 = np.float64(out['fc7'])featureleft=np.reshape(feature1,(test_num,4096))#np.savetxt('feature1.txt', feature1, delimiter=',')#提取右半部分的特征out = net.forward_all(data = rightdata)                                                                                   feature2 = np.float64(out['fc7'])featureright=np.reshape(feature2,(test_num,4096))#np.savetxt('feature2.txt', feature2, delimiter=',')return featureleft, featurerightdef calculate_accuracy(distance,labels,num):    '''#计算识别率,选取阈值,计算识别率'''    accuracy = {}predict = np.empty((num,))threshold = 0.1while threshold <= 0.9 :for i in range(num):if distance[i] >= threshold:predict[i] = 0else:predict[i] = 1predict_right = 0.0for i in range(num):if predict[i] == labels[i]:predict_right += 1.0current_accuracy = (predict_right / num)accuracy[str(threshold)] = current_accuracythreshold = threshold + 0.001#将字典按照value排序temp = sorted(accuracy.items(), key = lambda d:d[1], reverse = True)highestAccuracy = temp[0][1]thres = temp[0][0]return highestAccuracy, thresif __name__=='__main__':#模型初始化net = initilize()print 'network input :' ,net.inputs  print 'network output: ', net.outputs#读取图像数据leftdata,rightdata,labels = read_imagelist('tmp.txt')#计算特征featureleft, featureright = extractFeature(leftdata, rightdata)#计算每个特征之间的距离 cosine距离test_num = len(labels)mt = pw.pairwise_distances(featureleft, featureright, metric='cosine')distance = np.empty((test_num,))for i in range(test_num):distance[i] = mt[i][i]print 'Distance before normalization:\n', distanceprint 'Distance max:', np.max(distance), ' Distance min:', np.min(distance), '\n'# 距离需要归一化到0--1,与标签0-1匹配distance_norm = np.empty((test_num,))for i in range(test_num):distance_norm[i] = (distance[i]-np.min(distance))/(np.max(distance)-np.min(distance))print 'Distance after normalization:\n', distance_normprint 'Distance_norm max:', np.max(distance_norm), ' Distance_norm min:', np.min(distance_norm), '\n'#根据label和distance_norm计算精确度highestAccuracy, threshold = calculate_accuracy(distance_norm,labels,len(labels))print ("the highest accuracy is : %.4f, and the corresponding threshold is %s \n"%(highestAccuracy, threshold))

基于VGG-Face的人脸识别测试相关推荐

  1. 基于深度学习的人脸识别系统(Caffe+OpenCV+Dlib)【三】VGG网络进行特征提取

    前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...

  2. 基于face_recognition库的摄像头实时人脸识别测试

    前言 介绍一个基于python的开源人脸识别库,且其离线识别率高达99.38%, github上的网址:github链接 该库可以通过python或者命令行即可实现人脸识别的功能.使用dlib深度学习 ...

  3. 《繁凡的论文精读》(一)CVPR 2019 基于决策的高效人脸识别黑盒对抗攻击(清华朱军)

    点我一文弄懂深度学习所有基础和各大主流研究方向! <繁凡的深度学习笔记>,包含深度学习基础和 TensorFlow2.0,PyTorch 详解,以及 CNN,RNN,GNN,AE,GAN, ...

  4. 第十九课.基于sklearn的SVM人脸识别

    目录 数据集 确定人脸的类别标记 划分训练集和测试集与训练 实验为基于sklearn的SVM人脸识别,使用 SVM 算法对戴眼镜的人脸和不戴眼镜的人脸进行分类,从而完成 识别戴眼镜的人脸 的任务:实验 ...

  5. 基于改进的RPCA人脸识别算法

    from:http://www.chinaaet.com/article/3000011311 基于改进的RPCA人脸识别算法 作者:首照宇,杨晓帆,莫建文 2015/11/15 18:04:00 摘 ...

  6. 基于稀疏表示的人脸识别 (SRC,LASRC,RASL,MRR)

    FROM:http://blog.csdn.net/loadstar_kun/article/details/39453839 1.  问题背景 信号的稀疏表示并不是新的东西.我们很早就一直在利用这一 ...

  7. 基于PYQT编写一个人脸识别软件(2)

    前言 以前在博客:基于PYQT编写一个人脸识别软件 中给出了我自己用PYQT编写的一个小软件.鉴于使用的是开源库--face_recogniton,尽管使用很简单,但是还有些问题,比如:识别黄种人时效 ...

  8. python dlib caffe人脸相似度_基于深度学习的人脸识别系统(Caffe+OpenCV+Dlib)【一】如何配置caffe属性表...

    前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...

  9. Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38%

    Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38% 仅用 Python 和命令行就可以实现人脸识别的库开源了.该库使用 dlib 顶尖的深度学习人脸识别技术构建 ...

  10. python人脸识别库_基于facenet的实时人脸识别系统

    facenet_facerecognition opencv+mtcnn+facenet+python+tensorflow 实现实时人脸识别 Abstract:本文记录了在学习深度学习过程中,使用o ...

最新文章

  1. 409 Longest Palindrome
  2. [转载]mysql操作if...else...来查问
  3. C# Httpclient编程
  4. 后端返回数据带有标签_越来越火的图数据库究竟是什么?是否在制造企业可以应用...
  5. 互斥锁pthread_mutex_t的使用
  6. 什么是云计算?云计算学习基础
  7. Paypal 在线支付接口应用从零开始,第2节,[支付API原理及流程]
  8. 牛!2020年,这项技术将获得1,000,000,000元人民币注资!
  9. 单招计算机英语面试口语,单招面试英语自我介绍范文 自我介绍说什么
  10. AD19一键同时修改PCB各元器件黄色标志字符的尺寸(大了太占空间改小一点
  11. 吉他指弹入门——双手泛音
  12. 如何对开发团队的人员进行绩效管理?
  13. 微信自研生产级 Paxos 类库 PhxPaxos 实现原理介绍
  14. 高分辨率屏幕下,软件界面分辨率过低的解决办法(经验贴汇总+自身实践)
  15. 技术干货|eBay对流量控制说“so easy”!
  16. html效果浮窗效果,jQuery简单实现中间浮窗效果
  17. 【Python数据挖掘】用朴素贝叶斯预测人类活动识别
  18. PTA 数据结构与算法 7-29 修理牧场
  19. HTML5中的data-id与id
  20. 黑科技网站第三弹 怀旧游戏集锦

热门文章

  1. shell中获取文件夹中文件名
  2. ICLR 2021 | GSL:通过可控的解耦表征学习模拟人脑想象力
  3. 兰州大学2018计算机考研复试线,2018年兰州大学考研复试分数线已公布
  4. 字节跳动笔试题-特征提取
  5. 树莓派趣味编程——从ABC到XYZ
  6. Docker 安装 rancher
  7. Laetus控制器维修20C-A323包装机控制模块
  8. CNN推理时opencv图像Mat数组从HWC转换到CHW方法
  9. python神经网络包_使用Python实现神经网络的成本函数(第5周Coursera)
  10. centos安装pngquant及python使用pngquant