【目的】
RaspberryPi 4でCoral USB TPU Accelerator(EdgeTPU)を動かせるようにします。
【はじめに】
本手順は公式に則ったものではありません。
動作させるために一部無理やり対応している部分がありますのでご注意ください。
ちなみに、公式の手順が更新されておりRaspberryPi4でもテストしたと書かれていますが、現時点ではうまく動かせていません。
このため、本手順では過去に公開されていた手順を元にしています。
公式手順で動作することを確認しました。
なお、最新の公式手順は以下を参照してください。
【Get started with the USB Accelerator】 - Coral
【EdgeTPU用モジュールのインストール】
下記コマンドを実行して、最新のモジュールの取得とインストールを行います。
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
# または、最大クロック版
# sudo apt install libedgetpu1-max
TFLiteのランタイムのインストール
下記コマンドを実行して、TFLiteのランタイムをインストールします。
詳細は下記公式手順を参照してください。
【Install just the TensorFlow Lite interpreter - TensorFlow】
sudo pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl
【TensorFlow Lite APIを使ったサンプルの実行】
GitHubからサンプルコードをダウンロードします。
mkdir coral && cd coral
git clone https://github.com/google-coral/tflite.git
サンプルに必要なリソース(推論モデル及びテスト画像)をダウンロードします。
cd tflite/python/examples/classification
bash install_requirements.sh
サンプルプログラムを実行して、TPUを使った推論を行います。
python3 classify_image.py \
--model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--labels models/inat_bird_labels.txt \
--input images/parrot.jpg
以下のように出力されれば動作しています。
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
12.5ms
4.4ms
3.5ms
4.1ms
4.3ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.76562
【Python APIのインストール】
下記コマンドを実行してEdge TPU Python APIをインストールします。
sudo apt install python3-edgetpu
【最後に】
とりあえずEdgeTPUが動くようになりました。
過去に作ったプログラムも動くことは確認し現時点では問題は出ていませんが、非公式な手順ゆえ、予期せぬ動作をする可能性がありますので、ご注意ください。
【編集前の非公式手順 (非推奨)】
下記コマンドを実行して、最新のモジュールの取得とインストールを行います。 なお、このまま やってることは 上記インストーラでインストールされるモジュールはPython3.5用です。 以上でインストール作業は完了です。 続いてサンプルプログラムを実行します。(非公式手順ゆえ、非推奨)
【EdgeTPU用モジュールのインストール】
cd ~/
wget https://dl.google.com/coral/edgetpu_api/edgetpu_api_latest.tar.gz -O edgetpu_api.tar.gz --trust-server-names
tar xzf edgetpu_api.tar.gz
cd edgetpu_api
install.sh
を実行すると、 Your platform is not supported.
と怒られます。
そこで、 install.sh
を以下のように変更します。diff install.sh install.org
52,53c52,53
< if [[ "${MODEL}" == "Raspberry Pi 4 Model B Rev"* ]]; then
< info "Recognized as Raspberry Pi 4 B."
---
> if [[ "${MODEL}" == "Raspberry Pi 3 Model B Rev"* ]]; then
> info "Recognized as Raspberry Pi 3 B."
Raspberry Pi 3
の記述を Raspberry Pi 4
に変更しているだけです。
変更が完了したら保存して、 install.sh
を実行します。sudo bash ./install.sh
install.sh: line 51: warning: command substitution: ignored null byte in input
Recognized as Raspberry Pi 4 B.
Warning: During normal operation, the Edge TPU Accelerator may heat up, depending
on the computation workloads and operating frequency. Touching the metal part of the
device after it has been operating for an extended period of time may lead to discomfort
and/or skin burns. As such, when running at the default operating frequency, the device is
intended to safely operate at an ambient temperature of 35C or less. Or when running at
the maximum operating frequency, it should be operated at an ambient temperature of
25C or less.
Google does not accept any responsibility for any loss or damage if the device is operated
outside of the recommended ambient temperature range.
.............................................................
Would you like to enable the maximum operating frequency? Y/N
y
Using maximum operating frequency.
Installing library dependencies...
Reading package lists... Done
Building dependency tree
Reading state information... Done
libgcc1 is already the newest version (1:8.3.0-6+rpi1).
libunwind8 is already the newest version (1.2.1-9).
libunwind8 set to manually installed.
libusb-1.0-0 is already the newest version (2:1.0.22-2).
libusb-1.0-0 set to manually installed.
python3-numpy is already the newest version (1:1.16.2-1).
python3-pil is already the newest version (5.4.1-2).
python3-pil set to manually installed.
The following additional packages will be installed:
libc++1-7 libc++abi1-7
Suggested packages:
clang
The following NEW packages will be installed:
libc++1 libc++1-7 libc++abi1 libc++abi1-7 python3-pip
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 443 kB of archives.
After this operation, 1,688 kB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian buster/main armhf python3-pip all 18.1-5+rpt1 [171 kB]
Get:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libc++abi1-7 armhf 1:7.0.1-8+rpi2 [77.6 kB]
Get:3 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libc++1-7 armhf 1:7.0.1-8+rpi2 [181 kB]
Get:4 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libc++1 armhf 1:7.0-47 [7,112 B]
Get:5 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libc++abi1 armhf 1:7.0-47 [7,044 B]
Fetched 443 kB in 3s (150 kB/s)
Selecting previously unselected package libc++abi1-7:armhf.
(Reading database ... 95881 files and directories currently installed.)
Preparing to unpack .../libc++abi1-7_1%3a7.0.1-8+rpi2_armhf.deb ...
Unpacking libc++abi1-7:armhf (1:7.0.1-8+rpi2) ...
Selecting previously unselected package libc++1-7:armhf.
Preparing to unpack .../libc++1-7_1%3a7.0.1-8+rpi2_armhf.deb ...
Unpacking libc++1-7:armhf (1:7.0.1-8+rpi2) ...
Selecting previously unselected package libc++1:armhf.
Preparing to unpack .../libc++1_1%3a7.0-47_armhf.deb ...
Unpacking libc++1:armhf (1:7.0-47) ...
Selecting previously unselected package libc++abi1:armhf.
Preparing to unpack .../libc++abi1_1%3a7.0-47_armhf.deb ...
Unpacking libc++abi1:armhf (1:7.0-47) ...
Selecting previously unselected package python3-pip.
Preparing to unpack .../python3-pip_18.1-5+rpt1_all.deb ...
Unpacking python3-pip (18.1-5+rpt1) ...
Setting up libc++abi1-7:armhf (1:7.0.1-8+rpi2) ...
Setting up python3-pip (18.1-5+rpt1) ...
Setting up libc++abi1:armhf (1:7.0-47) ...
Setting up libc++1-7:armhf (1:7.0.1-8+rpi2) ...
Setting up libc++1:armhf (1:7.0-47) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for libc-bin (2.28-10+rpi1) ...
Done.
Installing device rule file [/etc/udev/rules.d/99-edgetpu-accelerator.rules]...
Done.
Installing Edge TPU runtime library [/usr/lib/arm-linux-gnueabihf/libedgetpu.so.1.0]...
Done.
Installing Edge TPU Python API...
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Processing ./edgetpu-2.11.1-py3-none-any.whl
Installing collected packages: edgetpu
Successfully installed edgetpu-2.11.1
Done.
RaspberryPi 4はPython3.7なのでそのままでは動きません。
そこでJetsonNanoの時と同様に、無理やりですが3.5用のモジュールを3.7用として流用します。# python3.7なのでpython3.5用のモジュールを使う
cd /usr/local/lib/python3.7/dist-packages/edgetpu/swig/
sudo ln -s _edgetpu_cpp_wrapper.cpython-35m-arm-linux-gnueabihf.so _edgetpu_cpp_wrapper.so
【サンプルプログラムの実行】
下記コマンドを実行するだけです。mkdir ~/Downloads
cd ~/Downloads/
wget https://dl.google.com/coral/canned_models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
https://dl.google.com/coral/canned_models/inat_bird_labels.txt \
https://coral.withgoogle.com/static/docs/images/parrot.jpg
cd /usr/local/lib/python3.7/dist-packages/edgetpu/demo
python3 classify_image.py \
--model ~/Downloads/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--label ~/Downloads/inat_bird_labels.txt \
--image ~/Downloads/parrot.jpg
INFO: Initialized TensorFlow Lite runtime.
---------------------------
Ara macao (Scarlet Macaw)
Score : 0.76171875