How to Run Tensorflow 2.4.0 and AutoKeras (AutoML) on Raspberry Pi 4B with 64-bit OS

This is a simple article, originating from one of my experiments for a failed company project proposal.

Tensorflow/Keras is (still) a popular AI package, and AutoKeras (a open AutoML or automated machine learning Python package) lets you train a neural network model in TF without having to set any parameters. It requires Tensorflow 2, which in turn needs 64-bit Python runtime. However, official Raspberry Pi OS is still using 32-bit kernels.

It’s not that hard to find 64-bit OS, but there are so many dependencies that are never fully documented. I managed to find the solution: here I’ll show you how to install and use them without really complex configurations. I’ve tested these instructions several times, and so far they worked every time.

However, this may become obsolete in the future when Tensorflow and related packages finally get supports for ARM64 (AArch64) platforms.

Hardware

I use a standard Raspberry Pi 4B with 4 GB RAM:

I put on a big heat sink stripped from an old motherboard. A cooling fan is recommended for the model training process though: the CPU temperature goes up to ~70 °C without overclocking. (clock speed would be automatically reduced if it reaches 85 °C.)

Software

Go to the following link to download the 64-bit kernel Raspberry Pi OS (beta):

This OS comes with a Python 3.7.3 64-bit runtime. The kernel can be updated to a more recent version, so don’t worry about it.

Use Raspberry Pi Imager to burn this .iso image to your micro SD card. (Choose OS -> Use Custom)

I’ve also successfully overclocked the Pi up to 1750 MHz. It can actually run at 2000 MHz but not during model training (the Pi would crash and reboot). If you want to try overclocking, check out the link below:

I used the following commands in config.txt (this is not necessary if you just want to play it safe)

The whole installation process took about an hour on my overclocked-to-1750 MHz Pi.

Boot Up and System Update

Boot up your Pi and open the terminal:

Setup other things you want to set, and reboot.

This is the system information I’ve got after update:

Install Dependencies

Some of the packages might be re-installed by the Tensorflow wheels later, but as far as I tried, this is guaranteed to make those wheels work. I’ve also use NumPy 1.18.5, since 1.19.x are not supported well by TF 2.4.0.

The second instruction has the upgrade parameter behind it, since some of the packages might be already exists on your system. Don’t worry if you see messages of cannot uninstall something.

Install Tensorflow

Since there are no official TF support for ARM64 platforms (yet), we will be using one of the wheels built by other people:

There are in fact several wheels avaliable:

I’ve tried two of them and both works. Remember to choose the link with cp37 (Python 3.7) and aar64.

Install AutoKeras

This will take a longer time, since AutoKeras will install Pandas, SciPy and scikit-learn as well, and each of them need to build their own wheel to be installed.

My installed AutoKeras version is 1.0.12.

Test Run

Now let’s run some neural network model training using TF and AutoKeras (be warned that this will take at least several hours to finish, even if we set the max_trail parameter to 1):

This is all the code you’ll need. That’s the beauty of AutoML.

Here we use TF’s builtin fasion_mnist dataset, which consists of 60,000 28 x 28 pixel gray-scale images labeled with 10 objects (10 class). AutoKeras’ ImageClassifier can be used to process two-dimensional arrays like this. (Check out AutoKeras’ online doc to see different classifiers.)

Noticed the import sklearn line in the beginning? Due to some reason, AutoKeras would throw an error trying to import scikit-learn on its own (it would do so when you import AutoKeras for the first time). This line is a workable workaround.

Below is the training result (only the last iteration and the result is shown):

So we’ve achieved 92% test data prediction accuracy, which is better than 88–89% from the examples I’ve found on books. There you have it!

Former translator, after-hours Maker, sunny-day analog film shooter. Currently a junior tech-book editor based in Taiwan. https://krantasblog.blogspot.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store