我使用https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM作为灵感,在安装了CUDA(没有驱动程序)的Ubuntu 14.04虚拟服务器上安装Caffe。在安装过程中,我在构建之前编辑了MakeFile以包含"CPU_ONLY := 1"
。然而,似乎Caffe仍在尝试使用GPU。当我尝试运行测试示例时,我收到以下错误:
python python/classify.py examples/images/cat.jpg foo
Traceback (most recent call last):
File "python/classify.py", line 130, in <module>
main(sys.argv)
File "python/classify.py", line 103, in main
channel_swap=channel_swap)
TypeError: __init__() got an unexpected keyword argument 'gpu'
如何解决此问题并完全依靠CPU运行?
答案 0 :(得分:14)
我要在Mailerdaimon的回答中添加几句话。
我按照安装指南(https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM)在我的流浪虚拟机中设置了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()'和其他转换函数封装到了类中 '变压器'。新的'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.')
'''
Voila,问题解决了。
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')]
答案 1 :(得分:2)
由于caffe开发人员引入了大量的界面更改,目前存在一些问题。 Python Wrapper尚未更新这些更改。
请参阅此PR以解决问题:https://github.com/BVLC/caffe/pull/1964
答案 2 :(得分:2)
另一个谷歌小组解决了同样的错误: -
你需要做的就是改变这个:
mean=np.load(mean_file)
到此:
mean=np.load(mean_file).mean(1).mean(1)
答案 3 :(得分:0)
来自user2696499的一个错误
if ms != 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.')
'''