opencv: error when load onnx model

I am trying to load a onnx model which is converted from pytorch.

import cv2 model = cv2.dnn.readNetFromONNX('tmp.onnx')

cv2.error: OpenCV(4.1.2) /io/opencv/modules/dnn/src/dnn.cpp:525: error: (-2:Unspecified error) Can’t create layer “166” of type “Cast” in function ‘getLayerInstance’

here is my model https://www.dropbox.com/s/sg0n1kdpbbio848/tmp.onnx?dl=0

anyone have idea how to fix this problem? Thanks a lot.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 27 (16 by maintainers)

Most upvoted comments

@ashishkrshrivastava, probably, it’s about shape values. Usually, they are constant and we just cast them to integers manually,

self.up = nn.ConvTranspose2d(int(in_channels//2), int(in_channels//2), kernel_size=2, stride=2)

So this is a workaround - export model but with int(*) casts.

However if it is impossible, it would be good feature to support. I believe this graph fusion for ONNX models can help to make it easier if we can determine which subgraph should be fused: https://github.com/opencv/opencv/pull/16291

@ashishkrshrivastava @dkurt , Thanks for your answer. Now, I have installed OpenCV from master branch. But, now I am getting another error. cv2.error: OpenCV(4.2.0-dev) /Users/adityakumar/opencv/modules/dnn/src/graph_simplifier.cpp:79: error: (-212:Parsing error) Input node with name 2885 not found in function 'getInputNodeId' Here is the link to my model: model Please, check what can i do to resolve it?

@ashishkrshrivastava I didn’t actually install onnx in my pytorch environment. I am importing pytorch.onnx. Don’t know what version they are using.