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.
- Complete installation of JetPack-3.1 on the target Jetson TX2.
- Build and install opencv-3.3.0, and make sure its python3 bindings are working properly. You can reference my How to Install OpenCV (3.3.0) on Jetson TX2 post.
- Official Caffe installation instructions: http://caffe.berkeleyvision.org/installation.html
- Official Caffe tutorial: http://tutorial.caffe.berkeleyvision.org/installation.html
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-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 $ sudo apt-get install python3-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.
USE_CUDNN := 1
OPENCV_VERSION := 3
compute_62(for TX2) and
compute_53(for TX1) into
- Replace python2.7 stuffs with python3.5
WITH_PYTHON_LAYER := 1
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 upgrade python-dateutil specifically, and I also had to install leveldb-0.20 and matplotlib (2.0.2) from source to make them work properly. Specifically I referenced this article, Resolved: Matplotlib figures not showing up or displaying, to fix the problem of matplotlib for python3.
$ sudo apt-get install python3-pip $ sudo pip3 install --upgrade pip $ sudo pip3 install --upgrade setuptools $ sudo pip3 install -r ~/caffe/python/requirements.txt $ sudo pip3 install --upgrade python-dateutil $ sudo pip3 install protobuf $ cd ~ $ mkdir -p src $ cd src ### Build and install leveldb-0.20 $ 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 ### Build and install matplotlib (2.0.2) $ cd ~/src $ git clone https://github.com/matplotlib/matplotlib.git $ cd matplotlib $ python3 setup.py build $ sudo python3 setup.py install $ sudo apt-get install tcl-dev tk-dev $ sudo pip3 install pytk ### Final step: modify matplotlibrc (line #38) to use TkAgg as the default backend $ sudo vim /usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/matplotlibrc # ### build pycaffe cd ~/caffe make pycaffe
Finally, I’d add the following line to
At this point, the installation was completed. I’d verify it with:
$ python3 >>> import numpy as np >>> import matplotlib.pyplot as plt >>> import caffe
In addition, I’d also benchmark Caffe performance on Jetson TX2 by: (Set Jetson TX2 to max performance mode with
~/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 *** $