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

Build TensorFlow on NVIDIA Jetson TX1 Development Kit

$
0
0

In this article, we will build TensorFlow v1.3.0 on the Jetson TX1 running L4T 28.1 from source, and then install it. 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 TX1 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 r0.11 back in December, 2016 for the Jetson TX1 running L4T 24.2.1. This article is the update to build TensorFlow for L4T 28.1 (JetPack 3.1). In order to build a version greater than v0.12 of TensorFlow, cuDNN version 6.0 is needed. Because that earlier version of L4T was running cuDNN 5.X, upgrading to the new L4T 28.1 now allows us to run versions r1.0 and greater of TensorFlow on the Jetson TX1.

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 three hours to build and install. This procedure is very similar to the Jetson TX2 installation with the exception being that a modified kernel is needed to enable swap memory.

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 TX1. 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 set the CPU clocks to their maximum settings:

$ sudo ./jetson_clocks.sh

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

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

Before installing TensorFlow, a swap file should be created (minimum of 8GB recommended). The Jetson TX1 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.

Unfortunately the L4T 28.1 release does not have swap files enabled in the kernel. You will need to build to build a custom kernel with the swap file enabled. You may do this in the NVIDIA approved manner of building everything on the host, or cheat and build it on the Jetson itself. Here’s and article on how to Build Kernel and ttyACM Module on the Jetson TX1.

You will need to enable the “Support for paging of anonymous memory (swap)” The symbol is CONFIG_SWAP. Here’s what it looks like:

Kernel Enable Swap Setting
Kernel Enable Swap Setting

Once you have the new kernel installed and swap memory enabled, there is a convenience script for building a swap file.

For example, to build a 8GB swapfile:

$ ./createSwapfile.sh -d [filename] -s 8

where [filename] is a directory path to an external device. The Jetson TX1 probably does not have enough free room on the eMMC to place the swap file there. If you have a SSD attached to the Jetson, that’s probably the fastest. USB is next, and then a SD card.

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

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.
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 TX1 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:

$ swapoff [swap file path]
$ 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 TX1 Development Kit appeared first on JetsonHacks.


Viewing all articles
Browse latest Browse all 339

Trending Articles