Vengineerの戯言

人生は短いけど、長いです。人生を楽しみましょう!

Google Edge TPUの offline compiler


Google Edge TPUのModel Compilerに制約がなくなったから1か月半、Google Edge TPUが利用できるようになってずーと死角だと思っていた、Model Compilerがやっとonlineではなく、offline になったよ。


Model Compiler が online だと、いろいろ不便なのと、モデルの内容を知られたくないという場合には。。
趣味などで使う分には全く問題ないんだけどね。。。


Google Edge TPU DevBoardだけじゃなくて、Debian系なら使えるって。。。ただし、x86-64とARM64だけでね。
Raspberry Pi 2/3+、Zeroも非公式ながら使えるよって。

早速、WSL (Ubuntu 18.04LTS)で下記のようにやってみたんだけど、
    $ echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

    $ sudo apt update
    $ sudo apt install edgetpu

update でエラーが発生したよ。

Get:1 https://packages.cloud.google.com/apt coral-edgetpu-stable InRelease [5,028 B]
Err:1 https://packages.cloud.google.com/apt coral-edgetpu-stable InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 6A030B21BA07F4FB
Err:2 http://archive.ubuntu.com/ubuntu bionic InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://security.ubuntu.com/ubuntu bionic-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
}

WSLではどうやらエラーになるので、ここに書いてあることを参考にして、


を実行してから、

    $ sudo apt update
    $ sudo apt install edgetpu

したら、インストールできたよ。

早速、
$ edgetpu_compiler
Edge TPU Compiler version 1.0.249710469

Usage:
edgetpu_compiler [options] model...

Options:
-o, --out_dir <dir>
       Output the compiled model and log files to directory <dir>.
       Default is the current directory (.)
-m, --min_runtime_version <val>
       Minimum Edge TPU runtime version required by your model.
       Models are forward-compatible with new Edge TPU runtimes.
       Default: 10; Min: 10; Max: 11
-s, --show_operations
       Print the log showing operations that mapped to the Edge TPU.
-v, --version
       Print compiler version and exit.
-h, --help
       Print this help and exit.

No input model(s).

Hosted models | TensorFlow LiteからMobilenet_V2_1.0_224_quantInception_V4_quantをダウンロードして、Edge TPU compiler にかけてみました。

-s オプションを付けて、Edge TPU で実行するOpを表示しました。
$ edgetpu_compiler -s mobilenet_v2_1.0_224_quant.tflite
Edge TPU Compiler version 1.0.249710469
INFO: Initialized TensorFlow Lite runtime.

Model compiled successfully in 970 ms.

Input model: mobilenet_v2_1.0_224_quant.tflite
Input size: 3.41MiB
Output model: mobilenet_v2_1.0_224_quant_edgetpu.tflite
Output size: 3.89MiB
On-chip memory available for caching model parameters: 6.91MiB
On-chip memory used for caching model parameters: 3.75MiB
Off-chip memory used for streaming uncached model parameters: 0.00B
Number of Edge TPU subgraphs: 1
Total number of operations: 65
Operation log: mobilenet_v2_1.0_224_quant_edgetpu.log

Operator                       Count      Status

ADD                            10         Mapped to Edge TPU
AVERAGE_POOL_2D                1          Mapped to Edge TPU
CONV_2D                        36         Mapped to Edge TPU
DEPTHWISE_CONV_2D              17         Mapped to Edge TPU
RESHAPE                        1          Mapped to Edge TPU

元のtfliteのファイルサイズが3.41MiBで、EdgeTPU用に生成されたtfliteのファイルサイズは3.89MiBで多少大きくなっていますね。

Edge TPUには、内部8MiBのSRAMがあるようで、
ここではモデルパラメータ用には6.91MiBが利用可能で、モデルパラメータで3.75MiBを使ったと。
また、外部メモリに置くパラメータは無いようです。

Edge TPU用の subgraphの数は、1個で、65個のオペレータを含んでいて、その下にあるのが詳細で
    ADD                10個
    AVERAGE_POOL_2D     1個
    CONV_2D            36個
    DEPTHWISE_CONV_2D  17個
    RESHAPE             1個
になっているようです。

ログファイル(mobilenet_v2_1.0_224_quant_edgetpu.log)に中は、下記のように表示された情報以上のものはありませんでした。
-s オプションを付けないと、この情報は表示されませんでしたが、mobilenet_v2_1.0_224_quant_edgetpu.logの内容は同じでした。
Edge TPU Compiler version 1.0.249710469
Input: mobilenet_v2_1.0_224_quant.tflite
Output: mobilenet_v2_1.0_224_quant_edgetpu.tflite

Operator                       Count      Status

ADD                            10         Mapped to Edge TPU
AVERAGE_POOL_2D                1          Mapped to Edge TPU
CONV_2D                        36         Mapped to Edge TPU
DEPTHWISE_CONV_2D              17         Mapped to Edge TPU
RESHAPE                        1          Mapped to Edge TPU

inception_v4_299_quantでは、subgraphは1個で、195個のOpのようです。

edgetpu_compiler -s inception_v4_299_quant.tflite
Edge TPU Compiler version 1.0.249710469
INFO: Initialized TensorFlow Lite runtime.

Model compiled successfully in 6607 ms.

Input model: inception_v4_299_quant.tflite
Input size: 40.87MiB
Output model: inception_v4_299_quant_edgetpu.tflite
Output size: 43.03MiB
On-chip memory available for caching model parameters: 5.91MiB
On-chip memory used for caching model parameters: 5.91MiB
Off-chip memory used for streaming uncached model parameters: 36.25MiB
Number of Edge TPU subgraphs: 1
Total number of operations: 195
Operation log: inception_v4_299_quant_edgetpu.log

Operator                       Count      Status

MAX_POOL_2D                    4          Mapped to Edge TPU
AVERAGE_POOL_2D                15         Mapped to Edge TPU
CONCATENATION                  25         Mapped to Edge TPU
CONV_2D                        149        Mapped to Edge TPU
FULLY_CONNECTED                1          Mapped to Edge TPU
SOFTMAX                        1          Mapped to Edge TPU

こちらは、36.25MiBもチップ外にパラメータ置くので、遅くなっちゃいますね。