コンテンツへスキップ

刈り込み/スパース・チュートリアル

📚 このガイドでは、YOLOv5 🚀 モデルにプルーニングを適用する方法を説明します。

始める前に

レポをクローンし、requirements.txtをPython>=3.8.0環境にインストールする。 PyTorch>=1.8.モデルとデータセットは、最新のYOLOv5 リリースから自動的にダウンロードされます。

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

通常テスト

プルーニングの前に、比較するためのベースライン・パフォーマンスを確立したい。このコマンドは、画像サイズ640ピクセルのCOCO val2017でYOLOv5xをテストする。 yolov5x.pt が最も大きく、最も正確なモデルである。その他のオプションは yolov5s.pt, yolov5m.pt そして yolov5l.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_hybrid=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/exp2/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

COCOでYOLOv5xをテスト(疎密度0.30)

を使い、枝刈りしたモデルで上記のテストを繰り返す。 torch_utils.prune() コマンド我々は val.py YOLOv5xを0.3スパースリティまで削減する:

スクリーンショット 2022-02-02 at 22 54 18

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_hybrid=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 mAP

Evaluating pycocotools mAP... saving runs/val/exp3/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/exp3

その結果、我々は次のような結果を得ることができた。 30%の疎らさ つまり、プルーニング後のモデルでは、モデルのウェイト・パラメータの30%が nn.Conv2d 層は0に等しい。 推論時間は基本的に変わらない一方、モデルの APとARのスコアが若干低下.

対応環境

Ultralytics は、CUDAや CUDNNといった必要不可欠な依存関係がプリインストールされた、すぐに使えるさまざまな環境を提供します、 Pythonそして PyTorchなどの不可欠な依存関係がプリインストールされています。

プロジェクト状況

YOLOv5 CI

このバッジは、YOLOv5 GitHub ActionsContinuous Integration (CI) テストがすべて正常にパスしていることを示します。これらのCIテストは、トレーニング検証推論エクスポートベンチマークといったさまざまな重要な側面にわたって、YOLOv5 の機能とパフォーマンスを厳密にチェックします。これらのテストは、macOS、Windows、Ubuntu上で、24時間ごとおよび新しいコミットごとに実施され、一貫した信頼性の高い動作を保証します。



作成日:2023-11-12 更新日:2023-12-03
作成者:glenn-jocher(2)

コメント