Assertion failed in function total (dnn::forward) [closed]

asked 2017-10-05 09:59:27 -0500

Maups gravatar image

Hello,

I'm facing a problem that I have no idea why it is happening. I have the following code:

#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;
using namespace cv::dnn;
using namespace std;

int main(int argc, char **argv) {
    dnn::Net net = readNetFromTensorflow(argv[1]);
    if(net.empty()) exit(-1);

    for(string s : net.getLayerNames())
        cout << s << " " << net.getLayerId(s) << endl;

    //Mat img(224, 224, CV_8UC3);   
    Mat img(96, 96, CV_8UC1);

    Mat inputBlob = blobFromImage(img);
    net.setInput(inputBlob);

    //Mat result = net.forward("softmax2");
    Mat result = net.forward("own_namespace_123/out/add");

    for(int i=0; i < result.dims; i++)
        cout << result.size[i] << " ";
    cout << endl;
}

When I use the inception network from the example (https://storage.googleapis.com/downlo...), everything works:

conv2d0_pre_relu/conv 2
conv2d0 3
maxpool0 4
localresponsenorm0 5
conv2d1_pre_relu/conv 6
...
softmax1 158
avgpool0/reshape 159
softmax2_pre_activation/matmul 160
softmax2 161
1 1008

However, when a try to use my own network, the loading part works, but the forward function gives the following error:

OpenCV Error: Assertion failed (start < (int)shape.size() && end <= (int)shape.size() && start <= end) in total, file /home/mauricio/Libraries/OpenCV3.3/opencv-3.3.0/modules/dnn/include/opencv2/dnn/shape_utils.hpp, line 159
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/mauricio/Libraries/OpenCV3.3/opencv-3.3.0/modules/dnn/include/opencv2/dnn/shape_utils.hpp:159: error: (-215) start < (int)shape.size() && end <= (int)shape.size() && start <= end in function total
Aborted (core dumped)

Has anyone any idea why?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Maups
close date 2017-10-05 14:56:36.696964

Comments

@Maups, we need to know what the network is. Can you share a graph definition?

dkurt gravatar imagedkurt ( 2017-10-05 14:32:06 -0500 )edit

Looks like I need to replace tf.add for tf.nn.bias_add, as said in other threads. Just discovered that by dumping input and output sizes and checking where it stops:

MatShape input;
input.push_back(1); input.push_back(1); input.push_back(96); input.push_back(96);
for(string s : net.getLayerNames()) {
    cout << s << " " << net.getLayerId(s) << endl;;
    vector<MatShape> in, out;
    net.getLayerShapes(input, net.getLayerId(s), &in, &out);
    cout << "IN" << endl;
    for(MatShape ms : in) {
        for(int i : ms)
            cout << i << " ";
        cout << endl;
    }
    cout << "OUT" << endl;
    for(MatShape ms : out) {
        for(int i : ms)
            cout << i << " ";
        cout << endl;
    }
}
Maups gravatar imageMaups ( 2017-10-05 14:56:02 -0500 )edit