Quantcast
Channel: JetsonHacks
Viewing all articles
Browse latest Browse all 339

Build TensorFlow on NVIDIA Jetson TX2 Development Kit

$
0
0

In this article, we will build and install TensorFlow v1.3.0 on the Jetson TX2 running L4T 28.1 from source. Looky here:

Background

TensorFlow is one of the major deep learning systems. Created at Google, it is an open-source software library for machine intelligence. The Jetson TX2 ships with TensorRT. TensorRT is what is called an “Inference Engine“, the idea being that large machine learning systems can train models which are then transferred over and “run” on the Jetson.

Note: We built TensorFlow back in April, 2017 for the Jetson TX2 running L4T 27.1 (JetPack 3.0). This article is the update to build TensorFlow for L4T 28.1 (JetPack 3.1).

Some people would like to use the entire TensorFlow system on a Jetson. In this article, we’ll go over the steps to build TensorFlow v1.3.0 on the Jetson TX2 from source. This should take about two hours to build.

Note: Please read through this article before starting installation. This is not a simple installation, you may want to tailor it to your needs.

Preparation

This article assumes that Jetson 3.1 is used to flash the Jetson TX2. At a minimum, install:

  • L4T 28.1 an Ubuntu 16.04 64-bit variant (aarch64)
  • CUDA 8.0
  • cuDNN 6.0.1

TensorFlow will use CUDA and cuDNN in this build.

It may be helpful to enable all of the CPU cores for the build:

$ sudo nvpmodel -m 0

There is a repository on the JetsonHacks account on Github named installTensorFlowTX2. Clone the repository and switch over to that directory.

$ git clone https://github.com/jetsonhacks/installTensorFlowTX2
$ cd installTensorFlowTX2
$ git checkout vL4T28.1TF1.3V3

Prerequisites

There is a convenience script which will install the required prerequisites such as Java and Bazel. The script also patches the source files appropriately for ARM 64.

Before installing TensorFlow, a swap file should be created (minimum of 8GB recommended). The Jetson TX2 does not have enough physical memory to compile TensorFlow. The swap file may be located on the internal eMMC, and may be removed after the build. Note that placing the swap file on a SATA drive if available will be faster.

If you do not already have one available on the Jetson, there is a convenience script for building a swap file. For example, to build a 8GB swapfile on the eMMC in the home directory:

$ ./createSwapfile.sh -d ~/ -s 8

After TensorFlow has finished building, the swap file is no longer needed and may be removed (See below).

Scripts in this repository will build TensorFlow with Python 2.7 support, and/or Python 3.5 support.

For Python 2.7

$ ./installPrerequisites.sh

From the video installation of the prerequisites takes a little over 30 minutes, but will depend on your internet connection speed.
First, clone the TensorFlow repository and patch for Arm 64 operation:

$ ./cloneTensorFlow.sh

then setup the TensorFlow environment variables. This is a semi-automated way to run the TensorFlow configure.sh file. You should look through this script and change it according to your needs. Note that most of the library locations are configured in this script. The library locations are determined by the JetPack installation.

$ ./setTensorFlowEV.sh

Continue to Building and Installation

For Python 3.5

$ ./installPrerequisitesPy3.sh

From the video installation of the prerequisites takes a little over 30 minutes, but will depend on your internet connection speed.
First, clone the TensorFlow repository and patch for Arm 64 operation:

$ ./cloneTensorFlow.sh

then setup the TensorFlow environment variables. This is a semi-automated way to run the TensorFlow configure.sh file. You should look through this script and change it according to your needs. Note that most of the library locations are configured in this script. The library locations are determined by the JetPack installation.

$ ./setTensorFlowEVPy3.sh

Build TensorFlow and Install

We’re now ready to build TensorFlow:

$ ./buildTensorFlow.sh

This should take less than two hours. After TensorFlow is finished building, we package it into a ‘wheel’ file:

$ ./packageTensorFlow.sh

The wheel file will be placed in the $HOME directory.

Install wheel file

For Python 2.X

$ pip install $HOME/wheel file

For Python 3.X

$ pip3 install $HOME/wheel file

Validation

You can go through the procedure on the TensorFlow installation page: Tensorflow: Validate your installation

Validate your TensorFlow installation by doing the following:

Start a Terminal.
Change directory (cd) to any directory on your system other than the tensorflow subdirectory from which you invoked the configure command.
Invoke python or python3 accordingly, for python 3.X for example:

$ python3

Enter the following short program inside the python interactive shell:

>>> import tensorflow as tf
>>> hello = tf.constant(‘Hello, TensorFlow!’)
>>> sess = tf.Session()
>>> print(sess.run(hello))

If the Python program outputs the following, then the installation is successful and you can begin writing TensorFlow programs.

Hello, TensorFlow!”

Conclusion

So there you have it. Building TensorFlow is quite a demanding task, but hopefully some of these scripts may make the job a little bit simpler.

Notes

  • The install in the video was performed directly after flashing the Jetson TX2 with JetPack 3.1
  • The install is lengthy, however it certainly should take much less than 4 hours once all the files are downloaded. If it takes that long, something is wrong.
  • TensorFlow 1.3.0 is installed
  • Github recently upgraded their operating system, and regenerated checksums for some of their archives. The TensorFlow project relies on those older checksums in some of their code, which can lead to dependency files not being downloaded. Here we use a patch to update the file workspace.bzl to ignore the old checksums that are applied after TensorFlow is git cloned, but there may be other instances of this issue as time goes on. Beware.

Removing the Swap File

If you created a swap file for this installation, you may wish to remove it after building TensorFlow. There are several ways to do this. First, if you did not AUTOMOUNT the swapfile, you may reboot the machine and then delete the file either through the Terminal or through the GUI.

If you wish to delete the swapfile without rebooting the machine, you should turn the swap off and then remove the swap file. For example, for the swapfile located in the home directory:

$ swapoff ~/swapfile
$ swapoff -a
$ rm ~/swapfile

If you used the AUTOMOUNT option, you will probably also need to edit the file /etc/fstab.

The post Build TensorFlow on NVIDIA Jetson TX2 Development Kit appeared first on JetsonHacks.


Viewing all articles
Browse latest Browse all 339

Trending Articles