Ask Your Question

Revision history [back]

DNN use of neural network foward() to several images

Hi, i used the tutorial in the link https://github.com/Itseez/opencv_contrib/blob/master/modules/dnn/samples/caffe_googlenet.cpp then i changed the code and wrote a for loop to use the cv::dnn::net::foward() to same image different times and an error in output has occurred.

cv::dnn::Net net;
importer->populateNet(net);
dnn::Blob inputBlob = dnn::Blob(img);
net.setBlob(".data", inputBlob);

for(size_t i = 0; i <5; i++)
{
        net.forward();
        dnn::Blob prob = net.getBlob("prob");   //gather output of "prob" layer
    int classId;
    double classProb;
    getMaxClass(prob, &classId, &classProb);//find the best class
    std::vector<String> classNames = readClassNames();
    for (size_t i = 0; i < classNames.size(); i++)
    {
        cout << classNames[i]<<endl;
    }

    std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
    std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
}

The problem is that i gave to the neural network inputBlob of the same image in a for loop and the predictions are different. Somebody can help? Have another question, how can i give several input image blobs to a neural network in a for loop to predict different images?