How to Install Caffe and PyCaffe on Jetson TX2

2019-05-16 update: I just added the Installing and Testing SSD Caffe on Jetson Nano post. If you are installing caffe on a Jetson Nano, or on a Jetson TX2 / AGX Xavier with JetPack-4.2, do check out the new post.

Recently I started to use Caffe on Jetson TX2/TX1 since it is the deep learning framework best supported by NVIDIA TensorRT. At the time of this writing, the latest version of TensorRT for TX2/XT1 is TensorRT 2.1, which is included in JetPack-3.1.

Below I documented how I installed Caffe and PyCaffe for python3 on my Jetson TX2. Note that most of the Caffe installation tutorials I found online were using python2.7. I had to modify a few things to make everything working for python3.

Prerequisite:

  • Complete installation of JetPack-3.1 on the target Jetson TX2.
  • Build and install opencv-3.4.0, and make sure its python3 bindings are working properly. You can reference my How to Install OpenCV (3.4.0) on Jetson TX2 post.

Reference:

Installation Steps:

Note that in the following installation steps I omitted OpenCV and CUDA toolkit stuffs since they were already installed by the prerequisites.

### Install dependencies for Ubuntu (< 17.04), while omitting
### libopencv-dev
$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev \
                       libhdf5-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev
$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev

Next I’d grab Caffe source code from GitHub and create a Makefile.config for Jetson TX2. Basically I modified the following things from Makefile.config.example.

  • Set USE_CUDNN := 1
  • Set OPENCV_VERSION := 3
  • Add compute_62 (for TX2) and compute_53 (for TX1) into CUDA_ARCH
  • Replace python2.7 stuffs with python3.5
  • Replace boost_python3 with boost_python-py35 in PYTHON_LIBRARIES
  • Replace /usr/lib/python3.5/dist-packages/numpy/core/include with /usr/local/lib/python3.5/dist-packages/numpy/core/include in PYTHON_INCLUDE
  • Set WITH_PYTHON_LAYER := 1
  • Add /usr/include/hdf5/serial into INCLUDE_DIRS
  • Add /usr/lib/aarch64-linux-gnu /usr/lib/aarch64-linux-gnu/hdf5/serial into LIBRARY_DIRS

The resulting Makefile.config could be downloaded from here.

$ cd ~
$ git clone https://github.com/BVLC/caffe
$ cd caffe
$ cp Makefile.config.example Makefile.config
$ vim Makefile.config   # Modify this file according to the above
$ make -j4 all
$ make -j4 test
### Test and verify the caffe build
$ make runtest

The rest of the steps were for python3. Note that I had to install leveldb-0.20 from source to make it work properly.

### I assume 'python3-dev' and 'python3-pip' are alerady installed
### Manually build and install 'leveldb-0.20' for python3, since the
### default version 0.194 fails to be compiled on Jetson TX2.
$ mkdir -p ~/src
$ cd ~/src
$ wget https://pypi.python.org/packages/03/98/1521e7274cfbcc678e9640e242a62cbcd18743f9c5761179da165c940eac/leveldb-0.20.tar.gz
$ tar xzvf leveldb-0.20.tar.gz
$ cd leveldb-0.20
$ python3 setup.py build
$ sudo python3 setup.py install
### Install other required pip packages
### Note I ignore version numbers specified in the requirements.txt
### file, and simply let pip3 install the latest (default) version
### of the pip modules.
$ pkgs=`sed 's/[>=<].*$//' ~/caffe/python/requirements.txt`
$ for pkg in $pkgs; do sudo pip3 install $pkg; done
### build pycaffe
$ cd ~/caffe
$ make pycaffe

Finally, I’d add the following line to ~/.bashrc.

export PYTHONPATH=/home/nvidia/caffe/python

At this point, the installation is completed. I’d verify it with:

$ python3
>>> import numpy as np
>>> import caffe

In addition, I’d also benchmark Caffe performance on Jetson TX2 by: (Set Jetson TX2 to max performance mode with nvpmodel and ~/jetson-clocks.sh beforehand. Reference link.)

$ cd ~/caffe
$ ./build/tools/caffe time --gpu 0 --model ./models/bvlc_alexnet/deploy.prototxt
......
I0913 10:54:53.395604  5992 caffe.cpp:417] Average Forward pass: 47.4552 ms.
I0913 10:54:53.395627  5992 caffe.cpp:419] Average Backward pass: 71.7691 ms.
I0913 10:54:53.395647  5992 caffe.cpp:421] Average Forward-Backward: 119.431 ms.
I0913 10:54:53.395689  5992 caffe.cpp:423] Total Time: 5971.55 ms.
I0913 10:54:53.395800  5992 caffe.cpp:424] *** Benchmark ends ***
$

blog built using the cayman-theme by Jason Long. LICENSE