user8430 user8430 - 2 years ago 324
Python Question

Error when using classify in caffe

I am using caffe in python to classify. I get code from here. In here, I just use simple code such as

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
proto_data = open(mean_filename, "rb").read()
a =
mean =[0]
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
image_dims=(256, 256))

However, I got error such as

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

Could you help me to reslove it? Thanks

Answer Source

Let go to line 253-254 in caffe/python/caffe/ Replace

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


if ms != self.inputs[in_][1:]:
    in_shape = self.inputs[in_][1:]
    m_min, m_max = mean.min(), mean.max()
    normal_mean = (mean - m_min) / (m_max - m_min)
    mean = resize_image(normal_mean.transpose((1,2,0)),in_shape[1:]).transpose((2,0,1)) * (m_max - m_min) + m_min
    #raise ValueError('Mean shape incompatible with input shape.')

Rebuild. Hope it help

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download