近期使用caffe模型进行人脸识别时,需要提取指定网络层中的数据进行特征比较,常见的比如提取全连接层等。本文基于caffe的python接口实现了一个简单的特征提取及人脸验证测试。测试使用的数据是LFW官网的6000对图片
本文主要参考caffe官网的例子:
Image Classification and Filter Visualization
具体内容见代码注释: feature_extract_and_evaluate.py
# -*- coding: utf-8 -*-
"""
Created on Tue. Mar 07 16:59:55 2017
@author: wujiyang
@brief:读取lfw数据库6000对人脸数据进行精确度验证
"""
import numpy as np
import matplotlib.pyplot as plt
import os
import sys
import sklearn
import sklearn.metrics.pairwise as pw
caffe_root = '/home/wujiyang/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe
#图片库路径,若label.txt中使用的是绝对路径,则无需修改
imageBasePath = ''
#模型初始化相关操作
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 net
#读取图像标签
def readImagelist(labelFile):
'''
@brief:从列表文件中,读取图像数据到矩阵文件中
@param: labelfile 图像列表文件
@return :需要对比的图片路径,以及是否为同一个人的标记
labelfile的格式说明:每一行的数据如下所示,左边分别为要对比的图片,右边为是否是同一个人的标记
同一个表示为1,不同人表示为0
img1 img2 label
'''
file = open(labelFile)
lines = file.readlines()