Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytorch to onnx #26

Open
chenzx2 opened this issue Aug 8, 2023 · 53 comments
Open

pytorch to onnx #26

chenzx2 opened this issue Aug 8, 2023 · 53 comments

Comments

@chenzx2
Copy link

chenzx2 commented Aug 8, 2023

/home/aigroup/chenzx/ws_internImage/bin/python3.8 /home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/init.py:20: UserWarning: On January 1, 2023, MMCV will release v2.0.0, in which it will remove components related to the training process and add a data transformation module. In addition, it will rename the package names mmcv to mmcv-lite and mmcv-full to mmcv. See https://github.com/open-mmlab/mmcv/blob/master/docs/en/compatibility.md for more details.
warnings.warn(
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py:284: UserWarning: Arguments like --mean, --std, --dataset would be parsed directly from config file and are deprecated and will be removed in future releases.
warnings.warn('Arguments like --mean, --std, --dataset would be
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/onnx/symbolic.py:481: UserWarning: DeprecationWarning: This function will be deprecated in future. Welcome to use the unified model deployment toolbox MMDeploy: https://github.com/open-mmlab/mmdeploy
warnings.warn(msg)
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3483.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
2023-08-08 16:30:39,106 - mmcv - INFO - initialize RPNHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01}
2023-08-08 16:30:39,108 - mmcv - INFO -
rpn_conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,108 - mmcv - INFO -
rpn_conv.bias - torch.Size([256]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,108 - mmcv - INFO -
rpn_cls.weight - torch.Size([9, 256, 1, 1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,108 - mmcv - INFO -
rpn_cls.bias - torch.Size([9]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,108 - mmcv - INFO -
rpn_reg.weight - torch.Size([36, 256, 1, 1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,108 - mmcv - INFO -
rpn_reg.bias - torch.Size([36]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,185 - mmcv - INFO - initialize Shared2FCBBoxHead with init_cfg [{'type': 'Normal', 'std': 0.01, 'override': {'name': 'fc_cls'}}, {'type': 'Normal', 'std': 0.001, 'override': {'name': 'fc_reg'}}, {'type': 'Xavier', 'distribution': 'uniform', 'override': [{'name': 'shared_fcs'}, {'name': 'cls_fcs'}, {'name': 'reg_fcs'}]}]
2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.fc_cls.weight - torch.Size([81, 1024]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.fc_cls.bias - torch.Size([81]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.fc_reg.weight - torch.Size([320, 1024]):
NormalInit: mean=0, std=0.001, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.fc_reg.bias - torch.Size([320]):
NormalInit: mean=0, std=0.001, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.shared_fcs.0.weight - torch.Size([1024, 12544]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.shared_fcs.0.bias - torch.Size([1024]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.shared_fcs.1.weight - torch.Size([1024, 1024]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 16:30:39,236 - mmcv - INFO -
bbox_head.shared_fcs.1.bias - torch.Size([1024]):
XavierInit: gain=1, distribution=uniform, bias=0

/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/anchor_head.py:116: UserWarning: DeprecationWarning: num_anchors is deprecated, for consistency or also use num_base_priors instead
warnings.warn('DeprecationWarning: num_anchors is deprecated, '
2023-08-08 16:30:39,248 - mmcv - INFO - initialize CoATSSHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01, 'override': {'type': 'Normal', 'name': 'atss_cls', 'std': 0.01, 'bias_prob': 0.01}}
2023-08-08 16:30:39,255 - mmcv - INFO -
cls_convs.0.conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,255 - mmcv - INFO -
cls_convs.0.gn.weight - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
cls_convs.0.gn.bias - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
reg_convs.0.conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,255 - mmcv - INFO -
reg_convs.0.gn.weight - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
reg_convs.0.gn.bias - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
atss_cls.weight - torch.Size([80, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=-4.59511985013459

2023-08-08 16:30:39,255 - mmcv - INFO -
atss_cls.bias - torch.Size([80]):
NormalInit: mean=0, std=0.01, bias=-4.59511985013459

2023-08-08 16:30:39,255 - mmcv - INFO -
atss_reg.weight - torch.Size([4, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,255 - mmcv - INFO -
atss_reg.bias - torch.Size([4]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,255 - mmcv - INFO -
atss_centerness.weight - torch.Size([1, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,255 - mmcv - INFO -
atss_centerness.bias - torch.Size([1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 16:30:39,255 - mmcv - INFO -
scales.0.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
scales.1.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
scales.2.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
scales.3.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
scales.4.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 16:30:39,255 - mmcv - INFO -
scales.5.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

load checkpoint from local path: /home/aigroup/chenzx/ws_internImage/code/Co-DETR/model/co_dino_5scale_swin_large_3x_coco.pth
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:423: 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 W % self.patch_size[1] != 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:425: 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 H % self.patch_size[0] != 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:362: TracerWarning: Converting a tensor to a Python integer 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!
Hp = int(np.ceil(H / self.window_size)) * self.window_size
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:363: TracerWarning: Converting a tensor to a Python integer 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!
Wp = int(np.ceil(W / self.window_size)) * self.window_size
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:203: 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 L == H * W, "input feature has wrong size"
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:66: TracerWarning: Converting a tensor to a Python integer 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!
B = int(windows.shape[0] / (H * W / window_size / window_size))
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:241: 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 pad_r > 0 or pad_b > 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:272: 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 L == H * W, "input feature has wrong size"
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:277: 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!
pad_input = (H % 2 == 1) or (W % 2 == 1)
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:278: 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 pad_input:
============= Diagnostic Run torch.onnx.export version 2.0.0+cu117 =============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================

Traceback (most recent call last):
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py", line 320, in
pytorch2onnx(
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py", line 90, in pytorch2onnx
torch.onnx.export(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 506, in export
_export(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 1548, in _export
graph, params_dict, torch_out = _model_to_graph(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 1113, in _model_to_graph
graph, params, torch_out, module = _create_jit_graph(model, args)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 989, in _create_jit_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 893, in _trace_and_get_graph_from_model
trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 1274, in _get_trace_graph
outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 133, in forward
graph, out = torch._C._create_graph_by_tracing(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 124, in wrapper
outs.append(self.inner(*trace_inputs))
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1488, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/runner/fp16_utils.py", line 119, in new_func
return old_func(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/detectors/base.py", line 169, in forward
return self.onnx_export(img[0], img_metas[0])
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/detectors/co_detr.py", line 382, in onnx_export
outs = self.query_head(x)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1488, in _slow_forward
result = self.forward(*input, **kwargs)
TypeError: forward() missing 1 required positional argument: 'img_metas'

@TempleX98
Copy link
Collaborator

I have modified the onnx_export function, you can try it again

@chenzx2
Copy link
Author

chenzx2 commented Aug 8, 2023

I have modified the onnx_export function, you can try it again

the problem is solved,there is a bug

/home/aigroup/chenzx/ws_internImage/bin/python3.8 /home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/init.py:20: UserWarning: On January 1, 2023, MMCV will release v2.0.0, in which it will remove components related to the training process and add a data transformation module. In addition, it will rename the package names mmcv to mmcv-lite and mmcv-full to mmcv. See https://github.com/open-mmlab/mmcv/blob/master/docs/en/compatibility.md for more details.
warnings.warn(
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py:284: UserWarning: Arguments like --mean, --std, --dataset would be parsed directly from config file and are deprecated and will be removed in future releases.
warnings.warn('Arguments like --mean, --std, --dataset would be
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/onnx/symbolic.py:481: UserWarning: DeprecationWarning: This function will be deprecated in future. Welcome to use the unified model deployment toolbox MMDeploy: https://github.com/open-mmlab/mmdeploy
warnings.warn(msg)
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3483.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
2023-08-08 17:01:24,154 - mmcv - INFO - initialize RPNHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01}
2023-08-08 17:01:24,156 - mmcv - INFO -
rpn_conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,156 - mmcv - INFO -
rpn_conv.bias - torch.Size([256]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,156 - mmcv - INFO -
rpn_cls.weight - torch.Size([9, 256, 1, 1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,156 - mmcv - INFO -
rpn_cls.bias - torch.Size([9]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,156 - mmcv - INFO -
rpn_reg.weight - torch.Size([36, 256, 1, 1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,156 - mmcv - INFO -
rpn_reg.bias - torch.Size([36]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,232 - mmcv - INFO - initialize Shared2FCBBoxHead with init_cfg [{'type': 'Normal', 'std': 0.01, 'override': {'name': 'fc_cls'}}, {'type': 'Normal', 'std': 0.001, 'override': {'name': 'fc_reg'}}, {'type': 'Xavier', 'distribution': 'uniform', 'override': [{'name': 'shared_fcs'}, {'name': 'cls_fcs'}, {'name': 'reg_fcs'}]}]
2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.fc_cls.weight - torch.Size([81, 1024]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.fc_cls.bias - torch.Size([81]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.fc_reg.weight - torch.Size([320, 1024]):
NormalInit: mean=0, std=0.001, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.fc_reg.bias - torch.Size([320]):
NormalInit: mean=0, std=0.001, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.shared_fcs.0.weight - torch.Size([1024, 12544]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.shared_fcs.0.bias - torch.Size([1024]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.shared_fcs.1.weight - torch.Size([1024, 1024]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 17:01:24,283 - mmcv - INFO -
bbox_head.shared_fcs.1.bias - torch.Size([1024]):
XavierInit: gain=1, distribution=uniform, bias=0

/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/anchor_head.py:116: UserWarning: DeprecationWarning: num_anchors is deprecated, for consistency or also use num_base_priors instead
warnings.warn('DeprecationWarning: num_anchors is deprecated, '
2023-08-08 17:01:24,294 - mmcv - INFO - initialize CoATSSHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01, 'override': {'type': 'Normal', 'name': 'atss_cls', 'std': 0.01, 'bias_prob': 0.01}}
2023-08-08 17:01:24,299 - mmcv - INFO -
cls_convs.0.conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,299 - mmcv - INFO -
cls_convs.0.gn.weight - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
cls_convs.0.gn.bias - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
reg_convs.0.conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,299 - mmcv - INFO -
reg_convs.0.gn.weight - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
reg_convs.0.gn.bias - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
atss_cls.weight - torch.Size([80, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=-4.59511985013459

2023-08-08 17:01:24,299 - mmcv - INFO -
atss_cls.bias - torch.Size([80]):
NormalInit: mean=0, std=0.01, bias=-4.59511985013459

2023-08-08 17:01:24,299 - mmcv - INFO -
atss_reg.weight - torch.Size([4, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,299 - mmcv - INFO -
atss_reg.bias - torch.Size([4]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,299 - mmcv - INFO -
atss_centerness.weight - torch.Size([1, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,299 - mmcv - INFO -
atss_centerness.bias - torch.Size([1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:01:24,299 - mmcv - INFO -
scales.0.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
scales.1.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
scales.2.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
scales.3.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
scales.4.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:01:24,299 - mmcv - INFO -
scales.5.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

load checkpoint from local path: /home/aigroup/chenzx/ws_internImage/code/Co-DETR/model/co_dino_5scale_swin_large_3x_coco.pth
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:423: 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 W % self.patch_size[1] != 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:425: 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 H % self.patch_size[0] != 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:362: TracerWarning: Converting a tensor to a Python integer 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!
Hp = int(np.ceil(H / self.window_size)) * self.window_size
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:363: TracerWarning: Converting a tensor to a Python integer 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!
Wp = int(np.ceil(W / self.window_size)) * self.window_size
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:203: 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 L == H * W, "input feature has wrong size"
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:66: TracerWarning: Converting a tensor to a Python integer 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!
B = int(windows.shape[0] / (H * W / window_size / window_size))
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:241: 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 pad_r > 0 or pad_b > 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:272: 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 L == H * W, "input feature has wrong size"
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:277: 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!
pad_input = (H % 2 == 1) or (W % 2 == 1)
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:278: 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 pad_input:
Traceback (most recent call last):
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py", line 320, in
pytorch2onnx(
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py", line 90, in pytorch2onnx
torch.onnx.export(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 506, in export
_export(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 1548, in _export
graph, params_dict, torch_out = _model_to_graph(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 1113, in _model_to_graph
graph, params, torch_out, module = _create_jit_graph(model, args)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 989, in _create_jit_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 893, in _trace_and_get_graph_from_model
trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 1274, in _get_trace_graph
outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 133, in forward
graph, out = torch._C._create_graph_by_tracing(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 124, in wrapper
outs.append(self.inner(*trace_inputs))
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1488, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/runner/fp16_utils.py", line 119, in new_func
return old_func(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/detectors/base.py", line 169, in forward
return self.onnx_export(img[0], img_metas[0])
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/detectors/co_detr.py", line 382, in onnx_export
outs = self.query_head.forward_onnx(x, img_metas)[:2]
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/detr_head.py", line 724, in forward_onnx
return multi_apply(self.forward_single_onnx, feats, img_metas_list)
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/core/utils/misc.py", line 30, in multi_apply
return tuple(map(list, zip(*map_results)))
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/detr_head.py", line 753, in forward_single_onnx
x = self.input_proj(x)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1614, in getattr
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'CoDINOHead' object has no attribute 'input_proj'
============= Diagnostic Run torch.onnx.export version 2.0.0+cu117 =============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================

@chenzx2
Copy link
Author

chenzx2 commented Aug 8, 2023

I fixed it like this
企业微信截图_16914854619898

here is my new problem

/home/aigroup/chenzx/ws_internImage/bin/python3.8 /home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/init.py:20: UserWarning: On January 1, 2023, MMCV will release v2.0.0, in which it will remove components related to the training process and add a data transformation module. In addition, it will rename the package names mmcv to mmcv-lite and mmcv-full to mmcv. See https://github.com/open-mmlab/mmcv/blob/master/docs/en/compatibility.md for more details.
warnings.warn(
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py:284: UserWarning: Arguments like --mean, --std, --dataset would be parsed directly from config file and are deprecated and will be removed in future releases.
warnings.warn('Arguments like --mean, --std, --dataset would be
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/onnx/symbolic.py:481: UserWarning: DeprecationWarning: This function will be deprecated in future. Welcome to use the unified model deployment toolbox MMDeploy: https://github.com/open-mmlab/mmdeploy
warnings.warn(msg)
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3483.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
2023-08-08 17:02:19,967 - mmcv - INFO - initialize RPNHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01}
2023-08-08 17:02:19,969 - mmcv - INFO -
rpn_conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:19,969 - mmcv - INFO -
rpn_conv.bias - torch.Size([256]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:19,969 - mmcv - INFO -
rpn_cls.weight - torch.Size([9, 256, 1, 1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:19,969 - mmcv - INFO -
rpn_cls.bias - torch.Size([9]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:19,969 - mmcv - INFO -
rpn_reg.weight - torch.Size([36, 256, 1, 1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:19,969 - mmcv - INFO -
rpn_reg.bias - torch.Size([36]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,044 - mmcv - INFO - initialize Shared2FCBBoxHead with init_cfg [{'type': 'Normal', 'std': 0.01, 'override': {'name': 'fc_cls'}}, {'type': 'Normal', 'std': 0.001, 'override': {'name': 'fc_reg'}}, {'type': 'Xavier', 'distribution': 'uniform', 'override': [{'name': 'shared_fcs'}, {'name': 'cls_fcs'}, {'name': 'reg_fcs'}]}]
2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.fc_cls.weight - torch.Size([81, 1024]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.fc_cls.bias - torch.Size([81]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.fc_reg.weight - torch.Size([320, 1024]):
NormalInit: mean=0, std=0.001, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.fc_reg.bias - torch.Size([320]):
NormalInit: mean=0, std=0.001, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.shared_fcs.0.weight - torch.Size([1024, 12544]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.shared_fcs.0.bias - torch.Size([1024]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.shared_fcs.1.weight - torch.Size([1024, 1024]):
XavierInit: gain=1, distribution=uniform, bias=0

2023-08-08 17:02:20,095 - mmcv - INFO -
bbox_head.shared_fcs.1.bias - torch.Size([1024]):
XavierInit: gain=1, distribution=uniform, bias=0

/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/anchor_head.py:116: UserWarning: DeprecationWarning: num_anchors is deprecated, for consistency or also use num_base_priors instead
warnings.warn('DeprecationWarning: num_anchors is deprecated, '
2023-08-08 17:02:20,106 - mmcv - INFO - initialize CoATSSHead with init_cfg {'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01, 'override': {'type': 'Normal', 'name': 'atss_cls', 'std': 0.01, 'bias_prob': 0.01}}
2023-08-08 17:02:20,111 - mmcv - INFO -
cls_convs.0.conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,111 - mmcv - INFO -
cls_convs.0.gn.weight - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,111 - mmcv - INFO -
cls_convs.0.gn.bias - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,111 - mmcv - INFO -
reg_convs.0.conv.weight - torch.Size([256, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,111 - mmcv - INFO -
reg_convs.0.gn.weight - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,111 - mmcv - INFO -
reg_convs.0.gn.bias - torch.Size([256]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,111 - mmcv - INFO -
atss_cls.weight - torch.Size([80, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=-4.59511985013459

2023-08-08 17:02:20,111 - mmcv - INFO -
atss_cls.bias - torch.Size([80]):
NormalInit: mean=0, std=0.01, bias=-4.59511985013459

2023-08-08 17:02:20,111 - mmcv - INFO -
atss_reg.weight - torch.Size([4, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,111 - mmcv - INFO -
atss_reg.bias - torch.Size([4]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,111 - mmcv - INFO -
atss_centerness.weight - torch.Size([1, 256, 3, 3]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,111 - mmcv - INFO -
atss_centerness.bias - torch.Size([1]):
NormalInit: mean=0, std=0.01, bias=0

2023-08-08 17:02:20,111 - mmcv - INFO -
scales.0.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,111 - mmcv - INFO -
scales.1.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,112 - mmcv - INFO -
scales.2.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,112 - mmcv - INFO -
scales.3.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,112 - mmcv - INFO -
scales.4.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

2023-08-08 17:02:20,112 - mmcv - INFO -
scales.5.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

load checkpoint from local path: /home/aigroup/chenzx/ws_internImage/code/Co-DETR/model/co_dino_5scale_swin_large_3x_coco.pth
The model and loaded state dict do not match exactly

missing keys in source state_dict: query_head.input_proj.weight, query_head.input_proj.bias, query_head.fc_cls.weight, query_head.fc_cls.bias, query_head.reg_ffn.layers.0.0.weight, query_head.reg_ffn.layers.0.0.bias, query_head.reg_ffn.layers.1.weight, query_head.reg_ffn.layers.1.bias, query_head.fc_reg.weight, query_head.fc_reg.bias

/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:423: 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 W % self.patch_size[1] != 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:425: 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 H % self.patch_size[0] != 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:362: TracerWarning: Converting a tensor to a Python integer 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!
Hp = int(np.ceil(H / self.window_size)) * self.window_size
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:363: TracerWarning: Converting a tensor to a Python integer 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!
Wp = int(np.ceil(W / self.window_size)) * self.window_size
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:203: 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 L == H * W, "input feature has wrong size"
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:66: TracerWarning: Converting a tensor to a Python integer 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!
B = int(windows.shape[0] / (H * W / window_size / window_size))
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:241: 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 pad_r > 0 or pad_b > 0:
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:272: 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 L == H * W, "input feature has wrong size"
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:277: 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!
pad_input = (H % 2 == 1) or (W % 2 == 1)
/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/swin_transformer.py:278: 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 pad_input:
/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/cnn/bricks/wrappers.py:45: 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 x.numel() == 0 and obsolete_torch_version(TORCH_VERSION, (1, 4)):
Traceback (most recent call last):
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py", line 320, in
pytorch2onnx(
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/tools/deployment/pytorch2onnx.py", line 90, in pytorch2onnx
torch.onnx.export(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 506, in export
_export(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 1548, in _export
graph, params_dict, torch_out = _model_to_graph(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 1113, in _model_to_graph
graph, params, torch_out, module = _create_jit_graph(model, args)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 989, in _create_jit_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/onnx/utils.py", line 893, in _trace_and_get_graph_from_model
trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 1274, in _get_trace_graph
outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 133, in forward
graph, out = torch._C._create_graph_by_tracing(
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/jit/_trace.py", line 124, in wrapper
outs.append(self.inner(*trace_inputs))
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1488, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/runner/fp16_utils.py", line 119, in new_func
return old_func(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/detectors/base.py", line 169, in forward
return self.onnx_export(img[0], img_metas[0])
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/detectors/co_detr.py", line 382, in onnx_export
outs = self.query_head.forward_onnx(x, img_metas)[:2]
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/detr_head.py", line 724, in forward_onnx
return multi_apply(self.forward_single_onnx, feats, img_metas_list)
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/core/utils/misc.py", line 30, in multi_apply
return tuple(map(list, zip(*map_results)))
File "/home/aigroup/chenzx/ws_internImage/code/Co-DETR/mmdet/models/dense_heads/detr_head.py", line 753, in forward_single_onnx
x = self.input_proj(x)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1488, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/mmcv/cnn/bricks/wrappers.py", line 59, in forward
return super().forward(x)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 463, in forward
return self._conv_forward(input, self.weight, self.bias)
File "/home/aigroup/chenzx/ws_internImage/lib/python3.8/site-packages/torch/nn/modules/conv.py", line 459, in _conv_forward
return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Given groups=1, weight of size [256, 2048, 1, 1], expected input[1, 256, 200, 304] to have 2048 channels, but got 256 channels instead
============= Diagnostic Run torch.onnx.export version 2.0.0+cu117 =============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================

@jielanZhang
Copy link

Is this problem solved?

@chenzx2
Copy link
Author

chenzx2 commented Sep 6, 2023

Is this problem solved?

没有解决,现在看到工程项目又更新了,我找个时间更新项目再试试

@xueyingliu
Copy link

xueyingliu commented Sep 6, 2023

Is this problem solved?

没有解决,现在看到工程项目又更新了,我找个时间更新项目再试试

@TempleX98
更新了以后还是有这个问题 TAT

return F.conv2d(input, weight, bias, self.stride,
RuntimeError: Given groups=1, weight of size [256, 2048, 1, 1], expected input[1, 256, 200, 304] to have 2048 channels, but got 256 channels instead

@TempleX98
Copy link
Collaborator

@xueyingliu, @chenzx2, @jielanZhang, Hi, I am sorry that there are some unsolved torch2onnx issues. Our repo is implemented using an older version of mmdet v2.25, which no longer maintains the feature of model export. Co-DETR has been recently incorporated into the official mmdet v3.x repo, and you can use this official implementation as well as MMDeploy for model export.

@MarouaneMja
Copy link

@TempleX98 I have tried your suggestion but I encounter some problems but when verify the documentation in mmdeploy, I found that CO-DETR is not supported, here is the list of the supported models https://github.com/open-mmlab/mmdeploy/blob/main/docs/en/03-benchmark/supported_models.md

@MarouaneMja
Copy link

Hello,

Any updates about the model exportation to onnx , please ?

@TempleX98
Copy link
Collaborator

@MarouaneMja Hi, I find this PR (open-mmlab/mmdetection#10910) of mmdet v3.x supports onnx export and hope this can help you.

@MarouaneMja
Copy link

Hi @TempleX98 , thank you I will look it up

@Mayyyybe
Copy link

Mayyyybe commented Nov 8, 2023

Any updates? Does MMdeploy already support ?

@TempleX98
Copy link
Collaborator

@Mayyyybe The inference architecture of Co-DINO is the same as DINO. And MMdeploy supports the model export of DINO method.

@MarouaneMja
Copy link

Hi @TempleX98 , I managed to export the CO-DETR to onnx using mmdeploy as you suggested, however the SoftNonMaxSuppression is not supported by onnxruntime failed:Fatal error: SoftNonMaxSuppression is not a registered function/op, do you have any suggestion how to bypass this problem?

@TempleX98
Copy link
Collaborator

Hi @TempleX98 , I managed to export the CO-DETR to onnx using mmdeploy as you suggested, however the SoftNonMaxSuppression is not supported by onnxruntime failed:Fatal error: SoftNonMaxSuppression is not a registered function/op, do you have any suggestion how to bypass this problem?

Just remove the NMS operation in the config.

@MarouaneMja
Copy link

Thank you for your help @TempleX98 , it worked. However when I launch infernce with triton inference server , the onnx model takes more space in gpu memory than using a simple Python backend which very stange lolll

@xinlin-xiao
Copy link

嗨,我设法按照您的建议使用 mmdeploy 将 CO-DETR 导出到 onnx,但是 onnxruntime 不支持 SoftNonMaxSuppression ,您有什么建议如何绕过这个问题吗?failed:Fatal error: SoftNonMaxSuppression is not a registered function/op
Hello, I am also trying to export CO-DETR to onnx using mmdeploy.
I would like to ask which model_cfg you are using is“https://github.com/RunningLeon/mmdetection/blob/support_dino_onnx/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py
or ”https://github.com/Sense-X/Co-DETR/blob/main/projects/configs/co_dino/co_dino_5scale_swin_large_16e_o365tococo.py“
And could you please share the deploy_cfg for me to learn it.I would be very grateful if you could help me!

@MarouaneMja
Copy link

@xinlin-xiao I am using the first config https://github.com/RunningLeon/mmdetection/blob/support_dino_onnx/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py “, for the deploy_config I am using "detection_onnxruntime_dynamic.py" in mmdeploy

@xinlin-xiao
Copy link

@xinlin-xiao I am using the first config https://github.com/RunningLeon/mmdetection/blob/support_dino_onnx/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py “, for the deploy_config I am using "detection_onnxruntime_dynamic.py" in mmdeploy

Thank you for your help!But,I trying to export CO-DETR to onnx using mmdeploy in this:
python3 /root/workspace/mmdeploy/tools/deploy.py \ /root/workspace/mmdeploy/configs/mmdet/detection/detection_onnxruntime_dynamic.py\ /mnt/data/train-yolov7-images/pytorch2onn-mmdeploy/co_dino-new/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py \ /mnt/data/train-yolov7-images/pytorch2onn-mmdeploy/co_dino_5scale_swin_large_16e_o365tococo.pth\ /root/workspace/mmdetection-support_dino_onnx/demo/demo.jpg \ --work-dir /mnt/data/train-yolov7-images/pytorch2onn-mmdeploy \ --device cuda \ --dump-info
It reported an error:
`============= Diagnostic Run torch.onnx.export version 2.0.0+cu118 =============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================

Process Process-2:
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/root/workspace/mmdeploy/mmdeploy/apis/core/pipeline_manager.py", line 107, in call
ret = func(*args, **kwargs)
File "/root/workspace/mmdeploy/mmdeploy/apis/pytorch2onnx.py", line 98, in torch2onnx
export(
File "/root/workspace/mmdeploy/mmdeploy/apis/core/pipeline_manager.py", line 356, in wrap
return self.call_function(func_name
, *args, **kwargs)
File "/root/workspace/mmdeploy/mmdeploy/apis/core/pipeline_manager.py", line 326, in call_function
return self.call_function_local(func_name, *args, **kwargs)
File "/root/workspace/mmdeploy/mmdeploy/apis/core/pipeline_manager.py", line 275, in call_function_local
return pipe_caller(*args, **kwargs)
File "/root/workspace/mmdeploy/mmdeploy/apis/core/pipeline_manager.py", line 107, in call
ret = func(*args, **kwargs)
File "/root/workspace/mmdeploy/mmdeploy/apis/onnx/export.py", line 138, in export
torch.onnx.export(
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 506, in export
_export(
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 1548, in _export
graph, params_dict, torch_out = _model_to_graph(
File "/root/workspace/mmdeploy/mmdeploy/apis/onnx/optimizer.py", line 27, in model_to_graph__custom_optimizer
graph, params_dict, torch_out = ctx.origin_func(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 1113, in _model_to_graph
graph, params, torch_out, module = _create_jit_graph(model, args)
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 989, in _create_jit_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args)
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 893, in _trace_and_get_graph_from_model
trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
File "/usr/local/lib/python3.8/dist-packages/torch/jit/_trace.py", line 1268, in _get_trace_graph
outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1501, in _call_impl
return forward_call(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/jit/_trace.py", line 127, in forward
graph, out = torch._C._create_graph_by_tracing(
File "/usr/local/lib/python3.8/dist-packages/torch/jit/_trace.py", line 121, in wrapper
out_vars, _ = _flatten(outs)
RuntimeError: Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: DetDataSample
11/16 16:55:21 - mmengine - ERROR - /root/workspace/mmdeploy/mmdeploy/apis/core/pipeline_manager.py - pop_mp_output - 80 - mmdeploy.apis.pytorch2onnx.torch2onnx with Call id: 0 failed. exit.
`
The ”co_dino_5scale_swin_large_16e_o365tococo.pth“is in https://github.com/Sense-X/Co-DETR/tree/main
image

Could you please tell me which checkpoint is you use?And I find someone use ”model_checkpoint = 'checkpoints/co_dino_5scale_r50_lsj_8xb2_1x_coco-69a72d67.pth'“ in https://github.com/open-mmlab/mmdetection/issues/11011
It is also return the same error! So how did you succeed export CO-DETR to onnx using mmdeploy without SoftNonMaxSuppression.I would be very grateful if you could help me!

@MarouaneMja
Copy link

Just remove the soft_nms from the config, and you can add it later

@xinlin-xiao
Copy link

Just remove the soft_nms from the config, and you can add it later

Do you have any suggestions on how to fix this mistake?
RuntimeError:Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: DetDataSample

@MarouaneMja
Copy link

Yes, DetDataSample are not supported by JIT , you have to convert the final output to a tuple format. Try, to run a simple Detr to get an idea what the output looks like , then do the same

@xinlin-xiao
Copy link

xinlin-xiao commented Nov 22, 2023

@MarouaneMja

Yes, DetDataSample are not supported by JIT , you have to convert the final output to a tuple format. Try, to run a simple Detr to get an idea what the output looks like , then do the same

I am trying find the model output is :
`out:[<DetDataSample(

META INFORMATION
img_path: '/root/workspace/mmdetection-support_dino_onnx/demo/demo.jpg'
scale_factor: (2.9984375, 2.997658079625293)
img_shape: (1280, 1919)
batch_input_shape: (1280, 1919)
ori_shape: (427, 640)
img_id: 0
pad_shape: (1280, 1919)

DATA FIELDS
pred_instances: <InstanceData(
    
        META INFORMATION
    
        DATA FIELDS
        scores: tensor([0.1154, 0.1098, 0.1047, 0.0994, 0.0982, 0.0867, 0.0844, 0.0714, 0.0704,
                    0.0603, 0.0577, 0.0564, 0.0543, 0.0538, 0.0520, 0.0511, 0.0509, 0.0509,
                    0.0502, 0.0498, 0.0495, 0.0491, 0.0461, 0.0460, 0.0440, 0.0434, 0.0432,
                    0.0425, 0.0422, 0.0419, 0.0409, 0.0407, 0.0407, 0.0407, 0.0391, 0.0390,
                    0.0381, 0.0375, 0.0372, 0.0372, 0.0369, 0.0367, 0.0367, 0.0367, 0.0362,
                    0.0358, 0.0355, 0.0350, 0.0349, 0.0349, 0.0347, 0.0347, 0.0345, 0.0342,
                    0.0341, 0.0340, 0.0340, 0.0339, 0.0339, 0.0338, 0.0332, 0.0332, 0.0331,
                    0.0330, 0.0327, 0.0326, 0.0326, 0.0326, 0.0325, 0.0324, 0.0322, 0.0322,
                    0.0318, 0.0317, 0.0316, 0.0315, 0.0314, 0.0312, 0.0312, 0.0310, 0.0305,
                    0.0304, 0.0304, 0.0304, 0.0299, 0.0299, 0.0299, 0.0298, 0.0298, 0.0297,
                    0.0296, 0.0295, 0.0293, 0.0291, 0.0290, 0.0290, 0.0290, 0.0290, 0.0289,
                    0.0287, 0.0285, 0.0284, 0.0283, 0.0282, 0.0281, 0.0281, 0.0281, 0.0280,
                    0.0280, 0.0280, 0.0279, 0.0279, 0.0277, 0.0277, 0.0276, 0.0275, 0.0274,
                    0.0274, 0.0272, 0.0271, 0.0271, 0.0269, 0.0266, 0.0266, 0.0265, 0.0265,
                    0.0263, 0.0263, 0.0262, 0.0262, 0.0260, 0.0260, 0.0260, 0.0260, 0.0260,
                    0.0259, 0.0258, 0.0256, 0.0256, 0.0255, 0.0255, 0.0255, 0.0254, 0.0253,
                    0.0251, 0.0250, 0.0248, 0.0246, 0.0246, 0.0246, 0.0246, 0.0246, 0.0244,
                    0.0244, 0.0244, 0.0244, 0.0244, 0.0243, 0.0243, 0.0243, 0.0243, 0.0242,
                    0.0241, 0.0236, 0.0235, 0.0235, 0.0234, 0.0234, 0.0234, 0.0233, 0.0233,
                    0.0233, 0.0232, 0.0232, 0.0231, 0.0231, 0.0231, 0.0230, 0.0230, 0.0230,
                    0.0230, 0.0230, 0.0228, 0.0228, 0.0227, 0.0226, 0.0226, 0.0225, 0.0225,
                    0.0225, 0.0223, 0.0222, 0.0221, 0.0220, 0.0220, 0.0219, 0.0219, 0.0218,
                    0.0218, 0.0216, 0.0216, 0.0216, 0.0215, 0.0214, 0.0214, 0.0214, 0.0213,
                    0.0213, 0.0213, 0.0213, 0.0213, 0.0212, 0.0212, 0.0212, 0.0212, 0.0211,
                    0.0209, 0.0209, 0.0208, 0.0208, 0.0208, 0.0208, 0.0208, 0.0208, 0.0207,
                    0.0207, 0.0207, 0.0207, 0.0207, 0.0206, 0.0205, 0.0205, 0.0205, 0.0205,
                    0.0204, 0.0204, 0.0068, 0.0067, 0.0056, 0.0051, 0.0048, 0.0048, 0.0046,
                    0.0041, 0.0040, 0.0037, 0.0036, 0.0033, 0.0030, 0.0030, 0.0028, 0.0028,
                    0.0027, 0.0022, 0.0021, 0.0016, 0.0015, 0.0014, 0.0014, 0.0013, 0.0013,
                    0.0012, 0.0012, 0.0011, 0.0010], device='cuda:0')
        bboxes: tensor([[575.6778, 238.4051, 640.0000, 377.5103],
                    [  6.4190,   1.7029, 638.2518, 377.3401],
                    [402.1796, 282.1749, 640.0000, 426.7463],
                    ...,
                    [  0.9140,   0.0000, 640.0000, 249.7452],
                    [  0.0000, 206.1276, 422.8578, 427.0000],
                    [  0.9140,   0.0000, 640.0000, 249.7452]], device='cuda:0')
        labels: tensor([39,  0,  0, 34,  0,  0, 69, 60, 41, 72, 79, 39, 71, 34, 39, 60, 60, 39,
                    39, 34,  0,  0,  0, 56, 39, 41, 43, 71, 34, 44, 39, 34, 69,  0,  0, 39,
                    41, 17, 39, 76,  0,  0, 34, 39, 79, 79,  0, 72, 12, 39, 39, 34, 39, 79,
                    44, 39, 39, 44,  0, 39, 41, 41, 34, 44, 45,  0, 78, 39, 79, 60, 34,  0,
                    41, 32, 39, 69, 39, 71,  0, 79,  0, 39,  0, 44, 55,  0, 56, 39, 34, 78,
                    71,  4, 44, 38, 34, 72, 43, 60, 72,  0, 12, 34, 39, 39, 45,  0,  0, 62,
                    61,  0,  0, 61, 34, 34, 69, 39, 72, 79,  0, 34,  0, 78, 67,  0, 72, 34,
                    41, 39,  0, 70,  0, 71, 40,  0, 41, 44, 60,  0, 79,  0, 39,  0, 38, 34,
                     0, 71, 79, 39,  0,  0,  0, 29, 45,  0, 53, 34, 12,  0, 60, 67, 79, 34,
                     0,  0, 78, 39, 34, 45, 43, 40, 44,  6, 62, 79,  0, 78,  0,  0, 67, 71,
                    68,  0, 59, 32, 41,  0,  0, 39, 44, 12, 56, 44, 39, 41, 39, 39, 12, 56,
                    34, 69, 71, 40, 15, 38, 79, 60, 44, 72, 79, 44, 56, 44,  0, 40, 12, 72,
                    39, 60, 39,  0, 79,  0,  0, 44, 79, 72, 38, 79, 40, 41, 34, 13, 43, 79,
                    41, 79, 34, 60, 39,  0,  0,  0, 39, 44,  0, 34, 39, 39,  0, 39,  0,  0,
                    39, 34, 69,  0, 34, 41, 60,  0, 79, 44, 71, 60, 17], device='cuda:0')
    ) at 0x7fb8680766d0>
ignored_instances: <InstanceData(
    
        META INFORMATION
    
        DATA FIELDS
    ) at 0x7fb82c7c1820>
gt_instances: <InstanceData(
    
        META INFORMATION
    
        DATA FIELDS
    ) at 0x7fb82c7c1850>

) at 0x7fb82c7c18e0>]

But,I do not know the right output looks like,trying use torch.jit.script to keep only labels, bboxes, tensor, scores in pred_instances is also return errors.
if you succeed export CO-DETR to onnx or TorchScript,could you please share your modified code and the right output to help me fix the errors?
I would be very grateful if you could help me! This problem has been bothering me for a long time.

@lzxpaipai
Copy link

Just remove the soft_nms from the config, and you can add it later

hello,the config file,I don’t find the soft_nms

@bibekyess
Copy link

bibekyess commented Jan 10, 2024

Hi! I figured out to convert co-dino to onnx and would like to share with you guys. I am using the model trained using mmdet:v3.3.0 and using mmdeploy:v1.3.1, onnxruntime:1.16.3. I found that the model trained using co-detr official repo (mmdet-2.25) requires lot of tinkering as the model backbone(swintransformer-v1 vs v2) is little different and also preprocessing functions and some utilities functions required for inference are different. So, if you train a new model from mmdet:v3.3.0 repo, I think you will be able to export it to onnx.

To solve this issue:

RuntimeError:Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: DetDataSample

I modified ...(your-env-path)/site-packages/torch/jit/trace.py
In line 125, I added try except as a temporary fix. I am using the tuple format so that the code doesn't break later during visualization. I am following the same format as the plain dino export to onnx. The change is like this:

try:
    result_ = self.inner(*trace_inputs)
    result = result_[0]
    scores, bboxes, labels = result.pred_instances.scores, result.pred_instances.bboxes, result.pred_instances.labels
    
    # Combining scores and bboxes into the dino format
    combined_tensor = torch.cat((bboxes, scores.unsqueeze(1)), dim=1)
    formatted_labels = labels.unsqueeze(0).long()  # Retaining integer format

    # Creating tuples with combined tensors and labels
    formatted_result = (combined_tensor.unsqueeze(0), formatted_labels) 
    outs.append(formatted_result)
except:
    print(self.inner(*trace_inputs))
    outs.append(self.inner(*trace_inputs))

Also, we need to remove soft_nms operations, just comment this section out like below in your config file:

   ...
  test_cfg=[
        dict(
            max_per_img=300, 
            # nms=dict(iou_threshold=0.8, type='soft_nms')
            ),
  ...

Hope this helps! Thank you! :)

@chenzx2
Copy link
Author

chenzx2 commented Jan 10, 2024

Hi! I figured out to convert co-dino to onnx and would like to share with you guys. I am using the model trained using mmdet:v3.3.0 and using mmdeploy:v1.3.1, onnxruntime:1.16.3. I found that the model trained using co-detr official repo (mmdet-2.25) requires lot of tinkering as the model backbone(swintransformer-v1 vs v2) is little different and also preprocessing functions and some utilities functions required for inference are different. So, if you train a new model from mmdet:v3.3.0 repo, I think you will be able to export it to onnx.

To solve this issue:

RuntimeError:Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: DetDataSample

I modified ...(your-env-path)/site-packages/torch/jit/trace.py In line 125, I added try except as a temporary fix. I am using the tuple format so that the code doesn't break later during visualization. I am following the same format as the plain dino export to onnx. The change is like this:

try:
    result_ = self.inner(*trace_inputs)
    result = result_[0]
    scores, bboxes, labels = result.pred_instances.scores, result.pred_instances.bboxes, result.pred_instances.labels
    
    # Combining scores and bboxes into the dino format
    combined_tensor = torch.cat((bboxes, scores.unsqueeze(1)), dim=1)
    formatted_labels = labels.unsqueeze(0).long()  # Retaining integer format

    # Creating tuples with combined tensors and labels
    formatted_result = (combined_tensor.unsqueeze(0), formatted_labels) 
    outs.append(formatted_result)
except:
    print(self.inner(*trace_inputs))
    outs.append(self.inner(*trace_inputs))

Also, we need to remove soft_nms operations, just comment this section out like below in your config file:

   ...
  test_cfg=[
        dict(
            max_per_img=300, 
            # nms=dict(iou_threshold=0.8, type='soft_nms')
            ),
  ...

Hope this helps! Thank you! :)

cool, I will try

@xinlin-xiao
Copy link

try:
    result_ = self.inner(*trace_inputs)
    result = result_[0]
    scores, bboxes, labels = result.pred_instances.scores, result.pred_instances.bboxes, result.pred_instances.labels
    
    # Combining scores and bboxes into the dino format
    combined_tensor = torch.cat((bboxes, scores.unsqueeze(1)), dim=1)
    formatted_labels = labels.unsqueeze(0).long()  # Retaining integer format

    # Creating tuples with combined tensors and labels
    formatted_result = (combined_tensor.unsqueeze(0), formatted_labels) 
    outs.append(formatted_result)
except:
    print(self.inner(*trace_inputs))
    outs.append(self.inner(*trace_inputs))

is pytoch2torchscript can modified ...(your-env-path)/site-packages/torch/jit/trace.py to slove `RuntimeError: Tracer cannot infer type of [<DetDataSample(

META INFORMATION
scale_factor: (2.9984375, 2.997658079625293)
img_id: 0
ori_shape: (427, 640)
batch_input_shape: (1280, 1919)
img_shape: (1280, 1919)
img_path: '/mnt/data/train-yolov7-images/pytorch2onn-mmdeploy/mmdetection-support_dino_onnx/mmdetection-support_dino_onnx/demo/demo.jpg'
pad_shape: (1280, 1919)

DATA FIELDS
ignored_instances: <InstanceData(
    
        META INFORMATION
    
        DATA FIELDS
    ) at 0x7f2ef4d998b0>
pred_instances: <InstanceData(
    
        META INFORMATION
    
        DATA FIELDS
        labels: tensor([ 0,  0,  0,  0,  0, 25,  0,  0,  0,  0, 16,  2,  2, 25,  2, 25, 25,  0,
                    16, 29,  0, 14,  2,  0,  2, 16,  0,  0, 16, 16, 10, 25, 25, 14, 25,  2,
                     0, 25,  2,  2, 37, 58, 25, 14, 10, 14,  0,  2, 58, 26, 10, 25,  2, 25,
                     2,  2, 58, 25, 10, 25, 58,  3,  2, 33,  0, 25, 58, 58,  0,  0,  0,  9,
                     0, 56, 26, 14,  0, 25,  0,  2, 25,  2, 17, 25, 25, 25, 25, 11, 25,  0,
                     0, 58,  0,  2, 14, 58,  0, 25, 25, 29,  2, 25,  2, 58,  2, 40,  2,  3,
                    26,  3,  9, 38, 25,  9, 25,  3, 33,  0,  0, 17, 55, 11, 10, 58, 58, 16,
                    16, 16,  2, 56, 36, 26, 58, 17, 17, 56,  0, 35,  0, 12, 12, 25,  2,  2,
                    25, 77,  8,  9, 46, 36,  4, 36, 25,  2, 26,  2,  9,  0,  0, 15, 25,  0,
                    25,  2, 14, 33,  0,  0, 10,  8, 60, 33, 25, 58, 13, 26,  0,  0,  0,  0,
                     0,  0, 25,  2, 25,  2, 58,  0,  2, 25, 25, 14,  2, 14, 16, 25, 25, 26,
                     0,  0,  0,  2,  0,  9, 58,  3, 26, 25, 17, 58,  2,  2, 56, 25, 14,  2,
                     0, 25, 16, 10, 25,  2, 58, 16, 25, 25, 58, 25,  2,  0,  0,  2,  0, 25,
                     2,  0,  0])
        scores: tensor([0.2160, 0.1991, 0.1698, 0.1452, 0.1358, 0.1283, 0.0971, 0.0791, 0.0758,
                    0.0723, 0.0697, 0.0694, 0.0666, 0.0625, 0.0615, 0.0583, 0.0566, 0.0557,
                    0.0551, 0.0535, 0.0527, 0.0525, 0.0512, 0.0511, 0.0509, 0.0474, 0.0474,
                    0.0470, 0.0468, 0.0457, 0.0454, 0.0446, 0.0443, 0.0439, 0.0435, 0.0432,
                    0.0431, 0.0429, 0.0428, 0.0419, 0.0400, 0.0399, 0.0398, 0.0397, 0.0396,
                    0.0393, 0.0390, 0.0386, 0.0383, 0.0379, 0.0377, 0.0376, 0.0373, 0.0369,
                    0.0359, 0.0354, 0.0351, 0.0351, 0.0347, 0.0342, 0.0341, 0.0340, 0.0337,
                    0.0336, 0.0335, 0.0334, 0.0333, 0.0331, 0.0331, 0.0323, 0.0323, 0.0320,
                    0.0317, 0.0312, 0.0312, 0.0310, 0.0309, 0.0305, 0.0302, 0.0301, 0.0300,
                    0.0299, 0.0299, 0.0297, 0.0297, 0.0296, 0.0294, 0.0292, 0.0292, 0.0291,
                    0.0290, 0.0290, 0.0289, 0.0287, 0.0287, 0.0286, 0.0286, 0.0285, 0.0283,
                    0.0282, 0.0282, 0.0282, 0.0281, 0.0281, 0.0280, 0.0278, 0.0276, 0.0276,
                    0.0276, 0.0275, 0.0271, 0.0270, 0.0269, 0.0269, 0.0269, 0.0268, 0.0267,
                    0.0266, 0.0266, 0.0263, 0.0263, 0.0262, 0.0258, 0.0258, 0.0257, 0.0256,
                    0.0255, 0.0254, 0.0253, 0.0249, 0.0249, 0.0248, 0.0247, 0.0246, 0.0244,
                    0.0243, 0.0242, 0.0242, 0.0241, 0.0240, 0.0240, 0.0239, 0.0239, 0.0237,
                    0.0237, 0.0235, 0.0235, 0.0235, 0.0235, 0.0232, 0.0232, 0.0229, 0.0229,
                    0.0229, 0.0228, 0.0228, 0.0227, 0.0227, 0.0226, 0.0224, 0.0224, 0.0223,
                    0.0222, 0.0222, 0.0222, 0.0221, 0.0220, 0.0220, 0.0219, 0.0219, 0.0219,
                    0.0218, 0.0218, 0.0218, 0.0218, 0.0217, 0.0217, 0.0194, 0.0167, 0.0114,
                    0.0099, 0.0099, 0.0088, 0.0084, 0.0072, 0.0069, 0.0064, 0.0060, 0.0056,
                    0.0055, 0.0052, 0.0052, 0.0051, 0.0049, 0.0049, 0.0048, 0.0044, 0.0043,
                    0.0043, 0.0042, 0.0042, 0.0041, 0.0039, 0.0039, 0.0038, 0.0037, 0.0037,
                    0.0037, 0.0036, 0.0035, 0.0035, 0.0034, 0.0034, 0.0032, 0.0032, 0.0031,
                    0.0031, 0.0030, 0.0029, 0.0029, 0.0028, 0.0028, 0.0028, 0.0026, 0.0025,
                    0.0025, 0.0023, 0.0023, 0.0023, 0.0021, 0.0020, 0.0016, 0.0016, 0.0015,
                    0.0013, 0.0011, 0.0010])
        bboxes: tensor([[1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [0.0000e+00, 7.2158e+01, 3.8210e+02, 4.2700e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 6.0318e+01, 2.8487e+02, 4.2700e+02],
                    [2.6185e+02, 7.1847e+01, 6.4000e+02, 4.2432e+02],
                    [2.9458e+02, 1.4595e+02, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 5.4273e+01, 2.9559e+02, 3.2202e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [2.8250e+02, 4.3829e+01, 6.3907e+02, 4.2441e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.4563e+02, 1.4659e+01, 6.3662e+02, 3.9578e+02],
                    [4.7329e+00, 4.1630e+01, 6.3977e+02, 3.9011e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [2.1382e+02, 1.6523e+02, 6.4000e+02, 4.2700e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [2.1120e+02, 1.2138e+02, 6.3890e+02, 3.8925e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [2.4776e+02, 1.5199e+01, 6.4000e+02, 3.9079e+02],
                    [0.0000e+00, 4.1204e+01, 3.8788e+02, 3.6426e+02],
                    [4.6684e+02, 7.1337e+01, 6.4000e+02, 4.2491e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [0.0000e+00, 1.6564e+02, 5.1611e+02, 4.2700e+02],
                    [0.0000e+00, 1.5713e+02, 3.6014e+02, 4.2700e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 8.2242e-01, 3.9962e+02, 3.2408e+02],
                    [0.0000e+00, 5.4273e+01, 2.9559e+02, 3.2202e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [3.2242e+02, 7.0608e+01, 6.4000e+02, 2.5587e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [1.0147e+01, 1.5368e+02, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 4.1166e+01, 4.4898e+02, 3.7798e+02],
                    [5.6229e+02, 4.6781e+01, 6.4000e+02, 4.2581e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 6.9887e+00, 2.8779e+02, 4.1326e+02],
                    [0.0000e+00, 3.9866e+01, 2.5395e+02, 3.0416e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [0.0000e+00, 5.4554e+01, 5.3191e+02, 4.1821e+02],
                    [0.0000e+00, 6.9887e+00, 2.8779e+02, 4.1326e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [1.6044e+02, 5.3330e+01, 5.0755e+02, 3.8331e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [2.8250e+02, 4.3829e+01, 6.3907e+02, 4.2441e+02],
                    [2.5501e+01, 6.2984e+00, 6.4000e+02, 3.8557e+02],
                    [0.0000e+00, 8.2242e-01, 3.9962e+02, 3.2408e+02],
                    [0.0000e+00, 5.4273e+01, 2.9559e+02, 3.2202e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 5.4554e+01, 5.3191e+02, 4.1821e+02],
                    [0.0000e+00, 7.2158e+01, 3.8210e+02, 4.2700e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [2.1382e+02, 1.6523e+02, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 1.3510e+02, 3.0765e+02, 4.1102e+02],
                    [1.5868e+02, 2.7586e+01, 6.4000e+02, 3.8983e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [4.2014e+02, 1.6466e+02, 6.4000e+02, 3.7918e+02],
                    [3.8734e+02, 1.7227e+02, 6.4000e+02, 4.2472e+02],
                    [0.0000e+00, 3.9866e+01, 2.5395e+02, 3.0416e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [4.4463e-01, 0.0000e+00, 1.0380e+02, 4.2604e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [1.6612e+00, 1.8108e+02, 1.3982e+02, 4.2700e+02],
                    [0.0000e+00, 1.2050e+00, 3.0532e+02, 2.0350e+02],
                    [0.0000e+00, 1.5517e+02, 2.3322e+02, 4.2700e+02],
                    [3.8747e+00, 0.0000e+00, 6.4000e+02, 2.2986e+02],
                    [1.6039e+02, 7.8185e+01, 4.4346e+02, 2.8939e+02],
                    [0.0000e+00, 5.4273e+01, 2.9559e+02, 3.2202e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 6.9887e+00, 2.8779e+02, 4.1326e+02],
                    [7.7882e+01, 1.5648e+01, 4.9433e+02, 3.7667e+02],
                    [3.8785e+02, 8.7845e+01, 5.7135e+02, 2.0960e+02],
                    [0.0000e+00, 6.2847e-01, 2.2725e+02, 1.3132e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 0.0000e+00, 5.2475e+02, 1.3465e+02],
                    [2.0440e+02, 3.4097e+01, 6.4000e+02, 3.8894e+02],
                    [5.7069e+02, 1.0939e+02, 5.8223e+02, 1.1748e+02],
                    [0.0000e+00, 4.1204e+01, 3.8788e+02, 3.6426e+02],
                    [0.0000e+00, 8.3078e+00, 2.6751e+02, 3.6747e+02],
                    [1.5868e+02, 2.7586e+01, 6.4000e+02, 3.8983e+02],
                    [2.6185e+02, 7.1847e+01, 6.4000e+02, 4.2432e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [3.5561e+00, 0.0000e+00, 6.4000e+02, 2.4098e+02],
                    [1.0667e+02, 6.9062e+01, 4.2385e+02, 3.6903e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [6.2002e+01, 0.0000e+00, 6.2688e+02, 1.4552e+02],
                    [4.1718e+00, 4.1516e-01, 1.7336e+02, 1.3076e+02],
                    [0.0000e+00, 0.0000e+00, 5.5502e+02, 1.4696e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [4.4757e+02, 2.5681e+01, 6.4000e+02, 4.2443e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [3.3409e+02, 6.9283e+01, 5.2455e+02, 3.9823e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [2.3454e+02, 8.3355e+01, 6.4000e+02, 3.8414e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [1.7801e+02, 2.0820e+02, 6.4000e+02, 4.2700e+02],
                    [4.0794e+02, 5.2874e+01, 6.4000e+02, 3.9537e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [4.4463e-01, 0.0000e+00, 1.0380e+02, 4.2604e+02],
                    [0.0000e+00, 8.7248e+01, 2.9231e+02, 4.2453e+02],
                    [2.6185e+02, 7.1847e+01, 6.4000e+02, 4.2432e+02],
                    [5.7069e+02, 1.0939e+02, 5.8223e+02, 1.1748e+02],
                    [2.1382e+02, 1.6523e+02, 6.4000e+02, 4.2700e+02],
                    [5.8778e+02, 1.0198e+01, 6.4000e+02, 4.0520e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [0.0000e+00, 3.9866e+01, 2.5395e+02, 3.0416e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [2.8250e+02, 4.3829e+01, 6.3907e+02, 4.2441e+02],
                    [5.8725e+02, 8.0794e+01, 5.9527e+02, 8.9090e+01],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [5.6229e+02, 4.6781e+01, 6.4000e+02, 4.2581e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.9076e+02, 8.4498e+01, 6.3985e+02, 4.1969e+02],
                    [4.4463e-01, 0.0000e+00, 1.0380e+02, 4.2604e+02],
                    [0.0000e+00, 7.2158e+01, 3.8210e+02, 4.2700e+02],
                    [2.9458e+02, 1.4595e+02, 6.4000e+02, 4.2700e+02],
                    [5.8774e+01, 2.0932e+00, 2.9747e+02, 3.0555e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [7.4486e+01, 1.8788e+01, 3.0107e+02, 3.5681e+02],
                    [0.0000e+00, 4.1204e+01, 3.8788e+02, 3.6426e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [4.2666e+02, 1.7290e+02, 6.4000e+02, 4.2700e+02],
                    [5.6229e+02, 4.6781e+01, 6.4000e+02, 4.2581e+02],
                    [0.0000e+00, 4.0296e+01, 4.7342e+02, 3.8573e+02],
                    [2.4282e+00, 2.4388e+00, 6.3059e+01, 4.2700e+02],
                    [1.6500e+02, 7.7726e+01, 6.4000e+02, 4.2700e+02],
                    [3.8403e+02, 3.8851e+00, 6.3406e+02, 1.7161e+02],
                    [0.0000e+00, 8.2242e-01, 3.9962e+02, 3.2408e+02],
                    [3.0750e+02, 7.8088e+01, 4.9292e+02, 4.0430e+02],
                    [2.8905e+00, 4.7422e-01, 4.8275e+02, 9.4585e+01],
                    [5.7069e+02, 1.0939e+02, 5.8223e+02, 1.1748e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [5.8047e+02, 8.1446e+01, 5.9600e+02, 9.0194e+01],
                    [0.0000e+00, 2.8271e+02, 4.8164e+02, 4.2694e+02],
                    [0.0000e+00, 6.0318e+01, 2.8487e+02, 4.2700e+02],
                    [0.0000e+00, 7.3228e+01, 2.9850e+02, 2.0027e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [2.4165e+02, 1.6792e+01, 6.4000e+02, 3.9862e+02],
                    [0.0000e+00, 8.2242e-01, 3.9962e+02, 3.2408e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [0.0000e+00, 5.4273e+01, 2.9559e+02, 3.2202e+02],
                    [0.0000e+00, 1.1854e+02, 1.3432e+02, 3.3936e+02],
                    [4.7329e+00, 4.1630e+01, 6.3977e+02, 3.9011e+02],
                    [3.9284e+02, 9.1945e+01, 6.4000e+02, 3.8028e+02],
                    [0.0000e+00, 7.4748e+01, 4.2332e+02, 4.2700e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [0.0000e+00, 6.9589e+01, 2.9147e+02, 3.7202e+02],
                    [1.0793e+00, 7.7588e+01, 6.4000e+02, 4.2700e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [0.0000e+00, 4.1204e+01, 3.8788e+02, 3.6426e+02],
                    [2.8507e+02, 1.1117e+01, 6.4000e+02, 3.7584e+02],
                    [4.7329e+00, 4.1630e+01, 6.3977e+02, 3.9011e+02],
                    [0.0000e+00, 1.8234e+02, 4.6567e+02, 4.2700e+02],
                    [3.9010e+02, 1.0139e+01, 6.4000e+02, 4.0455e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [0.0000e+00, 8.8411e-01, 2.8525e+02, 3.2094e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [2.6185e+02, 7.1847e+01, 6.4000e+02, 4.2432e+02],
                    [4.7329e+00, 4.1630e+01, 6.3977e+02, 3.9011e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [0.0000e+00, 2.4596e+00, 3.6105e+02, 2.9753e+02],
                    [1.5847e+02, 6.8899e+01, 4.7434e+02, 3.8014e+02],
                    [4.7329e+00, 4.1630e+01, 6.3977e+02, 3.9011e+02],
                    [4.3726e+02, 1.6330e+02, 6.4000e+02, 3.6836e+02],
                    [4.1968e+02, 9.7537e+01, 6.4000e+02, 4.2626e+02],
                    [2.8250e+02, 4.3829e+01, 6.3907e+02, 4.2441e+02],
                    [0.0000e+00, 2.4596e+00, 3.6105e+02, 2.9753e+02],
                    [4.2666e+02, 1.7290e+02, 6.4000e+02, 4.2700e+02],
                    [3.3403e+02, 7.9988e+01, 6.4000e+02, 4.2429e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [2.5501e+01, 6.2984e+00, 6.4000e+02, 3.8557e+02],
                    [3.2040e+02, 6.6847e+01, 6.4000e+02, 3.8979e+02],
                    [1.6356e+00, 5.5374e+01, 3.0322e+02, 3.5976e+02],
                    [5.5122e+02, 4.9658e+01, 6.4000e+02, 4.1820e+02],
                    [1.0730e+02, 0.0000e+00, 6.3086e+02, 1.3961e+02],
                    [3.3831e+02, 7.2650e+01, 6.4000e+02, 4.2263e+02],
                    [3.9284e+02, 9.1945e+01, 6.4000e+02, 3.8028e+02],
                    [2.8250e+02, 4.3829e+01, 6.3907e+02, 4.2441e+02],
                    [0.0000e+00, 0.0000e+00, 5.2475e+02, 1.3465e+02],
                    [1.4443e+02, 7.2818e+01, 6.4000e+02, 4.2557e+02],
                    [0.0000e+00, 0.0000e+00, 5.5502e+02, 1.4696e+02],
                    [2.8250e+02, 4.3829e+01, 6.3907e+02, 4.2441e+02],
                    [2.1494e+02, 7.4293e+01, 6.4000e+02, 4.2397e+02],
                    [2.4880e-01, 7.6363e-01, 2.4526e+02, 1.3574e+02],
                    [5.8323e+02, 9.6534e+00, 6.4000e+02, 4.2157e+02],
                    [2.1494e+02, 7.4293e+01, 6.4000e+02, 4.2397e+02],
                    [2.5619e+02, 1.6856e+02, 6.4000e+02, 4.2599e+02],
                    [0.0000e+00, 4.0296e+01, 4.7342e+02, 3.8573e+02],
                    [1.7174e+02, 8.9912e+01, 4.4728e+02, 2.8728e+02],
                    [0.0000e+00, 7.4748e+01, 4.2332e+02, 4.2700e+02],
                    [4.6382e+00, 0.0000e+00, 6.4000e+02, 2.1783e+02],
                    [0.0000e+00, 7.4748e+01, 4.2332e+02, 4.2700e+02],
                    [2.3454e+02, 8.3355e+01, 6.4000e+02, 3.8414e+02],
                    [0.0000e+00, 7.5742e+01, 3.5057e+02, 4.2451e+02],
                    [1.4443e+02, 7.2818e+01, 6.4000e+02, 4.2557e+02],
                    [0.0000e+00, 6.9887e+00, 2.8779e+02, 4.1326e+02],
                    [1.4443e+02, 7.2818e+01, 6.4000e+02, 4.2557e+02],
                    [4.6382e+00, 0.0000e+00, 6.4000e+02, 2.1783e+02],
                    [0.0000e+00, 8.8411e-01, 2.8525e+02, 3.2094e+02],
                    [1.6356e+00, 5.5374e+01, 3.0322e+02, 3.5976e+02]])
    ) at 0x7f2e5428f8e0>
gt_instances: <InstanceData(
    
        META INFORMATION
    
        DATA FIELDS
    ) at 0x7f2ef4d99880>

) at 0x7f2ef4d99970>]
:Could not infer type of list element: Only tensors and (possibly nested) tuples of tensors, lists, or dictsare supported as inputs or outputs of traced functions, but instead got value of type DetDataSample.
`

@bibekyess
Copy link

@xinlin-xiao Mm it seems it is returning in list format, ao just get the entry at 0th index of that list and it should solve your issue

@sirolf-otrebla
Copy link

sirolf-otrebla commented Feb 15, 2024

Hi @bibekyess, could you please provide the entire _trace.py file? mine seems to look different from yours, as I can't add anything to line 125 (that line for me is in the middle of a function call).

Moreover, I'm using the implementation that's included in mmdetection v3.3.0 and I can confirm that issue is present also here.

@bibekyess
Copy link

Hi @sirolf-otrebla I am now on vacation so I will provide it later. But as far as I remember, line 125 includes this instruction outs.append(self.inner(*trace_inputs)) Now just substitute that with the above try-except snippet. Or you can just search for that instruction in this file and substitute.

@Sunny20236
Copy link

Hi @sirolf-otrebla ,when using version 3.3.0 of mmdet, how should these two lines in mmdet/apis/inference.py be modified?
'from mmcv.parallel import collate, scatter'

Alternatively, could you tell me how to train with version 3.3.0? I'm not sure where I might be going wrong. Thank you very much!

@Sunny20236
Copy link

Hi @bibekyess ,when using version 3.3.0 of mmdet, how should these two lines in mmdet/apis/inference.py be modified?

'from mmcv.parallel import collate, scatter'

I found that these two modules have been removed in this version of mmdet, but I haven't found any alternative modules. Could you please tell me how you trained with version 3.3.0? This issue has been bothering me for a long time, and I would greatly appreciate your help.

@sirolf-otrebla
Copy link

@Sunny20236 I am not sure if this answers your question because it's not clear to me what the issue you're having is. I can tell you that I'm using this ( https://github.com/open-mmlab/mmdetection/blob/main/tools/train.py ) script to train the network, directly from their repo. I didn't have to modify anything inside mmdetection itself.

@sirolf-otrebla
Copy link

sirolf-otrebla commented Mar 5, 2024

Also, for future reference in case someone needs it:

this git diff applies the patch @bibekyess suggested. It works with torch 2.0.0. For other versions of torch, you can write a similar patch yourself, but i encountered other issues with more modern versions (hence the downgrade to 2.0.0)

index 4afe7349690..fb993b7371e 100644
--- ./jit/_trace.py
+++ ./jit/_trace.py
@@ -29,6 +29,14 @@ from torch.testing._comparison import default_tolerances
 _flatten = torch._C._jit_flatten
 _unflatten = torch._C._jit_unflatten
 
+def _unpack_mmdet_det_data_sample(sample):
+    try:
+        scores, bboxes, labels = sample.pred_instances.scores, sample.pred_instances.bboxes, sample.pred_instances.labels
+        bboxes_scores_cat_tensor = torch.cat([bboxes, scores.unsqueeze(1)], dim=1)
+        labels_long_tensor = labels.long()
+        return bboxes_scores_cat_tensor.unsqueeze(0), labels_long_tensor.unsqueeze(0)
+    except:
+        raise TypeError("not a mmdetection data sample")
 
 def _create_interpreter_name_lookup_fn(frames_up=1):
     def _get_interpreter_name_for_var(var):
@@ -115,7 +123,22 @@ class ONNXTracedModule(torch.nn.Module):
             )
             if self._return_inputs_states:
                 inputs_states.append(_unflatten(in_args, in_desc))
-            outs.append(self.inner(*trace_inputs))
+            # ---------------------------------------------------------------------------------------------------------
+            # ------------------START OF PATCH-------------------------------------------------------------------------
+            # -------------------------------------------------------------------------------------------------------------
+
+            result_ = self.inner(*trace_inputs)
+            try:
+                if result_[0].__class__.__name__ == "DetDataSample":
+                    result_ = _unpack_mmdet_det_data_sample(result_[0])
+            except Exception as e:
+                warnings.warn("Failed to unpack mmdet det data sample, using standard torch output tracing")
+            finally:
+                outs.append(result_)
+
+            # ---------------------------------------------------------------------------------------------------------
+            # ------------------END OF PATCH---------------------------------------------------------------------------
+            # ---------------------------------------------------------------------------------------------------------
             if self._return_inputs_states:
                 inputs_states[0] = (inputs_states[0], trace_inputs)
             out_vars, _ = _flatten(outs)

@Sunny20236
Copy link

@sirolf-otrebla I'm sorry for the interruption again. Initially, I encountered the error mentioned by @bibekyess . Then, I made modifications according to the file you provided, which resulted in the following error:

onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from /data/songsl/mmdetection/projects/CO-DETR/onnx/end2end.onnx failed:Fatal error: mmcv:SoftNonMaxSuppression(-1) is not a registered function/op
03/08 20:23:37 - mmengine - ERROR - ./tools/deploy.py - create_process - 82 - visualize onnxruntime model failed.

Do you happen to know how to resolve this issue? I would greatly appreciate it.

@bibekyess
Copy link

@Sunny20236 Can you read the entire message I wrote for solving this issue. The solution to your issue of softnms is already there. Thanks! :)

@sirolf-otrebla
Copy link

sirolf-otrebla commented Mar 8, 2024

@Sunny20236 you have to remove the soft nms module from you model config file. You are not going to need it anyway for inference, and you can always add it later as a post-processing step.

EDIT:

when i say "model config file" I mean, for example:

nms=dict(type='soft_nms', iou_threshold=0.8)),

of course, the specific file depends on your specific case.

@Sunny20236
Copy link

Sunny20236 commented Mar 14, 2024

@sirolf-otrebla I'm sorry to bother you again. I encountered a new problem during the conversion process. The detection boxes in the ONNX image are particularly far away from the target.Have you encountered this problem before, or do you know how to handle it? Thank you very much.The following content is a set of warning messages.

03/14 09:51:49 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "Codebases" registry tree. As a workaround, the current "Codebases" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:51:49 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "mmdet_tasks" registry tree. As a workaround, the current "mmdet_tasks" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:51:52 - mmengine - INFO - Start pipeline mmdeploy.apis.pytorch2onnx.torch2onnx in subprocess
03/14 09:51:53 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "Codebases" registry tree. As a workaround, the current "Codebases" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:51:53 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "mmdet_tasks" registry tree. As a workaround, the current "mmdet_tasks" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
[W shape_type_inference.cpp:1920] Warning: The shape inference of mmdeploy::grid_sampler type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function. (function UpdateReliable)
03/14 09:54:02 - mmengine - WARNING - Can not optimize model, please build torchscipt extension.
More details: https://github.com/open-mmlab/mmdeploy/tree/main/docs/en/experimental/onnx_optimizer.md
================ Diagnostic Run torch.onnx.export version 2.0.0 ================
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 60 WARNING 0 ERROR =======================
60 WARNING were not printed due to the log level.

03/14 09:54:08 - mmengine - INFO - Finish pipeline mmdeploy.apis.pytorch2onnx.torch2onnx
03/14 09:54:09 - mmengine - INFO - Start pipeline mmdeploy.apis.utils.utils.to_backend in main process
03/14 09:54:09 - mmengine - INFO - Finish pipeline mmdeploy.apis.utils.utils.to_backend
03/14 09:54:09 - mmengine - INFO - visualize onnxruntime model start.
03/14 09:54:13 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "Codebases" registry tree. As a workaround, the current "Codebases" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:54:13 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "mmdet_tasks" registry tree. As a workaround, the current "mmdet_tasks" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:54:13 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "backend_detectors" registry tree. As a workaround, the current "backend_detectors" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:54:13 - mmengine - INFO - Successfully loaded onnxruntime custom ops from /data/songsl/anaconda/envs/mmdet330/lib/python3.8/site-packages/mmdeploy/lib/libmmdeploy_onnxruntime_ops.so
03/14 09:54:55 - mmengine - WARNING - render and display result skipped for headless device, exception no display name and no $DISPLAY environment variable
03/14 09:54:57 - mmengine - INFO - visualize onnxruntime model success.
03/14 09:54:57 - mmengine - INFO - visualize pytorch model start.
03/14 09:55:01 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "Codebases" registry tree. As a workaround, the current "Codebases" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
03/14 09:55:01 - mmengine - WARNING - Failed to search registry with scope "mmdet" in the "mmdet_tasks" registry tree. As a workaround, the current "mmdet_tasks" registry in "mmdeploy" is used to build instance. This may cause unexpected failure when running the built modules. Please check whether "mmdet" is a correct scope, or whether the registry is initialized.
Loads checkpoint by local backend from path: /data/songsl/mmdetection/projects/CO-DETR/results/DOTA_3c/epoch_15.pth
/data/songsl/anaconda/envs/mmdet330/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /opt/conda/conda-bld/pytorch_1678402421473/work/aten/src/ATen/native/TensorShape.cpp:3483.)
return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]
03/14 09:55:37 - mmengine - WARNING - render and display result skipped for headless device, exception no display name and no $DISPLAY environment variable
/data/songsl/anaconda/envs/mmdet330/lib/python3.8/site-packages/mmengine/visualization/visualizer.py:760: UserWarning: Warning: The bbox is out of bounds, the drawn bbox may not be in the image
warnings.warn(
/data/songsl/anaconda/envs/mmdet330/lib/python3.8/site-packages/mmengine/visualization/visualizer.py:831: UserWarning: Warning: The polygon is out of bounds, the drawn polygon may not be in the image
warnings.warn(
03/14 09:55:38 - mmengine - INFO - visualize pytorch model success.
03/14 09:55:38 - mmengine - INFO - All process success.

@sirolf-otrebla
Copy link

sirolf-otrebla commented Mar 14, 2024

@Sunny20236 i'm deploying against TensorRT and the model performs almost exactly the same as the pure pytorch one.

@bibekyess
Copy link

Hi @Sunny20236! As far as I have understood, the example input passed during the torch-to-onnx export is used to trace the model through the forward pass and the traced graph is converted into the ONNX format. This results
in a static graph where the input size is fixed based on the provided example. So, the output should be scaled according
to the input example image scale.

If (h, w) is used in the test-image sample used during onnx conversion, then I need to calculate the factor using this:
Say an example image of shape (201, 284) is used during onnx conversion,
Then, for current inference image, with its current height and width, I need to resize the bboxes:
current_height, current_width = image.shape[0], image.shape[1]
height_factor = 284/current_height 
width_factor = 201/current_width
This is used to calculate the new x-coordinate and y-coordinates:
x_new = x/width_factor
y_new = y/height_factor

Long story short, if you resize the inference image to the same size of the example image used to convert torch2onnx, it should work. In my case, it worked like that.
Hope this helps! :)

@Sunny20236
Copy link

Finally! Finally! Finally! I have finally achieved the result I wanted. Thank you for your response, and also thank you to everyone who answered my questions. Thank you very much! @bibekyess ,@sirolf-otrebla

@bibekyess
Copy link

By the way, did you guys have any speedup when using onnx model compared to the torch? I converted codino to onnx and I found performance is very similar. Specifically,

# CPU- Speed & VRAM Usage
Torch: 1.9768s per image ⇒ 0.506FPS, 3.03GB
Onnx-CPU: 1.8396s per image ⇒ 0.5436FPS, 2.94GB

I found a significant improvement when converting to TensorRT and running on GPU but with CPU, I didn't have any gain. So, please let me know if you guys have improvement on running in CPU.
Thank you!

@xdd12135
Copy link

did anyone use the onnx for tensorrt? I get this error when using TVM.
Traceback (most recent call last): File "dump_relay_ir.py", line 59, in <module> save_relay_ir(args.path, args.relay_ir_file, args.dtype, pass_path=args.pass_path, toposort=args.toposort) File "dump_relay_ir.py", line 38, in save_relay_ir ir_module, param = relay.frontend.from_onnx(onnx_model, dtype=dtype) File "/tvm/relay/frontend/onnx.py", line 6246, in from_onnx mod, params = g.from_onnx(graph, opset) File "/tvm/relay/frontend/onnx.py", line 5911, in from_onnx self._check_for_unsupported_ops(graph) File "/tvm/relay/frontend/onnx.py", line 5998, in _check_for_unsupported_ops raise tvm.error.OpNotImplemented(msg) tvm.error.OpNotImplemented: The following operators are not supported for frontend ONNX: grid_sampler
it seem tobe the operators grid_sampler cannot be found in TVM; but I really found a operators called GridSample .This two suppose be the same.
Does anyone know how to solve it?

@zhurou603
Copy link

zhurou603 commented Mar 26, 2024

@Sunny20236 i'm deploying against TensorRT and the model performs almost exactly the same as the pure pytorch one.
@Sunny20236
Can you provide your software stack version,like mmdet/mmdeploy/trt/torch/cuda version,I am trying convert Co-DETR from pth to onnx, then use tensorrt to infer but get many errors...

DEPLOY_CFG=./configs/mmdet/detection/detection_onnxruntime-fp16_dynamic.py
MODEL_CFG=/workspace/mmdetection/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py
CHECKPOINT=../co_dino_5scale_swin_large_16e_o365tococo-614254c9.pth
INPUT_IMG=/workspace/mmdetection/demo/demo.jpg
WORK_DIR=./save_onnx
python tools/torch2onnx.py \
    ${DEPLOY_CFG} \
    ${MODEL_CFG} \
    ${CHECKPOINT} \
    ${INPUT_IMG} \
    --work-dir ${WORK_DIR} \
    --device cuda \

image

@YOODS-Xu
Copy link

YOODS-Xu commented Apr 2, 2024

@sirolf-otrebla
It is very kind of you giving detailed advice.
I modified files according your advice, but I got another errors.

The bbox is out of bounds, the drawn bbox may not be in the image
The polygon is out of bounds, the drawn polygon may not be in the image

I used the same image from train dataset.

@caraevangeline
Copy link

caraevangeline commented Apr 25, 2024

Hi @Sunny20236! As far as I have understood, the example input passed during the torch-to-onnx export is used to trace the model through the forward pass and the traced graph is converted into the ONNX format. This results in a static graph where the input size is fixed based on the provided example. So, the output should be scaled according to the input example image scale.

If (h, w) is used in the test-image sample used during onnx conversion, then I need to calculate the factor using this:
Say an example image of shape (201, 284) is used during onnx conversion,
Then, for current inference image, with its current height and width, I need to resize the bboxes:
current_height, current_width = image.shape[0], image.shape[1]
height_factor = 284/current_height 
width_factor = 201/current_width
This is used to calculate the new x-coordinate and y-coordinates:
x_new = x/width_factor
y_new = y/height_factor

Long story short, if you resize the inference image to the same size of the example image used to convert torch2onnx, it should work. In my case, it worked like that. Hope this helps! :)

Hi @bibekyess, @Sunny20236

I have read through the above issue and comments and have been trying to follow the same. In my case, I trained Co-DETR model (ViT backbone- which gives 66AP on COCO test dataset) using this repo. I am using mmdeploy to do the model conversion to onnx and faced several issues but with the above help and support I managed to resolve the issues. Right now, the model is being exported to onnx format but the model just doesn't appear to be right. deploy.py script from mmdeploy converts the model to onnx format and outputs the visualisation of the test image and the problem appears to be right here.

2024-04-25 11:30:02,623 - mmdeploy - INFO - Finish pipeline mmdeploy.apis.pytorch2onnx.torch2onnx
2024-04-25 11:30:03,834 - mmdeploy - INFO - Start pipeline mmdeploy.apis.utils.utils.to_backend in main process
2024-04-25 11:30:03,864 - mmdeploy - INFO - Finish pipeline mmdeploy.apis.utils.utils.to_backend
2024-04-25 11:30:03,864 - mmdeploy - INFO - visualize onnxruntime model start.
2024-04-25 11:30:16,279 - mmdeploy - INFO - Successfully loaded onnxruntime custom ops from /opt/anaconda3/envs/codetr/lib/python3.7/site-packages/mmdeploy/lib/libmmdeploy_onnxruntime_ops.so
2024-04-25:11:30:16 - mmdeploy - INFO - Successfully loaded onnxruntime custom ops from /opt/anaconda3/envs/codetr/lib/python3.7/site-packages/mmdeploy/lib/libmmdeploy_onnxruntime_ops.so
/home/Co-DETR/mmdet/datasets/utils.py:70: UserWarning: "ImageToTensor" pipeline is replaced by "DefaultFormatBundle" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.
  'data pipeline in your config file.', UserWarning)
2024-04-25:11:31:17 - root - ERROR - too many indices for tensor of dimension 2
Traceback (most recent call last):
  File "/home/mmdeploy/mmdeploy/utils/utils.py", line 41, in target_wrapper
    result = target(*args, **kwargs)
  File "/home/mmdeploy/mmdeploy/apis/visualize.py", line 72, in visualize_model
    result = task_processor.run_inference(model, model_inputs)[0]
  File "/home/mmdeploy/mmdeploy/codebase/mmdet/deploy/object_detection.py", line 204, in run_inference
    return model(**model_inputs, return_loss=False, rescale=True)
  File "/opt/anaconda3/envs/codetr/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/mmdeploy/mmdeploy/codebase/mmdet/deploy/object_detection_model.py", line 200, in forward
    outputs = End2EndModel.__clear_outputs(outputs)
  File "/home/mmdeploy/mmdeploy/codebase/mmdet/deploy/object_detection_model.py", line 109, in __clear_outputs
    inds = test_outputs[0][i, :, 4] > 0.0
IndexError: too many indices for tensor of dimension 2
2024-04-25 11:31:18,453 - mmdeploy - ERROR - visualize onnxruntime model failed.

Could you please kindly let me know if you can spot something that's going wrong here? Thanks for your help

Note:

  • I had removed soft_nms from the config file, so I thought that's the reason the script wasn't able to interpret the output but in general I see the onnx graph giving different output and in my case I got 11 output nodes

@bibekyess
Copy link

Hi @caraevangeline Mmm it looks like a familiar dimension issue. I would like to recommend these things:

  1. In /home/mmdeploy/mmdeploy/codebase/mmdet/deploy/object_detection_model.py",#109, try to print(test_outputs.shape) or something similar to check the dimension of output. From my guess, it looks like the model inference is successfully done but maybe your input is in batch, instead of single input or something is different from expected shape, so it fails when it does negative score filtering. Maybe you can simply change in this filtering to avoid error in this line and make it work for your specific usecase.
  2. Sometimes, the version of mmdeploy may be issue. Try to downgrade or use latest version of mmdeploy and test it.
  3. Also, if you have some extra preprocessing steps in the test_pipeline in your model_config, which is different than train_pipeline, maybe that will cause issue. I am not sure, but I would recommend trying with the default config of CoDetr with the open-source weights, and if you can convert that author's Co-Detr model to onnx successfully, then the issue is with your custom model-config.

Hope it helps and Happy Learning!! 🙂

@caraevangeline
Copy link

Hi @caraevangeline Mmm it looks like a familiar dimension issue. I would like to recommend these things:

  1. In /home/mmdeploy/mmdeploy/codebase/mmdet/deploy/object_detection_model.py",#109, try to print(test_outputs.shape) or something similar to check the dimension of output. From my guess, it looks like the model inference is successfully done but maybe your input is in batch, instead of single input or something is different from expected shape, so it fails when it does negative score filtering. Maybe you can simply change in this filtering to avoid error in this line and make it work for your specific usecase.
  2. Sometimes, the version of mmdeploy may be issue. Try to downgrade or use latest version of mmdeploy and test it.
  3. Also, if you have some extra preprocessing steps in the test_pipeline in your model_config, which is different than train_pipeline, maybe that will cause issue. I am not sure, but I would recommend trying with the default config of CoDetr with the open-source weights, and if you can convert that author's Co-Detr model to onnx successfully, then the issue is with your custom model-config.

Hope it helps and Happy Learning!! 🙂

Thanks for your quick reply, I will try it out

@KungFuPandaPro
Copy link

Hi! I figured out to convert co-dino to onnx and would like to share with you guys. I am using the model trained using mmdet:v3.3.0 and using mmdeploy:v1.3.1, onnxruntime:1.16.3. I found that the model trained using co-detr official repo (mmdet-2.25) requires lot of tinkering as the model backbone(swintransformer-v1 vs v2) is little different and also preprocessing functions and some utilities functions required for inference are different. So, if you train a new model from mmdet:v3.3.0 repo, I think you will be able to export it to onnx.

To solve this issue:

RuntimeError:Only tuples, lists and Variables are supported as JIT inputs/outputs. Dictionaries and strings are also accepted, but their usage is not recommended. Here, received an input of unsupported type: DetDataSample

I modified ...(your-env-path)/site-packages/torch/jit/trace.py In line 125, I added try except as a temporary fix. I am using the tuple format so that the code doesn't break later during visualization. I am following the same format as the plain dino export to onnx. The change is like this:

try:
    result_ = self.inner(*trace_inputs)
    result = result_[0]
    scores, bboxes, labels = result.pred_instances.scores, result.pred_instances.bboxes, result.pred_instances.labels
    
    # Combining scores and bboxes into the dino format
    combined_tensor = torch.cat((bboxes, scores.unsqueeze(1)), dim=1)
    formatted_labels = labels.unsqueeze(0).long()  # Retaining integer format

    # Creating tuples with combined tensors and labels
    formatted_result = (combined_tensor.unsqueeze(0), formatted_labels) 
    outs.append(formatted_result)
except:
    print(self.inner(*trace_inputs))
    outs.append(self.inner(*trace_inputs))

Also, we need to remove soft_nms operations, just comment this section out like below in your config file:

   ...
  test_cfg=[
        dict(
            max_per_img=300, 
            # nms=dict(iou_threshold=0.8, type='soft_nms')
            ),
  ...

Hope this helps! Thank you! :)

my environment is same with you,but I failure,the error is :
06/25 11:15:59 - mmengine - INFO -
scales.5.scale - torch.Size([]):
The value is the same before and after calling init_weights of CoATSSHead

Loads checkpoint by local backend from path: ..//mmdetection/co_detr_best.pth
06/25 11:16:01 - mmengine - WARNING - DeprecationWarning: get_onnx_config will be deprecated in the future.
06/25 11:16:01 - mmengine - INFO - Export PyTorch model to ONNX: ./work-dir\end2end.onnx.
torch.Size([197408, 1, 256])
06/25 11:19:28 - mmengine - INFO - Execute onnx optimize passes.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
_jit_pass_merge_shape_concate(): incompatible function arguments. The following argument types are supported:
1. (graph: torch::jit::Graph) -> None

Invoked with: graph(%input : Float(*, 3, *, *, strides=[7084032, 2361344, 2048, 1], requires_grad=0, device=cpu),
%backbone.patch_embed.projection.weight : Float(192, 3, 4, 4, strides=[48, 16, 4, 1], requires_grad=1, device=cpu),
%backbone.patch_embed.projection.bias : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.patch_embed.norm.weight : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.patch_embed.norm.bias : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.norm1.weight : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.norm1.bias : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.attn.w_msa.qkv.bias : Float(576, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.attn.w_msa.proj.bias : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.norm2.weight : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.norm2.bias : Float(192, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.ffn.layers.0.0.bias : Float(768, strides=[1], requires_grad=1, device=cpu),
%backbone.stages.0.blocks.0.ffn.layers.1.bias : Float(192, strides=[1], requires_grad=1, device=cpu),

could you help me ? is my config not right?could you provide your relate config?my order is :
image

@Clichong
Copy link

Clichong commented Sep 29, 2024

Finally! Finally! Finally! I have finally achieved the result I wanted. Thank you for your response, and also thank you to everyone who answered my questions. Thank you very much! @bibekyess ,@sirolf-otrebla

Can I consult how to use codetr's onnx for inference? @Sunny20236

@mkloftdyn
Copy link

Could someone point out what a working checkpoint/config pair is? I am currently trying to export the co-dino with mmdeploy.

my current attempts are

cd mmdeploy
python3 tools/deploy.py configs/mmdet/detection/detection_tensorrt_static-640x640.py ../mmdetection/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py ../checkpoints/co-detr/co_dino_5scale_swin_large_16e_o365tococo.pth demo/resources/det.jpg --work-dir /co-detr/co-dino-swin-l/ --device cuda:0 --show

where as the checkpoint is from this repository
image
and the config from the official mmdetection repo

it seems that there is a missing project 'projects.CO-DETR.codetr'

Is it required to train a new checkpoint with the official mmdetection repo?

@mkloftdyn
Copy link

mkloftdyn commented Oct 3, 2024

To answer my own question:

The following call creates both a .onnx and .engine file, if some changes are made as described in #26 (comment).

  • I had to additionally add the mmdetection path to the PYTHONPATH `export PYTHONPATH=$PYTHONPATH:/...dir.../mmdetection
  • the nms config is most likely located in the mmdetection/projects/CO-DETR/codino/co_dino_5_scale_r50_lsj_8xb2_1x_coco.py: line ~255 , since the configs base off each other (I used a config from the mmdetection repository)
  • the _trace.py file change worked as explained
cd /root/workspace/mmdeploy
python3 tools/deploy.py configs/mmdet/detection/detection_tensorrt_static-640x640.py ../mmdetection/projects/CO-DETR/configs/codino/co_dino_5scale_swin_l_16xb1_16e_o365tococo.py ../checkpoints/co-detr/co_dino_5scale_swin_large_16e_o365tococo.pth demo/resources/det.jpg --work-dir ../co-detr/co-dino-swin-l/ --device cuda:0 --show 

But, I was not able to properly visualize the output, unsure if it is an issue with the model or the post/preprocessing of the data, since I only want to investigate the inference speed I don't care about this right now.

FYI, on a laptop gpu NVIDIA RTX 2000 Ada, I get ~300ms/image, the model size decreased from 2.64GB -> 898MB(onnx) & 868MB(.engine)

@DataXujing
Copy link

I've implemented co-dino's onnx-based end-to-end TensorRT model acceleration inference:https://github.com/DataXujing/Co-DETR-TensorRT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests