onnx / turnkeyml

The no-code AI toolchain
Apache License 2.0
74 stars 16 forks source link

Handle inconsistent opset placement in ONNX models #150

Open pcolange opened 7 months ago

pcolange commented 7 months ago

The current opset retrieval function assumes the default opset (ai.onnx) domain is always at the first index as shown below:

https://github.com/onnx/turnkeyml/blob/69a203e9d45cbce5c256c2bfe9238255a7227c7e/src/turnkeyml/build/onnx_helpers.py#L135-L136

However, some ONNX models may have this domain located elsewhere, leading to incorrect opset identification, for example:

image

Proposed solution:

def get_opset(model: onnx.ModelProto) -> int:
    opset = [x.version for x in model.opset_import if x.domain == ""]
    if opset:
        return opset[0]
    else:
        return None

Note that in the above code snippet x.domain == "" indicates the default domain which is ai.onnx.