Onnx forward error

Hi, I got the following error when I testing my onnx model
error: (-215:Assertion failed) start <= (int)shape.size() && end <= (int)shape.size() && start <= end in function 'total'

the code is

net = cv2.dnn.readNetFromONNX("test.onnx")
image = cv2.imread("20200922_101253.jpg")
blob = cv2.dnn.blobFromImage(image, 1.0 / 255, swapRB=True, crop=False)
preds = net.forward()

you probably need a size argument in blobFromImage()

(it seems, your network does not accept arbitrary input size)

can you look up, how you exported it from onnx, and what size your input was there ?

@berak I fixed my problem by changing my code for converting. previouly, I used dynamic_axes as this

    torch.onnx.export(model,  # model being run
                      generated_input,  # model input (or a tuple for multiple inputs)
                      args.output_path,  # where to save the model (can be a file or file-like object)
                      # export_params=True,  # store the trained parameter weights inside the model file
                      opset_version=11,  # the ONNX version to export the model to
                      do_constant_folding=True,  # whether to execute constant folding for optimization
                      input_names=['input'],  # the model's input names
                      output_names=['output'],  # the model's output names
                      dynamic_axes={'input': {0: 'batch_size'},  # variable length axes
                                    'output': {0: 'batch_size'}}

Later I canceled dynamic_axes. That fixed my problem.

1 Like