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

模型转onnx后,verify计算误差报错 #50

Open
a773783082 opened this issue Nov 6, 2024 · 2 comments
Open

模型转onnx后,verify计算误差报错 #50

a773783082 opened this issue Nov 6, 2024 · 2 comments
Labels
question Further information is requested

Comments

@a773783082
Copy link

Question

onnx模型转成功了,但是后续的verify过程导入onnx模型报错。想问下该怎么解决?

## version
onnx                         1.17.0
onnxruntime-gpu              1.14.0
onnxsim                      0.4.36
onnx-graphsurgeon            0.5.2
torch                        1.11.0+cu113
WARNING: The shape inference of prim::Constant type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function.
WARNING: The shape inference of prim::Constant type is missing, so it may result in wrong shape inference for the exported graph. Please consider adding it in symbolic function.
Successfully exported ONNX model: /workspace/log_7cls_det/salience_detr_swin_l_480_800/2024-10-15-00_53_43/best_ap_save.onnx
Uncaught exception
Traceback (most recent call last):
  File "/workspace/train_code/Salience-DETR-main/tools/pytorch2onnx.py", line 145, in <module>
    pytorch2onnx()
  File "/workspace/train_code/Salience-DETR-main/tools/pytorch2onnx.py", line 132, in pytorch2onnx
    onnx_model = ONNXDetector(args.save_file)
  File "/workspace/train_code/Salience-DETR-main/tools/pytorch2onnx.py", line 21, in __init__
    self.session = onnxruntime.InferenceSession(
  File "/root/miniconda3/lib/python3.9/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 360, in __init__
    self._create_inference_session(providers, provider_options, disabled_optimizers)
  File "/root/miniconda3/lib/python3.9/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 397, in _create_inference_session
    sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : Load model from /workspace/log_7cls_det/salience_detr_swin_l_480_800/2024-10-15-00_53_43/best_ap_save.onnx failed:This is an invalid model. Type Error: Type 'tensor(int32)' of input parameter (onnx::TopK_19989) of operator (TopK) in node (TopK_16698) is invalid.

补充信息

No response

@a773783082 a773783082 added the question Further information is requested label Nov 6, 2024
@xiuqhou
Copy link
Owner

xiuqhou commented Nov 7, 2024

看报错信息是topk运算的输入参数类型不对,应该输入的是int64类型的参数。
我这边没有复现出来这个问题,但代码中用到topk的地方不多,你可以直接全文搜索,将topk的k参数换成int64类型再导出试一试,类似这样:

# 修改前
valid_score.topk(level_token_nums[level_idx], dim=1)
# 修改后
valid_score.topk(level_token_nums[level_idx].to(torch.int64), dim=1)

@a773783082
Copy link
Author

@xiuqhou ,感谢,修改后成功了,👍,只是误差有点大

Successfully exported ONNX model: /workspace/log_7cls_det/salience_detr_swin_l_480_800/2024-10-15-00_53_43/best_ap_save.onnx
Uncaught exception
Traceback (most recent call last):
  File "/workspace/train_code/Salience-DETR-main/tools/pytorch2onnx.py", line 145, in <module>
    pytorch2onnx()
  File "/workspace/train_code/Salience-DETR-main/tools/pytorch2onnx.py", line 138, in pytorch2onnx
    np.testing.assert_allclose(
  File "/root/miniconda3/lib/python3.9/site-packages/numpy/testing/_private/utils.py", line 1592, in assert_allclose
    assert_array_compare(compare, actual, desired, err_msg=str(err_msg),
  File "/root/miniconda3/lib/python3.9/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/root/miniconda3/lib/python3.9/site-packages/numpy/testing/_private/utils.py", line 862, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Not equal to tolerance rtol=0.001, atol=1e-05
The numerical values are different between Pytorch and ONNXBut it does not necessarily mean the exported ONNX is problematic.
Mismatched elements: 255 / 300 (85%)
Max absolute difference: 0.00075046
Max relative difference: 0.04880792
 x: array([0.043474, 0.041977, 0.041789, 0.04153 , 0.041011, 0.040653,
       0.040344, 0.039332, 0.038988, 0.038698, 0.03841 , 0.038371,
       0.038008, 0.037157, 0.036468, 0.035893, 0.035884, 0.035836,...
 y: array([0.043971, 0.042558, 0.042212, 0.041955, 0.040835, 0.040663,
       0.040282, 0.03931 , 0.039249, 0.039102, 0.038791, 0.038654,
       0.038188, 0.037154, 0.036845, 0.036507, 0.036416, 0.036176,...

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

No branches or pull requests

2 participants