InternImage icon indicating copy to clipboard operation
InternImage copied to clipboard

Cannot export classification model to ONNX

Open smallriver0316 opened this issue 2 years ago • 3 comments

I tried to export a classification model to ONNX model according to README in the classification directory. But it was failed with following error messages.

(internimage) ubuntu@ip-10-0-0-189:~/InternImage/classification$  python export.py --model_name internimage_t_1k_224 --ckpt_dir ckpt_dir/ --onnx
=> merge config from ./configs/internimage_t_1k_224.yaml
using core type: DCNv3
using activation layer: GELU
using main norm layer: LN
using dpr: linear, 0.2
level2_post_norm: False
level2_post_norm_block_ids: None
res_post_norm: False
remove_center: False
Traceback (most recent call last):
  File "export.py", line 121, in <module>
    main()
  File "export.py", line 112, in main
    torch2onnx(args, cfg)
  File "export.py", line 65, in torch2onnx
    output_names=['output'])
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/__init__.py", line 309, in export
    export_modules_as_functions)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 122, in export
    custom_opsets=custom_opsets, export_modules_as_functions=export_modules_as_functions)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 724, in _export
    dynamic_axes=dynamic_axes)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 507, in _model_to_graph
    module=module)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 232, in _optimize_graph
    graph = torch._C._jit_pass_onnx(graph, operator_export_type)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/__init__.py", line 359, in _run_symbolic_method
    return utils._run_symbolic_method(*args, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 846, in _run_symbolic_method
    return symbolic_fn(g, *args)
  File "/home/ubuntu/InternImage/classification/ops_dcnv3/functions/dcnv3_func.py", line 105, in symbolic
    remove_center=int(remove_center),
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 952, in _graph_op
    n = g.insertNode(_newNode(g, opname, outputs, *args, **kwargs))
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 905, in _newNode
    _add_attribute(n, k, v, aten=aten)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 869, in _add_attribute
    " must be suffixed with type, e.g. 'dim_i' or 'dims_i'").format(key))
IndexError: Invalid attribute specifier 'remove_center' names  must be suffixed with type, e.g. 'dim_i' or 'dims_i'

The environment is

smallriver0316 avatar Oct 08 '23 10:10 smallriver0316

I found that changes of commit 8f2d158399b95b2188b86dd46da93c25c2dbb7cc have probably caused this problem.

https://github.com/OpenGVLab/InternImage/commit/8f2d158399b95b2188b86dd46da93c25c2dbb7cc

$ python export.py --model_name internimage_t_1k_224 --ckpt_dir ckpt_dir/ --onnx
=> merge config from ./configs/internimage_t_1k_224.yaml
using core type: DCNv3
using activation layer: GELU
using main norm layer: LN
using dpr: linear, 0.2
level2_post_norm: False
level2_post_norm_block_ids: None
res_post_norm: False
remove_center: False
Traceback (most recent call last):
  File "export.py", line 121, in <module>
    main()
  File "export.py", line 112, in main
    torch2onnx(args, cfg)
  File "export.py", line 65, in torch2onnx
    output_names=['output'])
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/__init__.py", line 309, in export
    export_modules_as_functions)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 122, in export
    custom_opsets=custom_opsets, export_modules_as_functions=export_modules_as_functions)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 724, in _export
    dynamic_axes=dynamic_axes)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 499, in _model_to_graph
    graph, params, torch_out, module = _create_jit_graph(model, args)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 440, in _create_jit_graph
    graph, torch_out = _trace_and_get_graph_from_model(model, args)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/onnx/utils.py", line 391, in _trace_and_get_graph_from_model
    torch.jit._get_trace_graph(model, args, strict=False, _force_outplace=False, _return_inputs_states=True)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/jit/_trace.py", line 1166, in _get_trace_graph
    outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/jit/_trace.py", line 132, in forward
    self._force_outplace,
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/jit/_trace.py", line 118, in wrapper
    outs.append(self.inner(*trace_inputs))
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1098, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/InternImage/classification/models/intern_image.py", line 766, in forward
    x = self.forward_features(x)
  File "/home/ubuntu/InternImage/classification/models/intern_image.py", line 725, in forward_features
    x = level(x)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1098, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/InternImage/classification/models/intern_image.py", line 512, in forward
    x = blk(x)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1098, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/InternImage/classification/models/intern_image.py", line 430, in forward
    x = _inner_forward(x)
  File "/home/ubuntu/InternImage/classification/models/intern_image.py", line 416, in _inner_forward
    x = x + self.drop_path(self.dcn(self.norm1(x)))
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/nn/modules/module.py", line 1098, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "/home/ubuntu/InternImage/classification/ops_dcnv3/modules/dcnv3.py", line 345, in forward
    self.remove_center)
  File "/home/ubuntu/anaconda3/envs/internimage/lib/python3.7/site-packages/torch/cuda/amp/autocast_mode.py", line 110, in decorate_fwd
    return fwd(*args, **kwargs)
  File "/home/ubuntu/InternImage/classification/ops_dcnv3/functions/dcnv3_func.py", line 49, in forward
    group_channels, offset_scale, ctx.im2col_step, **kwargs)
TypeError: dcnv3_forward(): incompatible function arguments. The following argument types are supported:
    1. (arg0: at::Tensor, arg1: at::Tensor, arg2: at::Tensor, arg3: int, arg4: int, arg5: int, arg6: int, arg7: int, arg8: int, arg9: int, arg10: int, arg11: int, arg12: int, arg13: float, arg14: int, arg15: int) -> at::Tensor

Invoked with: tensor([[[[-4.6765e-01, -4.2197e-01,  6.0807e-02,  ...,  1.0714e-01,
            5.4229e-01,  1.9236e-02],
          [-1.0968e-01,  2.8766e-01,  2.4005e-01,  ..., -6.1618e-02,
            7.7031e-01,  5.1768e-02],
          [-3.8835e-01,  3.7148e-01, -1.2781e-01,  ..., -2.2630e-01,
            4.8191e-01,  3.8509e-02],
          ...,
          [-2.7780e-01,  5.5153e-01, -1.3241e-01,  ..., -2.0382e-01,
            4.1559e-01,  3.0929e-01],
          [-4.4095e-01,  5.9974e-01,  6.8127e-03,  ...,  6.2164e-03,
            2.8223e-01,  7.7597e-02],
          [-4.4174e-02,  3.5850e-01,  2.4612e-01,  ..., -3.9771e-01,
            8.4857e-01, -3.9927e-02]],

         [[ 5.4233e-02, -1.1951e-01, -1.0995e-01,  ...,  2.2794e-01,
            3.4253e-01,  1.1734e-01],
          [-1.9957e-01, -4.4482e-01, -5.7037e-01,  ..., -2.7812e-01,
           -3.8517e-01,  1.2238e-01],
          [ 3.0182e-01, -1.4350e-01, -5.1033e-01,  ..., -5.5779e-02,
            1.1078e-01,  3.9052e-02],
          ...,
          [ 2.5119e-01, -1.2241e-01, -4.9132e-02,  ..., -2.7280e-01,
           -2.3096e-02, -1.9215e-01],
          [-5.2790e-01,  3.7251e-02,  2.1808e-01,  ..., -3.8262e-01,
           -3.1527e-02,  1.1366e-02],
          [ 4.3142e-01, -4.2621e-02, -1.5395e-01,  ..., -2.0733e-01,
           -1.1442e-01, -1.8908e-02]],

         [[-3.5645e-01,  6.6657e-02,  5.2657e-01,  ...,  2.2563e-01,
            5.6672e-01, -8.8984e-02],
          [-3.9088e-01,  6.6069e-01,  9.8569e-02,  ..., -2.7920e-01,
           -2.3524e-01,  1.2398e-01],
          [-4.8302e-01, -1.1691e-01,  3.0573e-01,  ..., -3.4909e-01,
           -1.9151e-01, -2.5360e-01],
          ...,
          [-3.0950e-01,  2.9786e-01,  7.0305e-02,  ..., -2.2046e-01,
           -7.6774e-02,  1.5473e-01],
          [ 5.3203e-01, -3.0058e-01,  1.5836e-03,  ..., -7.5064e-02,
            4.0212e-01,  2.4620e-01],
          [-1.9144e-01,  9.2998e-05, -8.9839e-02,  ..., -3.4417e-01,
            2.4298e-01, -1.2038e-01]],

         ...,

         [[ 1.2952e-01, -4.0685e-01,  8.5675e-01,  ...,  6.6157e-01,
            5.7936e-01, -4.3543e-01],
          [-1.7371e-01,  1.0144e-01,  4.1771e-01,  ...,  2.0275e-01,
           -9.2321e-02,  6.3147e-04],
          [-2.9344e-02, -4.4817e-01,  1.4441e-01,  ..., -1.2976e-01,
           -3.7015e-01, -5.9597e-02],
          ...,
          [ 6.8723e-03,  4.2173e-01, -1.6490e-01,  ..., -1.2973e-01,
            8.1808e-02, -1.6639e-01],
          [-3.8542e-01,  3.0564e-01, -5.3050e-03,  ..., -3.9722e-01,
            6.1607e-02, -2.4981e-02],
          [ 1.0085e-01, -2.0305e-01, -1.6138e-01,  ..., -7.7343e-02,
           -1.3430e-01,  1.9485e-01]],

         [[-7.9828e-02, -1.8743e-01,  3.2408e-01,  ...,  1.7548e-01,
            4.7418e-01, -1.9687e-01],
          [-5.8841e-01, -1.3589e-01, -3.6579e-01,  ..., -3.5333e-01,
            2.7716e-02,  1.2175e-01],
          [-7.5884e-02,  2.7846e-01,  8.2954e-02,  ..., -4.0592e-01,
            6.5394e-02,  3.8879e-01],
          ...,
          [-9.1677e-02,  1.4918e-01, -2.3913e-01,  ..., -1.6788e-01,
           -8.4728e-02, -1.0471e-01],
          [ 7.6186e-01, -4.4413e-01, -3.5728e-01,  ..., -2.0602e-01,
           -4.3217e-02,  2.8945e-01],
          [-9.2108e-02,  8.9774e-02, -5.4875e-01,  ..., -2.8431e-01,
           -1.2403e-01,  7.7167e-02]],

         [[-2.3886e-01, -1.3138e-01,  3.9558e-01,  ...,  2.6036e-01,
            3.7969e-01, -3.6619e-01],
          [-1.2701e-01,  5.2801e-01, -4.5345e-02,  ..., -2.8627e-01,
            2.7628e-02,  1.7306e-01],
          [-7.2136e-02,  2.5477e-02,  1.4034e-01,  ..., -2.4256e-01,
           -2.0102e-01, -1.4860e-01],
          ...,
          [-5.3404e-02,  4.2754e-01,  1.8371e-02,  ..., -1.2657e-01,
           -2.5988e-01, -1.1474e-01],
          [-2.9916e-01,  6.7820e-01, -2.9635e-01,  ..., -1.6857e-01,
           -1.9597e-02,  1.7084e-01],
          [ 4.0832e-01, -3.5684e-02,  9.1102e-01,  ..., -2.0259e-01,
            3.1585e-01, -3.9386e-01]]]], device='cuda:0',
       grad_fn=<AddBackward0>), tensor([[[[-0.5928,  0.6790,  1.3195,  ...,  4.5488, -0.9982,  6.1315],
          [-0.5782,  0.6907,  1.2045,  ...,  4.5504, -1.2954,  6.0554],
          [-0.6523,  0.5459,  1.0743,  ...,  4.4112, -1.3595,  5.8574],
          ...,
          [-0.5950,  0.5799,  1.2367,  ...,  4.6545, -1.0932,  6.2976],
          [-0.6645,  0.9539,  1.2341,  ...,  4.5624, -1.2763,  6.1479],
          [-0.3889,  0.7678,  1.2711,  ...,  4.9012, -1.1082,  6.6677]],

         [[-0.5805,  0.7389,  1.0743,  ...,  4.5676, -0.7191,  6.0390],
          [-0.7277,  0.7867,  0.9045,  ...,  4.6617, -1.0535,  6.3922],
          [-0.9212,  0.8381,  0.7461,  ...,  4.5766, -0.9332,  5.9639],
          ...,
          [-0.8907,  0.5791,  1.0640,  ...,  4.7195, -0.9491,  6.2605],
          [-0.8991,  0.7592,  0.9439,  ...,  4.4987, -0.8906,  6.0442],
          [-0.9392,  0.9288,  0.9961,  ...,  4.7385, -0.8684,  6.5438]],

         [[-0.7160,  0.6456,  1.1418,  ...,  4.7414, -0.9239,  6.2186],
          [-0.8872,  0.6170,  0.9740,  ...,  4.7376, -1.0611,  6.2250],
          [-0.9317,  0.4504,  1.0573,  ...,  4.5252, -1.1890,  5.8526],
          ...,
          [-0.7416,  0.6710,  1.1090,  ...,  4.6707, -1.1684,  6.6294],
          [-0.9892,  0.4752,  0.9795,  ...,  4.4836, -0.9691,  5.8065],
          [-0.8462,  0.5647,  1.0336,  ...,  4.7973, -0.9707,  6.3381]],

         ...,

         [[-0.4761,  0.7986,  1.0850,  ...,  4.8306, -0.8325,  6.5305],
          [-0.8435,  0.5465,  1.1957,  ...,  4.5269, -1.2263,  6.1522],
          [-0.7890,  0.4995,  1.1099,  ...,  4.4734, -1.0913,  6.0248],
          ...,
          [-0.8840,  0.6294,  0.9372,  ...,  4.4714, -0.9068,  5.8156],
          [-0.8075,  0.6659,  1.0219,  ...,  4.6203, -1.0859,  6.0948],
          [-0.6666,  0.5310,  0.9849,  ...,  4.5458, -0.9914,  6.1484]],

         [[-0.6323,  0.6200,  1.1372,  ...,  4.6640, -0.8417,  6.1914],
          [-0.5248,  0.8177,  1.1210,  ...,  4.7104, -1.1385,  6.6570],
          [-0.9032,  0.6442,  0.9184,  ...,  4.8537, -1.0460,  6.2843],
          ...,
          [-0.8778,  0.5821,  0.9644,  ...,  4.5857, -0.9153,  6.1122],
          [-0.8188,  0.5875,  1.0151,  ...,  4.5335, -1.0260,  6.1869],
          [-0.7261,  0.6305,  1.0700,  ...,  4.7015, -1.0177,  6.2973]],

         [[-0.4941,  0.6583,  1.2299,  ...,  4.7833, -0.8020,  6.6197],
          [-0.6274,  0.6860,  1.1028,  ...,  4.7337, -0.9022,  6.5687],
          [-0.7527,  0.7736,  0.9567,  ...,  4.8420, -0.8560,  6.4241],
          ...,
          [-0.6877,  0.8529,  1.0633,  ...,  4.6611, -0.9536,  6.3576],
          [-0.7523,  0.7166,  1.0473,  ...,  4.7613, -0.9024,  6.3001],
          [-0.6006,  0.7225,  1.0519,  ...,  4.7283, -0.8990,  6.6008]]]],
       device='cuda:0', grad_fn=<AddBackward0>), tensor([[[[0.0582, 0.0902, 0.0730,  ..., 0.0925, 0.1473, 0.1316],
          [0.0816, 0.0706, 0.0920,  ..., 0.0771, 0.1516, 0.1330],
          [0.0595, 0.0559, 0.0757,  ..., 0.0693, 0.1427, 0.1357],
          ...,
          [0.0497, 0.0875, 0.0602,  ..., 0.1015, 0.1339, 0.1121],
          [0.0569, 0.0770, 0.0789,  ..., 0.0840, 0.1249, 0.1358],
          [0.0535, 0.0876, 0.0515,  ..., 0.1120, 0.1249, 0.1072]],

         [[0.0351, 0.0672, 0.0434,  ..., 0.1184, 0.1499, 0.1115],
          [0.0285, 0.0568, 0.0371,  ..., 0.1190, 0.1599, 0.1127],
          [0.0243, 0.0515, 0.0363,  ..., 0.1130, 0.1350, 0.1191],
          ...,
          [0.0198, 0.0526, 0.0457,  ..., 0.1188, 0.1338, 0.1098],
          [0.0239, 0.0560, 0.0456,  ..., 0.0991, 0.1318, 0.1188],
          [0.0281, 0.0650, 0.0517,  ..., 0.0972, 0.1355, 0.1237]],

         [[0.0330, 0.0589, 0.0356,  ..., 0.1244, 0.1215, 0.1079],
          [0.0240, 0.0479, 0.0377,  ..., 0.1250, 0.0981, 0.1093],
          [0.0191, 0.0615, 0.0468,  ..., 0.1433, 0.1210, 0.1016],
          ...,
          [0.0196, 0.0737, 0.0327,  ..., 0.1409, 0.0842, 0.1022],
          [0.0155, 0.0561, 0.0380,  ..., 0.1514, 0.0934, 0.1040],
          [0.0168, 0.0532, 0.0353,  ..., 0.1395, 0.0935, 0.0934]],

         ...,

         [[0.0593, 0.0598, 0.0369,  ..., 0.1055, 0.1213, 0.1170],
          [0.0153, 0.0501, 0.0477,  ..., 0.1125, 0.1358, 0.1177],
          [0.0222, 0.0660, 0.0312,  ..., 0.1348, 0.1213, 0.0964],
          ...,
          [0.0329, 0.0486, 0.0364,  ..., 0.1475, 0.0951, 0.0975],
          [0.0168, 0.0510, 0.0300,  ..., 0.1319, 0.0962, 0.1054],
          [0.0138, 0.0772, 0.0332,  ..., 0.1113, 0.1095, 0.1210]],

         [[0.0392, 0.0582, 0.0335,  ..., 0.1467, 0.1250, 0.1012],
          [0.0174, 0.0611, 0.0312,  ..., 0.1188, 0.0983, 0.1089],
          [0.0303, 0.0415, 0.0314,  ..., 0.1314, 0.0940, 0.1021],
          ...,
          [0.0328, 0.0422, 0.0473,  ..., 0.1295, 0.1302, 0.1033],
          [0.0177, 0.0501, 0.0304,  ..., 0.1322, 0.1225, 0.1058],
          [0.0124, 0.0667, 0.0227,  ..., 0.1218, 0.1098, 0.1161]],

         [[0.0259, 0.0795, 0.0280,  ..., 0.1336, 0.1134, 0.1033],
          [0.0160, 0.0682, 0.0237,  ..., 0.1181, 0.1016, 0.1130],
          [0.0171, 0.0654, 0.0226,  ..., 0.1153, 0.0839, 0.1107],
          ...,
          [0.0207, 0.0680, 0.0375,  ..., 0.1207, 0.1208, 0.1201],
          [0.0160, 0.0641, 0.0284,  ..., 0.1263, 0.1019, 0.1122],
          [0.0157, 0.0729, 0.0223,  ..., 0.1279, 0.0907, 0.1017]]]],
       device='cuda:0', grad_fn=<ReshapeAliasBackward0>), 3, 3, 1, 1, 1, 1, 1, 1, 4, 16, 1.0, 256

I confirmed that the version of b64d9ca3000f4753e26d3b8c8fec05052114a126, latest commit before 8f2d158399b95b2188b86dd46da93c25c2dbb7cc is possible to export a ckpt model to an ONNX model.

smallriver0316 avatar Oct 14 '23 00:10 smallriver0316

change remove_center=int(remove_center) to remove_center_i=int(remove_center) in dcnv3_func.py can solve it

senlinuc avatar Oct 20 '23 07:10 senlinuc

change remove_center=int(remove_center) to remove_center_i=int(remove_center) in dcnv3_func.py can solve it

The same problem occurs even if I fix it. Do you happen to know the solution?

taewookum avatar Mar 07 '24 08:03 taewookum