python 中的 VGG 人脸描述符与 caffe

VGG Face Descriptor in python with caffe

我想在 python 中实现 VGG Face Descriptor。但我一直收到错误消息:

TypeError: can only concatenate list (not "numpy.ndarray") to list

我的代码:

import numpy as np
import cv2 
import caffe
img = cv2.imread("ak.png")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
net = caffe.Net("VGG_FACE_deploy.prototxt","VGG_FACE.caffemodel",  caffe.TEST)
print net.forward(img)

你能帮帮我吗?

更新 1

此工作代码是 matlab 中的示例

%  Copyright (c) 2015, Omkar M. Parkhi
%  All rights reserved.
img = imread('ak.png');
img = single(img);

    Img = [129.1863,104.7624,93.5940] ;

img = cat(3,img(:,:,1)-averageImage(1),...
    img(:,:,2)-averageImage(2),...
    img(:,:,3)-averageImage(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();

尝试将单个元素列表传递给该方法。

net.forward ([img])

要使用 python 界面,您需要在将输入图像传送到网络之前对其进行转换

img = caffe.io.load_image( "ak.png" )
img = img[:,:,::-1]*255.0 # convert RGB->BGR
avg = np.array([93.5940, 104.7624, 129.1863])  # BGR mean values
img = img - avg # subtract mean (numpy takes care of dimensions :)

现在 imgH-by-W-by-3 numpy 数组。
Caffe 期望其输入为 4D:batch_index x 通道 x 宽度 x 高度。
因此,您需要 transpose 输入并添加一个单一维度来表示 "batch_index" 领先维度

img = img.transpose((2,0,1)) 
img = img[None,:] # add singleton dimension

现在你可以运行向前传球

out = net.forward_all( data = img )

OpenCV 默认读取 BGR 并缩放为 255 格式,因此:

img = cv2.imread('ak.png')
avg = np.array([93.5940,104.7624,129.1863]) # BGR mean from VGG
img -= avg # subtract mean
img = img.transpose((2,0,1)) # to match image input dimension: 3x224x224
img = img[None,:] # add singleton dimension to match batch dimension
out = net.forward_all(data = img)