Skip to content

JetsonTensorflow

Netzeband edited this page Mar 20, 2017 · 2 revisions

Install Tensorflow

Sources

Pre-requirement

  • running Jetson setup with SSD as swap memory

General Preparations

  • install further requirements
sudo apt-get install git zip unzip autoconf automake libtool curl zlib1g-dev maven
sudo apt-get install python-numpy swig python-dev python-wheel python-setuptools
sudo apt-get install libcurl3-dev swig
  • install python pip
sudo easy_install pip
sudo pip install --upgrade virtualenv
  • clone Jetson im2txt repository
cd /media/ubuntu/jetson/
git clone https://github.com/Netzeband/JetsonTX1_im2txt.git im2txt

Pre-build tensorflow wheel (The easy way)

  • A pre-build tensorflow wheel can be found inside the repository

  • It can be installed as following:

sudo pip install /media/ubuntu/jetson/im2txt/tensorflow-jetson/tensorflow-0.11.0rc2-py2-none-any.whl
  • All further steps can be skipped. Simply perform the tensorflow test at the end of this page.

Preparations for compiling tensorflow (The hard way)

  • install Java
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Build Protobuf

  • clone repository
git clone https://github.com/google/protobuf.git
cd protobuf
  • configure build
git checkout master
./autogen.sh
git checkout v3.0.0-beta-3
./autogen.sh
LDFLAGS=-static ./configure --prefix=/home/ubuntu/protobuf
sed -i -e 's/LDFLAGS = -static/LDFLAGS = -all-static/' ./src/Makefile
  • build and install protobuf
make -j 4
make install
  • build another version of protobuf, which is used for bazel
git checkout v3.0.0-beta-2
./autogen.sh
LDFLAGS=-static ./configure --prefix=/home/ubuntu/protobuf
sed -i -e 's/LDFLAGS = -static/LDFLAGS = -all-static/' ./src/Makefile
make -j 4
cd ..

Build gRPC

git clone https://github.com/neo-titans/odroid.git
git clone https://github.com/grpc/grpc-java.git
cd grpc-java/
git checkout v0.15.0
patch -p0 < ../odroid/build_tensorflow/grpc-java.v0.15.0.patch
CXXFLAGS="-I/home/ubuntu/protobuf/include" LDFLAGS="-L/home/ubuntu/protobuf/lib" ./gradlew java_pluginExecutable -Pprotoc=~/protobuf/bin/protoc
cd ..

Build Bazel

  • download and prepare bazel
git clone https://github.com/bazelbuild/bazel.git
cd bazel
git checkout 0.3.2
cp ../protobuf/src/protoc third_party/protobuf/protoc-linux-arm32.exe
cp ../grpc-java/compiler/build/exe/java_plugin/protoc-gen-grpc-java third_party/grpc/protoc-gen-grpc-java-0.15.0-linux-arm32.exe
  • apply patch from im2txt directory
patch -p1 < ../im2txt/patches/bazel-patch.diff
  • build and install bazel
./compile.sh 
sudo cp output/bazel /usr/local/bin
cd ..

Build Tensorflow

  • download tensorflow
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout v0.11.0rc2
  • apply patches for tensorflow
patch -p1 < ../im2txt/patches/tensorflow-patch1.diff
patch -p1 < ../im2txt/patches/tensorflow-patch2.diff
  • set environment variables
export TF_NEED_CUDA=1
export TF_CUDA_VERSION=8.0
export CUDA_TOOLKIT_PATH=/usr/local/cuda
export TF_CUDNN_VERSION=5.1.5
export CUDNN_INSTALL_PATH=/usr/lib/aarch64-linux-gnu/
export TF_CUDA_COMPUTE_CAPABILITIES=5.3 
  • configure tensorflow
./configure
  • apply the following settings
/media/ubuntu/jetson/tensorflow /media/ubuntu/jetson/tensorflow
Please specify the location of python. [Default is /usr/bin/python]: 
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] n
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N] n
No Hadoop File System support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]

/usr/local/lib/python2.7/dist-packages
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
Extracting Bazel installation...
  • compile tensorflow
sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf'
sudo ldconfig
bazel build -c opt --jobs 2 --local_resources 1024,4.0,1.0 --verbose_failures --config=cuda //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
  • install tensorflow package
sudo pip install /tmp/tensorflow_pkg/tensorflow-0.11.0rc2-py2-none-any.whl

Test tensorflow

  • enter in the terminal:
time python tensorflow/models/image/mnist/convolutional.py
  • expected output:
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so.5.1.5 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so.8.0 locally
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] ARM has no NUMA node, hardcoding to return zero
I tensorflow/core/common_runtime/gpu/gpu_device.cc:951] Found device 0 with properties: 
name: NVIDIA Tegra X1
major: 5 minor: 3 memoryClockRate (GHz) 0.072
pciBusID 0000:00:00.0
Total memory: 3.90GiB
Free memory: 638.82MiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:972] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Creating TensorFlow device (/gpu:0) -> (device: 0, name: NVIDIA Tegra X1, pci bus id: 0000:00:00.0)
Initialized!
Step 0 (epoch 0.00), 40.7 ms
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%

...

Step 8400 (epoch 9.77), 58.5 ms
Minibatch loss: 1.601, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.7%
Step 8500 (epoch 9.89), 59.0 ms
Minibatch loss: 1.603, learning rate: 0.006302
Minibatch error: 0.0%
Validation error: 0.8%
Test error: 0.8%

real	8m37.041s
user	3m48.970s
sys	1m15.460s

Clean-up

  • delete the bazel cache directory
rm -Rf ~/.cache/bazel

Attention: If you do not delete this directory, you will not have enough space left to compile OpenCV3.

Next Step