在caffe中使用分类时出错
Error when using classify in caffe
我在python中使用caffe进行分类。我从 here 获取代码。在这里,我只使用简单的代码,例如
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.')
来自
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.')
重建。希望对你有帮助
我遇到了同样的问题,基于 imagenet 网络演示,我使用这种方式修改了脚本以在第 95 行加载平均文件
mean = np.load(args.mean_file).mean(1).mean(1)
我很害怕重建代码,因为 caffe 安装对我来说并不容易。
但是要修复,调整大小的解决方案意味着需要 in_shape(user8264 的响应),这是在 caffe/classifier.py
内部设置的
无论如何,我调试并找到 in_shape = (3, 227, 227) for 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))
但是mean需要先修改:
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: Mean shape incompatible with input shape"。但是我不确定准确性。显然,对我来说,跳过均值参数可以更好地预测年龄 :)
编辑 deploy_gender.prototxt 并设置:
input_dim: 256
input_dim: 256
不知道为什么写错了...
我在python中使用caffe进行分类。我从 here 获取代码。在这里,我只使用简单的代码,例如
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.')
来自
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.')
重建。希望对你有帮助
我遇到了同样的问题,基于 imagenet 网络演示,我使用这种方式修改了脚本以在第 95 行加载平均文件
mean = np.load(args.mean_file).mean(1).mean(1)
我很害怕重建代码,因为 caffe 安装对我来说并不容易。 但是要修复,调整大小的解决方案意味着需要 in_shape(user8264 的响应),这是在 caffe/classifier.py
内部设置的无论如何,我调试并找到 in_shape = (3, 227, 227) for 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))
但是mean需要先修改:
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: Mean shape incompatible with input shape"。但是我不确定准确性。显然,对我来说,跳过均值参数可以更好地预测年龄 :)
编辑 deploy_gender.prototxt 并设置: input_dim: 256 input_dim: 256
不知道为什么写错了...