ZhangGe6 / onnx-modifier

A tool to modify ONNX models in a visualization fashion, based on Netron and Flask.
MIT License
1.27k stars 157 forks source link

Cannot modify input / output sizes #69

Closed ghost closed 3 months ago

ghost commented 1 year ago

While editing ONNX, one might remove nodes between output / input, which in turn affect the input / output shapes. Unfortunately, the input and output shapes cannot be modified in the editor. Checking the net using the following command:

>>> model = onnx.load("model.onnx")
>>> onnx.checker.check_model(model)
>>> onnx.checker.check_model(model, True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/lib/python3.11/site-packages/onnx/checker.py", line 108, in check_model
    onnx.shape_inference.infer_shapes(model, check_type=True, strict_mode=True)
  File "/opt/homebrew/lib/python3.11/site-packages/onnx/shape_inference.py", line 34, in infer_shapes
    inferred_model_str = C.infer_shapes(model_str, check_type, strict_mode, data_prop)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnx.onnx_cpp2py_export.shape_inference.InferenceError: [ShapeInferenceError] (op_type:Conv, node name: Conv18): [ShapeInferenceError] Inferred shape and existing shape differ in dimension 2: (357) vs (30)

Yields various errors, depending whether the input or the output have wrong shapes. Please add a way to either strip or modify the input / output shapes, as saving the modified network will not result in a correct model that can be used.

ZhangGe6 commented 1 year ago

@piotrwierzba-tplx Thanks for reporting. It IS a problem that the current version cannot handle well. I am thinking about how to fix it. Could you try to add some ONNX operators that can modify the input / output shape, such as Reshape and Transpose?

KONGYOUYL commented 5 months ago

reporting @ZhangGe6

Hello, there are some questions in op reshape. I add the op before output that want to shape it from [1, 5, 640, 640] to [1,5,409600],but it failed, can you help me to solve this problem? wait for your reply. image image

ZhangGe6 commented 5 months ago

@KONGYOUYL Hi, Sorry for the late response. Can you explain the failure with more info? It is suggested to share the error log in the console. Thanks.

adizhol commented 4 months ago

@ZhangGe6 I'm facing a similar issue. The error I get is:

[04/16/2024-15:39:02] [E] [TRT] ModelImporter.cpp:771: While parsing node number 40 [Reshape -> "layer3_outconv_reshaped"]:
[04/16/2024-15:39:02] [E] [TRT] ModelImporter.cpp:772: --- Begin node ---
[04/16/2024-15:39:02] [E] [TRT] ModelImporter.cpp:773: input: "layer3_outconv_transp"
input: "2,4096,256"
output: "layer3_outconv_reshaped"
name: "custom_added_Reshape0"
op_type: "Reshape"

[04/16/2024-15:39:02] [E] [TRT] ModelImporter.cpp:774: --- End node ---
[04/16/2024-15:39:02] [E] [TRT] ModelImporter.cpp:777: ERROR: ModelImporter.cpp:150 In function parseGraph:
[5] Assertion failed: (ctx->tensors().count(inputName)) && "Node input was not registered."
[04/16/2024-15:39:02] [E] Failed to parse onnx file
ZhangGe6 commented 3 months ago

Editing input shape is supported in this commit. The Output sizes are not supposed to be changed manually and should rely on the result of shape inference. Please try the shape inference feature.