My 2020 late new year resolution is to build a wildlife camera with a Raspberry PI, Python, a PI camera, Tensorflow, OpenCV, and who knows what else. It is going to be an awesome camera!
But first I have to set up a development environment to work with.
Today I am going to be creating my Git Repository for the project, on Github, setting up Tensorflow on my Raspberry PI. And I will be using Docker in the PI because that is an amazing container framework.
And AI without Tensorflow is like football without a ball!
I know that Pytorch is more Pythonic than Tensorflow. But I am not ready yet to try something new.
Hold on to your beer, get your popcorns ready, cancel your Netflix subscription… you will have no time for it.
And get ready for a thriller, this is going to get exciting.
Setting up Tensorflow on a PI, not Tensorflow Lite, the full Tensorflow…. 100% TensorFlow!
We will be resolving compile errors, installing missing libraries, resolving cross-dependencies between libraries, searching for answers on Stackoverflow, waiting hours on end for something to compile in the PI.
And then …
Having to do it again and again and finally….
Before you know it,
The ducks are sleeping, the owls resting, the lions are feasting but we…. are still awake trying to figure out those build errors.
Get some extra coffee because things are going to get serious.
But we will get it done.
What are you waiting for? Let’s get started!
The Raspberry PI
I can’t get going without introducing the main star of the show. The Raspberry PI. I am using the Raspberry PI 4, with 4GB of memory. However, you can use the Raspberry PI 3 with 1GB of memory as well. Believe it or not, it is good enough to run the full TensorFlow. Not for training though. That is best left for a server with a GPU, or even better a TPU. Google has many of those. And you can do it for free if you know where to look.
Why not Tensorflow Lite?
This is a very good question to ask. Why did I decide to ignore Tensoflow Lite? Since I am only planning to use Tensorflow for inference only, should I not go with Tensorflow Lite instead? Yes, probably. But right now I am in development mode and I don’t know yet for sure to what extent I will be using Tensorflow for. So for now I decided to use the full Tensorflow on the Raspberry PI, but later I will probably downgrade it to Tensorflow Lite when my wildlife camera is ready for production.
Looking for a Docker image with Tensorflow for the Raspberry PI
The first thing that I did was to look for the latest Tensorflow Docker image for the Raspberry PI.
From my brief search on the official Tensorflow website, I was only able to find Docker images for x86. Did I look long enough? Perhaps not. What I did find was on how to build Tensorflow from source for the Raspberry PI. Oh no! that was what I was most fearing. I did that before and it takes hours.
If you ask me exactly how many hours, I can’t tell you for sure, I remember leaving the Raspberry PI 4 overnight compiling. Whether it was compiling Tensorflow, ffmpeg or even OpenCV, I do not remember exactly. But just know that if you have to compile something on the PI it will normally take a long time!
Maybe not all is lost, let’s keep searching. Maybe someone has already created a docker container with the latest Tensorflow version.
Creating a Dockerfile for Tensorflow
After searching the mighty internet, for an existing docker container with the latest Tensorflow failed. I gave up and decided to create a brand new docker container with the latest version of Tensorflow.
I did regret that decision, as trying to setup Tensorflow was giving me a host of compile and build errors, but I had no choice, right?
I felt like my google search powers were coming back to me and I looked again for an end for my agony. And finally, after finding a promising Youtube video which helped me a lot, I found a GitHub repository that saved my day.
This GitHub repository has a Dockerfile that installs Tensorflow 2.1 for the Raspberry PI 4. Thank you, François Gervais, you don’t know how much a difference your Dockerfile has made!
All I did, was to copy François’s Dockerfile, modify the Tensorflow version to the latest and hope it worked! There was no guarantee, as the Google Tensorflow team work very hard and keep improving their framework. And that invariably can break the build process.
So here is an excerpt of my new Dockerfile:
# debian:stretch-20200130FROM debian@sha256:872b72a2b8487e4b91ae27855c7de1671635d3dc2cc0b89651103e55c74ed34a AS base FROM base as build ... COPY --from=build /*.whl /RUN pip3 install *.whl RUN wget https://storage.googleapis.com/tensorflow/raspberrypi/tensorflow-2.3.0-cp35-none-linux_armv7l.whl RUN pip3 install tensorflow-*.whl RUN rm *.whl
So this is what I tried to build on the Raspberry PI. You will notice that I am not actually compiling Tensorflow. I am downloading a binary already compiled for the ARM processor(arm7l). This saves me a lot of time. All I am actually doing is to package this inside the Dockerfile.
So, after patiently waiting for the Docker container to build, I managed to have a working version of a docker container with Tensorflow 2.3 on the Raspberry PI 4!
This is a big win, as now I will be able to run powerful AI models directly on the Raspberry PI. That will help me identify any wild beasts which might end up prowling in my garden.
My last step is to push the docker image to the Docker hub in order to allow anyone in the future who might want to use Tensorflow 2.3 on the Raspberry PI using Docker to also benefit.
docker login docker push armindocachada/tensorflow2-raspberrypi4
And that is it! I have done my bit for the community. I still have a long way to go, to get my development environment ready. But this is a win!
Next, I will be installing OpenCV, a very popular machine vision library originally developed by Intel, on the docker container.
If you can’t wait until my next article in this series, why not subscribe to my Youtube Channel and see how I am getting on with this build? What are you waiting for?