返回列表 发新帖

树莓派5搭配双TPU扩展板在树莓派系统下的驱动安装和使用演示

[复制链接] 4
回复
2068
查看
打印 上一主题 下一主题

33

主题

158

帖子

1万

积分

允许发帖

积分
10619
楼主
跳转到指定楼层
发表于 2024-7-11 13:18:32 | 只看该作者 | 只看大图 回帖奖励 |倒序浏览 | 阅读模式
关键词:
树莓派5  PCIE  Switch TPU  DTPU 驱动安装   操作演示  AI  google Coral

概述:
MPDTPU是一款专为树莓派5设计的双TPU扩展板,通过PCIE Switch芯片将一路PCIE 1x Gen2扩展为两路PCIE信号,从而驱动来自Coral的双TPU模组。树莓派系统下使用TPU模块需要安装驱动以及操作环境配置,本文操作演示基于MPDTPU扩展板,对于单TPU的驱动安装也适用。
注意:此操作演示需要确保能够连接外网,否则许多文件无法下载。


关于双TPU的官方描述:
PRODUCT DETAILS
The Coral M.2 Accelerator with Dual Edge TPU is an M.2 module that brings two Edge TPU coprocessors to existing systems and products with an available M.2 E-key slot.

Features
Performs high-speed ML inferencing: Each Edge TPU coprocessor is capable of performing 4 trillion operations per second (4 TOPS), using 2 watts of power. For example, it can execute state-of-the-art mobile vision models such as MobileNet v2 at almost 400 FPS, in a power-efficient manner. With the two Edge TPUs in this module, you can double the inferences per second (8 TOPS) in several ways, such as by running two models in parallel or pipelining one model across both Edge TPUs.
Works with Debian Linux and Windows: Integrates with Debian-based Linux or Windows 10 systems with a compatible card module slot.
Supports TensorFlow Lite: No need to build models from the ground up. TensorFlow Lite models can be compiled to run on the Edge TPU.
Supports AutoML Vision Edge: Easily build and deploy fast, high-accuracy custom image classification models to your device with AutoML Vision Edge.
Description
The Coral M.2 Accelerator with Dual Edge TPU is an M.2 module (E-key) that includes two Edge TPU ML accelerators, each with their own PCIe Gen2 x1 interface.

The Edge TPU is a small ASIC designed by Google that accelerates TensorFlow Lite models in a power efficient manner: each one is capable of performing 4 trillion operations per second (4 TOPS), using 2 watts of power—that's 2 TOPS per watt. For example, one Edge TPU can execute state-of-the-art mobile vision models such as MobileNet v2 at almost 400 frames per second. This on-device ML processing reduces latency, increases data privacy, and removes the need for a constant internet connection.

With the two Edge TPUs in this module, you can double the inferences per second (8 TOPS) in several ways, such as by running two models in parallel or pipelining one model across both Edge TPUs.



回复

使用道具 举报

33

主题

158

帖子

1万

积分

允许发帖

积分
10619
沙发
发表于 2024-7-11 13:23:40 | 只看该作者
本帖最后由 Mcuzone_ZHZ 于 2024-7-11 13:26 编辑

操作系统:
Raspberry Pi
系统版本:
2024-07-04

在驱动安装之前首先在官网下载最新操作系统版本




系统烧写完成后插入树莓派5并安装好MPDTPU扩展板后启动进行驱动安装
回复 支持 反对

使用道具 举报

33

主题

158

帖子

1万

积分

允许发帖

积分
10619
板凳
发表于 2024-7-11 13:28:36 | 只看该作者
本帖最后由 Mcuzone_ZHZ 于 2024-7-11 14:33 编辑

系统启动后我们打开命令行终端输入
sudo nano /boot/firmware/config.txt
在文件最下方添加以下代码:
dtparam=pciex1
kernel=kernel8.img
dtoverlay=pineboards-hat-ai




代码添加成功后保存并重启系统
sudo reboot

系统启动后打开命令行终端输入
uname -r
确保内核版本大于4.18后输入
lsmod | grep apex
确保没有输出则可以开始安装TPU驱动




确保网络连接后
首先添加Google TPU软件库
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-get update

更新完成后安装必要的软件
sudo apt-get install cmake libedgetpu1-std devscripts debhelper dkms dh-dkms




等待安装完成后依次输入以下命令安装Gasket Driver
git clone https://github.com/google/gasket-driver.git
cd gasket-driver
sudo debuild -us -uc -tc -b
cd ..
sudo dpkg -i gasket-dkms_1.0-18_all.deb





安装完成后输入命令确保驱动以及软件安装成功
sudo apt-get install gasket-dkms libedgetpu1-std



接下来添加udev rule去获得设备操作权限
sudo sh -c "echo 'SUBSYSTEM==\"apex\", MODE=\"0660\", GROUP=\"apex\"' >> /etc/udev/rules.d/65-apex.rules"
添加用户到apex
sudo groupadd apex
sudo adduser $USER apex



设置完成后重启系统
sudo reboot

系统启动后验证模块检测以及驱动安装
lspci -nn | grep 089a
ls /dev/apex_0



输入sudo lspci -v可以看到驱动加载



回复 支持 反对

使用道具 举报

33

主题

158

帖子

1万

积分

允许发帖

积分
10619
地板
发表于 2024-7-11 14:04:19 | 只看该作者
本帖最后由 Mcuzone_ZHZ 于 2024-7-11 14:35 编辑

驱动安装完成后接下来进行代码运行环境配置

Google TPU模块代码运行需要在Python3.6-3.9之间
树莓派最新系统Python版本为3.11
因此我们需要使用Pyenv来下载老版本Python


首先进行依赖安装
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl




等待依赖安装完成后进行pyenv的安装
curl https://pyenv.run | bash



按照提示输入sudo nano ~/.bashrc
在文件最下面添加以下代码:
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"


添加完成后保存退出重新加载shell
exec "$SHELL"

接下来我们可以利用pyenv安装老系统版本Python
pyenv install -v 3.8.8




等待安装成功后我们将系统Python版本更改为3.8.8
pyenv global 3.8.8
更改成功后可以查看当前Python使用版本为3.8.8




环境配置完成后我们接下来下载演示代码
依次输入以下命令进行下载
mkdir coral && cd coral
git clone https://github.com/google-coral/pycoral.git
cd pycoral
cd test_data
git clone https://github.com/google-coral/test_data.git




接下来下载代码运行模组
pip3 install  numpy



pip3 install  Pillow



pip3 install --extra-index-url https://google-coral.github.io/py-repo/ pycoral



模组下载完成后返回 coral/pycoral路径下
cd..

输入以下代码进行代码运行
python3 examples/classify_image.py \
--model test_data/test_data/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--labels test_data/test_data/inat_bird_labels.txt \
--input test_data/test_data/parrot.jpg



回复 支持 反对

使用道具 举报

33

主题

158

帖子

1万

积分

允许发帖

积分
10619
5#
发表于 2024-7-26 11:12:53 | 只看该作者
上面示例代码为单TPU操作代码使用
下面演示双TPU跑多个模型:




双TPU跑一个模型:



回复 支持 反对

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表