Vai al contenuto

Esercitazione su potatura/parsimonia

📚 Questa guida spiega come applicare la potatura ai modelli YOLOv5 🚀.

Prima di iniziare

Clona il repo e installa il file requirements.txt in un file di tipo Python>=3.8.0 con l'inclusione di PyTorch>=1.8. I modelli e i dataset vengono scaricati automaticamente dall'ultimarelease di YOLOv5 .

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

Prova normalmente

Prima di procedere alla potatura, vogliamo stabilire una performance di base con cui confrontarci. Questo comando testa YOLOv5x su COCO val2017 con un'immagine di 640 pixel. yolov5x.pt è il modello più grande e accurato disponibile. Le altre opzioni sono yolov5s.pt, yolov5m.pt e yolov5l.ptoppure il tuo checkpoint dall'addestramento di un set di dati personalizzato ./weights/best.pt. Per maggiori dettagli su tutti i modelli disponibili, consulta il nostro README. Tavolo.

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

Uscita:

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

Prova YOLOv5x su COCO (0,30 sparsity)

Ripetiamo il test di cui sopra con un modello ridotto utilizzando l'opzione torch_utils.prune() comando. Aggiorniamo val.py per ridurre la sparsità di YOLOv5x a 0,3:

Schermata 2022-02-02 alle 22 54 18

30% di potatura:

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

Nei risultati si può osservare che abbiamo ottenuto un sparsità del 30% nel nostro modello dopo la potatura, il che significa che il 30% dei parametri di peso del modello in nn.Conv2d sono uguali a 0. Il tempo di inferenza è sostanzialmente invariato, mentre il modello I punteggi AP e AR sono leggermente ridotti.

Ambienti supportati

Ultralytics offre una serie di ambienti pronti all'uso, ciascuno preinstallato con le dipendenze essenziali come CUDA, CUDNN, Python, e PyTorchper dare il via ai tuoi progetti.

Stato del progetto

YOLOv5 CI

Questo badge indica che tutti i test di YOLOv5 GitHub Actions Continuous Integration (CI) sono stati superati con successo. Questi test CI verificano rigorosamente la funzionalità e le prestazioni di YOLOv5 in vari aspetti chiave: formazione, validazione, inferenza, esportazione e benchmark. Assicurano un funzionamento coerente e affidabile su macOS, Windows e Ubuntu, con test condotti ogni 24 ore e su ogni nuovo commit.



Creato 2023-11-12, Aggiornato 2023-12-03
Autori: glenn-jocher (2)

Commenti