Raspberry Pi 4 + Coral USB Accelerator — Docker Image for Image Classification using ML

Pratikhya Manas Behera
5 min readNov 4, 2020

--

Why Docker?

Docker is a containerization platform that bundles your application and all its dependencies together in the form called a docker container to ensure that your application works seamlessly in any environment. Docker empowers engineers to effectively pack, ship, and run any application as a lightweight, versatile, independent container, which can run virtually anywhere.

Pi & Coral USB Setup

Raspberry Pi + Coral Accelerator

The Coral USB accelerator connected to a Raspberry Pi 4 is a powerful computing system for Machine Learning and Deep Learning applications. The accelerator contains an edge TPU (Tensor Processing Unit) coprocessor which is optimized to process matrix operations. It currently only supports pre-compiled TensorFlow Lite models. It can perform 4 trillion operations per second. Therefore, it allows high inference speed for image classification and object detection using neural networks.

Setup & Installation

In this tutorial, you will learn how to install docker in Raspberry pi and built a docker image for Object detection with all configurations of Coral USB and Pi.

Installation of Docker in Pi

  1. Install Docker
curl -sSL https://get.docker.com | sh

2. Add permission to Pi User to run Docker Commands

sudo usermod -aG docker pi

3. Test Docker

docker run hello-world

If Docker is installed properly, you’ll see a “Hello from Docker!” message.

Building a Docker Image

Important Notice about Docker on the Raspberry Pi

Raspberry Pi’s use the ARM architecture, and as a result, won’t be compatible with all containers out of the box. Images will need to be built from an ARM base image. But, most of these images can easily be found on Docker Hub.

Step-1

Create a folder in Pi and let’s call it My_Docker_USB and run the following commands.

$ mkdir My_Docker_USB(create our docker folder)
$ cd My_Docker_USB (lets go into our folder)
$ nano Dockerfile (create a file called Dockerfile)

In the docker file paste, the following commands

# Importing
FROM balenalib/raspberry-pi-debian:latest# Maintainer Name
MAINTAINER Pratikhya Manas <manasxxxxxx@gmail.com># Add Google Coral sources lists
RUN echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list \
&& curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - \
&& curl -O https://bootstrap.pypa.io/get-pip.py

# Install the TPU packages we will need
RUN install_packages libedgetpu1-std \
libedgetpu-dev \
python3-edgetpu \
edgetpu-examples \
python3-pip# Creating Virtual Environment
RUN rm -rf ~/.cache/pip
RUN pip3 install virtualenv
RUN pip3 install virtualenvwrapper
RUN echo "# virtualenv and virtualenvwrapper" >> $HOME/.bashrc && \
echo "export WORKON_HOME=$HOME/.virtualenvs" >> $HOME/.bashrc && \
echo "export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3" >> $HOME/.bashrc && \
echo "source \"/usr/local/bin/virtualenvwrapper.sh\"" >> $HOME/.bashrc && \
dpkg -L python3-edgetpu

# Creating sym-link
WORKDIR ~/.virtualenvs/coral/lib/python3.7/site-packages \
&& ln -s /usr/lib/python3/dist-packages/edgetpu/ edgetpu
WORKDIR ~# Intializing the Environment Variables
ENV WORKON_HOME ~/.virtualenvs
ENV VIRTUALENVWRAPPER_PYTHON /usr/bin/python3# Installing the necessary packages
RUN /bin/bash -c "source /usr/local/bin/virtualenvwrapper.sh; mkvirtualenv coral_test -p python3" \
&& cd $HOME \
&& pip3 install "picamera[array]" \
&& pip3 install numpy \
&& pip3 install opencv-contrib-python==4.1.0.25 \
&& pip3 install imutils \
&& pip3 install scikit-image \
&& pip3 install pillow \
&& apt-get install edgetpu-examples \
&& chmod a+w /usr/share/edgetpu/examples

# Working on the Object Detection Problem
WORKDIR /usr/share/edgetpu/examples \
&& export DISPLAY=":0" \
&& python classify_image.py --mode models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --label models/inat_bird_labels.txt --image images/parrot.jpg
  1. FROM create a layer from the Debian docker image. It defines the base image to use to start the build process.
  2. MAINTAINER Declares the author of the image. # Usage: MAINTAINER [name]
  3. RUN builds your application
  4. ENV This is environment variables for re-usable commands.
  5. WORKDIR This command is used to define the working directory of a Docker container at any given time.

Step-2

After the configuration is complete, we now run the command to build our image.

$ docker build -t my_docker_usb1:1.0 .

The name of the image is my_docker_usb1:1.0 and the “.” operator specifies the current directory which tells docker to look through the current directory for a Dockerfile.

“-t” flag is used to tag the image. “docker build — help” command gives you the list of options about building an image.

Step-3

To see our container we can now run

$ docker build -t my_docker_usb1:1.0 .

To run the created container we can use the following command

docker container run -d --restart unless-stopped -v /dev/bus/usb:/dev/bus/usb  pmbehera/my_docker_usb1

We’re running a detached container (-d), which means docker starts it in the background.

The option –restart unless-stopped makes sure our container starts when the Raspberry PI reboots or if the container crashed.

USB devices are located here on Raspbian: /dev/bus/usb — hence we create a Docker volume mount like this: -v /dev/bus/usb:/dev/bus/usb.

Conclusion

This article shows you how to containerizing your EdgeTPU application for usage on a Raspberry Pi. I have just created a simple docker image and published it on my docker hub account. I am still trying to add some extra features on it and will be uploading to the docker hub very soon. Please feel free to edit and update the image as per your need.

Don’t forget to check out the image from my docker hub account.

References

In the meanwhile, here are some resources I find useful to learn about Docker Image

  1. Docker Container
  2. Coral USB Accelerator

--

--