Skip to content

readNetFromONNX error slice/index operations #14672

@cr145

Description

@cr145
System information (version)
  • OpenCV => 4.1.0
  • python 3.7.3
  • Operating System / Platform => Ubuntu 64 Bit
  • PyTorch => 1.1.0
  • onnx => 1.5.0
  • conda => 4.6.14
Detailed description

I'm trying to export a model from PyTorch to ONNX to be run in OpenCV, but am getting an error when running cv2.dnn.readNetFromONNX. I've isolated the cause of the error to slicing a tensor. As an example, I've attached a file that attempts to follow these steps for a simple model. The model slices the last dimension of the input and returns.

Script:

import torch
import torch.nn as nn
import onnx
import cv2 

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()

    def forward(self, x): 
        return x[...,2:5]

dummy = torch.rand((4,3,10,10))
net = Net()
torch.onnx.export(net, dummy, 'test.onnx', verbose=True)

model_cv2 = cv2.dnn.readNetFromONNX('test.onnx')

I get the following error:

cv2.error: OpenCV(4.1.0) /io/opencv/modules/dnn/src/layers/slice_layer.cpp:147: error: (-215:Assertion failed) requiredOutputs > 0 && inpShape[axis] % requiredOutputs == 0 in function 'getMemoryShapes'

The ONNX graph:

graph(%0 : Float(4, 3, 10, 10)):
  %1 : Float(4, 3, 10!, 3) = onnx::Slice[axes=[3], ends=[5], starts=[2]](%0), scope: Net
  return (%1) 
Steps to reproduce

I've attached a python script to reproduce.

I've also attached the ONNX model.
files.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions