如何在OpenCV中导入具有扁平层的TensorFlow模型? [英] How to import TensorFlow model with flatten layer in OpenCV?

查看:191
本文介绍了如何在OpenCV中导入具有扁平层的TensorFlow模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Keras创建了CNN.网上的代码是:

I have created a CNN with Keras. The code of the net is:

model = Sequential()

model.add(Conv2D(32, (3,3), data_format='channels_last', input_shape=(48, 32, 3), name='data'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid', name='result_class'))

然后,使用此脚本,我转换了Keras创建的.h5文件,以.pb.

Then, using this script, I have converted the .h5 file, created by Keras, in .pb.

现在,我想使用OpenCV(3.4)导入模型,但是当我执行以下代码时

Now I want to import the model using OpenCV (3.4), but when I execute the following code

Net net;
try {
    net = dnn::readNetFromTensorflow(model_path);
}
catch (cv::Exception& e) {
    cerr << "Exception: " << e.what() << endl;
    if (net.empty()) {
        cerr << "Can't load the model" << endl;
    }
}

我收到此错误:

OpenCV Error: Unspecified error (Unknown layer type Shape in op flatten_1/Shape) in populateNet, file /home/nicola/Scrivania/opencv-3.4.0/opencv-3.4.0/modules/dnn/src/tensorflow/tf_importer.cpp, line 1487
Exception: /home/nicola/Scrivania/opencv-3.4.0/opencv-3.4.0/modules/dnn/src/tensorflow/tf_importer.cpp:1487: error: (-2) Unknown layer type Shape in op flatten_1/Shape in function populateNet
Can't load the model

看来OpenCV无法处理扁平化层,对吗?有没有办法导入我的网?

It seems that OpenCV can't handle flatten layer, am I right? Is there a way to import my net?

感谢您的帮助.

推荐答案

是的,目前看来Opencv在处理扁平化层时遇到问题. 您可以在此处查看更多信息: https://github.com/opencv/opencv_contrib/issues/1241

Yes, for now it looks like Opencv has a problem handling the flatten layer. You can see more on this here: https://github.com/opencv/opencv_contrib/issues/1241

一种解决方法建议在网络上直接使用tf.reshape.但是我也在研究如何在keras层上做到这一点.

A workaround suggested there is to use directly tf.reshape on the network. But I'm also working on how to do that on keras layers.

这篇关于如何在OpenCV中导入具有扁平层的TensorFlow模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆