Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running without CUDA (CPU only) #25

Closed
martiningram opened this issue Mar 22, 2017 · 26 comments
Closed

Running without CUDA (CPU only) #25

martiningram opened this issue Mar 22, 2017 · 26 comments

Comments

@martiningram
Copy link

Hi,

I am just wondering if there is a way of running this model on a machine without a GPU? Running make attempts to find CUDA.

Thanks!

@endernewton
Copy link
Owner

you can install cuda without gpu. haven't tried that though.

@jwnsu
Copy link

jwnsu commented Mar 28, 2017 via email

@endernewton
Copy link
Owner

Ahh I see. thanks for the update. Closing.

@guotong1988
Copy link

guotong1988 commented Apr 1, 2017

@jwnsu Could you please describe how to run it on CPU in detail? Thank you.

@guotong1988
Copy link

Finally it shows:
iter: 20 / 70000, total loss: 2.201489

You need to:
change nms_wrapper.py.
change setup.py.
make at ./lib.
put the data at tf-faster-rcnn-master/data/VOCdevkit2007/VOC2007
and here are the parameters:

  parser.add_argument('--cfg', dest='cfg_file',
                      help='optional config file',
                      default="/home/gt/tf-faster-rcnn-master/experiments/cfgs/vgg16.yml", type=str)
  parser.add_argument('--weight', dest='weight',default="/home/gt/tf-faster-rcnn-master/vgg16.ckpt",
                      help='initialize with pretrained model weights',
                      type=str)
  parser.add_argument('--imdb', dest='imdb_name',
                      help='dataset to train on',
                      default='voc_2007_trainval', type=str)
  parser.add_argument('--imdbval', dest='imdbval_name',
                      help='dataset to validate on',
                      default='voc_2007_trainval', type=str)
  parser.add_argument('--iters', dest='max_iters',
                      help='number of iterations to train',
                      default=70000, type=int)
  parser.add_argument('--net', dest='net',
                      help='vgg16 or res101',
                      default='vgg16', type=str)

@bhack
Copy link
Contributor

bhack commented Apr 1, 2017

@guotong1988 Can you open a PR with a CPU option.. could be useful for users with GPU memory limits..

@caunion
Copy link

caunion commented Apr 27, 2017

I think you can add a parameter in commandline to force CPU. i.e.

./experiments/scripts/train_faster_rcnn.sh 1 pascal_voc vgg16 USE_GPU_NMS False

@Dinkz
Copy link

Dinkz commented May 8, 2017

@guotong1988
Can I know what change you did inside setup.py ?

@luoru
Copy link

luoru commented May 23, 2017

@guotong1988
what change ocured in nms_wrapper.py & setup.py?

@dancsalo
Copy link

@caunion's comment contains all that you need to do to force CPU. No need to change nms_wrapper.py or setup.py. Just tried it myself!

@joeliven
Copy link

joeliven commented Jun 5, 2017

@endernewton great implementation of Faster RCNN. Really appreciate it.
Question though: I can't get this to work on CPU only. I've read through the above and tried those suggestions but can't seem to get it to work. @caunion @guotong1988 has anybody had success in making this work on CPU? If so, how?
Thanks in advance for any suggestions. Much appreciated!

@endernewton
Copy link
Owner

endernewton commented Jun 6, 2017 via email

@riadhayachi
Copy link

use this command
./tools/demo.py cpu

@qrfaction
Copy link

@riadhayachi
demo.py: error: unrecognized arguments: CPU

@kgapost
Copy link

kgapost commented Nov 30, 2017

The nms_wrapper.py checks the model.config structure if USE_GPU_NMS flag is found. So, no need to change anything in nms_wrapper.py. Just set the flag in demo.py. Add the line cfg.USE_GPU_NMS =False after line 114 in demo.py. This worked for me.

@hang81
Copy link

hang81 commented Mar 21, 2018

Hi,

I would like to deploy this great project without GPU (I do not have GPU on my laptop). However, after reading all the threads above, I am still stuck in Step 3 as described on the project page w.r.t Installation:

  1. Build the Cython modules
    make clean
    make
    cd ..

It throws the error message as follows,

python setup.py build_ext --inplace --compiler=mingw64
Traceback (most recent call last):
File "setup.py", line 55, in
CUDA = locate_cuda()
File "setup.py", line 43, in locate_cuda
raise EnvironmentError('The nvcc binary could not be '
OSError: The nvcc binary could not be located in your $PATH. Either add it to your path, or set >$CUDAHOME
Makefile:2: recipe for target 'all' failed
mingw32-make: *** [all] Error 1

Any idea/suggestion how to proceed?

Thanks.

Regards,

Hang

@joeliven
Copy link

joeliven commented Mar 26, 2018

@hang81 try this:

def customize_compiler_for_nvcc(self):
    """inject deep into distutils to customize how the dispatch
    to gcc/nvcc works.

    If you subclass UnixCCompiler, it's not trivial to get your subclass
    injected in, and still have the right customizations (i.e.
    distutils.sysconfig.customize_compiler) run on it. So instead of going
    the OO route, I have this. Note, it's kind of like a weird functional
    subclassing going on."""

    # save references to the default compiler_so and _comple methods
    default_compiler_so = self.compiler_so
    super = self._compile

    # now redefine the _compile method. This gets executed for each
    # object but distutils doesn't have the ability to change compilers
    # based on source extension: we add it.
    def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
        print(extra_postargs)
        postargs = extra_postargs['gcc']

        super(obj, src, ext, cc_args, postargs, pp_opts)
        # reset the default compiler_so, which we might have changed for cuda
        self.compiler_so = default_compiler_so

    # inject our redefined _compile method into the class
    self._compile = _compile


# run the customize_compiler
class custom_build_ext(build_ext):
    def build_extensions(self):
        customize_compiler_for_nvcc(self.compiler)
        build_ext.build_extensions(self)

ext_modules = [
    Extension(
        "utils.cython_bbox",
        ["utils/bbox.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    ),
    Extension(
        "utils.cython_nms",
        ["utils/nms.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    ),
    Extension(
        "nms.cpu_nms",
        ["nms/cpu_nms.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    )
]

setup(
    name='tf_faster_rcnn',
    ext_modules=ext_modules,
    # inject our custom trigger
    cmdclass={'build_ext': custom_build_ext},
)

This is what I used when debugging locally on my machine that has cpu only. I named it setup_cpu.py and had it next to the included setup.py and used one or the other based on if gpu machine or cpu only machine. Note this does not include the import lines bc some are custom my specific project, but...for me this worked for running the setup code for cython extensions when no gpu avail. Hope it helps!

@lshe7842
Copy link

This works thanks @joeliven. But I think you need to remove the 2nd Extension since 'nms.pyx' doesn't exist, at least in my repo.

@joeliven
Copy link

No problem @lshe7842 , glad it worked for you!

@gepengxu
Copy link

gepengxu commented May 16, 2018

@joeliven
I just try to update the setup.py based on your suggestions; It Look like ok
But when I try to make, Just find other issue

sh make.sh
make.sh: 9 : make.sh: nvcc not found

My computer is not installed CUDA and nvcc,So if you change make.sh

@daa233
Copy link

daa233 commented Jun 5, 2018

Based on @shuang1330‘s answer in #72, what I have done to support running with CPU on Mac OS:

  • lib/setup.py: replace raise EnvironmentError to pass to prevent the no CUDA environment error; comment the block of Extension('nms.gpu_nms'...) to complile with out nms.gpu_nms.
  • lib/model/config.py: change __C.USE_GPU_NMS = True to __C.USE_GPU_NMS = False.
  • lib/model/nms_wrapper: comment from nms.gpu_nms import gpu_nms to prevent import error.

@Medj9
Copy link

Medj9 commented Jul 9, 2018

hi , i have a issue in running demo.py on cpu in ubuntu 16.04,
tensorflow version- 1.2
python 3.5
when i gave path to the model,it is present
error shows in saver.restore(sess, tfmodel)

Error:
Traceback (most recent call last):
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1139, in _do_call
return fn(*args)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1121, in _run_fn
status, run_metadata)
File "/usr/lib/python3.5/contextlib.py", line 66, in exit
next(self.gen)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.OutOfRangeError: Read less bytes than requested
[[Node: save/RestoreV2_529 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_529/tensor_names, save/RestoreV2_529/shape_and_slices)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/tf-faster-rcnn/tools/demo.py", line 155, in
saver.restore(sess, tfmodel)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1548, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.OutOfRangeError: Read less bytes than requested
[[Node: save/RestoreV2_529 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_529/tensor_names, save/RestoreV2_529/shape_and_slices)]]

Caused by op 'save/RestoreV2_529', defined at:
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/tf-faster-rcnn/tools/demo.py", line 153, in
saver = tf.train.Saver()
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1139, in init
self.build()
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 1170, in build
restore_sequentially=self._restore_sequentially)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 691, in build
restore_sequentially, reshape)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 407, in _AddRestoreOps
tensors = self.restore_op(filename_tensor, saveable, preferred_shard)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/training/saver.py", line 247, in restore_op
[spec.tensor.dtype])[0])
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/ops/gen_io_ops.py", line 640, in restore_v2
dtypes=dtypes, name=name)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/kpit/Desktop/Faster_Rcnn_Tensorflow/Virtualenv_tensorflow/tensorflow/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1269, in init
self._traceback = _extract_stack()

OutOfRangeError (see above for traceback): Read less bytes than requested
[[Node: save/RestoreV2_529 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_529/tensor_names, save/RestoreV2_529/shape_and_slices)]]

@Nioolek
Copy link

Nioolek commented Jul 18, 2018

I used the method commented by grimfix on 30 Nov 2017 , and it worked without gpu.The comment is on the front.

@smallnine9
Copy link

@Nioolek Do you use bash make.sh?

@smallnine9
Copy link

@endernewton Alright,I find this issue is about tf-faster-rcnn, but in pytorch-faster-rcnn, we should use bash make.sh,but without GPU,without nvcc, that would report error,how to solve it?

@salutdamour
Copy link

Can it run under python3.6?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests