microsoft / nni

An open source AutoML toolkit for automate machine learning lifecycle, including feature engineering, neural architecture search, model compression and hyper-parameter tuning.
https://nni.readthedocs.io
MIT License
13.88k stars 1.81k forks source link

Pruning Yolov8m model #5742

Open hafeelnm19 opened 4 months ago

hafeelnm19 commented 4 months ago

Describe the issue:

Environment:

Configuration:

Log message:

How to reproduce it?:

import torch

from nni.common.concrete_trace_utils import concrete_trace
from nni.compression.pruning import L1NormPruner
from nni.compression.utils import auto_set_denpendency_group_ids
from nni.compression.speedup import ModelSpeedup

from ultralytics import YOLO

model = YOLO("/content/yolov8n.pt")

model(torch.rand([1, 3, 640, 640]))

config_list = [{
    'sparsity': 0.5,
    'op_types': ['Conv2d'],
    'exclude_op_names_re': ['model.model.model.24.*'],  # this layer is detector head
}]

config_list = auto_set_denpendency_group_ids(model, config_list, torch.rand([1, 3, 640, 640]))

pruner = L1NormPruner(model, config_list)
masked_model, masks = pruner.compress()
pruner.unwrap_model()

graph_module = concrete_trace(model, (torch.rand([1, 3, 640, 640]), None, None, None))
ModelSpeedup(model, torch.rand([1, 3, 640, 640]), masks, graph_module=graph_module).speedup_model()

model(torch.rand([1, 3, 640, 640])

i tried to prune yolov8m model using the same code which is used to prune yolov5s model. But it is retraining in "config_list = auto_set_denpendency_group_ids(model, config_list, torch.rand([1, 3, 640, 640]))" this step and giving error like

/usr/local/lib/python3.10/dist-packages/ultralytics/data/loaders.py:456: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if im.shape[2] % stride or im.shape[3] % stride:
/usr/local/lib/python3.10/dist-packages/ultralytics/data/loaders.py:458: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if im.max() > 1.0 + torch.finfo(im.dtype).eps:  # torch.float32 eps is 1.2e-07
/usr/local/lib/python3.10/dist-packages/ultralytics/data/loaders.py:442: TracerWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).
  self.paths = [getattr(im, "filename", f"image{i}.jpg") for i, im in enumerate(im0)]
/usr/local/lib/python3.10/dist-packages/ultralytics/nn/modules/head.py:53: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if self.dynamic or self.shape != shape:
/usr/local/lib/python3.10/dist-packages/ultralytics/utils/ops.py:408: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  assert x.shape[-1] == 4, f"input shape last dimension expected 4 but input shape is {x.shape}"
/usr/local/lib/python3.10/dist-packages/ultralytics/utils/ops.py:231: TracerWarning: Iterating over a tensor might cause the trace to be incorrect. Passing a tensor of different shape won't change the number of iterations executed (and might lead to errors or silently give incorrect results).
  for xi, x in enumerate(prediction):  # image index, image inference
/usr/local/lib/python3.10/dist-packages/ultralytics/utils/ops.py:245: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  if not x.shape[0]:
/usr/local/lib/python3.10/dist-packages/ultralytics/utils/ops.py:829: TracerWarning: Converting a tensor to a NumPy array might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  return (batch.permute(0, 2, 3, 1).contiguous() * 255).clamp(0, 255).to(torch.uint8).cpu().numpy()
/usr/local/lib/python3.10/dist-packages/ultralytics/utils/ops.py:108: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[<ipython-input-8-a46380595ef0>](https://localhost:8080/#) in <cell line: 20>()
     18 }]
     19 
---> 20 config_list = auto_set_denpendency_group_ids(model, config_list, torch.rand([1, 3, 640, 640]))
     21 
     22 pruner = L1NormPruner(model, config_list)

14 frames
[/usr/local/lib/python3.10/dist-packages/ultralytics/utils/ops.py](https://localhost:8080/#) in scale_boxes(img1_shape, boxes, img0_shape, ratio_pad, padding, xywh)
    108         gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
    109         pad = (
--> 110             round((img1_shape[1] - img0_shape[1] * gain) / 2 - 0.1),
    111             round((img1_shape[0] - img0_shape[0] * gain) / 2 - 0.1),
    112         )  # wh padding

TypeError: type Tensor doesn't define __round__ method
Gooddz1 commented 4 months ago

请问,问题解决了吗