Learning to be Giant.

How to compile Caffe

|

Compiling caffe is really a pain in the ass. This may be a slightly better (maybe not) version of the Caffe installation instruction. Please check http://caffe.berkeleyvision.org/installation.html for more information. This tutorial is tested on a workstation with Titan Black and Tesla K40. Ubuntu 14.04 Linux is assumed.

First of all, download Caffe source code from Github:

git clone https://github.com/BVLC/caffe.git

Dependencies

General Dependencies

Follow the instructions on caffe official site and install all the general dependencies. Ignore the installation of python as we will use Miniconda.

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

For Ubuntu 14.04, also install

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

Also, don’t forget to install BLAS. Here we choose Atlas, you can also use MKL or OpenBLAS.

sudo apt-get install libatlas-base-dev

Miniconda

Miniconda makes the compilation of Caffe easier.

Download miniconda installation script from its website: http://conda.pydata.org/miniconda.html. Install conda following the instructions here: http://conda.pydata.org/docs/install/quick.html#linux-miniconda-install. Simply put, just run:

bash Miniconda-latest-Linux-x86_64.sh

Install Python Dependencies with Conda

Go to python directory under Caffe.

conda create --name caffe python pip # create an environment named caffe
source activate caffe # switch to caffe environment
for req in $(cat requirements.txt); do pip install $req; done # install dependencies

Optionally, you can add the following line to .profile or .bashrc.

export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

This adds PyCaffe into your PYTHONPATH, and whenever you want to import caffe from python, you can simply import caffe. Some people may prefer not to add too many things to PYTHONPATH, in this case when you use caffe in python, you can add the following code snippet to the beginning of your python code:

import sys

caffe_root = '<Your caffe root>'
sys.path.insert(0, caffe_root + 'python')

import caffe

CUDA and Nvidia driver

If you want the caffe to support GPU, then CUDA should be installed. At the time this post is written, the latest version of CUDA is 7.5, and most major deep learning frameworks including caffe recommends this version.

To install CUDA 7.5, first go to https://developer.nvidia.com/cuda-downloads and download the CUDA of your platform (Ubuntu 14.04). Then run the following commands:

sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb
sudo apt-get update
sudo apt install cuda-7-5

Once CUDA is installed, you should see a directory called cuda under /usr/local/. Then in your .bash_profile or .bashrc, add the following:

export CUDA_HOME=/usr/local/cuda
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64
export PATH=${CUDA_HOME}/bin:$PATH

Along with the CUDA, a Nvidia driver is installed. However, this driver is outdated. I would recommend to install a latest driver. Simply go to http://www.nvidia.com/Download/index.aspx?lang=en-us to download the driver of your GPU and follow the instructions to install.

cuDNN

Caffe RC3 supports cuDNN v4. Download cuDNN v4 from its website: https://developer.nvidia.com/rdp/cudnn-download. At the time this post is written, caffe and most other major deep learning frameworks has not supported cuDNN v5 yet.

Untar the downloaded package and you will get a folder that contains two sub-folders: lib64 and include, just copy the contents of the 2 folders to the respective folders in /usr/local/cuda:

sudo cp lib64/* /usr/local/cuda/lib64/
sudo cp include/* /usr/local/cuda/include/

Compiling

Copy the example Makefile.config.example to Makefile.config, and modify it. The Makefile.config I used is: https://gist.github.com/codinfox/61105162c46af3c8aa89.

Go to Caffe root, run:

make all -j8
make test -j8
make runtest

All testcases should pass. Otherwise, do troubleshooting. One problem I met is that caffe may complain about my locale during the compilation. Because I use Mac and SSH to a remote machine to compile, it seems iTerm2 set the wrong locale during SSH. A workaround is :

export LC_ALL=C

Finally, compile python interface:

make pycaffe

Comments