Installing Caffe with CUDA in Conda
The following guide shows you how to install install Caffe with CUDA under the Conda virtual environment.
Assumptions
- Ubuntu OS
- NVIDIA GPU with CUDA support
- Conda (see installation instructions here)
- CUDA (installed by system admin)
Specifications
This guide is written for the following specs:
- Ubuntu 16.04
- Python 2.7
- CUDA 8
- cuDNN v7.1
- Miniconda 2
- OpenCV3
Guide
First, get cuDNN by following this cuDNN Guide.
Let’s create a virtual Conda environment called “caffe”:
conda create -n caffe python=2.7You many of course use a different environment name, just be sure to adjust accordingly for the rest of this guide
After it prepares the environment and installs the default packages, activate the virtual environment via:
conda activate caffe
# to deactivate: conda deactivate caffeNow let’s install the necessary dependencies in our current caffe environment:
conda install lmdb openblas glog gflags hdf5 protobuf leveldb boost opencv cmake numpy=1.15 -y
conda install -c conda-forge doxygen -yLet’s clone caffe’s repo and its submodules into our home directory.
cd ~
git clone --recursive https://github.com/BVLC/caffe.git
cd caffe
git submodule update --init --recursiveWe shall avoid polluting the caffe source tree by building within a build folder
mkdir build && cd build
pwd #=> ~/caffe/buildWe shall now build the package using CMake with the following flags
cmake -DBLAS=open -DCUDNN_INCLUDE=$CUDA_HOME/include/ -DCUDNN_LIBRARY=$CUDA_HOME/lib64/libcudnn.so -DCMAKE_PREFIX_PATH=$CONDA_PREFIX -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DCMAKE_CXX_FLAGS="-std=c++11" ..- CMake variable
BLAS=openindicates that we would like use OpenBLAS instead of the default which is ATLAS - CMake variable
CUDNN_INCLUDEindicates where to find theincludedirectory for your cuDNN - CMake variable
CUDNN_LIBRARYindicates where to find the library path for your cuDNN - CMake variable
CMAKE_PREFIX_PATHtells CMake to look for packages in your conda environment before looking in system install locations (like/usr/local) - CMake variable
CMAKE_INSTALL_PREFIXindicates where to install Caffe binaries - CMake variable
DCMAKE_CXX_FLAGSindicates which C++ compiler version to use - CMake variable
CPUindicates whether or not to use CPU-only installation - Also see the list of the available make flags and their default values
Let’s find out how many cores your machine has
cat /proc/cpuinfo | grep processor | wc -l
#=> nLet’s make the package efficiently by maximising the number of jobs for it.
General rule of thumb is to use 1 + n number of jobs where n is the output from
the previous command. i.e. number of cores. Mine was 24 so I run the following
make all -j 25
make pycaffeAfter make is completed, we are now finally ready to install
make installNow we run test
make runtestYou’d think we’re done, but not quite! We have to point the $PYTHONPATH
environment variable to our build folder like so
export PYTHONPATH=$HOME/caffe/python:$PYTHONPATHHowever it will be tedious to type that everytime we activate our environment.
You may append that line to .bash_profile or .bashrc but some variables
such as $PYTHONPATH are potentially used in many environments and it could
lead to python import errors when the paths contain different modules sharing
the same name. For instance, both caffe and caffe2 contain a module named
‘caffe’.
The solution to overcome this is to write a script to save our environment variables within our environemnt so that they get loaded automatically every time we activate our environment and get unset automatically when we deactivate our environment. The following steps are an adaptation of this guide stated in the official Conda documentation.
Let’s enter our environment directory and do the following
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.shEdit ./etc/conda/activate.d/env_vars.sh as follows:
#!/bin/sh
export PYTHONPATH=$HOME/caffe/python:$PYTHONPATHEdit ./etc/conda/deactivate.d/env_vars.sh as follows:
#!/bin/sh
unset PYTHONPATHNow let’s reload the current environment to reflect the variables
conda activate caffeWe are now ready to test if caffe has been installed correctly with CUDA
cd ~
# To check if Caffe build was successful
python -c 'import caffe; caffe.set_mode_gpu()' 2>/dev/null && echo "Success" || echo "Failure"
#=> Success