2015-06-12 33 views
5

我在python中使用caffe進行分類。我從here獲得代碼。在這裏,我只是用簡單的代碼,如在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. 

你能幫我reslove呢?由於

回答

14

讓我們去排隊在朱古力/蟒蛇/朱古力253-254/io.py 更換

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

通過

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

重建。希望它能幫助

1

我有同樣的問題,總部設在imagenet網絡演示中,我使用這種方式來加載線的平均文件修改腳本95

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

0

我非常害怕重建作爲caffe安裝的代碼對我來說並不容易。 但是修復,將溶液調整大小意味着需要in_shape(user8264的反應),這是在CAFFE內部設定/ classifier.py

無論如何,我調試,發現值in_shape =(3,227,227),用於age_net.caffemodel

所以用於年齡和性別預測模型將如下變化:

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:平均形狀與輸入形狀不兼容」。但我不確定準確度。顯然,對我逃課平均參數給出了更好的年齡預測:)

0

編輯deploy_gender.prototxt並設置: input_dim:256 input_dim:256

不知道爲什麼有人寫錯了......