12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Coral USB TPU Accelerator(EdgeTPU)をWindows端末で使う

Last updated at Posted at 2020-01-31

【内容】

過去にWindowsで擬似的にEdgeTPUを使う記事を投稿しましたが、Coralの公式のEdgeTPUの導入手順にWindows版が追加されていたので試してみました。
下記の公式手順を踏襲していますが、一部そのままでは実行できないので手を入れています。

【Get started with the USB Accelerator On Windows - Coral】

【Requirements】

  • Windows10 64bit
  • python3.7 + pip
  • git コマンド

【必要モジュールのダウンロード & インストール】

公式手順に従い以下のURLよりダウンロードします。
【 download edgetpu_runtime_20200128.zip - Coral】
今回、このファイルが追加されたことで正式にWindowsからEdgeTPUにアクセスできるようになりました。

ダウンロードしたモジュールを解凍すると以下のようになっています。
image.png

解凍したファイルの中の install.bat を管理者権限で実行します。
すると、お決まりの最大周波数で実行するかどうかを聞いてきますので y または n を入力して先に進みます。

image.png

先に進むと「UsbDk」のインストールが開始されます。
(UsbDKのインストール中はUSBマウスなどが効かなくなります)
image.png

続いて「EdgeTPUのドライバ」をインストールします。
下記画面で「インストール」をクリックしてください。
image.png

以上でセットアップ完了です。

【デバイスの確認】

上記が完了したら、EdgeTPUをUSBコネクタに接続してみます。
うまくいっている場合はデバイスマネージャにて以下のように「Coral USB Accelerator」が認識されていることが確認できます。
image.png

【TensorFlow Liteのインストール】

続いてTensorFlow Liteをインストールします。
インストール用のパッケージのURLは以下を参照して、環境に応じて変更してください。
【TensorFlow Lite Python quickstart】

本例の場合、「Windows10」で「Python3.7」ですので以下のようになります。

tfliteのインストール.ps1
pip install https://dl.google.com/coral/python/tflite_runtime-2.1.0-cp37-cp37m-win_amd64.whl

【サンプルプログラムの実行】

まずは、下記コマンドを実行してサンプルプログラム用のリポジトリをクローンします。

なお、以降の操作は 「PowerShell」 を用いて実行することを想定していますが、途中の wget コマンドを使いたいだけですので、それ以外はコマンドプロンプトからでも実行可能です。

リポジトリのクローン.ps1
cd (適当なフォルダ)
git clone https://github.com/google-coral/tflite.git

Linux環境ではクローンしたリポジトリのtflite/python/examples/classification 内の install_requirements.shを実行すれば必要な環境ができあがりますがWindowsでは実行不能です。
そこで、以下のコマンドを実行して手動で環境を整えます。

まずはサンプルプログラムの依存モジュールをインストールします。

依存モジュールのインストール.ps1
# 依存モジュールのインストール
pip install numpy Pillow

続いてモデルやラベルなど、必要なファイルをダウンロードします。
PowerShellでは wget コマンドを利用してファイルをダウンロードすることが可能です。

モデルとラベルと画像の取得.ps1
cd tflite/python/examples/classification

# モデルのDL
wget https://github.com/google-coral/edgetpu/raw/master/test_data/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite -O mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite

# ラベルのDL
wget https://github.com/google-coral/edgetpu/raw/master/test_data/inat_bird_labels.txt -O inat_bird_labels.txt

# 推論用の画像のDL
wget https://github.com/google-coral/edgetpu/raw/master/test_data/parrot.jpg -O parrot.jpg

上記が完了したら、サンプルプログラムを動かして推論をしてみます。

推論実行.ps1
python .\classify_image.py --model mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --labels inat_bird_labels.txt --input 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

【EdgeTPUのPython APIのインストール】 (2020/02/03 追記)

続いてPython APIをインストールします。
インストール用のwhlファイルは以下に置いてあります。
【Edge TPU Python API for Mac and Windows 】

本例の場合、「Windows10」で「Python3.7」ですので以下のようになります。

PythonAPIのインストール.ps1
pip install https://dl.google.com/coral/edgetpu_api/edgetpu-2.13.0-cp37-cp37m-win_amd64.whl

インストールが完了したら、バージョンを確認してみます。
2020/02/03時点では 2.13.0 になります。

PythonAPIのバージョン確認.ps1
python -c "import edgetpu;print(edgetpu.__version__)"    
2.13.0

続いてPCが認識しているEdgeTPUを確認してみます。
以下のコマンドを実行すると、接続中のEdgeTPUが返ってきます。

EdgeTPUデバイスリスト.ps1
python -c "from edgetpu.basic import edgetpu_utils;print(edgetpu_utils.ListEdgeTpuPaths(edgetpu_utils.EDGE_TPU_STATE_UNASSIGNED))" 

下記例では3つのEdgeTPUを認識しています。

('/sys/bus/usb/devices/1-13', '/sys/bus/usb/devices/1-3', '/sys/bus/usb/devices/1-14')                                   

【最後に】

とりあえず、正式にWinodws環境でEdgeTPUを使うことが出来るようになりました。
これでLinuxなどで稼働するEdgeデバイスにアレルギーのあるおじさん達を丸め込めるようになります。

もっともWindows端末でわざわざEdgeTPUを使うメリットがあるのか? という疑問は残りますが…

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?