本人想测试VGG Face Descriptor提供的人脸识别的Caffe版本,官方提供了Matlab版本的测试用例,但是配置了很长时间的Matlab接口一直失败。其中Matlab测试程序为,
% Copyright (c) 2015, Omkar M. Parkhi
% All rights reserved.
img = imread('ak.png');
img = single(img);
averageImg = [129.1863,104.7624,93.5940] ;
img = cat(3,img(:,:,1)-averageImg(1),...
img(:,:,2)-averageImg(2),...
img(:,:,3)-averageImg(3));
img = img(:, :, [3, 2, 1]); % convert from RGB to BGR
img = permute(img, [2, 1, 3]); % permute width and height
model = 'VGG_FACE_16_deploy.prototxt';
weights = 'VGG_FACE.caffemodel';
caffe.set_mode_cpu();
net = caffe.Net(model, weights, 'test'); % create net and load weights
res = net.forward({img});
prob = res{1};
caffe_ft = net.blobs('fc7').get_data();
于是参考网络上的例子,加上自己的修改完成了下面的Python版本测试用例,
import numpy as np
import cv2
import caffe
img = caffe.io.load_image( "ak.png" )
img = img[:,:,::-1]*255.0 # convert RGB->BGR
avg = np.array([129.1863,104.7624,93.5940])
img = img - avg # subtract mean (numpy takes care of dimensions :)
img = img.transpose((2,0,1))
img = img[None,:] # add singleton dimension
net = caffe.Net("VGG_FACE_deploy.prototxt","VGG_FACE.caffemodel", caffe.TEST) #load net
out = net.forward_all( data = img ) # out is probability
labels=np.loadtxt('names.txt',str,delimiter='\n') # get the name
print labels[out['prob'].argmax()]
最终的输出就是Aamir_Khan