Ask Your Question
0

DNN assertion failed in getMemoryShapes

asked 2018-03-15 13:37:27 -0600

aibalit gravatar image

updated 2018-03-15 18:17:12 -0600

Hello! I have a Tensorflow model with the following input:

    x = tf.placeholder(tf.float32, shape=[None, img_height, img_width], name='x_input')
    x_image = tf.reshape(x, [-1, img_height, img_width, num_channels])
    layer_conv1, weights_conv1 = new_conv_layer(input=x_image,
                                        num_input_channels=num_channels,
                                        filter_size=filter_size1,
                                        num_filters=num_filters1,
                                        use_pooling=True)
    ...
    y_pred = tf.nn.softmax(layer_fc2, name='prediction')   #layer with results
    ...

And I load it into the C ++ program as follows:

String modelFile = "frozen_model.pb";
dnn::Net net = readNetFromTensorflow(modelFile);

But when I try to upload an image:

   //initialize network
    dnn::Net net = readNetFromTensorflow(modelFile);
    assert(!net.empty());
    //prepare blobas
    Mat img = imread(imageFile, IMREAD_GRAYSCALE);
    assert(!img.empty());

    Mat fimg(img.size(), CV_32FC1);
    for (int i = 0;  i < img.rows; i++) {
        for (int j = 0; j < img.cols; j++){
            fimg.at<float>(i, j) = img.at<uint8_t>(i, j) / 255. - 0.5;
        }
    }

    Mat inputBlob = blobFromImage(fimg);
    net.setInput(inputBlob, inBlobName);

I get the following error:

OpenCV Error: Assertion failed (ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0) in getMemoryShapes, file /home/vlad/opencv_3/opencv/modules/dnn/src/layers/convolution_layer.cpp, line 217 terminate called after throwing an instance of 'cv::Exception' what(): /home/vlad/opencv_3/opencv/modules/dnn/src/layers/convolution_layer.cpp:217: error: (-215) ngroups > 0 && inpCn % ngroups == 0 && outCn % ngroups == 0 in function getMemoryShapes

Could you tell me what I'm doing wrong and how can I fix this error?

P.S. Below is the code for saving the mode:

output_node_names = "x_input,prediction"
constant_graph = tf.graph_util.convert_variables_to_constants(session, session.graph_def, output_node_names.split(','))
with tf.gfile.GFile('frozen_model.pb', "wb") as f:
    f.write(constant_graph.SerializeToString())
edit retag flag offensive close merge delete

Comments

/*normalization here*/ -- what does it mean ? are there operations you don't show us ?

(if so, you should !)

berak gravatar imageberak ( 2018-03-15 13:51:41 -0600 )edit
1

Edited the code in the question. As a result, normalizing is the following code:

Mat fimg(img.size(), CV_32FC1);
for (int i = 0;  i < img.rows; i++) {
    for (int j = 0; j < img.cols; j++){
        fimg.at<float>(i, j) = img.at<uint8_t>(i, j) / 255. - 0.5;
    }
}
aibalit gravatar imageaibalit ( 2018-03-15 18:19:43 -0600 )edit
1

what is num_channels in your python script ?

can it be, your cnn (the 1st convolution layer) expects 3channel input, not 1(grayscale) ?

(hard to see from the errormsg, but something is wrong with the channels)

berak gravatar imageberak ( 2018-03-16 01:19:27 -0600 )edit
1

No, all images are 1-channel and network layers expect the same

aibalit gravatar imageaibalit ( 2018-03-16 02:07:50 -0600 )edit

@aibalit, please make it more reproducible. Provide a minimal working example with all the layers and definition of new_conv_layer. At least, attach a .pb file. Thank you!

dkurt gravatar imagedkurt ( 2018-03-17 10:01:36 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-03-18 04:16:04 -0600

aibalit gravatar image

updated 2018-03-18 04:16:37 -0600

@berak , @dkurt Thank you, guys. I found a problem. Error, actually was in the number of channels and decided to replace the lines:

x = tf.placeholder(tf.float32, shape=[None, img_height, img_width], name='x_input')
x_image = tf.reshape(x, [-1, img_height, img_width, num_channels])

with these:

x_image = tf.placeholder(tf.float32, shape=[None, img_height, img_width, num_channels], name='x_input')
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-15 13:31:43 -0600

Seen: 6,434 times

Last updated: Mar 15 '18