Skip to content

Instantly share code, notes, and snippets.

@emiroglu
Last active January 5, 2025 20:31
Show Gist options
  • Save emiroglu/eafba0fb063dc4b3aee6c8e4ae84a605 to your computer and use it in GitHub Desktop.
Save emiroglu/eafba0fb063dc4b3aee6c8e4ae84a605 to your computer and use it in GitHub Desktop.

Coral TPU on Debian 12 (Bookworm)

This installation is tested on the following Raspberry Pi SBCs:

  • Raspberry Pi 4B
  • Raspberry Pi 5 6.6.51-1+rpt3 (2024-10-08) aarch64 GNU/Linux)

with the following Coral TPUs:

  • Coral USB Accelerator
  • Coral Dual Edge TPU (installed on a Pineboards Hat AI!)

on a fresh installation of Debian 12 Bookworm. It does not require manual recompiling any of the libraries or modules.

Update packages

sudo apt update
sudo apt full-upgrade

Install pyenv

Install pyenv following the instructions on https://github.com/pyenv/pyenv:

curl https://pyenv.run | bash

Add the following to the .bashrc and restart shell:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec $SHELL

pyenv builds from source. Install Python build dependencies for installing different versions following the instructions on https://github.com/pyenv/pyenv/wiki#suggested-build-environment:

sudo apt update
sudo apt install build-essential libssl-dev zlib1g-dev \
  libbz2-dev libreadline-dev libsqlite3-dev curl \
  libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Check the possible python versions and install the latest compatible version:

pyenv install --list
pyenv install -v 3.9.20

If you want to enable optimizations run the following instead of above install command

CONFIGURE_OPTS="--enable-optimizations" pyenv install -v 3.9.20

Check the available installed versions and set the global python version to the installed version:

pyenv versions
pyenv global 3.9.20

Install pipenv

Install pipenv and set the environment variable such that the virtual environment is always created inside the project folder (second step is not a necessity):

pip install pipenv
echo "export PIPENV_VENV_IN_PROJECT=1" >> ~/.bashrc

Install libedgetpu library

Below follows from: https://coral.ai/docs/accelerator/get-started/

Install libedgetpu1-std library:

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 -
sudo apt update
sudo apt install libedgetpu1-std

Clone, build and install gasket drivet (only for Dual Edge TPU)

Install following additional packages

sudo apt-get install cmake devscripts debhelper dkms dh-dkms

Clone and build gasket-driver from source:

git clone https://github.com/google/gasket-driver.git
cd gasket-driver
debuild -us -uc -tc -b
cd ..
sudo dpkg -i gasket-dkms_*.deb

Create apex group and add the user to it

sudo sh -c "echo 'SUBSYSTEM==\"apex\", MODE=\"0660\", GROUP=\"apex\"' >> /etc/udev/rules.d/65-apex.rules"
sudo groupadd apex
sudo adduser $USER apex

Install pycoral

Move to the directory, where you would like to have your project and create a virtual environment:

cd ~/Documents
mkdir coral && cd coral
python -m venv .venv

Activate the virtual environment. This can be done either using pipenv shell or simply sourcing the activate script source .venv/bin/activate.

Install the pycoral module from private url using pip:

pip install numpy==1.26.4
pip install --extra-index-url https://google-coral.github.io/py-repo/ pycoral~=2.0

Setup the hardware

Now plug in the Coral USB Accelerator or if it was plugged in, remove it and plug in again. It should be ready to go.

If you want to run Dual Edge TPU add the following in your /boot/firmware/config.txt.

[all]
# Enable the PCIe External connector.
dtparam=pciex1
kernel=kernel8.img
# Enable Pineboards Hat Ai
dtoverlay=pineboards-hat-ai

Then reboot with

sudo reboot now

Get all detected Coral Edge TPU devices

Create and run the following python script in the previously created virtual environment:

from pycoral.utils.edgetpu import list_edge_tpus

if __name__ == '__main__':

  for edge_tpu in list_edge_tpus():
    print(edge_tpu)

Test pycoral examples

Assuming you are still in the coral directory created above, pull the repository and change into the directory:

git clone --recurse-submodules https://github.com/google-coral/pycoral
cd pycoral

Get the dependencies of the examples:

bash examples/install_requirements.sh

Run the image classifier example on https://coral.ai/docs/accelerator/get-started/#3-run-a-model-on-the-edge-tpu:

python3 examples/classify_image.py \
  --model test_data/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
  --labels test_data/inat_bird_labels.txt \
  --input test_data/parrot.jpg
@emiroglu
Copy link
Author

emiroglu commented Oct 5, 2024

thanks @fruttodelmondo pointing that out. I updated the instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment