ONNX Model with OpenCV.js

Hey all,

I have some issues using my ONNX model with OpenCV.js, which I trained with pytorch and then exported.

The model runs fine with inference when I use the onnx runtime engine in python:

import onnxruntime as ort
import cv2

# Load the ONNX model
model_path = "./models/ONNX_for_production/segment-sky-v1-resilient-pig.onnx"
session = ort.InferenceSession(model_path)

def infer(session, image_path):
    # Load the image
    image = cv2.imread(str(image_path))

    # Preprocess the image
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (512, 512))
    image = np.transpose(image, (2, 0, 1))
    image = np.expand_dims(image, axis=0)
    image = (image / 255.0).astype(np.float32)

    # Run inference
    output = session.run(None, {"input.1": image})
    ...

When I try to do the same in OpenCV.js, I only get an error code and don’t really know, how to go on troubleshooting the issue. Here is my JS code:

export async function semanticSegmentation(img, drawingTarget, statusFieldRef) {
    try {
        const input = getBlobFromImage(inputSize, mean, std, swapRB, img);
        let net = await loadDnnModel(modelFiles);
        net.setInput(input, "input.1");
        let result = net.forward("1066"); 
        [...]

const getBlobFromImage = function (inputSize, mean, std, swapRB, mat) {
    let matC3 = new cv.Mat(mat.matSize[0], mat.matSize[1], cv.CV_8UC3);
    cv.cvtColor(mat, matC3, cv.COLOR_BGR2RGB);
    let input = cv.blobFromImage(matC3, std, new cv.Size(inputSize[0], inputSize[1]),
        new cv.Scalar(mean[0], mean[1], mean[2]), swapRB);
    matC3.delete();
    return input;
}

The model is loaded fine, when I log the net, I get a proper dnn_Net. Also, the code works when I load another .pb model.
The only hint I get is this error code:
Error in net.forward(): 22869824

Can anyone help me troubleshooting or giving me a tip, on how to proceed? I’m trying to solve the issue since days already…

Thanks in advance!