python中带有caffe的VGG面描述符

python中带有caffe的VGG面描述符,python,matlab,deep-learning,caffe,vgg-net,Python,Matlab,Deep Learning,Caffe,Vgg Net,我想要用python实现。但我不断地得到一个错误: TypeError:只能将列表(而不是“numpy.ndarray”)连接到列表 我的代码: 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

我想要用python实现。但我不断地得到一个错误:

TypeError:只能将列表(而不是“numpy.ndarray”)连接到列表

我的代码:

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 :)
现在
img
H
-by-
W
-by-3numpy数组。
Caffe预计其输入为4D:batch_index
x
channel
x
width
x
height.
因此,您需要
转换输入并添加一个单例维度来表示“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)

当我尝试它时,我得到了这个错误:TypeError:unhabable type:'numpy.ndarray'为什么不使用
caffe.io.load_image
?如果我尝试
caffe.io.load_image
s,我得到了相同的错误
TypeError:只能将列表(而不是“numpy.ndarray”)连接到列表
。如果我尝试将单个元素列表传递给方法,我会得到错误
TypeError:unhabable类型:“numpy.ndarray”
try
net.forward\u all
而不是
forward
。同样的错误
TypeError:unhabable类型:“numpy.ndarray”
你能得到堆栈跟踪以显示哪一行代码导致了这个错误吗?这是在哪里发生的“平均值来自哪里?”马寿:这是问题的一部分。