Skip to content

Problem with loading ONNX networks with Resize layer #18300

@sl-sergei

Description

@sl-sergei
System information (version)
  • OpenCV => 4.4.0-dev or 3.4.11-pre
  • Operating System / Platform => Ubuntu 18.04
  • Compiler => gcc 7.5.0
Detailed description

OpenCV has the following problem with loading networks with Resize layer:

cv2.error: OpenCV(3.4.11-pre) /home/ssl/Documents/projects/OpenCV/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1424: error: (-2:Unspecified error) in function 'void cv::dnn::experimental_dnn_34_v18::ONNXImporter::populateNet(cv::dnn::experimental_dnn_34_v18::Net)'

(expected: 'shapes.depth() == CV_32S'), where
'shapes.depth()' is 5 (CV_32FC1)
must be equal to
'CV_32S' is 4 (CV_32SC1)

It prevents OpenCV from loading various networks from Segmentation Models Pytorch (https://github.com/qubvel/segmentation_models.pytorch) or Human Segmentation Pytorch (https://github.com/thuyngch/Human-Segmentation-PyTorch)

Steps to reproduce

reproducer_resize.py

import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F


class Conv2dReLU(nn.Sequential):
    def __init__(
            self,
            in_channels,
            out_channels,
            kernel_size,
            padding=0,
            stride=1,
            use_batchnorm=True,
    ):

        conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
            stride=stride,
            padding=padding,
            bias=not (use_batchnorm),
        )
        relu = nn.ReLU(inplace=True)

        if use_batchnorm and use_batchnorm != "inplace":
            bn = nn.BatchNorm2d(out_channels)

        else:
            bn = nn.Identity()

        super(Conv2dReLU, self).__init__(conv, bn, relu)

class Attention(nn.Module):

    def __init__(self, name, **params):
        super().__init__()

        if name is None:
            self.attention = nn.Identity(**params)
        else:
            raise ValueError("Attention {} is not implemented".format(name))

    def forward(self, x):
        return self.attention(x)

class DecoderBlock(nn.Module):
    def __init__(
            self,
            in_channels,
            skip_channels,
            out_channels,
            use_batchnorm=True,
            attention_type=None,
    ):
        super().__init__()
        self.conv1 = Conv2dReLU(
            in_channels + skip_channels,
            out_channels,
            kernel_size=3,
            padding=1,
            use_batchnorm=use_batchnorm,
        )
        self.conv2 = Conv2dReLU(
            out_channels,
            out_channels,
            kernel_size=3,
            padding=1,
            use_batchnorm=use_batchnorm,
        )

    def forward(self, x, skip=None):
        x = F.interpolate(x, scale_factor=2, mode="nearest")
        if skip is not None:
            x = torch.cat([x, skip], dim=1)
        x = self.conv1(x)
        x = self.conv2(x)
        return x

model = DecoderBlock(2, 0, 2)
x = torch.rand(1, 2, 10, 10)
model.eval()
print(cv2.__version__)
torch.onnx.export(model, x, "resize.onnx", opset_version=11)
opencv_net = cv2.dnn.readNetFromONNX("resize.onnx")
Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    answers.opencv.org, Stack Overflow, etc and have not found solution
  • I updated to latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

Labels

bugcategory: dnnconfirmedThere is stable reproducer / investigation complete

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions