9

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?

4 Answers 4

14

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')]
2
  • Could you please elaborate on "So I change the new 'set_mean()'"? What exactly did you changed? Mar 11, 2015 at 13:50
  • 1
    Sure. I have added details of my change which is quite naive as you can see :) Mar 15, 2015 at 9:24
2

There are some problems currently due to a lot of interface changes introduced by the caffe developers. The Python Wrapper is not yet updated with these changes.

See this PR which fixes the problem: https://github.com/BVLC/caffe/pull/1964

2

The same error was solved in another google group:-

Where all you need to do is to change this:

 mean=np.load(mean_file)

to this:

mean=np.load(mean_file).mean(1).mean(1)

Google Group: Mean shape incompatible with input shape

1
  • Thanks, it works. But currently this line looks like <br>mean = caffe.io.blobproto_to_array(a)[0]. I simply add another line after this one <br> mean = mean.mean(1).mean(1) Sep 12, 2016 at 7:59
0

just one typo from 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.')
    '''

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.