Skip to content

DNN/ONNX: outputs registration regression, feature request for new version of Clip operator #21698

@cesarpgouveia

Description

@cesarpgouveia
System information (version)
  • OpenCV => 4.5.5
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2019
Detailed description

I tried to infer using a Selfie Segmenter ONNX model (you can find the model here: https://github.com/PINTO0309/PINTO_model_zoo/tree/main/109_Selfie_Segmentation), however I get Nan on all output values.

Steps to reproduce

You can replicate this issue simply by running this simple script with OpenCV 4.5.5:

#include <iostream>

#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/core.hpp>

int main()
{
    cv::Size inputSizeNewBarracuda = cv::Size(256, 256);

    std::string imagefilename = "C:/Lixo/SantaNoel.jpg";
    std::string newBarracuda = "C:/Users/cesar.gouveia/Downloads/saved_model_openvino/model_float32.onnx";

    cv::dnn::Net net = cv::dnn::readNetFromONNX(newBarracuda);
    net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
    net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);

    cv::Mat img = cv::imread(imagefilename);
    cv::Mat resized;
    cv::resize(img, resized, inputSizeNewBarracuda);

    std::vector<cv::Mat> imgBatch = { resized };
    bool swapRBChannels = false;
    cv::Mat blob = cv::dnn::blobFromImages(imgBatch, 1.0, cv::Size(), cv::Scalar(), swapRBChannels, false, CV_32F);
    blob = blob.reshape(1, { 1, inputSizeNewBarracuda.height, inputSizeNewBarracuda.width, 3 }); // because the model has input in channels last

    net.setInput(blob);

    std::vector<cv::Mat> outputs;
    outputs.clear();

    std::vector<cv::String> unconnectedOutLayerNames = net.getUnconnectedOutLayersNames();
    net.forward(outputs, unconnectedOutLayerNames);

    const cv::Mat& targetMat = outputs[0];
    const float* targetBuffer = (float*)targetMat.data;

    std::cout << targetMat.size[0] << std::endl; // 1
    std::cout << targetMat.size[1] << std::endl; // 256
    std::cout << targetMat.size[2] << std::endl; // 256
    std::cout << targetMat.size[3] << std::endl; // 1

    // Access contiguos buffer
    for (size_t i = 0; i < 256 * 256; i++)
    {
        std::cout << targetBuffer[i] << std::endl;
    }

    // Access Mat
    targetMat.reshape(1, { 1, 256, 256 });
    for (size_t o = 0; o < targetMat.size[0]; o++)
    {
        for (size_t i = 0; i < targetMat.size[1]; i++)
        {
            for (size_t j = 0; j < outputs[0].size[2]; j++)
            {
                std::cout << outputs[0].at<float>(o, i, j);
            }
        }
    }
    
    std::cout << "Finished" << std::endl;
}

The model can be downloaded by the link I provided. This is the first "channels last" model that I use with OpenCVDNN, all my other models are channel first and I never had this code behavior before. I tried to access the mat and the contiguos memory array but neither of them worked.

Thanks,
César.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions