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.