Meet YOLO26: next-gen vision AI.

Link to this sectionYOLOv5におけるモデルのプルーニングとスパース性#

📚 本ガイドでは、YOLOv5 🚀モデルにプルーニングを適用し、パフォーマンスを維持しながらより効率的なネットワークを作成する方法を解説します。

Link to this sectionモデルのプルーニングとは?#

モデルのプルーニングは、ニューラルネットワークにおいて重要度の低いパラメータ(重みや接続)を削除することで、モデルのサイズと複雑さを削減する手法です。このプロセスにより、次のような利点を持つより効率的なモデルが構築されます。

  • リソースが制限されたデバイスへのデプロイを容易にするためのモデルサイズの縮小
  • 精度への影響を最小限に抑えつつ、推論速度を高速化
  • メモリ使用量とエネルギー消費の削減
  • リアルタイムアプリケーションにおける全体的な効率の向上

プルーニングは、モデルのパフォーマンスにほとんど寄与しないパラメータを特定して削除することで、同等の精度を維持しつつ軽量なモデルを実現します。

Link to this section始める前に#

Clone repo and install requirements.txt in a Python>=3.8.0 environment, including PyTorch>=1.8. Models and datasets download automatically from the latest YOLOv5 release.

git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install

Link to this sectionベースラインパフォーマンスのテスト#

プルーニングを行う前に、比較対象となるベースラインパフォーマンスを確認します。このコマンドでは、画像サイズ640ピクセルのCOCO val2017データセットでYOLOv5xをテストします。yolov5x.ptは、利用可能なモデルの中で最大かつ最も高精度なモデルです。その他のオプションにはyolov5s.ptyolov5m.ptyolov5l.ptがあるほか、独自のデータセットで学習したチェックポイントである./weights/best.ptを使用することも可能です。利用可能なすべてのモデルの詳細については、READMEのを参照してください。

python val.py --weights yolov5x.pt --data coco.yaml --img 640 --half

出力:

val: data=/content/yolov5/data/coco.yaml, weights=['yolov5x.pt'], batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.65, task=val, device=, workers=8, single_cls=False, augment=False, verbose=False, save_txt=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=True, dnn=False
YOLOv5 🚀 v6.0-224-g4c40933 torch 1.10.0+cu111 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)

Fusing layers...
Model Summary: 444 layers, 86705005 parameters, 0 gradients
val: Scanning '/content/datasets/coco/val2017.cache' images and labels... 4952 found, 48 missing, 0 empty, 0 corrupt: 100% 5000/5000 [00:00<?, ?it/s]
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100% 157/157 [01:12<00:00,  2.16it/s]
                 all       5000      36335      0.732      0.628      0.683      0.496
Speed: 0.1ms pre-process, 5.2ms inference, 1.7ms NMS per image at shape (32, 3, 640, 640)  # <--- base speed

Evaluating pycocotools mAP... saving runs/val/exp-2/yolov5x_predictions.json...
...
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.507  # <--- base mAP
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.689
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.552
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.345
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.559
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.652
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.381
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.630
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.682
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.526
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.731
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.829
Results saved to runs/val/exp

Link to this sectionYOLOv5xへのプルーニングの適用(スパース性30%)#

We can apply pruning to the model using the torch_utils.prune() command defined in utils/torch_utils.py. To test a pruned model, we update val.py to prune YOLOv5x to 0.3 sparsity (30% of weights set to zero):

YOLOv5 model pruning to 30% sparsity code

30%プルーニングされた出力結果:

val: data=/content/yolov5/data/coco.yaml, weights=['yolov5x.pt'], batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.65, task=val, device=, workers=8, single_cls=False, augment=False, verbose=False, save_txt=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=True, dnn=False
YOLOv5 🚀 v6.0-224-g4c40933 torch 1.10.0+cu111 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)

Fusing layers...
Model Summary: 444 layers, 86705005 parameters, 0 gradients
Pruning model...  0.3 global sparsity
val: Scanning '/content/datasets/coco/val2017.cache' images and labels... 4952 found, 48 missing, 0 empty, 0 corrupt: 100% 5000/5000 [00:00<?, ?it/s]
               Class     Images     Labels          P          R     mAP@.5 mAP@.5:.95: 100% 157/157 [01:11<00:00,  2.19it/s]
                 all       5000      36335      0.724      0.614      0.671      0.478
Speed: 0.1ms pre-process, 5.2ms inference, 1.7ms NMS per image at shape (32, 3, 640, 640)  # <--- prune speed

Evaluating pycocotools mAP... saving runs/val/exp-3/yolov5x_predictions.json...
...
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.489  # <--- prune mAP
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.677
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.537
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.334
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.542
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.635
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.370
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.612
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.664
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.496
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.722
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.803
Results saved to runs/val/exp-3

Link to this section結果の分析#

結果から以下のことが観察できます:

  • 30%のスパース性を達成nn.Conv2dレイヤーにおけるモデルの重みパラメータの30%がゼロになりました
  • 推論時間は変更なし:プルーニングを行っても、処理速度は本質的に変わりません
  • パフォーマンスへの影響は最小限:mAPが0.507から0.489へわずかに低下しました(削減率はわずか3.6%です)
  • モデルサイズの削減:プルーニングされたモデルは、ストレージに必要なメモリ容量が少なくなります

これは、プルーニングによってパフォーマンスへの影響を抑えながらモデルの複雑さを大幅に削減できることを示しており、リソースが制限された環境へのデプロイにおける効果的な最適化手法となります。

Link to this sectionプルーニングされたモデルのファインチューニング#

最良の結果を得るには、精度を回復させるためにプルーニング後にモデルをファインチューニングする必要があります。これは以下の手順で行えます:

  1. 希望するスパース性レベルでプルーニングを適用する
  2. より低い学習率を使用して、プルーニングされたモデルを数エポック分学習させる
  3. ファインチューニングされたプルーニングモデルをベースラインと比較評価する

このプロセスにより、残りのパラメータが削除された接続を補うように適応し、多くの場合、元の精度のほとんどまたはすべてを回復させることができます。

Link to this sectionサポートされている環境#

Ultralyticsは、プロジェクトを迅速に開始できるように、CUDACUDNNPythonPyTorch などの主要な依存関係が事前にインストールされた、すぐに使える環境を多数提供しています。

Link to this sectionプロジェクトの状態#

YOLOv5 CI

このバッジは、すべての YOLOv5 GitHub Actions 継続的インテグレーション(CI)テストが正常に合格していることを示します。これらのCIテストは、YOLOv5の機能とパフォーマンスをトレーニングバリデーション推論エクスポート、およびベンチマークといった様々な主要な観点から厳密にチェックします。これにより、macOS、Windows、Ubuntuで24時間ごと、および新しいコミットごとにテストが実施され、一貫した信頼性の高い動作が保証されます。

コントリビューター

コメント