Environment:VainF Torch Pruning YOLO Pruning Dependencies
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Object_Detection, Model_Compression |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
Ultralytics and related dependencies required for YOLO object detection model pruning workflows.
Description
This environment extends the core PyTorch environment with the Ultralytics library and related packages needed for the YOLO pruning examples. The examples/yolov8/yolov8_pruning.py script uses the Ultralytics YOLO API for model loading, training, validation, and export. The pruning pipeline replaces standard C2f modules with a pruning-compatible C2f_v2 variant and iteratively prunes and fine-tunes the model.
For YOLOv5 and YOLOv7 examples, the respective upstream repositories must be cloned separately, as they are not installable via pip.
Usage
Use this environment for the Object Detection Pruning workflow, specifically when:
- Pruning YOLOv8 models with iterative prune-finetune loops
- Exporting pruned models to ONNX format
- Generating pruning performance graphs (requires matplotlib)
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux (recommended) | Ultralytics supports Linux, macOS, Windows |
| Hardware | NVIDIA GPU | Training and fine-tuning require GPU |
| Disk | Varies | COCO dataset requires ~20GB |
Dependencies
Python Packages
torch>= 2.0numpyultralytics(YOLOv8 framework)torch-pruningmatplotlib(for performance graphs)onnx(for model export)
Credentials
No credentials required. COCO dataset is downloaded automatically by Ultralytics.
Quick Install
pip install torch>=2.0 numpy torch-pruning ultralytics matplotlib onnx
Code Evidence
Ultralytics imports from examples/yolov8/yolov8_pruning.py:1-15:
from ultralytics import YOLO, __version__
from ultralytics.nn.modules import C2f, Conv, Bottleneck, Detect
from ultralytics.engine.trainer import BaseTrainer
from ultralytics.nn.tasks import attempt_load_one_weight
YOLO model loading from examples/yolov8/yolov8_pruning.py:277:
model = YOLO(args.model)
ONNX export from examples/yolov8/yolov8_pruning.py:384:
model.export(format='onnx')
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
ModuleNotFoundError: No module named 'ultralytics' |
Ultralytics not installed | pip install ultralytics
|
AttributeError: 'C2f' has no attribute 'cv0' |
C2f_v2 replacement not applied | Ensure replace_c2f_with_c2f_v2() is called before pruning
|
FileNotFoundError: ... coco.yaml |
COCO dataset not found | Set data path in pruning config or let Ultralytics download automatically
|
Compatibility Notes
- YOLOv8 only: The pruning script uses Ultralytics-specific APIs. YOLOv5 and YOLOv7 have separate scripts that depend on their respective upstream repositories.
- C2f_v2 replacement: The standard Ultralytics C2f module fuses operations in a way that is incompatible with structural pruning. The C2f_v2 class splits the fused
cv1into separatecv0andcv1convolutions to allow proper channel pruning. - train_v2: The script patches the YOLO training loop to avoid saving in half-precision (which would lose the pruned architecture) and to support early stopping based on mAP drop.