Skip to content

Torch interpolate's onnx graph fails while parsing #23730

@Abdurrahheem

Description

@Abdurrahheem

System Information

OpenCV version: 4.6.0
Operating System / Platform: Ubuntu 20.04
Compiler & compiler version: GCC 7.5.0
Python version: 3.8.8

Detailed description

Trying to read .onnx file created using torch.nn.functional.interpolate. I get this error

opencv_net = cv2.dnn.readNetFromONNX(full_model_path)
cv2.error: OpenCV(4.7.0-dev) 
/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1083: error: (-2:Unspecified error) in function 'handleNode'
> Node [Floor@ai.onnx]:(onnx_node!/Floor) parse error: OpenCV(4.7.0-dev) /opencv/modules/dnn/src/layers/elementwise_layers.cpp:261: error: (-215:Assertion failed) src.size == dst.size && src.type() == dst.type() && src.isContinuous() && dst.isContinuous() && src.type() == CV_32F in function 'forward'

Steps to reproduce

import torch
import torch.nn as nn
import torch.nn.functional as F
import os
import onnx
import cv2
from onnx import shape_inference

class Resizer(nn.Module):

    def forward(self, image : torch.tensor):
        image = F.interpolate(
                    image,
                    size=None,
                    scale_factor=2,
                    mode="bilinear",
                    recompute_scale_factor=2,
                    align_corners=False,
                )
        return image



if __name__ == "__main__":

    resize = Resizer()
    x = torch.ones((1, 3, 608, 608))
    print(f"x: {x.shape}")
    x_r = resize(x)
    print(f"x_r: {x_r.shape}")

    onnx_model_path = "models"

    # define the name of further converted model
    onnx_model_name = "resize.onnx"

    os.makedirs(onnx_model_path, exist_ok=True)
    full_model_path = os.path.join(onnx_model_path, onnx_model_name)

    print("\t\t ---> EXPORTING <---")
    # model export into ONNX format
    torch.onnx.export(
        resize,
        x,
        full_model_path,
        verbose=False,
        input_names=["input"],
        output_names=["output"],
        opset_version=11
    )

    print("\t\t ---> READING ONNX FILE IN ONNX <---")
    onnx_model = onnx.load(full_model_path)
    onnx.checker.check_model(onnx_model)

    onnx_model = shape_inference.infer_shapes(onnx_model)
    onnx.save(onnx_model, full_model_path)


    print("\t\t ---> READING ONNX in OPENCV FILE <---")
    opencv_net = cv2.dnn.readNetFromONNX(full_model_path)

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions