VGG Face测试的Python版本

来源:互联网 发布:巴洛克的艺术风格 知乎 编辑:程序博客网 时间:2024/06/06 01:38

本人想测试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 BGRimg = permute(img, [2, 1, 3]); % permute width and heightmodel = 'VGG_FACE_16_deploy.prototxt';weights = 'VGG_FACE.caffemodel';caffe.set_mode_cpu();net = caffe.Net(model, weights, 'test'); % create net and load weightsres = net.forward({img});prob = res{1};caffe_ft = net.blobs('fc7').get_data();

于是参考网络上的例子,加上自己的修改完成了下面的Python版本测试用例,

import numpy as npimport cv2 import caffeimg = caffe.io.load_image( "ak.png" )img = img[:,:,::-1]*255.0 # convert RGB->BGRavg = 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 dimensionnet = caffe.Net("VGG_FACE_deploy.prototxt","VGG_FACE.caffemodel",  caffe.TEST) #load netout = net.forward_all( data = img ) # out is probabilitylabels=np.loadtxt('names.txt',str,delimiter='\n') # get the nameprint labels[out['prob'].argmax()]

最终的输出就是Aamir_Khan