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”:
You 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:
Now let’s install the necessary dependencies in our current caffe environment:
Let’s clone caffe’s repo and its submodules into our home directory.
We shall avoid polluting the caffe source tree by building within a build folder
We shall now build the package using CMake with the following flags
- CMake variable
BLAS=open
indicates that we would like use OpenBLAS instead of the default which is ATLAS - CMake variable
CUDNN_INCLUDE
indicates where to find theinclude
directory for your cuDNN - CMake variable
CUDNN_LIBRARY
indicates where to find the library path for your cuDNN - CMake variable
CMAKE_PREFIX_PATH
tells CMake to look for packages in your conda environment before looking in system install locations (like/usr/local
) - CMake variable
CMAKE_INSTALL_PREFIX
indicates where to install Caffe binaries - CMake variable
DCMAKE_CXX_FLAGS
indicates which C++ compiler version to use - CMake variable
CPU
indicates 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
Let’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
After make is completed, we are now finally ready to install
Now we run test
You’d think we’re done, but not quite! We have to point the $PYTHONPATH
environment variable to our build folder like so
However 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
Edit ./etc/conda/activate.d/env_vars.sh
as follows:
Edit ./etc/conda/deactivate.d/env_vars.sh
as follows:
Now let’s reload the current environment to reflect the variables
We are now ready to test if caffe has been installed correctly with CUDA