Using GPU despite setting CPU_Only, yielding unexpected keyword argument

pir picture pir · Feb 24, 2015 · Viewed 7.2k times · Source

I'm installing Caffe on an Ubuntu 14.04 virtual server with CUDA installed (without driver) using https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM as inspiration. During the installation process I edited the MakeFile to include "CPU_ONLY := 1" before building it. However, it seems that Caffe is still trying to make use of the GPU. When I try to run a test example I get the following error:

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'

How can I fix this and run entirely on CPU?

Answer

user2696499 picture user2696499 · Mar 11, 2015

I'm gonna add a few words to Mailerdaimon's answer.

I followed the installation guide (https://github.com/BVLC/caffe/wiki/Ubuntu-14.04-VirtualBox-VM) to setup Caffe in my vagrant virtual machine. FYI, virtual machines DO NOT support GPU accelerating. Back to the point, after I fix 'CPU / GPU switch in example scripts'(https://github.com/BVLC/caffe/pull/2058) and add '--print_results --labels_file' options(https://github.com/jetpacapp/caffe/blob/master/python/classify.py) to 'python/classify.py', this command './python/classify.py ./examples/images/cat.jpg foo --print_results' still throws the following error:

  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.

Then I dump the shape of 'mean'(which is 3*256*256) and 'input'(3*227*227). Obviously these two shapes are incompatible. But old versions of 'set_mean()' do NOT throw the error, so I dig into the python code and find out that old 'set_mean()' function looks like this(python/caffe/pycaffe.py, line 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

But in latest Caffe, contributors encapsulate 'set_mean()' and other transformation functions into class 'Transformer'. New 'set_mean()' function looks like this(python/caffe/io.py, line 253-254, https://github.com/BVLC/caffe/):

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

Jesus, how could these two be the same function? So I change the new 'set_mean()', comment out the error raising sentence, and add shape re-sizing procedure as in old '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, problem solved.

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