ZhangGe6 / onnx-modifier

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

Cannot download download onnx because input tensor data's shape [N, 3, 224, 224] is not valid #72

Closed SimonDemarty closed 1 year ago

SimonDemarty commented 1 year ago

Hi,

I'm facing an error while trying to download a modified model.

How to get the error

  1. download resnet18-v1-7.onnx
  2. with python, modify its opset
    resnet18 = onnx.load('path/to/resnet18-v1-7.onnx')
    resnet18 = version_converter.convert_version(resnet18, 14)
    onnx.save(resnet18, "path/to/resnet18-v1-14.onnx")
  3. upload resnet18-v1-14.onnx to onnx-modifier
  4. delete every thing below the last relu (at this point, i can still download the modified model) Screenshot from 2023-08-03 10-19-04
  5. add an output at the end of modified model Screenshot from 2023-08-03 10-22-52
  6. Then I cannot download

Error message

In the terminal, the error message is as follows:

[2023-08-03 10:21:45,202] ERROR in app: Exception on /download [POST]
Traceback (most recent call last):
  File "/home/sd/ml/onnx-modifier/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sd/ml/onnx-modifier/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sd/ml/onnx-modifier/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sd/ml/onnx-modifier/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/sd/ml/onnx-modifier/app.py", line 27, in modify_and_download_model
    onnx_modifier.modify(modify_info)
  File "/home/sd/ml/onnx-modifier/onnx_modifier.py", line 337, in modify
    self.add_outputs(modify_info['added_outputs'])
  File "/home/sd/ml/onnx-modifier/onnx_modifier.py", line 199, in add_outputs
    inferred_value_info = shape_inference_using_onnx_tool(copy.deepcopy(self.model_proto))
  File "/home/sd/ml/onnx-modifier/utils/thrid_party_tools.py", line 11, in shape_inference_using_onnx_tool
    g.shape_infer()
  File "/home/sd/ml/onnx-modifier/lib/python3.10/site-packages/onnx_tool/graph.py", line 735, in shape_infer
    raise ValueError(
ValueError: The input tensor data's shape [N,3,224,224] is not valid, Please set it to a valid shape.
127.0.0.1 - - [03/Aug/2023 10:21:45] "POST /download HTTP/1.1" 500 -

Thanks in advance for any help you can provide

ZhangGe6 commented 1 year ago

@SimonDemarty Thanks for the feedback. It is a runtime error raised by onnx-tool, as it does not support dynamic shape currently.

I have added a fallback strategy. If shape inference using onnx-tool fails, primitive ONNX Python API will be invoked. The code has been updated and it should work now.

SimonDemarty commented 1 year ago

It does work! Thank you for the quick reply