nni
nni copied to clipboard
Pruning Yolov8m model
Describe the issue:
Environment:
- NNI version:
- Training service (local|remote|pai|aml|etc):
- Client OS:
- Server OS (for remote mode only):
- Python version:
- PyTorch/TensorFlow version:
- Is conda/virtualenv/venv used?:
- Is running in Docker?:
Configuration:
- Experiment config (remember to remove secrets!):
- Search space:
Log message:
- nnimanager.log:
- dispatcher.log:
- nnictl stdout and stderr:
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
请问,问题解决了吗