I have trained a CNN in keras saving the result as.h5 and.json.
After that I converted the files to a.pb using a python script, and now I want to import the network into opencv in c++ using cv::dnn::readNetFromTensorflow.
But I am getting an assertion failed on 'getMemoryShapes', what can be the issue here?
The error message is the following:
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.3.0) /build/opencv/src/opencv-4.3.0/modules/dnn/src/layers/convolution_layer.cpp:340: error: (-2:Unspecified error) Number of input channels should be multiple of 3 but got 32 in function 'getMemoryShapes'
Aborted (core dumped)
My code for now is very simple, I am just feeding the net with a sample image 32x32 since it is the input size of the network:
int main(int argc, char** argv) {
cv::dnn::Net net;
net = cv::dnn::readNetFromTensorflow("model.pb");
cv::Mat image(32, 32, CV_8UC3, cv::Scalar(22,22,22));
net.setInput(image);
cv::Mat out;
out = net.forward();
return 0;
}
My input layer in keras is:
model.add(tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)))
It can be fixed by finding the blob and forwarding it to the network:
blob = cv::dnn::blobFromImage(image, 1.0, cv::Size(32,32));
net.setInput(blob);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.