Python 在caffe中使用分类时出错

Python 在caffe中使用分类时出错,python,python-2.7,caffe,Python,Python 2.7,Caffe,我在python中使用caffe进行分类。我从你那里得到代码。在这里,我只使用简单的代码,比如 plt.rcParams['figure.figsize'] = (10, 10) plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray' mean_filename='./mean.binaryproto' proto_data = open(mean_filename, "rb").re

我在python中使用caffe进行分类。我从你那里得到代码。在这里,我只使用简单的代码,比如

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
mean_filename='./mean.binaryproto'
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
mean=mean,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
然而,我得到的错误如下

Traceback (most recent call last):
File "cnn_age_gender_demo.py", line 25, in 
image_dims=(256, 256))
File "/home/john/Downloads/caffe/python/caffe/classifier.py", line 34, in init
self.transformer.set_mean(in_, mean)
File "/home/john/Downloads/caffe/python/caffe/io.py", line 255, in set_mean
raise ValueError('Mean shape incompatible with input shape.')
ValueError: Mean shape incompatible with input shape.

你能帮我重新爱上它吗?谢谢

让我们转到caffe/python/caffe/io.py中的第253-254行 替换

if ms != self.inputs[in_][1:]:
    raise ValueError('Mean shape incompatible with input shape.')


重建。希望它能有所帮助

我也遇到了同样的问题,在imagenet web演示中,我用这种方式修改了脚本,以加载第95行中的平均文件


mean=np.load(args.mean\u文件).mean(1).mean(1)

我非常害怕重建代码,因为caffe安装对我来说并不容易。 但要解决这个问题,resizemean的解决方案需要in_形状(user8264的响应),它在caffe/classifier.py内部设置

无论如何,我调试并找到了age_net.caffemodel的in_shape=(3227227)的值

因此,用于年龄和性别预测的模型将发生以下变化:

age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
                   mean=mean,
                   channel_swap=(2,1,0),
                   raw_scale=255,
                   image_dims=(227, 227))
但需要首先修改平均值:

m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
in_shape=(227, 227)
mean = caffe.io.resize_image(normal_mean.transpose((1,2,0)),in_shape)
                            .transpose((2,0,1)) * (m_max - m_min) + m_min

这将消除“ValueError:与输入形状不兼容的平均形状”。但我不确定准确度。显然,对我来说,跳过平均值参数给出了更好的年龄预测:)

编辑deploy\u gender.prototxt并设置: 输入尺寸:256 输入尺寸:256


不知道为什么写错了…

部署性别.prototxt文件在哪里?这个问题有
age\u net\u model\u file='./deploy\u age.prototxt'
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
in_shape=(227, 227)
mean = caffe.io.resize_image(normal_mean.transpose((1,2,0)),in_shape)
                            .transpose((2,0,1)) * (m_max - m_min) + m_min