Caffe2

Hi guys, this article is about installation process of caffe2 with Anaconda Python on ubuntu(16.04). Before start, I assume that you have already installed Anaconda Python 3 version. I installed Anaconda 4.0.0 Python 3.6 version. If you did not install the Anaconda, please follow the link here.

First of all you need to know that caffe2 is not support python3 so we need to create an environment for python 2.7. To do that with Anaconda you need to run this command below.

conda create -n py27 python=2.7.13 anaconda

This command will create a new virtual environment named py27 with anaconda packages (numpy, pandas etc.). Then we need to activate this newly created environment.

source activate py27

You must see (py27) at the start of the location indicator of command prompt. So we are ready to go.

sudo apt-get update
sudo apt-get install -y --no-install-recommends \
      build-essential \
      cmake \
      git \
      libgoogle-glog-dev \
      libprotobuf-dev \
      protobuf-compiler \
      python-dev \
      python-pip                          
pip install numpy protobuf

We need to install dependencies first, please run the instructions respectively.

Optional Installation

Caffe2 has also GPU support. If you want to use GPU instead of CPU only, you should install NVIDIA CUDA 8 and cuDNN v5.1 or v6.0.

sudo apt-get update && sudo apt-get install wget -y --no-install-recommends
wget "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.61-1_amd64.deb"
sudo dpkg -i cuda-repo-ubuntu1604_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda

When the installation of cuda is finished, then we continue with cuDNN

CUDNN_URL="http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/cudnn-8.0-linux-x64-v5.1.tgz"
wget ${CUDNN_URL}
sudo tar -xzf cudnn-8.0-linux-x64-v5.1.tgz -C /usr/local
rm cudnn-8.0-linux-x64-v5.1.tgz && sudo ldconfig

Instructions below are also optional but I really recommend them to install

sudo apt-get install -y --no-install-recommends \
      libgtest-dev \
      libiomp-dev \
      libleveldb-dev \
      liblmdb-dev \
      libopencv-dev \
      libopenmpi-dev \
      libsnappy-dev \
      openmpi-bin \
      libgflags-dev \
      openmpi-doc \
      python-pydot

pip install \
      flask \
      future \
      graphviz \
      hypothesis \
      jupyter \
      matplotlib \
      pydot python-nvd3 \
      pyyaml \
      requests \
      scikit-image \
      scipy \
      setuptools \
      six \
      tornado

Finally it is time to start installing caffe2

git clone --recursive https://github.com/caffe2/caffe2.git && cd caffe2
make && cd build && sudo make install -j4

If your computer has a single processer then please remove the -j4 part at the end of the command. -j4 helps us to build project as parallel.

python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

If shell echoes “Failure” then please try to import core module of caffe2 interactively using python shell. If the error is GLIBCXX_3.4.20 not found (required by caffe2/python/caffe2_pybind11_state.so), so you are very lucky guys. You just need to run the command below.

conda install libgcc

This will install required dependencies to our conda instance. Then you should successfully import the caffe2.

If you want to use caffe2 from any location then you need to add caffe2 to PYTHONPATH

You can adjust environment variables like below

export PYTHONPATH=/usr/local:$PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/caffe2/build
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

Add these lines to end of the .bashrc file.

Enjoy the life!! See you on next one.