microsoft / onnxjs

ONNX.js: run ONNX models using JavaScript
Other
1.77k stars 129 forks source link

Cannot resolve operator "Shape" with opset: ai.onnx v9 #265

Open weingaunity opened 3 years ago

weingaunity commented 3 years ago

Hi, i try to load a onnx model of mobilenet v3 which i have exportet with pytorch.

When i try to load the model in the browser using following exception is thrown:

Uncaught (in promise) TypeError: cannot resolve operator 'Shape' with opsets: ai.onnx v9

According to the documentation it should be supported starting from version 1+.

Which backend is used by onnx.min.js?

Here the onnx creation process:

from mobilenetv3 import mobilenetv3_large, mobilenetv3_small

net_large = mobilenetv3_large()
net_small = mobilenetv3_small()

net_large.load_state_dict(torch.load('pretrained/mobilenetv3-large-1cd25616.pth'))
net_small.load_state_dict(torch.load('pretrained/mobilenetv3-small-55df8e1f.pth'))

net_large.classifier=net_large.classifier[:-1]
net_small.classifier=net_small.classifier[:-1]

x = torch.randn(1, 3, 224, 224)
torch.onnx.export(
    net_small,
    x,
    "../01_WebApp/mobilenetv3_small.onnx",
    export_params=True,
    input_names=['input'],
    output_names=['output'],
    #opset_version=10,
)
31b41a59l26u53 commented 3 years ago

Similar here. Why is this not in the WebGl operator set?

weingaunity commented 3 years ago

Despite the fact, that in my case webgl seems not to work, i worked around the missing shape function issue by changing the network structure.

But at the end i used tensorflow to inference. So i converted the onnx model to a tensorflow model and exported to tensorflow.js. This works very well and supports webgl.

smilemakc commented 3 years ago

Uncaught (in promise) TypeError: cannot resolve operator 'Shape' with opsets: ai.onnx v9

You need replace code in places such as x.view(x.size(0), -1) on explicit value instead of x.size(0)