Go through the tutorial here to get the MaaXBoard setup in headless mode, expand the filesystem, increase the swap file size, and set up remote desktop.
Note: this tutorial is also applicable for MaaXBoard Mini.
GET TENSORFLOW FILESBuilding Tensorflow from source code can be a challenging step. This cannot be done on the MaaXBoard as is, and must be done with cross-compilation using another powerful machine (PC). Thankfully, Gianluca has provided us with two pre-compiled versions of the Tensorflow distribution:
- For version 1.x we have built r1.14, file: “tensorflow-1.14.0-cp37-none-linux_aarch64.whl”
- For version 2.x we have built r.2.1.0, file: “tensorflow-2.1.0-cp37-none-linux_aarch64.whl”
You can find them under "code" at the end of this tutorial. To setup your tensorflow install, select the file that you prefer and copy the .whl package onto the MaaXBoard using scp. For example with the command
scp ./tensorflow-2.1.0-cp37-none-linux_aarch64.whl ebv@10.0.0.245:
Do not forget the “:” at the end of the command.
The first steps for installing prerequisites, setting up python, and setting up the virtual environment are identical to those in the OpenCV setup tutorial.
If you've already gone through that tutorial, feel free to skip ahead to the "install tensorflow" step.
SET UP PYTHONYou can see the prerequisites for installing Tensorflow here.
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install build-essential cmake unzip pkg-config wget
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libcanberra-gtk*
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python3-dev
sudo apt install python3-tk libhdf5-dev
This will take an hour or so.
If you prefer, here they all are as one command with yes flags enabled:
sudo apt-get install build-essential cmake unzip pkg-config wget libjpeg-dev libpng-dev libtiff-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libgtk-3-dev libcanberra-gtk* libatlas-base-dev gfortran python3-dev python3-tk libhdf5-dev -y
SET UP VIRTUALENVOne of the best practice rules when working with python libraries is to create a “virtual environment” for your user where you can customize all the library versions you are going to install. You can have multiple virtualenv as long as you have space on your drive (virtualenv settings are stored into the ~/.virtualenv folder in your home directory) We will create virtualenv called “tf” to work on python/opencv/tensorflow
From your home folder use these commands to download PIP (package installer for python):
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 ./get-pip.py
Then install the package itself and remove the leftovers
sudo pip install virtualenv virtualenvwrapper
rm -rf ~/get-pip.py ~/.cache/pip
Now as user “ebv” we edit our bash shell configuration file ~/.bashrc using nano:
nano ~/.bashrc
At the very bottom of it we add these lines:
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
Save with CTRL+O, exit with CTRL+X and make sure you reload your shell config with:
source ~/.bashrc
When you run this for the first time you will see the following output:
Now we can create the “tf” virtual environment itself with the command:
mkvirtualenv tf -p python3
Which will produce the following output:
Note that the prompt has now the header “(tf)” in front of it, it means you are “inside” the new env. You can exit from the virtualenv anytime by typing:
deactivate
To list your virtual environments, simply type:
lsvirtualenv
You can enter the “tf” virtual environment by typing:
workon tf
The last package we need to install before TensorFlow itself is NumPy (https://numpy.org/) which is the backbone of any numerical processing done in python. Make sure you are inside your “tf” environment (check the prompt..) and type:
pip install numpy
pip install -U matplotlib
Since there are no prebuilt packages for armv8-a architecture this will take quite some time to fetch, compile & install all the required packages. (expect ~20min)
HINT: open another console and hit the command “htop”, or use the GKrellMonitor to keep an eye on your running activity so you know what is happening on the board, here is an example of htop while installing matplotlib:
Once all the packages are installed you can double check your python installation like this: In your console type “python” to enter the python interpreter, then type:
a = np.eye(3)
print (a)
You will see:
Hit CTRL+D to exit the python interpreter.
INSTALL TENSORFLOWOnce the file is placed on the board make a login (either by ssh or rdp). In a console make sure you enter your virtualenv:
workon tf
Unzip
the package we installed earlier, and move Tensorflow 2.1.0 binary into the home directory:
unzip TF.zip
mv TF/tensorflow-2.1.0-cp37-none-linux_aarch64.whl ~
then install the package with the pip command (we use version 2.x in this case) using the "-vvv" flag enabled (for verbose output).
pip install tensorflow-2.1.0-cp37-none-linux_aarch64.whl -vvv
This will trigger also fetching/compiling/installing of few packages that are needed for a complete installation (scypi, protobuf, h5py, urllib etc.). Due to all these packages not being available prebuilt for armv8-a this will take up to 2h (two hours) to finish.
NOTE: you need at least 2G of extra space on your SD card in order to install Tensorflow. If you don't have enough space, Tensorflow will likely fail with the cryptic message "failed building wheel for scipy:"
Output if build succeeded:
We will make a simple test in tensorflow+python to make sure that it works properly. These few lines of code will perform a simple addition between two vectors (i.e. tensors) and return the result.
Using the geany editor, open a file and enter the following python code (it's python, so pay attention to indentation):
import tensorflow as tf
with tf.compat.v1.Session() as sess:
# initialize two tensors
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])
# multiply
result = tf.multiply(x1,x2)
# set&run TF session
print(sess.run(result))
sess.close()
NOTE: this code is for Tensorflow 2.1.0
Save the file as tftest.py in the home directory, and run it from your terminal using python (make sure you enter your virtualenv first):
workon tf
python ./tftest.py
If everything was installed properly you will have the following output (it will take a minute):
Which is the correct result of the element-wise multiplication of x1, x2
UPDATE: INSTALL ALTERNATE VERSIONS OF TENSORFLOWTensorflow 2.1.0, the version originally included in this tutorial, is now woefully out of date a year later. Tensorflow is now at version 2.4.0, and models that are generated on Tensorflow 2.3 and beyond are not compatible with Tensorflow 2.1.0.
If you would like to install an alternate version of Tensorflow in addition to the version you already installed, make sure you check that you have enough spaces using df -h
or similar. I recommend using a 32GB or larger SD card and expanding the filesystem to take up the full space, since Tensorflow will take up a lot of space.
It's also possible to uninstall your current version of Tensorflow using "pip uninstall tensorflow
" before installing a new version in order to save space.
You may need to install an alternate version of python in order to install Tensorflow. On Debian, installing alterante versions of python involves several steps. First, run update and upgrade:
sudo apt update && sudo apt upgrade
I'll show how install python 3.8. If you would like to use a different version of python, go to https://www.python.org/downloads, scroll to the section titled Looking for a specific release, click on the version you'd like, scroll down to the Files section, and right click to copy the link for the Gzipped source tarball. You can download and send this to your MaaXBoard using scp, or just use curl on your MaaXBoard to download it:
curl -O https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
Once you have it downloaded, unzip and run "configure":
tar -xf Python-3.8.2.tar.xz
cd Python-3.8.2
./configure --enable-optimizations
Run make
to start the build process. 4 is the number of cores on MaaXBoard (you can get the number by running nproc
). This will take a long time.
make -j 4
Do not use the standard make install
as it will overwrite the default system python3
binary:
sudo make altinstall
Debian, installing python 3.8 involves several steps. First, run update:
sudo apt-get update
Next, download python 3.8, extract it, and run "configure":
curl -O https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
tar -xf Python-3.8.2.tar.xz
cd Python-3.8.2
./configure --enable-optimizations
Run make to start the build process. 4 is the number of cores on MaaXBoard (you can get the number by running nproc
):
make -j 4
Do not use the standard make install
as it will overwrite the default system python3
binary:
sudo make altinstall
Create alternate virtual environmentYou should create a new new virtual environment for every version of Tensorflow you wish to install. Make sure you exit the current virtual environment first (deactivate). Now create a new virtual environment using the Python your Tensorflow requires, in this case, Python 3.8. I'll name it python 38:
mkvirtualenv python38 -p python3.8
You can now test the python version on your virtual environment by typing python
. It should give the following output:
MaaXBoard requires Tensorflow that's built to work on Arm-cortex-A53. I've created a tutorial for building your own versions of Tensorflow for MaaXBoard here.
Once you're finished building Tensorflow, you'll end up with a Python wheel which you can scp to your board. The wheel filename is broken down into parts separated by hyphens:
{dist}-{version}(-{build})?-{python}-{abi}-{platform}.whl
For instance, my Tensorflow wheel is named:
tensorflow-2.3.0-cp37-none-linux_aarch64.whl
I can tell that this version of Tensorflow was built with Python 3.7. Make sure you're on a virtual environment using Python 3.7 and install Tensorflow using pip:
sudo pip install tensorflow-2.3.0-cp37-none-linux_aarch64.whl
OPTIONAL: INSTALL TENSORFLOW LITERead more about using Tensorflow Lite on MaaXBoard here.
Create a new virtual environment for tflite:
mkvirtualenv tflite -p python3
MaaXBoard is aarch64. Tensorflow provides instructions for how to build from source for Arm 64 yourself. Thankfully they also include the prebuilt python .whl, which can easily be installed with pip:
pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_aarch64.whl
You can test it out with the label_image.py file provided here.
That's it! Let me know what you build. And thanks again to Gianluca from EBV for creating the original tutorial.
Comments