Comprehensive Guide: Installing Caffe2 with GPU Support by Building from Source on Ubuntu 16.04

In the previous posts, we have gone through the installation processes for deep learning infrastructure, such as Docker, nvidia-docker, CUDA Toolkit and cuDNN. With the infrastructure setup, we may conveniently start delving into deep learning: building, training, and validating deep neural network models, and applying the models into a certain problem domain. Translating deep learning primitives into low level bytecode execution can be an enormous task, especially for practitioners without interests in the deep learning calculus. Fortunately, there are several deep learning frameworks that provide the high level programming interface to assist in performing deep learning tasks.

In this post, we will go through the installation of Caffe2, one of the major deep learning frameworks. Caffe2 is adopted from Caffe, a deep learning framework developed by the Barkeley Vision and Learning Center (BVLC) of UC Berkeley. Caffe2 was started with the aim to improve Caffe especially to better support large-scale distributed model training, mobile deployment, reduced precision computation, new hardware, and flexibility of porting to multiple platforms.

Why Installing from Source?

It is important to note that instead of installation by building from source, there are some easier options, as for example by pulling and running Caffe2 Docker images. The Docker repository for Caffe2 images is accessible here. Nonetheless, if you observe the , the images are not recent and they were built against older version of CUDA toolkit and cuDNN. Therefore, one who intends to fiddle with latest Caffe2, CUDA, or cuDNN features may seriously consider installing from source.

Caffe2 Is Now A Part of Pytorch

In the past, Caffe2 source was maintained as an independent repository on Github. This changed from the end of March 2018 since Caffe2 repository was merged into Pytorch repository. Consequently, the common build process is now integrated into that of Pytorch.

There can be questions about the motivation of the source code merging. As explained in the announcement post, the merging was preceded by the development infrastructure sharing between Caffe2 and Pytorch that led into increasing amount of shared code and common libraries. It was then concluded that merging the two projects into one codebase would result in better engineering efficiency and increase the robustness of the framework especially in the area of model deployment.

What if Caffe2 has been previously installed? When installing Caffe2 prior to the source code merging, the build process would output header files, dynamic library and python library that subsequently copied to designated directories. In theory, it would be suffice to replace the old header and library files with the one from the newest build. If it does not work, a clean up is necessary by first removing the old files from their installed directories prior to proceeding with the installation

Pre-Requisites

We will be installing Caffe2 with GPU support. This article only covers the installation process for machines with NVIDIA GPUs. Verify that these items are satisfied prior to going through the installation.

1. Verify that CUDA toolkit has been installed.
If you have not installed CUDA toolkit, are unsure whether it is already installed, or want to install the newer version of CUDA toolkit, refer to this article that provides more elaboration about CUDA toolkit installation.

2. Verify that cuDNN has been installed.
You can refer to this article for more details about cuDNN installation

3. Remove files from previous Caffe2 installation
If you have installed Caffe2 earlier and want to do an upgrade, you may need to clean the files created and copied in the previous Caffe 2 installation.
Note: this can usually be skipped as the build tool will first check the existing version of Caffe2 installed in the system prior to replacing it with a new one


$ sudo rm -vRf /usr/local/include/caffe2
$ sudo rm -vf /usr/local/lib/libcaffe2*

Additionally, we may need to remove the python APIs from the Python package directory (path may vary depending on the installed Python version).

$ sudo rm -vRf /usr/local/lib/python2.7/dist-packages/caffe2

After checking all the prerequisites, we can now proceed with the installation.

Installation Steps

The installation can be accomplished by going through these steps in order.

1. Update apt package index
$ sudo apt-get update

2. Install apt package dependencies

remote$ sudo apt-get install -y --no-install-recommends build-essential cmake git libgoogle-glog-dev libgtest-dev libiomp-dev libleveldb-dev liblmdb-dev libopencv-dev libopenmpi-dev libsnappy-dev libprotobuf-dev openmpi-bin openmpi-doc protobuf-compiler python-dev python-pip

3. Install pip dependencies

$ sudo pip install --upgrade pip
$ sudo pip install setuptools future numpy protobuf
$ sudo apt-get install -y --no-install-recommends libgflags-dev

4. Clone Caffe 2 into a local directory
Note: We create a directory named caffe2-pytorch and clone Pytorch git repository into this directory.

$ mkdir caffe2-pytorch && cd caffe2-pytorch
$ git clone --recursive https://github.com/pytorch/pytorch.git ./
$ git submodule update --init

5. Build Caffe 2

$ mkdir build && cd build
$ cmake ..
$ sudo make -j"$(nproc)" install

Note: when building the source, we supply the -j flag. The flag refers to the number of threads that can be spawned by the compiler (i.e. GCC) when building the source code. The nproc command itself will print the number of the CPUs available. In short, we want to speed up the compilation time by creating a number of threads that amount to the number of CPUs to perform the compilation in parallel.

6. Create the symbolic link for Caffe 2 shared library
$ sudo ldconfig

7. Verify that Caffe 2 library and the headers have been installed in the proper directory
– Update the locate database
$ sudo updatedb

– Ensure that libcaffe2.so is located in /usr/local/lib
$ locate libcaffe2.so

– Ensure that the header files have been copied into /usr/local/include
$ locate caffe2 | grep /usr/loca/include/caffe2

8. Add Caffe2 into python and library paths so that it can be immediately discovered and linked by other applications.
$ vim ~/.profile

…
# set python path
if [ -z “$PYTHONPATH” ]; then
    PYTHONPATH=/usr/local
else 
    PYTHONPATH=/usr/local:$PYTHONPATH
fi

#set library path
if [ -z “$LD_LIBRARY_PATH” ]; then
    LD_LIBRARY_PATH=/usr/local/lib
else
    LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
fi
…

We immediately export the new paths by invoking the source command as follows.
$ source ~/.profile

Alternatively, we can use the dot (.) command, to execute ~/.profile file and enable the new environment variables in the current shell.
$ . ~/.profile

9. Verify that the Caffe2 python module can be properly invoked
$ python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

10. Verify that Caffe2 can run with GPU support
$ python2 -c 'from caffe2.python import workspace; print(workspace.NumCudaDevices())'

The installation is now complete. We can now start using Caffe2 for deep learning modeling and implementation. If you have problem with your Caffe2 installation, simply write it in the comment section.

0 Responses to “Comprehensive Guide: Installing Caffe2 with GPU Support by Building from Source on Ubuntu 16.04”


  • No Comments

Leave a Reply