Caffe is one of most popular deep learning framework like TensorFlow. Many deep learning codes are based on Caffe, so knowing how to use this would be pretty useful for implementing deep learning programs.
Caffe has good documentation for installation but depends on the environment of the computer, it generates a bunch of errors. I also had so much error to install it, so I just would like to describe here how I installed and what kind of error I got during the installation procedure.
This post is based on the Caffe’s installation guide on their Github repo (https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide). I followed the instruction exactly, but as I mentioned, you might have some error even though you follow it, so I writing this post based on my experience.
Caffe requires some software installed before its installation.
- Python
- OpenCV
- Cuda Toolkit
- CuDNN
Actually each of the requirement will take much your time to install if you don’t have it yet, especially I spent so much time to install Cuda at the first time, so I will post this soon as well. Assuming you already have all of the required softwares on your machine, and please make sure your PYTHONPATH includes the OpenCV location you want to use(in case you have multiple version of OpenCVs). PYTHONPATH can be added from .bashrc file.
I am using Ubuntu 16.04, OpenCV 3.4, Cuda 8.0 and 5.1 CuDNN. Once you are ready with all the requirement, then git clone the Caffe repo to where you want and what you have to do after that is just three things.
- Modify makefile.config
- Modify makefile
- Build
Seems pretty simple, but this part gave me a lot of consecutive errors and I finally figured out what I have to do after searching for all the solution for the error. Once you completed the download, copy Makefile.config.example and change the name to Makefile.config. And then, the contents should be modified. Please find each of the lines in the file, and modify them as below.
CUDA_DIR := /usr/local/cuda-8.0
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/local/lib/python2.7/dist-packages/numpy/core/include
WITH_PYTHON_LAYER := 1
PYTHON_LIB := /usr/lib/x86_64-linux-gnu
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
After that, as shown in the official instruction, you need to make a symbolic link to libhdf5 using commands below.
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
Next step is modifying the makefile. If you find “USE_OPENCV” in Makefile, you will see some lines which specifies LIBRARIES. You can add it as below.
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system boost_filesystem hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio
Now build it.
cd /directory/to/Caffe/
mkdir build
cd build
make clean
make all
If you want to build PyCaffe,
cd /directory/to/Caffe/
make pycaffe
The last step is to add the Caffe directory to PYTHONPATH. You can open .bashrc file and add it as below.
gedit ~/.bashrc
export PYTHONPATH=/directory/to/caffe/python:$PYTHONPATH
The installation is done. Enjoy Caffe!
Troubleshoot
- NVCC sm20 sm21 related error
Delete lines below in makefile.config. This warning doesn’t seem critical for building, so you don’t need to do though…
-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
2. tools/CMakeFiles/upgrade_solver_proto_text.dir/build.make:128: recipe for target ‘tools/upgrade_solver_proto_text
Edit “caffe/cmake/Dependencies.cmake”, at line 28
--- list(APPEND Caffe_LINKER_LIBS ${HDF5_LIBRARIES})
+++ list(APPEND Caffe_LINKER_LIBS ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES})
(reference: https://devtalk.nvidia.com/default/topic/1037599/jetson-tx2/installation-of-caffe-error/)
3. //usr/local/lib/libopencv_core.so.3.4: undefined reference to `cudaEventElapsedTime@libcudart.so.9.0′
In my case, my OpenCV is installed on Cuda 9.0 from the first place, so I reinstalled OpenCV to depend on Cuda 8.0.