我要在Mailerdaimon的回答中添加几句话。
我按照安装指南(https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM)在我的 vagrant 虚拟机中设置 Caffe。仅供参考,虚拟机不支持 GPU 加速。回到正题,在我修复“示例脚本中的 CPU / GPU 切换”(https://github.com/BVLC/caffe/pull/2058)并添加“--print_results --labels_file”选项(https:// github.com/jetpacapp/caffe/blob/master/python/classify.py)到'python/classify.py',这个命令'./python/classify.py ./examples/images/cat.jpg foo --print_results ' 仍然抛出以下错误:
Traceback (most recent call last):
File "./python/classify.py", line 175, in <module>
main(sys.argv)
File "./python/classify.py", line 129, in main
channel_swap=channel_swap)
File "/home/vagrant/caffe/python/caffe/classifier.py", line 38, in __init__
self.transformer.set_mean(in_, mean)
File "/home/vagrant/caffe/python/caffe/io.py", line 267, in set_mean
raise ValueError('Mean shape incompatible with input shape.')
ValueError: Mean shape incompatible with input shape.
然后我转储'mean'(3 * 256 * 256)和'input'(3 * 227 * 227)的形状。显然这两种形状是不相容的。但是旧版本的 'set_mean()' 不会抛出错误,所以我深入研究了 python 代码并发现旧的 'set_mean()' 函数看起来像这样(python/caffe/pycaffe.py,第 195-202 行,https://github.com/jetpacapp/caffe/):
if mode == 'elementwise':
if mean.shape != in_shape[1:]:
# Resize mean (which requires H x W x K input in range [0,1]).
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
mean = caffe.io.resize_image(normal_mean.transpose((1,2,0)),
in_shape[2:]).transpose((2,0,1)) * (m_max - m_min) + m_min
但在最新的 Caffe 中,贡献者将“set_mean()”和其他转换函数封装到“Transformer”类中。新的 'set_mean()' 函数如下所示(python/caffe/io.py,第 253-254 行,https ://github.com/BVLC/caffe/ ):
if ms != self.inputs[in_][1:]:
raise ValueError('Mean shape incompatible with input shape.')
耶稣,这两个怎么可能是同一个功能?因此,我更改了新的“set_mean()”,注释掉了引发错误的句子,并像旧的“set_mean()”一样添加了形状重新调整大小的过程。
if ms != ins:
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.')
'''
瞧,问题解决了。
Classifying 1 inputs.
Done in 1.17 s.
[('tabby', '0.27933'), ('tiger cat', '0.21915'), ('Egyptian cat', '0.16064'), ('lynx', '0.12844'), ('kit fox', '0.05155')]