DNN assertion failed in getMemoryShapes
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())
/*normalization here*/
-- what does it mean ? are there operations you don't show us ?(if so, you should !)
Edited the code in the question. As a result, normalizing is the following code:
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)
No, all images are 1-channel and network layers expect the same
@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!