Google Coral USB Accelerator を試す その1

Google Coral USB Accelerator を試すことにする。

f:id:pongsuke:20190904163750j:plain

f:id:pongsuke:20190904163726j:plain

f:id:pongsuke:20190904163745j:plain

製品情報

coral.withgoogle.com

Features
Google Edge TPU ML accelerator coprocessor
USB 3.0 Type-C socket
Supports Debian Linux on host CPU
Requirementslink
Any Linux computer with a USB port
Debian 6.0 or higher, or any derivative thereof (such as Ubuntu 10.0+)
System architecture of either x86-64 or ARM32/64 with ARMv8 instruction set
And yes, this means Raspberry Pi is supported. However, we officially support only Raspberry Pi 2/3 Model B/B+ running Raspbian. Unofficially, support for Raspberry Pi Zero is also available (install the TAR from GitHub on your Pi Zero, instead of the one below).

Also note that you should connect to a USB 3.0 port for the fastest inferencing speed; if you connect to a USB 2.0 port, inferencing is much slower.

USB 3.0?

Also note that you should connect to a USB 3.0 port for the fastest inferencing speed; if you connect to a USB 2.0 port, inferencing is much slower.

USB3.0じゃないと、おそくなるよー、、、と言うことだ。

購入

Google Coral Edge TPU USB Accelerator G950-01456-01の通販ならマルツオンライン

TPUってなに?

テンサー・プロセッシング・ユニット(Tensor processing unit、TPU)はGoogleが開発した機械学習に特化した特定用途向け集積回路(ASIC) テンソル・プロセッシング・ユニット - Wikipedia

Coral USB Accelerator は何がすごい?

省電力で、非力なマシンでのAI推論を実現する。

セットアップ

注意事項その1 周波数と温度

Caution: During installation, you'll be asked, "Would you like to enable the maximum operating frequency?" Enabling this option improves the the inferencing speed but it also causes the USB Accelerator to become very hot to the touch during operation and might cause burn injuries. If you're not sure you need the increased performance, type N and press Enter to use the default operating frequency. (You can change this later by simply re-running the install script.)

最大の周波数にする? みたいなオプションが出るが、 Yes にすると、かなり熱くなるということだ。

注意事項その2 Pythonのバージョン

セットアップスクリプトでは、 Python3.5 が指定されているらしい。

別に3.5 upper ならいいらしいので、3.6を使用する場合などは、 python3 と書き換えて構わないらしい。

けども! 3.7 は正式にサポート出来ていないから、3.7を使用する場合には、以下のコマンドを実行せよ! とのことだ。

RPI3B+ でエラー

$ sudo rpi-update

$ python3 --version
Python 3.7.3

$ sudo edgetpu_api/install.sh
 ...
Would you like to enable the maximum operating frequency? Y/N
Y

 ...

Done.
Installing device rule file [/etc/udev/rules.d/99-edgetpu-accelerator.rules]...
Done.
Installing Edge TPU runtime library [/usr/lib/aarch64-linux-gnu/libedgetpu.so.1.0]...
cp: 通常ファイル '/usr/lib/aarch64-linux-gnu/libedgetpu.so.1.0' を作成できません: そのようなファイルやディレクトリは
ありません

あれまぁ・・・。

$ uname -m
aarch64

$ getconf LONG_BIT
32

どうやら、aarch64として設定しようとしてくれているけど、普通に Raspbian buster をインストールした場合、32bit で動いているら、そして、クロスコンパイラ?(よくわかっていません)も、はいっていないから、インストールの切り分けを修正する必要が有った。

なお、rpi-update しない状態では、

 $ uname -m
armv7l

ということで、普通に入りました。

Python 3.7 対応

cd /usr/local/lib/python3.7/dist-packages/edgetpu/swig/
        
sudo cp _edgetpu_cpp_wrapper.cpython-35m-arm-linux-gnueabihf.so _edgetpu_cpp_wrapper.cpython-37m-arm-linux-gnueabihf.so

PRI4

$ uname -m
armv7l

GitHub - leswright1977/RPi4-Google-Coral: Google Coral on the Raspberry Pi 4

こちらを使用させていただく。

$ sudo edgetpu_api/install.sh
 ...
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_api/edgetpu-2.11.1-py3-none-any.whl
Installing collected packages: edgetpu
Successfully installed edgetpu-2.11.1
Done.
Lib Fu.

接続

指す!

$ lsusb
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 1a6e:089a Global Unichip Corp. 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

サンプルスクリプトの実行

$ 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

Edge TPU 向けの Modelたち

coral.withgoogle.com