Part V: Deployment on Raspberry Pi

This post will give a detailed step by step guideline on how to set up tensorflow 2.1 and some other dependencies on a raspberry pi.

Requirements:

Hardware:
– Raspberry Pi 3b
– micro-SD card
Software packages:
– python 3.7
– tensorflow 2.1
– os: 2020-02-13-raspbian-buster (https://downloads.raspberrypi.org/raspbian/images/)
– Raspberry Pi imager to flash the miocro-SD card (https://www.raspberrypi.com/software/)

Preparing the Raspberry Pi

When flashing your SD card, make sure to NOT use the most recent version, scroll all to the bottom, choose your own image (2020-02-13-raspbian-buster), choose SD card, and write that image. The most recent version has a python version (as of writing 3.9) which is not compatible with the tensorflow 2.1 wheel that we will be using later. It is very likely however that a different version of tensorflow will work too (must be 2.x).

Follow the initial set up of the pi, dont update software! (“skip) –> restart

(change keyboard layout to your preference, default ist US, qwerty)

sudo apt update (NOT sudo apt-get update)

We want to use the “oldstable” repository, as we are not using the most recent os version.

Disable screen Blanking (it will also work with, but I find it annoying)

–> Preferences / Raspberry Pi Configuration / Display: Screen Blanking –> Disable (requires reboot)

sudo reboot (after update)

NAS setup

This section is only if you have a NAS (network attached storage), I have a Synology NAS to store and manage data from all my devices. It is very useful if you have many devices that need to access the same data. E.g. Raspberry Pi(s) as downloader, and workstations for data evaluation, machine learning, etc.

Mount your network drive to the pi: (you might need to configure the nas to allow access from a pi: quickguide for a Synology Disk Station: Control Panel  –> change configuration of the desired data Folder / NFS-authorization: add the ip adress of your Pi(s) )

create a folder (e.g. “nas” on the Desktop)

sudo mount -t nfs “your nas ip address”:/”your data path on the nas” /”your mount path on pi”

this can look like this:

sudo mount -t nfs 192.168.178.55:/volume1/myData/PiDownloads /home/pi/Desktop/nas

This mounting needs to be done after each restart. So far I could not get it to work that this command gets executed automatically on boot, as soon as I have this figured out I will update this post. Intermediate solution: create a text file with this mount command, so you only need to copy paste the command into the terminal after booting.

Additional dependencies and packages

We need to install a few more dependencys before we can use opencv, which we will need later:

sudo apt-get install libhdf5-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install libqt4-test

(these commands will only work if you use the raspbian buster version mentioned above, they will not work on the “latest” version)

then

pip3 install opencv-python==4.0.1.24

should work without errors, I use this specific version as it works, many other versions should work too ( the latest I cannot guarantee)

then we need tensorflow: I am using tensorflow 2.1 for all my developments (as of this writing), this guide will likely work just fine with other 2.x version. If you are using a 1.x version, you will need to adapt the code.

 Get the .whl file for tf 2.1 with python 3.7:

wget https://github.com/Qengineering/Tensorflow-Raspberry-Pi/raw/master/tensorflow-2.1.0-cp37-cp37m-linux_armv7l.whl

pip3 install tensorflow-2.1.0-cp37-cp37m-linux_armv7l.whl

(in the directory where the .whl is located)

install some other packages that will be needed (+there dependencies, e.g. pillow):

pip3 install matplotlib

pip3 install pandas

Troubleshooting

In a fresh install 2020-02-13-raspbian-buster, you have “chromium-browser” installed, you can start it either from the terminal, or from the internet button top left.

I encountered the following issue:

When I leave the browser open on a page with a live ticker that I want to read, after some days, the browser chrashes (segmentation fault), or the pi gets very slow in reacting / updating. I could not yet figure out what are the exact courses. I checked CPU usage, CPU temperature, Mermory use etc. Its all fine withing reasonable limits.

Fix for this: just restart the pi

(you should have chromium-browser configured in a way that it resumes your previous sesseion)

original installed: “chromium-browser” (in terminal)

After some weeks it usually happens that the segmentation fault of chromium-browser gets worse, that it no longer starts at all (also after reboot)

Intermediate solution: remove and reinstall

sudo apt remove –purge chromium(-browser)

sudo apt install chromium

if you install “chromium”, from then on you need to start it from a terminal “chromium”, not with the button on the top left 🙂

It happened to me once so far that 1 of my Pis broke completely: bad segmentation faults of the operation system and the apt package manager. I just set this Pi up fresh, like described in this guide.

With this guide you should be all set up to use tensorflow on the raspberry pi.