CPU ONLY py-faster-rcnn: Documentation

Getting started with this semester, I had set up target to do well with Machine learning implementations.
I have reached a stage where I am trying to implement deep learning networks with Caffe. I got some interest with Faster Region based CNN, or commonly called faster RCNN. Hence, I started with cloning the repository and then tried some demo. And I was like ” ERR?” after watching so many errors filling up my terminal. I started following up errors one by one, where the root cause was I specified to use GPU but my laptop wasn’t actually using it. Further, when I went deep into this issue I got some knowledge of NVIDIA hybrid graphics that doesn’t “EASILY” let setup CUDA for parallel processing with GPU. And finally, when I found solution to hybrid graphics problem, I acknowledged that I have held broken packages for which re-installing Ubuntu was the only solution. And I would never like to re-install my ubuntu, to loose up all my precious customizations and libraries installed.

The only solution, here was to run faster RCNN without GPU that is CPU ONLY mode that they say that they have very specified in their documentation (LOL). I ended up with much more errors upon running demo examples with CPU_ONLY and the root cause I found was using GPU on CPY ONLY Caffe. It finally took 4 days to note down the necessary modifications so as to run CPU ONLY which are not specified anywhere over the air at one place. Here is the documentation for all the modifications needed.

Lets start with cloning py-faster-rcnn repository from here . Its a reliable source. Follow up the readme.md file for cloning this repository since it has another git repository within itself ( own caffe ). Make sure that you follow –recursive option for cloning.

Having cloned that, look for Software requirement section in the same readme. It says to install Caffe into the system, specifically with PYTHON LAYERS. So, follow up the documentation on the website for cloning the Caffe source. Caffe itself has some other requirements like ATLAS, Boost, BLAS. Their sources are already mentioned in website and instructions for installation can be found on individual official websites. Take a note that it might take a lot time to install Boost. So, I would recommend you to install it before going to bed. 😛 ( Well, I did this). When you are done with installing caffe prerequisites, its time to make caffe. Uncomment the following lines in Makefile.config which you are supposed to copy from Makefile.config.example.

WITH_PYTHON_LAYER := 1
USE_CPU :=1

 

For running py-faster-rcnn its necessary to brew caffe with support from python layers. Moreover, we have to specify to only use CPU. Rest follow up the MAKE procedure from documentation now. Ignore anything related to CuDNN there. (Its only for GPU users)

RANDOM: I had some problem with installing dependencies for caffe. Later, I found root cause to be my pip installer that was outdated. So, I would recommend that if you get error for dependencies not found even after you had installed it (using that “for” loop which installs python packages for Caffe, specified in document), its probably outdated pip. Here is the solution.

Now, assuming you are done with installing caffe, lets move to py-faster-rcnn. First of all, py-faster-rcnn uses its own installed Caffe. Sounds weird, but lets not have a discussion for that here. This also holds some reason for errors you might get in terminal as the two caffe installations will conflict for PATHS. Ok, now the main modification which aren’t specified in any documentation goes like this:

cd $py-faster-rcnn/lib

modify setup.py as shown below. Basically, I tried to comment lines of code that related directly to GPU.

...
#CUDA = locate_cuda()
...
...
#self.set_executable('compiler_so', CUDA['nvcc'])
...
...
#Extension('nms.gpu_nms',
# ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
# library_dirs=[CUDA['lib64']],
# libraries=['cudart'],
# language='c++',
# runtime_library_dirs=[CUDA['lib64']],
# # this syntax is specific to this build system
# # we're only going to use certain compiler args with nvcc and not with
# # gcc the implementation of this trick is in customize_compiler() below
# extra_compile_args={'gcc': ["-Wno-unused-function"],
# 'nvcc': ['-arch=sm_35',
# '--ptxas-options=-v',
# '-c',
# '--compiler-options',
# "'-fPIC'"]},
# include_dirs = [numpy_include, CUDA['include']]
# )

 

Also don’t forget to remove nms_gpu() module from /lib/fast-rcnn/nms_wrapper.py.

Just uncomment the import module for nms_gpu() module and also the “if-else” condition for its usage just below it.

Now,
cd /lib and run command: make.

Moving on, we make sure that caffe-fast-rcnn folder is present in your py-faster-rcnn repository. Change directory to caffe-fast-rcnn and modify the Makefile.config in similar way we did for Caffe’s Makefile.config.

And finally run command: make -j8 && make pycaffe.
Checkout to faster-rcnn branch in caffe-fast-rcnn git repository of you local system with command “git checkout faster-rcnn”.

Download pre-computed Faster R-CNN detectors with the script described within py-faster-rcnn readme. That too will take lot of time due to slow server hosting it. (Run the script before going to bed :P)

And now finally we run the demo, but here again we come up with error while simply running “./tools/demo.py”. Problem is that by default it uses GPU option for processing and also the VGG16 as neural net.
So, we have to run the demo as ” ./tools/demo.py –cpu –net zf”
Here, it will force use the CPU and also use ZF net for running the demo (try to use VGG16 yourself. Its too huge for CPU to process)

I guess, the modifications I mentioned above are sufficient for the rest of the users also. Please comment if anyone is stuck with some error even after following this documentation.

7 Responses to “CPU ONLY py-faster-rcnn: Documentation

  • cloud
    7 years ago

    Hi,

    This is great! It works as documented except:

    1) Can you explain what is the purpose of “git checkout faster-rcnn”? When I ran the command, tt failed to complete, indicating that the directory was not a git repository.

    2) I tried running the demo with vgg16 net. It took a very very long time to detect the objects. Is this expected?

    Thanks!
    cloud

    • shubh
      7 years ago

      I am glad it worked.
      Here are the answers to your queries.
      1. Check if you had cloned the caffe-fast-rcnn repository recursively using the –recursive flag. If yes, then I am worried if you have tried to check out at wrong place. First check if the branch exists on your local system using “git branch” in caffe-fast-rcnn directory. I am sure you will find it. We need to checkout to faster-rcnn as py-faster-rcnn has its own caffe which is needed to be tweaked. The faster-rcnn branch is tweaked version made specially for py-faster-rcnn. I guess you have just ran the demo.py to test your installation. But when you will compile your own models, you are probably going to face errors coz of wrong installation.

      2. VGG16 is really a big network. It is expected that for CPU-ONLY mode, object detection and localization would take more than 4 sec. Don’t worry about this. This clearly explains why GPUs and CUDA are hot these days. They would give the same object detection within milli-seconds.

  • Akshay
    7 years ago

    Could you share the complete contents of your setup.py file?

    • shubh
      7 years ago

      Hey Akshay,
      I am really sorry but I don’t have the previous version of setup.py file as I have shifted GPU mode. But whatever I have mentioned to be edited in setup.py is just fine. Go ahead and keep me updated about your progress if possible. 🙂

  • Indhu
    7 years ago

    Hi,
    I am getting the following error when i run demo.py
    Traceback (most recent call last):
    File “/root/deep-learning/FRCNN/py-faster-rcnn/tools/../lib/rpn/proposal_layer.py”, line 10, in
    import yaml
    ImportError: No module named yaml

    What could be the cause

  • How to perform training in faster RCNN in CPU mode? After these modifications, i successfully executed demo.py but or my own dataset training it is showing error (cannot use GPU in CPU only Caffe). Any help?

    • shubh
      7 years ago

      Hey, I guess you just forgot to add cpu flag while executing train command. I hope the flag exists. Please go through the training code once to find it exists in arg parse section 🙂

%d bloggers like this: