ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.24k stars 16.44k forks source link

Yolov5 pruning throws error? #6514

Closed RizwanMunawar closed 2 years ago

RizwanMunawar commented 2 years ago

Search before asking

YOLOv5 Component

Other

Bug

I want to prune yolov5m custom trained. I followed yolov5 pruning/sparsity tutorial. When I implemented this, I face below mentioned error,

"ZeroDivisionError: division by zero"

anydesk00001

Environment

YOLO : Yolov5 OS : Ubuntu 20.04 Python : python 3.7

Minimal Reproducible Example

I have called below mentioned code, after model load in val.py.

    if training:  # called by train.py
      device, pt, jit, engine = next(model.parameters()).device, True, False, False  # get model device, PyTorch model
      half &= device.type != 'cpu'  # half precision only supported on CUDA
      model.half() if half else model.float()

   else:  # called directly
     device = select_device(device, batch_size=batch_size)

    `from utils.torch_utils import prune`     ------------------------------> Pruning---------------------
    `prune(model,0.3)`

    # Directories
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir

    # Load model
    model = DetectMultiBackend(weights, device=device, dnn=dnn)

Additional

No response

Are you willing to submit a PR?

glenn-jocher commented 2 years ago

@RizwanMunawar I just re-ran the Pruning/Sparsity tutorial and everything works correctly. I updated it to current master branch and results. See https://docs.ultralytics.com/yolov5/tutorials/model_pruning_and_sparsity

Screenshot 2022-02-02 at 22 55 51
glenn-jocher commented 2 years ago

@RizwanMunawar BTW there may be better methods for pruning PyTorch models including QAT that we have not had time to investigate. If you find better implementations please let us know so we can update the tutorial.

RizwanMunawar commented 2 years ago

Thanks @glenn-jocher

github-actions[bot] commented 2 years ago

πŸ‘‹ Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 πŸš€ resources:

Access additional Ultralytics ⚑ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 πŸš€ and Vision AI ⭐!

HankYe commented 1 year ago

Search before asking

  • [x] I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Other

Bug

I want to prune yolov5m custom trained. I followed yolov5 pruning/sparsity tutorial. When I implemented this, I face below mentioned error,

"ZeroDivisionError: division by zero"

anydesk00001

Environment

YOLO : Yolov5 OS : Ubuntu 20.04 Python : python 3.7

Minimal Reproducible Example

I have called below mentioned code, after model load in val.py.

    if training:  # called by train.py
      device, pt, jit, engine = next(model.parameters()).device, True, False, False  # get model device, PyTorch model
      half &= device.type != 'cpu'  # half precision only supported on CUDA
      model.half() if half else model.float()

   else:  # called directly
     device = select_device(device, batch_size=batch_size)

    `from utils.torch_utils import prune`     ------------------------------> Pruning---------------------
    `prune(model,0.3)`

    # Directories
    save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
    (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True)  # make dir

    # Load model
    model = DetectMultiBackend(weights, device=device, dnn=dnn)

Additional

No response

Are you willing to submit a PR?

  • [ ] Yes I'd like to help by submitting a PR!

Hi @RizwanMunawar, I have successfully pruned YOLOv5 of multiple scales. Maybe you can also refer to https://github.com/HankYe/PAGCP to get more helpful information. Hope my experiences can give you some help.