YOLOv4 の tflite を edgetpu.tflite に compile する

のびのびラーニング
11 min readMay 16, 2020

--

YOLOv4 を tflite に convert する
https://medium.com/@nobilearn/264be244e7af
の続きです。

遂に PINTO 先生が動き出しました。

https://twitter.com/PINTO03091/status/1261446333585477632

しかも、わざわざこちらの投稿へのリンクまで貼って頂き感謝です。

今回は convert した full-int8 tflite から edgetpu_compiler で edgetpu.tflite を作ります。
ただ、実行ではエラーになってしまいました。
私が煮詰まっている間に PINTO 先生が解決してくださるかもしれません。(チラッ)

apply edgetpu_compiler

_
前回の結果、手元には full-int8 の tflite ファイルがあります。

$ ll yolov4-full-int8.tflite
-rw-r--r--@ 1 nobi staff 63M 5 12 13:10 yolov4-full-int8.tflite

Ubuntu の環境で edgetpu-compiler package を install し、
edgetpu_compiler を実行します。
https://coral.ai/docs/edgetpu/compiler/

$ curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
$ 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-compiler

$ edgetpu_compiler -s yolov4-full-int8.tflite

Edge TPU Compiler version 2.1.302470888Model compiled successfully in 1584 ms.Input model: yolov4-full-int8.tflite
Input size: 62.67MiB
Output model: yolov4-full-int8_edgetpu.tflite
Output size: 62.57MiB
On-chip memory used for caching model parameters: 3.00KiB
On-chip memory remaining for caching model parameters: 7.84MiB
Off-chip memory used for streaming uncached model parameters: 0.00B
Number of Edge TPU subgraphs: 1
Total number of operations: 876
Operation log: yolov4-full-int8_edgetpu.log
Model successfully compiled but not all operations are supported by the Edge TPU. A percentage of the model will instead run on the CPU, which is slower. If possible, consider updating your model to use only operations supported by the Edge TPU. For details, visit g.co/coral/model-reqs.
Number of operations that will run on Edge TPU: 1
Number of operations that will run on CPU: 875
Operator Count StatusTANH 72 More than one subgraph is not supported
ADD 95 More than one subgraph is not supported
RESIZE_NEAREST_NEIGHBOR 2 More than one subgraph is not supported
LEAKY_RELU 35 Operation not supported
CONCATENATION 3 Tensor has unsupported rank (up to 3 innermost dimensions mapped)
CONCATENATION 10 More than one subgraph is not supported
PAD 7 More than one subgraph is not supported
CONV_2D 1 Mapped to Edge TPU
CONV_2D 109 More than one subgraph is not supported
DEQUANTIZE 144 Operation is working on an unsupported data type
DEQUANTIZE 3 Tensor has unsupported rank (up to 3 innermost dimensions mapped)
SPLIT_V 3 Tensor has unsupported rank (up to 3 innermost dimensions mapped)
LOG 72 Operation is working on an unsupported data type
LOGISTIC 6 Tensor has unsupported rank (up to 3 innermost dimensions mapped)
EXP 72 Operation is working on an unsupported data type
MAX_POOL_2D 3 More than one subgraph is not supported
QUANTIZE 19 More than one subgraph is not supported
QUANTIZE 145 Operation is otherwise supported, but not mapped due to some unspecified limitation
MUL 72 More than one subgraph is not supported
RESHAPE 3 Tensor has unsupported rank (up to 3 innermost dimensions mapped)

not supported や unsupported があるのは想定内ですが、

Number of operations that will run on Edge TPU: 1
Number of operations that will run on CPU: 875

これはほとんど意味が無い…?
とは言え、とりあえずファイルは生成されました。

$ ll yolov4-full-int8_edgetpu.*
-rw-r--r-- 1 nobi staff 1.9K 5 12 13:14 yolov4-full-int8_edgetpu.log
-rw-r--r-- 1 nobi staff 63M 5 12 13:14 yolov4-full-int8_edgetpu.tflite

execute object detection with yolov4-full-int8_edgetpu.tflite

_
早速 RasPi4 に Edge TPU (USB Accelerator) を接続して実行してみます。
OS は Ubuntu 19.04 (eoan) を利用しました。
20.04 (focal) は Python の version が 3.8 のため、edgetpu の package が install できませんでした。

The following packages have unmet dependencies:
python3-edgetpu : Depends: python3 (< 3.8) but 3.8.2-0ubuntu2 is to be installed
E: Unable to correct problems, you have held broken packages.

公式のコードを使って object detection してみます。

$ cat object_detection.sh

#!/bin/bashtime python3 object_detection.py \
--model yolov4-full-int8_edgetpu.tflite \
--label models/coco_labels.txt \
--input images/parrot.jpg
#--model models/ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite \

$ ./object_detection.sh

Traceback (most recent call last):
File "object_detection.py", line 97, in <module>
main()
File "object_detection.py", line 65, in main
engine = DetectionEngine(args.model)
File "/usr/lib/python3/dist-packages/edgetpu/detection/engine.py", line 73, in __init__
super().__init__(model_path)
File "/usr/lib/python3/dist-packages/edgetpu/basic/basic_engine.py", line 92, in __init__
self._engine = BasicEnginePythonWrapper.CreateFromFile(model_path)
RuntimeError: external/org_tensorflow/tensorflow/lite/kernels/split_v.cc:131 input_type == kTfLiteFloat32 || input_type == kTfLiteUInt8 || input_type == kTfLiteInt16 || input_type == kTfLiteInt32 || input_type == kTfLiteInt64 was not true.Node number 821 (SPLIT_V) failed to prepare.
Failed to allocate tensors.

だめみたいですね。
詳しいエラーログが出ているので、当該のコードを確認します。

「input_type の型がアカンぞ」と言われていることは分かったので、tflite の convert 設定の問題かなと思い、convert_tflite.py を

   converter = tf.lite.TFLiteConverter.from_keras_model(model)
+
+ converter.experimental_new_converter = False
+ converter.inference_input_type = tf.uint8
+ converter.inference_output_type = tf.uint8
+
if FLAGS.quantize_mode == 'int8':

のように変更したりしてみたのですが、解決しませんでした。

https://twitter.com/nobilearn/status/1260562100809805827

…、私の手記はここで終わっています。

今回の内容は以上です。
ご精読頂き、まことにありがとうございます。

次回は、Edge TPU ではなく TensorFlow Lite の動作を確認していきます。

--

--

のびのびラーニング
のびのびラーニング

Written by のびのびラーニング

AI 技術者 (TensorFlow Developer Certificate) です

No responses yet