Error using classification in caffe

I use caffe in python for classification. I got the code here . Here I just use simple code like

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)) 

However, I got an error, for example

 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. 

Could you help me redo it? Thanks

+5
source share
4 answers

Go to line 253-254 in the file caffe / python / caffe / io.py Replace

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

Across

 if ms != self.inputs[in_][1:]: print(self.inputs[in_]) 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 this helps.

+14
source

I had the same problem based on imagenet web demo, I modified the script using this method to load the middle file on line 95

mean = np.load(args.mean_file).mean(1).mean(1)

+1
source

I am very afraid to rebuild the code, since installing caffe did not become easy for me. But to fix the resizing solution requires in_shape (user8264 response), which is installed inside caffe / classifier.py

Anyway, I debugged and found the value for in_shape = (3, 227, 227) for age_net.caffemodel

Thus, the model used to predict age and gender will have the following change:

 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)) 

But first you need to change the value:

 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 

This will get rid of "ValueError: middle form incompatible with the input form." But I'm not sure about the accuracy. Apparently, for me, skipping the middle parameter gave a better prediction of age :)

0
source

Change deploy_gender.prototxt and set: input_dim: 256 input_dim: 256

I don’t know why it was spelled incorrectly ...

0
source

All Articles