If you've ever installed a package using pip, you've probably used wheel files. A Python .whl
file is essentially a zip archive with a specially crafted filename that tells installers what Python versions and platforms the wheel will support. Wheel files are the standard installation medium for python, and are also known as pippackages.
When you're simply installing Tensorflow on a PC using pip, you don't see the.whl extension - you can just use pip install tensorflow.
However, when installing Tensorflow on Arm CPUs, or hardware platforms that aren't officially supported, you may have to build your own Tensorflow wheel.
This is the case for the MaaXBoard, as there is no official release for armv8-a platform yet. We need to cross-compile the TF code for this architecture.
Getting all the set up for cross compilation can be challenging, but there is an excellent project “tensorflow-on-arm” (TFOA) which was initially used to cross-compile TF for Raspberry Pi and can be used to compile for MaaXBoard. They helpfully provide a Docker container, which makes it much easier since it includes the correct versions of Tensorflow and Bazel, so you don't have to install them.
It's not reasonable to build Tensorflow on the MaaXBoard, since even on a powerful PC it will take several hours. This tutorial will show how to build Tensorflow on a PC running Ubuntu 18.04.
Note: If you would like to use Tensorflow version 2.1.0, there is a prebuilt wheel included in my tutorial Setting Up Tensorflow on the MaaXBoard.
Install DockerHere's a condensed version of installing Docker, with just the commands from this tutorial for installing on Ubuntu 18.04:
sudo apt update && sudo apt upgrade
sudo apt-get install curl apt-transport-https ca-certificates software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
sudo systemctl status docker
If everything installed correctly, it should say something like this:
Install the prerequisites according to the TFOA instructions:
sudo apt-get install openjdk-8-jdk automake autoconf
sudo apt-get install curl zip unzip libtool swig libpng-dev zlib1g-dev pkg-config git g++ wget xz-utils
sudo apt-get install python3-numpy python3-dev python3-pip python3-mock
I also had to install python-distutils:
sudo apt install python3-distutils
Add the Arm architecture:
dpkg --add-architecture armhf
echo "deb [arch=armhf] http://httpredir.debian.org/debian/ buster main contrib non-free" >> /etc/apt/sources.list
sudo apt-get install libpython3-dev
You may also need to add python3 to path. Get the path to your python3 installation by typing:
whereis python
Then add it to path:
export PYTHONPATH=/usr/bin/python3
Build the docker imageClone the Tensorflow-on-arm repository. Enter the build_tensorflow folder:
git clone https://github.com/lhelontra/tensorflow-on-arm.git
cd tensorflow-on-arm/build_tensorflow/
Build the docker image:
sudo docker build -t tf-arm -f Dockerfile .
It should output something like this:
Successfully built c9db049beaaf
Successfully tagged tf-arm:latest
Once the docker image has been built, open a console on your Ubuntu machine and start the container:
sudo docker run -it -v /tmp/tensorflow_pkg/:/tmp/tensorflow_pkg/ --env TF_PYTHON_VERSION=3.7 tf-arm
You will be placed into the tensorflow-on-arm folder inside your docker. Before launching the cross-compilation we need to create the proper config file. We use the “rk3399.conf” file as a starting point:
apt install nano
cd configs
cp ./rk3399.conf ./maaxboard.conf
nano maaxboard.conf
Edit the BAZEL_COPT_FLAGS
so it looks like this:
BAZEL_COPT_FLAGS="--copt=-march=armv8-a --copt=-mtune=cortex-a53 --copt=-O3 --copt=-flax-vectorconversions"
Edit the config scripts if you want a different version of Tensorflow:
The config scripts typically have the latest version of Tensorflow. If you would like to use a different version of Tensorflow, you will have to edit TF_VERSION
and BAZEL_VERSION
, e.g. for Tensorflow 1.14.0 it would become:
TF_VERSION="v1.14.0"
BAZEL_VERSION="0.24.1"
Building Tensorflow depends on having the correct version of Bazel. each time you checkout a specific TF release there will be a makefile documenting which versions of Bazel are allowed. It's called configure.py, and it will have lines like the following:
_TF_CURRENT_BAZEL_VERSION = None
_TF_MIN_BAZEL_VERSION = '2.0.0'
_TF_MAX_BAZEL_VERSION = '2.0.0'
Once you're done editing, save the file (CTRL+O) and exit (CTRL+X). You can now run the build scripts to build Tensorflow for MaaXBoard:
cd ..
chmod +x build_tensorflow.sh
./build_tensorflow.sh configs/maaxboard.conf
This will last few hours, depending on your PC configuration, and at the end you will have a.whl package of TF that must be retrieved from the docker container. This can be done with “scp” once again.
IMPORTANT: once the build is completed DO NOT close the docker container or all the build artifacts will be deleted
- First you need to identify the numerical “id” of your container, on your Ubuntu shell (open a separate console) use the command “
docker container
ls
” :
- Second you need to copy the whl package in your home folder. From the docker container command line make a “cp yourfile.whl ~/”
- Third from your Ubuntu console use scp to retrieve the file:
scp root@<DOCKERID>:<YOURFILE.WHL> ./
This will copy the build result (.whl package) on your Ubuntu machine, now you can close (exit) the docker container.
You can now proceed to install Tensorflow on your MaaXBoard.
Comments