Skip to content

Parsing ONNX with AND layer fails. #24979

@Abdurrahheem

Description

@Abdurrahheem

System Information

OpenCV python version: 5.x
Operating System / Mac OS Sonoma 14.3
Python version: 3.8.18

Detailed description

Parsing ONNX graph with AND layer fails. Error log:

[ INFO:0@0.545] global onnx_importer.cpp:806 populateNet DNN/ONNX: loading ONNX v8 model produced by 'pytorch':2.2.0. Number of nodes = 1, initializers = 0, inputs = 2, outputs = 1
[ INFO:0@0.545] global onnx_importer.cpp:699 parseOperatorSet DNN/ONNX: ONNX opset version = 17
[ INFO:0@0.548] global onnx_importer.cpp:977 handleNode DNN/ONNX: processing node with 2 inputs and 1 outputs: [And]:(onnx_node!/And) from domain='ai.onnx'
[ INFO:0@0.548] global onnx_importer.cpp:3307 parseCustomLayer DNN/ONNX: unknown node type, try using custom handler for node with 2 inputs and 1 outputs: [And]:(onnx_node!/And)
OpenCV(5.0.0-pre) Error: Unspecified error (Can't create layer "onnx_node!/And" of type "And") in getLayerInstance, file /Users/abd/work/opencv/modules/dnn/src/net_impl.hpp, line 106
[ERROR:0@0.549] global onnx_importer.cpp:1035 handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [And]:(onnx_node!/And) from domain='ai.onnx'
[ INFO:0@0.549] global onnx_importer.cpp:1039 handleNode     Input[0] = 'input_x'
[ INFO:0@0.549] global onnx_importer.cpp:1039 handleNode     Input[1] = 'input_y'
[ INFO:0@0.549] global onnx_importer.cpp:1043 handleNode     Output[0] = 'output'
OpenCV(5.0.0-pre) Error: Unspecified error (> Node [And@ai.onnx]:(onnx_node!/And) parse error: OpenCV(5.0.0-pre) /Users/abd/work/opencv/modules/dnn/src/net_impl.hpp:106: error: (-2:Unspecified error) Can't create layer "onnx_node!/And" of type "And" in function 'getLayerInstance'
> ) in handleNode, file /Users/abd/work/opencv/modules/dnn/src/onnx/onnx_importer.cpp, line 1054
Traceback (most recent call last):
  File "genOnnx.py", line 50, in <module>
    net = cv2.dnn.readNetFromONNX(f"{name}.onnx");
cv2.error: OpenCV(5.0.0-pre) /Users/abd/work/opencv/modules/dnn/src/onnx/onnx_importer.cpp:1054: error: (-2:Unspecified error) in function 'handleNode'
> Node [And@ai.onnx]:(onnx_node!/And) parse error: OpenCV(5.0.0-pre) /Users/abd/work/opencv/modules/dnn/src/net_impl.hpp:106: error: (-2:Unspecified error) Can't create layer "onnx_node!/And" of type "And" in function 'getLayerInstance'

Steps to reproduce

import cv2
import torch
import torch.nn as nn
import numpy as np

class And(nn.Module):
    def __init__(self):
        super(And, self).__init__()

    def forward(self, x, y):
        return x & y

if __name__ == '__main__':
    name = "and"
    model = And()

    x = torch.ones(5, dtype=torch.bool )
    y = torch.zeros(5, dtype=torch.bool )

    inputs = [x, y]
    outputs = [model(x, y)]

    print("\n\t\t **** TORCH INFERENCE END ****\n\n")

    torch.onnx.export(
        model,
        (x, y),
        f"{name}.onnx",
        verbose=True,
        input_names=[f"input_{i}" for i in range(len(inputs))],
        output_names=["output"]
    )

    print("\n\t\t **** EXPORTED ONNX ****\n\n")

    ## save the inputs and outpus as npy files
    for i in range(len(inputs)):
        inputs[i] = inputs[i].detach().numpy()
        with open(f"input_{name}_{i}.npy", "wb") as f:
            np.save(f, inputs[i])

    for i in range(len(outputs)):
        outputs[i] = outputs[i].detach().numpy()
        with open(f"output_{name}_{i}.npy", "wb") as f:
            np.save(f, outputs[i])

    for i in range(len(inputs)):
        inputs[i] = cv2.Mat(inputs[i])

    net = cv2.dnn.readNetFromONNX(f"{name}.onnx");
    print("\n\t\t **** CV2 MODEL PARSED ****\n\n")

    ## save the inputs and outpus as npy files
    for i in range(len(inputs)):
        inputs[i] = inputs[i].detach().numpy()
        with open(f"input_{name}_{i}.npy", "wb") as f:
            np.save(f, inputs[i])

    for i in range(len(outputs)):
        outputs[i] = outputs[i].detach().numpy()
        with open(f"output_{name}_{i}.npy", "wb") as f:
            np.save(f, outputs[i])

    for i in range(len(inputs)):
        inputs[i] = cv2.Mat(inputs[i])

    ## set inputs
    for i in range(len(inputs)):
        net.setInput(inputs[i], f"input_{name}_{i}")
    print("\n\t\t **** CV2 INPUTS SET ****\n\n")

    ## forward
    output_cv = net.forward()
    print("\n\t\t **** CV2 FORWARD END ****\n\n")

    if len(outputs) == 1:
        print(np.allclose(output_cv[0], outputs[0], atol=1e-6))
    else:
        for i in range(len(outputs)):
            print(np.allclose(output_cv[i], outputs[i], atol=1e-6))

    ## print in green test PASSED
    print("\033[92m\t\t **** TEST PASSED ****\n\n")

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

Relationships

None yet

Development

No branches or pull requests

Issue actions