opencv: segment fault when using dnn with yolov5 model in detection

System Information

OpenCV version: 4.7.0 Operation System: Windows 10 Compiler: Microsoft Visual Studio 2022

Detailed description

The following code was called when calling net.forward. The file is located at src/modules/dnn/layers/fast_convolution/winograd_3x3s1_f63.cpp.

 for (int k = k0; k < k1; k++)
                {
                    float biasv = conv->biasBuf[g*Kg + k];
                    for (int block_id = block_id0; block_id < block_id1; block_id++)
                    {

conv->biasBuf is an empty vector. so the g*Kg+k index will raise an index out of range exception.

Steps to reproduce

Steps: 1、train a yolov5 model 2、convert the pytorch model to onnx python export.py --opset 12 --img 640 --weights models/best640.pt --include onnx 3、using cv::dnn model to detect image with the exported model

       auto net = cv::dnn::readNet("assets/models/best640.onnx");
        if (is_cuda)
        {
            std::cout << "Attempty to use CUDA\n";
            net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
            net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA_FP16);
        }
        else
        {
            std::cout << "Running on CPU\n";
            net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
            net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
        }

        cv::dnn::blobFromImage(input_image, blob, 1. / 255., cv::Size(INPUT_WIDTH, INPUT_HEIGHT), cv::Scalar(), true, false);
        net.setInput(blob);
        std::vector<cv::Mat> result;
        net.forward(result, net.getUnconnectedOutLayersNames());

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)

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Guys, I think one of reasons for empty biasBuf vector is following code:

{
    int k = 0, nbias = K + VEC_ALIGN;
    conv->biasBuf.reserve(nbias);
    float* biasBufPtr = conv->biasBuf.data();
    for(; k < K; k++)
        biasBufPtr[k] = srcBias ? srcBias[k] : 0.f;
    for(; k < nbias; k++)
        biasBufPtr[k] = 0.f;
}

dnn\src\layers\fast_convolution\fast_convolution.cpp

It is only reserve the vector but not really resize it. According to this code it must be resize instead of reserve.

please try the latest code, this issue should be fixed.

Yes. It was solved.

hi @aditya72515 , thanks. Your sample may be useful to work with opencv 4.7.0. But the exposed code is a bit complex to just predict with a model. I found another way to bypass this problem. The dnn::Net interface provides a feature to disable the fast conv dnnNet.enableWinograd(false);.

@zihaomu Could you take a look?