The Google Coral TPUs are often the first experience of AI for many users on their Raspberry Pi, and PyCoral is one of the most well-known solutions to make use of your own Coral device
It doesn’t come without a few challenges though but stick with me and we’ll have a working installation for you by the end!
NOTE: This guide will work for both the standard Google Coral TPU and the Dual Edge Coral TPU.
What You’ll Need
The bare minimum to get things up and running here is a Raspberry Pi 5 with the latest Raspberry Pi OS installed to a microSD card, as well as a PCIe HAT to connect your Google Coral TPU to.
To get the best of both worlds, check out the HatDrive! AI where you can run your TPU alongside fast NVMe boot storage
Setting up your Raspberry Pi 5 for PyCoral
Ensure Raspberry Pi OS is up to date
If you’ve just installed Raspberry Pi OS for this, you likely won’t have too many (if any) updates but it’s good practice to make sure just in case.
Run the following commands to ensure everything is up to date:
sudo apt update
sudo apt upgrade
Modifying the config.txt file
A few modifications are needed in your "config.txt" file, so open "/boot/firmware/config.txt" with your chosen text editor and add the following lines at the end of the file before saving.
kernel=kernel8.img
dtoverlay=pineboards-hat-ai
These will enable the PCIe connection on the Raspberry Pi 5 (which should be enabled by default on newer Raspberry Pi OS versions), switch the kernel to use 4K block sizes (a requirement of the Coral TPU), and enable our AI Hat device tree overlay to tweak a few things that are required for the correct functioning of your TPU.
Now you’ve saved the file, we need to reboot to apply the changes. You can do this with a simple "sudo reboot" command.
Once back online, log back in and run "uname –a" to check the Linux kernel version. You should see a section that looks like "6.6.31+rpt-rpi-v8", and it’s the v8 at the end there that lets us know that the 4K block size kernel change was successful.
We can also check that the Google Coral is showing up with "lspci | grep Coral" which should then report `System peripheral: Global Unichip Corp. Coral Edge TPU`. If it doesn’t, make sure that everything is physically connected as per the instructions of your PCIe HAT and that all previous instructions in this guide have been followed correctly.
Installing the Necessary Drivers for the Coral TPU
For this step, you’ll want to check out our guide on how to configure the Google Coral TPU on the Raspberry Pi 5.
Installing Docker for Raspberry Pi 5
Following Docker’s own instructions for installing via their apt repository, run the following:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
This will add Docker’s GPG key, install some necessary packages, and add the Docker APT repository to your `sources.list` file so that you can then download their packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Once you confirm the installation of the above packages, you should be ready to go with Docker!
Creating PyCoral Docker Container
With Docker installed, we can get to work on creating the Docker container that will be running PyCoral. We noticed Jeff Geerling had a ready to go Dockerfile for this, so why reinvent the wheel? Thanks, Jeff!
Create a file called "Dockerfile" with the following:
FROM debian:10
WORKDIR /home
ENV HOME /home
RUN cd ~
RUN apt-get update
RUN apt-get install -y git nano python3-pip python-dev pkg-config wget usbutils curl
RUN echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" \
| tee /etc/apt/sources.list.d/coral-edgetpu.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN apt-get update
RUN apt-get install -y edgetpu-examples
Then we’ll build the image:
sudo docker build –t "coral" .
Testing the PyCoral Docker Container
If you’ve made this far without issue, you’re ready to test your new Google Coral TPU with the previously created Docker container.
First, let’s check that the "/dev/apex_0" device is showing on your machine:
ls /dev | grep apex_0
It should display apex_0 if it’s there but if not, you’ll need to again check your physical connections, and that all previous steps were followed correctly.
We’ll now initialize the Docker container, pass the "apex_0" device to it, and drop ourselves into a BASH prompt:
sudo docker run -it --device /dev/apex_0:/dev/apex_0 coral /bin/bash
Now, in the BASH prompt within the container we can run one of the examples we installed:
python3 /usr/share/edgetpu/examples/classify_image.py
--model /usr/share/edgetpu/examples/models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite
--label /usr/share/edgetpu/examples/models/inat_bird_labels.txt --image /usr/share/edgetpu/examples/images/bird.bmp
This should then result in the following:
---------------------------
Poecile atricapillus (Black-capped Chickadee)
Score : 0.44140625
---------------------------
Poecile carolinensis (Carolina Chickadee)
Score : 0.29296875
You can now press CTRL+D to exit the container and get back to your main Raspberry Pi OS.
Conclusion
The Google Coral TPUs are a great, affordable way to dip your toes into running software like Frigate without having to rely on the Pi’s own CPU. Couple that with our HatDrive AI and you have the perfect setup for your foray into AI.