简体   繁体   English

dnn opencv c++ 'getMemoryShapes' 断言失败

[英]dnn opencv c++ 'getMemoryShapes' assertion failed

I have trained a CNN in keras saving the result as.h5 and.json.我在 keras 中训练了一个 CNN,将结果保存为.h5 和.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.之后,我使用 python 脚本将文件转换为 a.pb,现在我想使用 cv::dnn::readNetFromTensorflow 将网络导入 c++ 中的 opencv

But I am getting an assertion failed on 'getMemoryShapes', what can be the issue here?但是我在“getMemoryShapes”上得到一个断言失败,这可能是什么问题?

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:我现在的代码非常简单,我只是给网络提供了一个 32x32 的示例图像,因为它是网络的输入大小:

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:我在 keras 中的输入层是:

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 并将其转发到网络来修复它:

   blob = cv::dnn::blobFromImage(image, 1.0, cv::Size(32,32));
   net.setInput(blob);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM