Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Caffe C++ Classification Example

I recently modified the Caffe C++ classification example file and I am trying to recompile it. However, I'm having trouble linking a simple g++ compilation to the .hpp files in the include directory. I know this is a basic question but I can't seem to work it out - can someone help me work out how to compile this program? The compilation looks like this now:

g++ -I /home/jack/caffe/include classification.cpp -o classify

But I'm getting this error:

In file included from /home/jack/caffe/include/caffe/common.hpp:19:0,
    from /home/jack/caffe/include/caffe/blob.hpp:8,
    from /home/jack/caffe/include/caffe/caffe.hpp:7,
    from classification.cpp:1:
/home/jack/caffe/include/caffe/util/device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or directory
 #include <cublas_v2.h>

I'm running this on a machine without Nvidia GPUs so when I looked at the device_alternate.hpp file I realised this is calling a lot of cuda-related .hpp files as well which don't exist.

like image 887
Jack Simpson Avatar asked Aug 26 '15 05:08

Jack Simpson


3 Answers

Usually, in order to help the compiler locate header files you need to add -I /path/to/include/folder option to the compilation line:

~$ g++ -I /path/to/caffe/include myfile.cpp
like image 81
Shai Avatar answered Oct 05 '22 04:10

Shai


If you want to build custom files in caffe, there are two ways

The easy way

  • Make the necessary changes and keep the file ( in your case - classification.cpp ) inside a directory ( say test ) in examples folder in th e caffe root directory.
  • run make. This will automatically add the necessary cxxflags and ldflags and compile your code and place the executable in the build/examples/test folder. This also ensure the flag CPU_ONLY is set ( as mentioned in the Makefile.config )

The Hard way

  • Run make without the pretty print option ( mentioned inside Makefile.config ). You will be able to see the compile and link options used to build the examples and tools. You can copy and paste these options ( and make necessary changes to relative paths if used ) to compile your file

Hope this helps

Edit As the op requested an easy way, it can be done as follows

This is a very minimal example and I encourage the OP to refer to full online documentation and example of cmake usage.

  • Requirements
    • Caffe needs to be built with cmake - Relatively easy as the current master branch has CMakeLists and everything defined. Use the Cmake-gui or ccmake to set your options

Now, I am assuming you have a project structure as follows.

-project  
    - src  
         - class1.cpp
         - CMakeLists.txt ( to be added )
    - include
         - class1.hpp

    - main.cpp
    - CMakeLists.txt ( to be added )

The CMakeLists.txt ( src ) needs to contain (at minimum) the following lines,

cmake_minimum_required(VERSION 2.8)
find_package(OpenCV REQUIRED) # Optional in case of dependency on opencv 
add_library( c1 class1.cpp )

Note: In case class1 depends on other external libraries, the path to headers must be included using include_directories.

The CMakeLists.txt ( outermost ) needs to contain the following at minimum

cmake_minimum_required(VERSION 2.8)
PROJECT(MyProject)

find_package(OpenCV REQUIRED)
find_package(Caffe REQUIRED)

include_directories( "${PROJECT_SOURCE_DIR}/include" )
add_subdirectory( src )

include_directories( "$Caffe_INCLUDE_DIRS}" )
add_executable(MyProject main.cpp)

target_link_libraries( MyProject ${OpenCV_LIBS} c1 ${Caffe_LIBRARIES} )    

Now, the following commands from inside the project directory will create the executable MyProject inside the build folder.

mkdir build
cd build
cmake ..
make

You can then run your program with ./MyProject (arguments)

EDIT 2

Satisfying the requirement of building caffe with CMake is very important for this to work. You need to configure and generate the Makefiles using CMake. Use cmake-gui or ccmake for this purpose so that you can set your options like CPU_ONLY, etc.

You should create a build directory inside caffe and execute the following for a basic setup

mkdir build
cd build
cmake ..
make -jX #X is the number of threads your CPU can handle

Now, the .cmake directory in your $HOME folder consists of the following /home/user/.cmake/packages/Caffe/<random_string> file. This file points to the install location of caffe ( which is our build directory )

Now, the find_package command should run without errors for your other projects. And since you are using CMake you can keep your project folder outside the Caffe folder ( and it is better to keep it outside as the make process of caffe will try to build your files but it will fail )

Note: In case that the error persists, you can manually set the Caffe_DIR during cmake configuration.

like image 34
ImgPrcSng Avatar answered Oct 05 '22 02:10

ImgPrcSng


Change Makefile.config to set CPU_ONLY := 1

# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
like image 26
Borisgin Avatar answered Oct 05 '22 02:10

Borisgin