我正在尝试使用EmotiW_VGG_S caffemodel CNN。 mean_file binaryproto包含一个3x256x256的图像,我想我必须从要预测的图像中减去。
我获得平均值的方法是:
def binaryproto2npy():
proto_data = open(mean_file, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean_values = caffe.io.blobproto_to_array(a)[0]
return mean_values
在加载mean_values之后,我尝试将其应用于我的图像(一张脸)并以这种方式对其进行分类:
def process_face(face, mean_values):
image_dim = 256
batch_size = 1
channels = 3
data = []
resize = (image_dim, image_dim, 3)
face = np.resize(face, (face.shape[0], face.shape[1], 1))
if not ((face.shape[0] == resize[0]) & (face.shape[1] == resize[1])):
face = caffe.io.resize_image(face, resize)
# Setting up the right transformer for an input image
transformer = caffe.io.Transformer({'data':
net.blobs['data'].data.shape})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_channel_swap('data', (2, 1, 0))
transformer.set_raw_scale('data', 255.0)
transformer.set_mean('data', mean_values)
processed_image = transformer.preprocess('data', face)
data.append(processed_image)
net.blobs['data'].reshape(batch_size, channels, 224, 224)
net.blobs['data'].data[...] = data[0]
out = net.forward()
问题是我尝试了这段代码,但均值和图像似乎具有不同的形状,因此无法找到解决方案。