0006 MPTPU(M.2 E):修订间差异

来自Mcuzone Wiki
第85行: 第85行:


04
04
05


添加完成后更新软件列表:
添加完成后更新软件列表:
第157行: 第155行:


15
15
16


=== 4.6 代码运行环境配置 ===
=== 4.6 代码运行环境配置 ===
第174行: 第170行:


<code>curl <nowiki>https://pyenv.run</nowiki> | bash</code>
<code>curl <nowiki>https://pyenv.run</nowiki> | bash</code>
18


19
19
第256行: 第250行:
输入以下代码进行代码运行
输入以下代码进行代码运行


python3 examples/classify_image.py \
<code>python3 examples/classify_image.py \</code>
 
<code>--model test_data/test_data/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \</code>


--model test_data/test_data/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
<code>--labels test_data/test_data/inat_bird_labels.txt \</code>


--labels test_data/test_data/inat_bird_labels.txt \
<code>--input test_data/test_data/parrot.jpg</code>


--input test_data/test_data/parrot.jpg
31

2024年7月19日 (五) 14:58的版本

关键词

树莓派5、PCIE、TPU、DTPU、驱动安装、操作演示、AI、google、Coral

一、简介

MPW7/TPU是一款专为树莓派5设计的TPU扩展板,通过PCIE 1x Gen2驱动来自Coral的TPU模组。树莓派系统下使用TPU模块需要安装驱动以及操作环境配置,本文操作演示基于MPW7/TPU扩展板,对于双TPU的驱动安装也适用。

注意:此操作演示需要确保能连通外网(需自备方法),否则许多文件无法下载。

二、硬件资源

关于双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.

三、系统烧写

3.1 本文档使用的镜像为2024-07-04-raspios-bookworm-arm64-full.img.xz(树莓派OS,Raspberry Pi OS with desktop and recommended software)。

树莓派OS下载地址:

https://www.raspberrypi.com/software/operating-systems/#raspberry-pi-os-64-bit

3.2 系统烧写在SD(TF)卡上,点击直达烧写方法说明

四、驱动安装与配置

4.1 配置config.txt

系统启动后打开树莓派终端输入命令:

sudo nano /boot/firmware/config.txt

在文件最下方添加以下代码:

dtparam=pciex1

kernel=kernel8.img

dtoverlay=pineboards-hat-ai

01

02

代码添加成功后,保存并重启系统:

sudo reboot

4.2 确认软件环境

系统重启后打开打开树莓派终端输入命令:

uname -r

确保内核版本大于4.18后输入:

lsmod | grep apex

确保没有输出则可以开始安装TPU驱动。

03

4.3 安装必要软件

确保网络能连通外网(需自备方法)后,添加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 -

04

添加完成后更新软件列表:

sudo apt-get update

06

更新完成后安装必要的软件:

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

07

08

4.4 安装Gasket Driver

依次输入以下命令安装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

09

10

11

安装完成后输入以下命令确保驱动以及软件安装成功:

sudo apt-get install gasket-dkms libedgetpu1-std

12

接下来添加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

13

设置完成后重启系统:

sudo reboot

4.5 验证模块检测以及驱动安装

系统启动后验证模块检测以及驱动安装:

lspci -nn | grep 089a

ls /dev/apex_0

14

这里板载单TPU,因此可以看到一个TPU模块。

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

15

4.6 代码运行环境配置

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

17

18

依赖安装完成后进行pyenv的安装:

curl https://pyenv.run | bash

19

输入sudo nano ~/.bashrc,在文件最下面添加以下代码:

export PYENV_ROOT="$HOME/.pyenv"

[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init -)"

20

21

添加完成后保存退出并重新加载shell:

exec "$SHELL"

22

接下来我们可以利用pyenv安装老系统版本Python:

pyenv install -v 3.8.8

23

24

等待安装成功后我们将系统Python版本更改为3.8.8:

pyenv global 3.8.8

更改成功后可以查看当前Python使用版本为3.8.8:

pyenv versions

python3 -V

25

五、下载和运行演示代码

5.1 下载代码和运行模组

依次输入以下命令进行下载:

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

26

27

接下来下载代码运行模组

pip3 install numpy

28

pip3 install Pillow

29

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

30

模组下载完成后返回 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

31