Google Coral用にモデルをEdge TPU TFlite形式に変換する
はじめに
Coral M.2 Accelerator with Dual Edge TPUは、利用可能なM.2 E-keyスロットを持つ既存のシステムや製品に2つのEdge TPUコプロセッサを追加するM.2モジュールです。TensorflowとPytorchは最も人気のある深層学習フレームワークです。そのため、Edge TPUを使用するには、モデルをEdge TPU形式にコンパイルする必要があります。
このwiki記事では、モデルをコンパイルしてGoogle Coral TPU上で実行するプロセスをガイドし、高性能機械学習アプリケーションでその機能を活用できるようにします。
ハードウェアの準備
ハードウェアのインストール

モデルの変換
開始する前に、インストールガイドに従ってGoogle Coral TPUをPi 5にインストールしていることを確認してください。
- Tensorflowモデル用
- For Pytorch Model
- For Yolo Model
すべてのプロセスはPython 3.11.9でテストされています。
Tensorflowのインストール
pip install tensorflow
tflite_converterの確認
tflite_convert -h
結果は次のようになるはずです:
2024-07-23 10:41:03.750087: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-07-23 10:41:04.276520: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
usage: tflite_convert [-h] --output_file OUTPUT_FILE [--saved_model_dir SAVED_MODEL_DIR | --keras_model_file KERAS_MODEL_FILE] [--saved_model_tag_set SAVED_MODEL_TAG_SET]
[--saved_model_signature_key SAVED_MODEL_SIGNATURE_KEY] [--enable_v1_converter] [--experimental_new_converter [EXPERIMENTAL_NEW_CONVERTER]]
[--experimental_new_quantizer [EXPERIMENTAL_NEW_QUANTIZER]]
Command line tool to run TensorFlow Lite Converter.
optional arguments:
-h, --help show this help message and exit
--output_file OUTPUT_FILE
Full filepath of the output file.
--saved_model_dir SAVED_MODEL_DIR
Full path of the directory containing the SavedModel.
--keras_model_file KERAS_MODEL_FILE
Full filepath of HDF5 file containing tf.Keras model.
--saved_model_tag_set SAVED_MODEL_TAG_SET
Comma-separated set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags must be present. In order to pass in an empty tag set, pass in "". (default "serve")
--saved_model_signature_key SAVED_MODEL_SIGNATURE_KEY
Key identifying the SignatureDef containing inputs and outputs. (default DEFAULT_SERVING_SIGNATURE_DEF_KEY)
--enable_v1_converter
Enables the TensorFlow V1 converter in 2.0
--experimental_new_converter [EXPERIMENTAL_NEW_CONVERTER]
Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True)
--experimental_new_quantizer [EXPERIMENTAL_NEW_QUANTIZER]
Experimental flag, subject to change. Enables MLIR-based quantizer instead of flatbuffer conversion. (default True)
TensorflowモデルをTFliteモデルに変換する
tflite_convert --saved_model_dir=YOUR_MODEL_PATH --output_file=YOUR_MODEL_NAME.tflite
TFliteモデルをEdge TPUモデルに変換する
tfliteモデルをedge tupモデルに変換する前に、モデルを最適化する必要があります。Optimize Tensorflow Modelを確認してください
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-get update
sudo apt-get install edgetpu-compiler
TFliteモデルをEdge TPUモデルに変換する
edgetpu_compiler YOUR_MODEL_NAME.tflite
そして、YOUR_MODEL_NAME_edgetpu.tflite という名前の新しいファイルが作成されます
実際のプロセスでは多くの競合するパッケージがあるため、このアプローチは推奨しません。また、TensorFlow Liteは限られた操作セットをサポートしており、一部のPyTorch操作はサポートされていない可能性があります。
Pytorchモデルをtfliteモデルに変換
依存関係のインストール
pip install -r https://github.com/google-ai-edge/ai-edge-torch/releases/download/v0.1.1/requirements.txt
pip install ai-edge-torch==0.1.1
変換
import ai_edge_torch
import numpy
import torch
import torchvision
resnet18 = torchvision.models.resnet18(torchvision.models.ResNet18_Weights.IMAGENET1K_V1).eval()
sample_inputs = (torch.randn(1, 3, 224, 224),)
torch_output = resnet18(*sample_inputs)
edge_model = ai_edge_torch.convert(resnet18.eval(), sample_inputs)
edge_model.export('resnet.tflite')
resnet.tfliteが得られます
tflite_converterを確認する
tfliteモデルをedge tupモデルに変換する前に、モデルを最適化する必要があります。Optimize Tensorflow Modelを確認してください
tflite_convert -h
結果は次のようになるはずです:
2024-07-23 10:41:03.750087: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-07-23 10:41:04.276520: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
usage: tflite_convert [-h] --output_file OUTPUT_FILE [--saved_model_dir SAVED_MODEL_DIR | --keras_model_file KERAS_MODEL_FILE] [--saved_model_tag_set SAVED_MODEL_TAG_SET]
[--saved_model_signature_key SAVED_MODEL_SIGNATURE_KEY] [--enable_v1_converter] [--experimental_new_converter [EXPERIMENTAL_NEW_CONVERTER]]
[--experimental_new_quantizer [EXPERIMENTAL_NEW_QUANTIZER]]
Command line tool to run TensorFlow Lite Converter.
optional arguments:
-h, --help show this help message and exit
--output_file OUTPUT_FILE
Full filepath of the output file.
--saved_model_dir SAVED_MODEL_DIR
Full path of the directory containing the SavedModel.
--keras_model_file KERAS_MODEL_FILE
Full filepath of HDF5 file containing tf.Keras model.
--saved_model_tag_set SAVED_MODEL_TAG_SET
Comma-separated set of tags identifying the MetaGraphDef within the SavedModel to analyze. All tags must be present. In order to pass in an empty tag set, pass in "". (default "serve")
--saved_model_signature_key SAVED_MODEL_SIGNATURE_KEY
Key identifying the SignatureDef containing inputs and outputs. (default DEFAULT_SERVING_SIGNATURE_DEF_KEY)
--enable_v1_converter
Enables the TensorFlow V1 converter in 2.0
--experimental_new_converter [EXPERIMENTAL_NEW_CONVERTER]
Experimental flag, subject to change. Enables MLIR-based conversion instead of TOCO conversion. (default True)
--experimental_new_quantizer [EXPERIMENTAL_NEW_QUANTIZER]
Experimental flag, subject to change. Enables MLIR-based quantizer instead of flatbuffer conversion. (default True)
TFliteモデルをEdge TPUモデルに変換する
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-get update
sudo apt-get install edgetpu-compiler
Transform TFlite Model to Edge TPU Model
edgetpu_compiler resnet18.tflite
そして、resnet18_edgetpu.tflite という名前の新しいファイルが作成されます
Ultralytics をインストール
pip install ultralytics
YOLOモデルをedge TPUモデルに変換する
# For example, if you want to convert yolov8n.pt to yolov8n_integer_quant_edgetpu.tflite
yolo export model=yolov8n.pt format=edge int8=True
結果は次のようになるはずです:
jiahao@PC:~/yolov8s_saved_model$ ls
assets saved_model.pb yolov8s_float32.tflite yolov8s_full_integer_quant.tflite
fingerprint.pb variables yolov8s_full_integer_quant_edgetpu.log yolov8s_int8.tflite
metadata.yaml yolov8s_float16.tflite yolov8s_full_integer_quant_edgetpu.tflite yolov8s_integer_quant.tflite
yolov8s_full_integer_quant_edgetpu.tflite が必要なモデルです。
以下のコマンドを使用して、他のtfliteモデルをedge TPUモデルに変換できます
# For example, you can convert yolov8s_int8.tflite to edge TPU model
edgetpu_compiler yolov8s_int8.tflite
技術サポート & 製品ディスカッション
弊社製品をお選びいただき、ありがとうございます!お客様の製品体験を可能な限りスムーズにするため、さまざまなサポートを提供いたします。異なる好みやニーズに対応するため、複数のコミュニケーションチャネルをご用意しています。


