MacでCoral USB Acceleratorを動かす


Edge TPUで推論

イントロ

Coral USB AcceleratorはUSB 3.0のポートに挿す事で機械学習処理の推論処理をオフロードできるアクセラレーターです。

Google Reserachにより提供されており、DesktopCPU比でも10倍程度の速度向上が見込めるのでM1 Macを持っていなくても機械学習の推論処理を高速化できます。

初期はDebian系のOSしかサポートしていなかったのですが、既存モデルを使うだけならMacやWindowsでも動くようになっており、手元のMacで動かしてみます。

基本

公式docに従ってサンプルを動かしていきます。
https://coral.ai/docs/accelerator/get-started/

Edge TPU Runtimeのインストール

curl -O https://dl.google.com/coral/edgetpu_api/edgetpu_runtime_20201204.zip
unzip edgetpu_runtime_20201204.zip
cd edgetpu_runtime
sudo bash install.sh
cd ..

pathにEdge TPU Runtimeのlocationを追加。shellを再起動

# ~/.bashrcに追加
# libedgetpu.1.dylibの読み込み
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

アクセラレーターをUSBポートに挿します。

TnesorFlowのインストール

# virtualenvの作成. 3.8系はサポートしていない
pyenv local 3.7.4
python -m venv coral_accelerator
source coral_accelerator/bin/activate

pip install --upgrade pip
pip install tensorflow
pip install https://github.com/google-coral/pycoral/releases/download/release-frogfish/tflite_runtime-2.5.0-cp37-cp37m-macosx_10_15_x86_64.whl

分類のサンプルを動かします。

mkdir coral && cd coral
git clone https://github.com/google-coral/tflite.git
bash install_requirements.sh
python 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.
14.4ms
4.4ms
4.3ms
4.7ms
4.4ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.77734

無事インコをインコとして認識しました。

PyCoralで推論

2020/11の更新でPyCoral APIが発表されています。それに伴いEdge TPU Python libraryの使用は非推奨。

PyCoralも独立したrepositoryがあるのでそのサンプルも動かしてみます。

# macOS 10.15 Python 3.7
pip install https://github.com/google-coral/pycoral/releases/download/release-frogfish/pycoral-1.0.0-cp37-cp37m-macosx_10_15_x86_64.whl
git clone --recurse-submodules https://github.com/google-coral/pycoral
cd pycoral
git submodule init && git submodule update
$ python3 examples/detect_image.py   --model test_data/ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite   --labels test_data/coco_labels.txt   --input test_data/grace_hopper.bmp   --output ${HOME}/grace_hopper_processed.bmp
----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
32.00 ms
14.98 ms
15.08 ms
16.18 ms
16.02 ms
-------RESULTS--------
tie
  id:     31
  score:  0.83984375
  bbox:   BBox(xmin=227, ymin=419, xmax=292, ymax=541)
person
  id:     0
  score:  0.8046875
  bbox:   BBox(xmin=0, ymin=9, xmax=519, ymax=590)

$ python3 examples/semantic_segmentation.py \
>   --model test_data/deeplabv3_mnv2_pascal_quant_edgetpu.tflite \
>   --input test_data/bird.bmp \
>   --keep_aspect_ratio \
>   --output ${HOME}/segmentation_result.jpg

所感

似たようなアクセラレーターとしてIntelのNeural Compute Stick 2があります. そちらはラズパイのような組み込み用のCPU比だと十分速度向上が見込めるようなのですが、Desktop CPU比だと負けるという話もあります。またNCS2で使用するのはマイナーなOpenVINOというIntel製の機械学習ライブラリー(複数backend選択できるラッパー)です。

一方Coral USB AcceleratorはTFLiteベースなので慣れ親しんだTensorFlowですね。発熱の問題もあるので全ての場合にEdge TPUの方がいいというわけではないのですが、組み込みでない汎用PC向けのアクセラレーターとしてはCoralの方が向いている気はしました。