Ask Your Question

Revision history [back]

Dimension error with inference using TensorFlow exported model

Hello everyone,

I'm currently tring to import a Tensorflow trained model (.pb file) with the dnn module, however I'm stuck when referencing.

The Tensorflow model a basic MNIST CNN model here , with some minor changes (removing the randoms).

The model is exported after freeze_graph and optimize_for_inference

The only input is the 28*28 gray image X = tf.placeholder(tf.float32, [None, 784]), The following Python opencv code works perfectly

    img = cv2.imread(path)
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    gray_float = np.array(1.0 - gray / 255.0, dtype = np.float32)
    input gray_float.flatten()

However in C++:

Net net = readNetFromTensorflow("../../resource/save1/model2.pb");

Mat img = imread("2.png");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
gray = gray.reshape(1, 1);
bitwise_not(gray, gray);
gray.convertTo(gray, CV_32F);
gray = gray / 255.0;

net.setInput(gray, "Placeholder_1");  // gray is (1, 28*28, CV_32F)

I got this error on line setInput

OpenCV Error: Assertion failed (inpCn % ngroups == 0 && outCn % ngroups == 0) in getMemoryShapes, file C:\Users\E507067\Downloads\openCV\sources\modules\dnn\src\layers\convolution_layer.cpp, line 190

The error to me seems related to the shape of input gray, but I can't get it to work. Does anyone have any idea to solve this? Thanks any help.

Dimension error with inference using TensorFlow exported model

Hello everyone,

I'm currently tring to import a Tensorflow trained model (.pb file) with the dnn module, however I'm stuck when referencing.

The Tensorflow model a basic MNIST CNN model here , with some minor changes (removing the randoms).

X = tf.placeholder(tf.float32, [None, 784])
X_img = tf.reshape(X, [-1, 28, 28, 1])
Y = tf.placeholder(tf.float32, [None, 10])

W1 = tf.Variable(tf.ones([3, 3, 1, 32])*0.005)
L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME')
...

The model is exported after freeze_graph and optimize_for_inference

The only input is the 28*28 gray image X = tf.placeholder(tf.float32, [None, 784]), The following Python opencv code works perfectly

 img = cv2.imread(path)
 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
 gray_float = np.array(1.0 - gray / 255.0, dtype = np.float32)
 input gray_float.flatten()

However in C++:

Net net = readNetFromTensorflow("../../resource/save1/model2.pb");

Mat img = imread("2.png");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
gray = gray.reshape(1, 1);
bitwise_not(gray, gray);
gray.convertTo(gray, CV_32F);
gray = gray / 255.0;

net.setInput(gray, "Placeholder_1");  // gray is (1, 28*28, CV_32F)

I got this error on line setInput

OpenCV Error: Assertion failed (inpCn % ngroups == 0 && outCn % ngroups == 0) in getMemoryShapes, file C:\Users\E507067\Downloads\openCV\sources\modules\dnn\src\layers\convolution_layer.cpp, line 190

The error to me seems related to the shape of input gray, but I can't get it to work. Does anyone have any idea to solve this? Thanks any help.