尽管设置了CPU_Only,仍然使用GPU,产生意外的关键字参数

pir*_*pir 9 python caffe

我正在安装CUDA(没有驱动程序)的Ubuntu 14.04虚拟服务器上安装Caffe,使用https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM作为灵感.在安装过程中,我"CPU_ONLY := 1"在构建之前编辑了MakeFile以包含它.然而,似乎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'
Run Code Online (Sandbox Code Playgroud)

我该如何修复此问题并完全依靠CPU运行?

use*_*499 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.
Run Code Online (Sandbox Code Playgroud)

然后我转储'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
Run Code Online (Sandbox Code Playgroud)

但在最新的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.')
Run Code Online (Sandbox Code Playgroud)

耶稣,这两个怎么可能是同一个功能?所以我改变了新的'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.')
    '''
Run Code Online (Sandbox Code Playgroud)

瞧,问题解决了.

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')]
Run Code Online (Sandbox Code Playgroud)