Ask Your Question

Using two dnn networks causes crash [closed]

asked 2018-11-07 00:47:03 -0600

pouya.1991 gravatar image

updated 2018-11-07 01:24:22 -0600

Hi I wanna to using two yolo networks in my program (an yolo3-tiny and an yolov3-spp), but when I call net.forward function for the second network, I'm got following exception :

terminate called after throwing an instance of 'cv::Exception'
what():  OpenCV(4.0.0-pre) /home/pouya/Develop/opecv4/opencv/modules/dnn/src/dnn.cpp:1149: error: (-204:Requested object was not found) Layer with requested id=-1 not found in function 'getLayerData'

there is my code :

bool Detector::postprocesstiny(const std::vector<cv::Mat>& outs)

    for (size_t i = 0; i < outs.size(); ++i)
        float* data = (float*)outs[i].data;
        for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
            cv::Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
            cv::Point classIdPoint;
            double confidence;
            minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);

            if (classIdPoint.x==2 && confidence > 0.5)
                return true ;

    return false ;


std::vector<cv::String> Detector::getOutputsNames(const cv::dnn::Net& net)
    static std::vector<cv::String> names;
    if (names.empty())
        std::vector<int> outLayers = net.getUnconnectedOutLayers();
        std::vector<cv::String> layersNames = net.getLayerNames();
        for (size_t i = 0; i < outLayers.size(); ++i)
            names[i] = layersNames[outLayers[i] - 1];
    return names;

bool Detector::isThereAnyCar(cv::Mat image) {

cv::Mat inputBlob = cv::dnn::blobFromImage(image, 1 / 255.F , cv::Size(416, 416), cv::Scalar(), true, false); 
std::vector<cv::Mat> outs;

net_tiny.forward(outs, getOutputsNames(net_tiny));  
return postprocesstiny(outs);


cv::dnn::Net net = cv::dnn::readNetFromDarknet(modelConfiguration, modelBinary);
cv::dnn::Net net_tiny = cv::dnn::readNetFromDarknet(modelConfigurationTiny, modelBinaryTiny);
cv::Mat frame;
cv::VideoCapture cap(source);
cap >> frame;
if (isThereAnyCar(frame)) {
        cv::Mat inputBlob = cv::dnn::blobFromImage(frame, 1 / 255.F , cv::Size(416, 416), cv::Scalar(), true, false); 
        std::vector<cv::Mat> outs;

        std::vector<DetectionResult> detection_results;
        net.forward(outs, getOutputsNames(net));  //This line causes error 


and when I comment one of the networks, it runs without any problem. Is there any solution for this problem?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by dkurt
close date 2018-11-08 05:47:43.459830



There is the configuration of this network :

pouya.1991 gravatar imagepouya.1991 ( 2018-11-07 00:54:41 -0600 )edit

so, the problem is NOT running 2 networks, but that there is something unsupported in the (new) ssp model, right ?

(if you thing it has to do with 2 networks, the, ofc. we need to see your code !)

berak gravatar imageberak ( 2018-11-07 00:56:41 -0600 )edit

No I can run the ssp model without any problem, but when I want to run two networks to gather I get this error

pouya.1991 gravatar imagepouya.1991 ( 2018-11-07 00:59:06 -0600 )edit

thank you, just added :) the last line causes error but when I comment if (isThereAnyCar(frame)) or net.forward(outs, getOutputsNames(net)) there is no error

pouya.1991 gravatar imagepouya.1991 ( 2018-11-07 01:26:53 -0600 )edit

i can only guess now, but the tiny-yolo is v2, and the ssp one v3, right ? imho, you cannot reuse the outs Mat vector for both of them, and they will have different outnames, too.

i also guess, if you change the order, you get an error in the other network ;)

berak gravatar imageberak ( 2018-11-07 01:32:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-11-07 01:51:29 -0600

pouya.1991 gravatar image

Both of them are v3, thank you I just omit if (names.empty()) in getOutputsNames and the error vanished :)

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-11-07 00:47:03 -0600

Seen: 2,229 times

Last updated: Nov 07 '18