Skip to main content

YOLOv5物体検出モデルのトレーニングとデプロイ

note

この文書は AI によって翻訳されています。内容に不正確な点や改善すべき点がございましたら、文書下部のコメント欄または以下の Issue ページにてご報告ください。
https://github.com/Seeed-Studio/wiki-documents/issues

YOLOv5物体検出モデルのデプロイ

このWikiでは、公式のYOLOv5物体検出モデルをトレーニングし、トレーニング済みモデルをGrove Vision AI(V2)またはXIAO ESP32S3デバイスにデプロイする方法を紹介します。

データセットの準備

データセットにはroboflowプラットフォームの使用を推奨します。このプラットフォームでは、データセットのアノテーションやデータ拡張戦略を実行でき、複数のデータセット形式のエクスポートをサポートしています。YOLOv5データセット準備の紹介をご覧ください。

YOLOv5公式リポジトリのクローン

  • デフォルトでは、Python環境とpipパッケージ管理ツール、およびPython>=3.8がインストールされている必要があります。
# YOLOv5公式リポジトリをクローン
git clone https://github.com/ultralytics/yolov5
  • 必要な環境をインストール
# YOLOv5フォルダに移動
cd yolov5
# pipを使用して必要な依存関係をインストール
pip install -r requirements.txt

トレーニング

  • 以下のコマンドを実行してモデルのトレーニングを開始します
python train.py  --weights yolov5n.pt --data ${dataset yaml file path} --imgsz 192

モデルをtflite形式にエクスポート

  • トレーニング後、モデルはruns/train/exp*/weights/フォルダに保存されます。モデルの評価指標が要件を満たしていることを確認してください。
  • まず、以下のコマンドを使用してsaved_model形式のモデルをエクスポートします
python export.py --weights ${Your trained model path (.pt format)}  --imgsz 192 --include saved_model
  • 次に、以下のコードを使用してエクスポートされたsaved_modelモデルを量子化し、tflite形式に変換します
import tensorflow as tf
import os.path as osp

converter = tf.lite.TFLiteConverter.from_saved_model(r'Your saved_model folder path')

tflite_model = converter.convert()

def representative_dataset():
for _ in range(100):
yield [
tf.random.uniform((1, 192, 192, 3))
]

converter.optimizations = [
tf.lite.Optimize.DEFAULT
]
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS_INT8
]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
converter.representative_dataset = representative_dataset

tflite_quant_model = converter.convert()

with open(osp.join(r'The location path to be saved','yolov5n_int8.tflite'), 'wb') as f:
f.write(tflite_quant_model)
  • その後、保存先フォルダにyolov5n_int8.tfliteモデルファイルが表示されます。このモデルファイルはGrove Vision AI(V2)またはXIAO ESP32S3デバイスにデプロイできます。

モデルグラフの最適化

  • Grove Vision AI (V2)はvelaで最適化されたモデルをサポートしており、モデル推論を加速することもできます。まず、以下のコマンドを実行してvelaコマンドラインツールをインストールします(XIAO ESP32S3デバイスはまだサポートされていません)。
pip3 install ethos-u-vela
  • 次に、こちらからvela関連の設定ファイルをダウンロードするか、以下の内容をファイルにコピーして保存します。このファイルはvela_config.iniと名付けることができます。
; file: my_vela_cfg.ini ; ----------------------------------------------------------------------------- 
; Vela configuration file ; -----------------------------------------------------------------------------
; System Configuration

; My_Sys_Cfg
[System_Config.My_Sys_Cfg]
core_clock=400e6
axi0_port=Sram
axi1_port=OffChipFlash
Sram_clock_scale=1.0
Sram_burst_length=32
Sram_read_latency=16
Sram_write_latency=16
Dram_clock_scale=0.75
Dram_burst_length=128
Dram_read_latency=500
Dram_write_latency=250
OnChipFlash_clock_scale=0.25
OffChipFlash_clock_scale=0.015625
OffChipFlash_burst_length=32
OffChipFlash_read_latency=64
OffChipFlash_write_latency=64
; -----------------------------------------------------------------------------
; Memory Mode
; My_Mem_Mode_Parent
[Memory_Mode.My_Mem_Mode_Parent]
const_mem_area=Axi1
arena_mem_area=Axi0
cache_mem_area=Axi0
  • 最後に、以下のコマンドを使用してグラフを最適化します
vela --accelerator-config ethos-u55-64 \ 
--config vela_config.ini \
--system-config My_Sys_Cfg \
--memory-mode My_Mem_Mode_Parent \
--output-dir ${Save path of the optimized model} \
${The path of the tflite model that needs to be optimized}

デプロイ

  • デプロイする必要があるモデルファイルは、上記でエクスポートしたtfliteファイルです。以下のチュートリアルに従って、モデルファイルをターゲットデバイスに書き込むことができます。

  • トレーニング済みのtfliteモデルをデバイスに書き込むには、当社のWebツールを使用することを強くお勧めします。詳細な操作はデプロイチュートリアルで提供されています。

注意: ESP32S3デバイスはvelaグラフ最適化後のモデルデプロイをサポートしていないため、XIAO ESP32S3デバイスにモデルをデプロイする場合はtfliteモデルのグラフ最適化を行う必要はありません。

Loading Comments...