mortont / axon_onnx

Easily convert models between ONNX and Axon
Apache License 2.0
95 stars 20 forks source link

Unsupported Dimension Type for Valid Model #33

Closed jnnks closed 2 years ago

jnnks commented 2 years ago

The model Tiny YOLOv3 raises an ArgumentError when trying to import it:

{model, params} = AxonOnnx.import("models/tiny-yolov3-11.onnx")

** (ArgumentError) unsupported dimension type
    (axon_onnx 0.1.0) lib/axon_onnx/deserialize.ex:1824: anonymous fn/2 in AxonOnnx.Deserialize.shape!/2
    (elixir 1.13.2) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.13.2) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
    (axon_onnx 0.1.0) lib/axon_onnx/deserialize.ex:1809: AxonOnnx.Deserialize.shape!/2
    (axon_onnx 0.1.0) lib/axon_onnx/deserialize.ex:55: anonymous fn/4 in AxonOnnx.Deserialize.get_inputs/3
    (elixir 1.13.2) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (axon_onnx 0.1.0) lib/axon_onnx/deserialize.ex:42: AxonOnnx.Deserialize.graph_to_axon/2
    (axon_onnx 0.1.0) lib/axon_onnx/deserialize.ex:27: AxonOnnx.Deserialize.to_axon/2

15:34:24.856 [warn]  N has no specified dimension, assuming nil

I tested the model with the ONNX checker script and it seems to be valid.

That happens on master. Please let me know what information you need. Thanks

seanmor5 commented 2 years ago

Thanks for reporting this! It seems the dimension type is somehow nil, which shouldn't be possible according to the protocol: https://github.com/onnx/onnx/blob/main/onnx/onnx.proto#L646

@ahamez Do you think this could possibly be a bug with how we are parsing the protocol buffer?

ahamez commented 2 years ago

@seanmor5 It's not mandatory to set a oneof field. Thus, this nil value is legit. However, the proto file does not define how this case should be handled 🤔

seanmor5 commented 2 years ago

@ahamez I did not know that! I will see if I can dig further to figure out what the expected behavior is in this cases

seanmor5 commented 2 years ago

@jnnks Okay so the fix for this is going to have to happen in Axon upstream. Basically the dimensions are just supposed to be filled in at runtime, but Axon does not yet allow that. I was already planning to make Axon's shape inference stuff "Lazy", so that should allow you to successfully import this model. In the mean time, if you are able to specify the empty dimensions for input height and width by editing the ONNX model manually, then it should import :)

jnnks commented 2 years ago

Thanks for your quick response. Manually adding the input dimension worked and the error does not come up anymore.

jnnks commented 2 years ago

could this be solved during Axon.init? At this point we have the input Tensor template provided by the user. Can the shape be "injected", what do you think?