Posst date : 2020-03-07
エッジで推論を行うために設計された Google の専用 ASIC,Google Edge TPU (Tensor Processing Unit),を用いた USB デバイス
Linux (debian, ubuntu, Raspbian), Mac, Windows で利用可能
主として次のサイトに従って,ubunutu18.04LTS (ThinkaPad X280 i5) にインストールを行った
$ wget -qO- https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
OK
$ sudo apt-get update
ヒット:1 http://jp.archive.ubuntu.com/ubuntu bionic InRelease
ヒット:2 http://jp.archive.ubuntu.com/ubuntu bionic-updates InRelease
ヒット:3 http://jp.archive.ubuntu.com/ubuntu bionic-backports InRelease
ヒット:4 https://repo.skype.com/deb stable InRelease
取得:5 https://packages.cloud.google.com/apt coral-edgetpu-stable InRelease [6,332 B]
取得:6 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 Packages [1,390 B]
取得:7 https://packages.cloud.google.com/apt coral-edgetpu-stable/main i386 Packages [673 B]
取得:8 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
ヒット:9 http://archive.ubuntu.com/ubuntu bionic InRelease
97.1 kB を 6秒 で取得しました (15.1 kB/s)
パッケージリストを読み込んでいます... 完了
$ sudo apt-get install libedgetpu1-std
v.13.0
$ lsusb
Bus 003 Device 003: ID 18d1:9302 Google Inc.
python3 のバージョンを確認
$ python3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
python3 のバージョンに合った runtime をインストール
$ pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_x86_64.whl
Collecting tflite-runtime==2.1.0.post1 from https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_x86_64.whl
Downloading https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp36-cp36m-linux_x86_64.whl (1.9MB)
100% |████████████████████████████████| 1.9MB 505kB/s
Collecting numpy>=1.12.1 (from tflite-runtime==2.1.0.post1)
Using cached https://files.pythonhosted.org/packages/62/20/4d43e141b5bc426ba38274933ef8e76e85c7adea2c321ecf9ebf7421cedf/numpy-1.18.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy, tflite-runtime
Successfully installed numpy-1.18.1 tflite-runtime-2.1.0.post1
$ git clone https://github.com/google-coral/tflite.git
Cloning into 'tflite'...
remote: Enumerating objects: 65, done.
remote: Counting objects: 100% (65/65), done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 193 (delta 23), reused 59 (delta 19), pack-reused 128
Receiving objects: 100% (193/193), 675.91 KiB | 755.00 KiB/s, done.
Resolving deltas: 100% (69/69), done.
$ cd tflite/python/examples/classification
$ bash install_requirements.sh
Collecting numpy
Using cached https://files.pythonhosted.org/packages/62/20/4d43e141b5bc426ba38274933ef8e76e85c7adea2c321ecf9ebf7421cedf/numpy-1.18.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting Pillow
Downloading https://files.pythonhosted.org/packages/19/5e/23dcc0ce3cc2abe92efd3cd61d764bee6ccdf1b667a1fb566f45dc249953/Pillow-7.0.0-cp36-cp36m-manylinux1_x86_64.whl (2.1MB)
100% |████████████████████████████████| 2.1MB 341kB/s
Installing collected packages: numpy, Pillow
Successfully installed Pillow-7.0.0 numpy-1.18.1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 189 100 189 0 0 282 0 --:--:-- --:--:-- --:--:-- 282
100 3988k 100 3988k 0 0 280k 0 0:00:14 0:00:14 --:--:-- 411k
100 181 100 181 0 0 301 0 --:--:-- --:--:-- --:--:-- 176k
100 3448k 100 3448k 0 0 265k 0 0:00:12 0:00:12 --:--:-- 308k
100 158 100 158 0 0 528 0 --:--:-- --:--:-- --:--:-- 528
100 40895 100 40895 0 0 63304 0 --:--:-- --:--:-- --:--:-- 670k
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 148 100 148 0 0 266 0 --:--:-- --:--:-- --:--:-- 266
100 3068k 100 3068k 0 0 472k 0 0:00:06 0:00:06 --:--:-- 609k
$ 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.6ms
4.3ms
4.1ms
4.1ms
4.3ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.76172
$ cd tflite/python/examples/detection
$ ./install_requirements.sh
Collecting numpy
Using cached https://files.pythonhosted.org/packages/62/20/4d43e141b5bc426ba38274933ef8e76e85c7adea2c321ecf9ebf7421cedf/numpy-1.18.1-cp36-cp36m-manylinux1_x86_64.whl
Collecting Pillow
Using cached https://files.pythonhosted.org/packages/19/5e/23dcc0ce3cc2abe92efd3cd61d764bee6ccdf1b667a1fb566f45dc249953/Pillow-7.0.0-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: numpy, Pillow
Successfully installed Pillow-7.0.0 numpy-1.18.1
./install_requirements.sh: 行 26: /proc/device-tree/model: そのようなファイルやディレクトリはありません
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 192 100 192 0 0 1548 0 --:--:-- --:--:-- --:--:-- 1548
100 6785k 100 6785k 0 0 788k 0 0:00:08 0:00:08 --:--:-- 1081k
100 184 100 184 0 0 4717 0 --:--:-- --:--:-- --:--:-- 4717
100 6030k 100 6030k 0 0 813k 0 0:00:07 0:00:07 --:--:-- 1079k
100 153 100 153 0 0 3923 0 --:--:-- --:--:-- --:--:-- 3923
100 930 100 930 0 0 2224 0 --:--:-- --:--:-- --:--:-- 0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 154 100 154 0 0 312 0 --:--:-- --:--:-- --:--:-- 313
100 918k 100 918k 0 0 443k 0 0:00:02 0:00:02 --:--:-- 1053k
$ python3 detect_image.py \
--model models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite \
--labels models/coco_labels.txt \
--input images/grace_hopper.bmp \
--output images/grace_hopper_processed.bmp
----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
28.14 ms
11.95 ms
12.14 ms
12.87 ms
12.63 ms
-------RESULTS--------
person
id: 0
score: 0.83984375
bbox: BBox(xmin=1, ymin=2, xmax=516, ymax=599)
tie
id: 31
score: 0.83984375
bbox: BBox(xmin=228, ymin=421, xmax=293, ymax=545)
##### without TPU #####
$ python3 detect_image.py \
--model models/mobilenet_ssd_v2_coco_quant_postprocess.tflite \
--labels models/coco_labels.txt \
--input images/grace_hopper.bmp
--labels models/coco_labels.txt --input images/grace_hopper.bmp
----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
550.27 ms
557.99 ms
544.21 ms
542.66 ms
542.42 ms
-------RESULTS--------
person
id: 0
score: 0.83984375
bbox: BBox(xmin=2, ymin=5, xmax=513, ymax=596)
tie
id: 31
score: 0.83984375
bbox: BBox(xmin=228, ymin=418, xmax=293, ymax=544)
性能比 : 543.42/12.63 = 42.95
(https://github.com/google-coral/project-posenet)
Pose Net Model : Pose detection
$ sudo apt-get install python3-edgetpu
$ sh ./install_requirements.sh
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
python3-numpy はすでに最新バージョン (1:1.13.3-2ubuntu1) です。
gstreamer1.0-plugins-good はすでに最新バージョン (1.14.5-0ubuntu1~18.04.1) です。
gstreamer1.0-plugins-good は手動でインストールしたと設定されました。
python3-gi はすでに最新バージョン (3.26.1-2ubuntu1) です。
python3-gi は手動でインストールしたと設定されました。
gstreamer1.0-plugins-bad はすでに最新バージョン (1.14.5-0ubuntu1~18.04.1) です。
以下のパッケージが新たにインストールされます:
python3-gst-1.0
アップグレード: 0 個、新規インストール: 1 個、削除: 0 個、保留: 0 個。
14.3 kB のアーカイブを取得する必要があります。
この操作後に追加で 64.5 kB のディスク容量が消費されます。
取得:1 http://jp.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-gst-1.0 amd64 1.14.5-0ubuntu1~18.04.1 [14.3 kB]
14.3 kB を 0秒 で取得しました (45.9 kB/s)
以前に未選択のパッケージ python3-gst-1.0 を選択しています。
(データベースを読み込んでいます ... 現在 305932 個のファイルとディレクトリがインストールされています。)
.../python3-gst-1.0_1.14.5-0ubuntu1~18.04.1_amd64.deb を展開する準備をしています ...
python3-gst-1.0 (1.14.5-0ubuntu1~18.04.1) を展開しています...
python3-gst-1.0 (1.14.5-0ubuntu1~18.04.1) を設定しています ...
libc-bin (2.27-3ubuntu1) のトリガを処理しています ...
Collecting svgwrite
Downloading https://files.pythonhosted.org/packages/4f/2e/f36cfec1da6162055b884e6366074cff18475a9538559ceae0c0bc58e186/svgwrite-1.3.1-py2.py3-none-any.whl (67kB)
100% |████████████████████████████████| 71kB 919kB/s
Collecting pyparsing>=2.0.1 (from svgwrite)
Downloading https://files.pythonhosted.org/packages/5d/bc/1e58593167fade7b544bfe9502a26dc860940a79ab306e651e7f13be68c2/pyparsing-2.4.6-py2.py3-none-any.whl (67kB)
100% |████████████████████████████████| 71kB 1.2MB/s
Installing collected packages: pyparsing, svgwrite
Successfully installed pyparsing-2.4.6 svgwrite-1.3.1
[ERROR]
$ python3 simple_pose.py
Traceback (most recent call last):
File "simple_pose.py", line 18, in <module>
from pose_engine import PoseEngine
File "/home/ttks/src/Coral/tflite/python/examples/project-posenet/pose_engine.py", line 20, in <module>
from edgetpu import __version__ as edgetpu_version
ModuleNotFoundError: No module named 'edgetpu'
[FIX] python3-edgetpu をインストール
$ apt-get install python3-edgetpu
python3-edgetpu_13.0_amd64.deb
$ python3 simple_pose.py
--2020-03-07 17:07:35-- https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Hindu_marriage_ceremony_offering.jpg/640px-Hindu_marriage_ceremony_offering.jpg
upload.wikimedia.org (upload.wikimedia.org) をDNSに問いあわせています... 103.102.166.240
upload.wikimedia.org (upload.wikimedia.org)|103.102.166.240|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 83328 (81K) [image/jpeg]
`couple.jpg' に保存中
couple.jpg 100%[===================>] 81.38K 377KB/s 時間 0.2s
2020-03-07 17:07:36 (377 KB/s) - `couple.jpg' へ保存完了 [83328/83328]
Inference time: 20ms
Pose Score: 0.61885977
nose x=210 y=152 score=1.0
left eye x=224 y=138 score=1.0
right eye x=199 y=136 score=1.0
left ear x=245 y=135 score=1.0
right ear x=183 y=129 score=0.8
left shoulder x=268 y=168 score=0.8
right shoulder x=161 y=172 score=1.0
left elbow x=282 y=255 score=0.6
right elbow x=154 y=254 score=0.9
left wrist x=236 y=333 score=0.7
right wrist x=163 y=301 score=0.6
left hip x=323 y=181 score=0.2
right hip x=191 y=251 score=0.0
left knee x=343 y=84 score=0.8
right knee x=162 y=295 score=0.0
left ankle x=318 y=174 score=0.1
right ankle x=167 y=309 score=0.0
Pose Score: 0.5783217
nose x=398 y=145 score=1.0
left eye x=416 y=129 score=1.0
right eye x=382 y=127 score=1.0
left ear x=457 y=110 score=0.9
right ear x=370 y=121 score=0.2
left shoulder x=490 y=168 score=0.9
right shoulder x=362 y=150 score=0.8
left elbow x=466 y=291 score=0.8
right elbow x=329 y=246 score=0.7
left wrist x=339 y=303 score=0.9
right wrist x=234 y=331 score=0.5
left hip x=525 y=106 score=0.0
right hip x=361 y=163 score=0.2
left knee x=501 y=90 score=0.6
right knee x=353 y=183 score=0.1
left ankle x=503 y=177 score=0.0
right ankle x=308 y=177 score=0.1
$ python3 pose_camera.py
OK
References :
インストール方法は Linux 用を参照 (ubuntu と同じ)
$ lsusb
Bus 001 Device 007: ID 1a6e:089a Global Unichip Corp.
Bus 001 Device 005: ID 04ca:004f Lite-On Technology Corp. SK-9020 keyboard
Bus 001 Device 004: ID 1bcf:0007 Sunplus Innovation Technology Inc. Optical Mouse
Bus 001 Device 006: ID 0424:7800 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ 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.
117.3ms
11.9ms
11.9ms
11.7ms
12.1ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.76562
### with TPU ###
$ python3 detect_image.py \
> --model models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite \
> --labels models/coco_labels.txt \
> --input images/grace_hopper.bmp \
> --output images/grace_hopper_processed.bmp
----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
231.92 ms
55.53 ms
55.38 ms
52.67 ms
54.86 ms
-------RESULTS--------
person
id: 0
score: 0.83984375
bbox: BBox(xmin=1, ymin=2, xmax=516, ymax=599)
tie
id: 31
score: 0.83984375
bbox: BBox(xmin=228, ymin=421, xmax=293, ymax=545)
### without TPU ###
$ python3 detect_image.py --model models/mobilenet_ssd_v2_coco_quant_postprocess.tflite --labels models/coco_labels.txt --input images/grace_hopper.bmp
----INFERENCE TIME----
Note: The first inference is slow because it includes loading the model into Edge TPU memory.
520.56 ms
508.28 ms
508.13 ms
508.34 ms
508.36 ms
-------RESULTS--------
person
id: 0
score: 0.83984375
bbox: BBox(xmin=2, ymin=5, xmax=513, ymax=596)
tie
id: 31
score: 0.83984375
bbox: BBox(xmin=228, ymin=418, xmax=293, ymax=544)
simple_pose.py
$ python3 simple_pose.py
--2020-03-11 15:10:58-- https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Hindu_marriage_ceremony_offering.jpg/640px-Hindu_marriage_ceremony_offering.jpg
upload.wikimedia.org (upload.wikimedia.org) をDNSに問いあわせています... 103.102.166.240
upload.wikimedia.org (upload.wikimedia.org)|103.102.166.240|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 83328 (81K) [image/jpeg]
`couple.jpg' に保存中
couple.jpg 100%[===================>] 81.38K 366KB/s 時間 0.2s
2020-03-11 15:10:59 (366 KB/s) - `couple.jpg' へ保存完了 [83328/83328]
Inference time: 119ms
Pose Score: 0.61855656
nose x=210 y=152 score=1.0
left eye x=224 y=138 score=1.0
right eye x=199 y=137 score=1.0
left ear x=244 y=135 score=1.0
right ear x=182 y=129 score=0.8
left shoulder x=268 y=168 score=0.8
right shoulder x=160 y=173 score=1.0
left elbow x=282 y=255 score=0.6
right elbow x=154 y=256 score=0.9
left wrist x=236 y=333 score=0.8
right wrist x=162 y=299 score=0.6
left hip x=324 y=179 score=0.2
right hip x=203 y=233 score=0.0
left knee x=344 y=86 score=0.8
right knee x=170 y=294 score=0.0
left ankle x=316 y=175 score=0.1
right ankle x=166 y=307 score=0.0
Pose Score: 0.5867945
nose x=398 y=145 score=1.0
left eye x=416 y=128 score=1.0
right eye x=382 y=127 score=1.0
left ear x=457 y=110 score=0.9
right ear x=370 y=120 score=0.2
left shoulder x=491 y=169 score=0.9
right shoulder x=362 y=151 score=0.8
left elbow x=465 y=290 score=0.9
right elbow x=328 y=246 score=0.8
left wrist x=340 y=303 score=0.9
right wrist x=233 y=331 score=0.5
left hip x=525 y=106 score=0.0
right hip x=361 y=161 score=0.3
left knee x=501 y=90 score=0.6
right knee x=350 y=167 score=0.1
left ankle x=504 y=178 score=0.0
right ankle x=306 y=176 score=0.1
$ python3 pose_camera.py
PoseNet 102ms (9.8fps) TrueFPS:8fps