将Keras模型与OpenCV 3中的Flatten层一起使用 [英] Use Keras model with Flatten layer inside OpenCV 3

查看:386
本文介绍了将Keras模型与OpenCV 3中的Flatten层一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Keras与TensorFlow作为后端来创建和训练一个简单的CNN. 我能够将模型及其权重保存在.pb文件中,将其冻结并对其进行优化以进行推断,但是当我尝试将其加载到OpenCV 3.4.1中时,出现错误消息:

I use Keras with TensorFlow as a backend to create and train a simple CNN. I am able to save the model and its weights in a .pb file, freeze it and optimize it for inference but when I try to load it into OpenCV 3.4.1 I get the error:

flatten/Shape:Shape(max_pooling2d/MaxPool)
T:0
out_type:[ ]
OpenCV(3.4.1) Error: Unspecified error (Unknown layer type Shape in op flatten/Shape) in populateNet, file /home/dev/opencv-3.4.1/modules/dnn/src/tensorflow/tf_importer.cpp, line 1582
Traceback (most recent call last):
  File "test.py", line 67, in <module>
    net = cv.dnn.readNetFromTensorflow('graph.pb')
cv2.error: OpenCV(3.4.1) /home/dev/opencv-3.4.1/modules/dnn/src/tensorflow/tf_importer.cpp:1582: error: (-2) Unknown layer type Shape in op flatten/Shape in function populateNet

这与另一个问题基本上是相同的问题:

This is basically the same problem as the other question: How to import TensorFlow model with flatten layer in OpenCV?.

此线程中的错误原因已得到很好的解释. 建议的解决方法是直接使用tf.reshape而不是使用Keras API.

The reason for the error is pretty well explained in this thread. The proposed workaround is to use directly tf.reshape instead of using the Keras API.

但是我不知道该怎么做. 我尝试使用功能性API并替换:

However I don't know exactly how to do this. I tried to use the functional API and replace:

x = Flatten()(x)

作者:

x = tf.reshape(x, [-1, some_value])

但这不起作用,并且出现以下错误:

but this doesn't work and I get the following error:

Traceback (most recent call last):
  File "test.py", line 57, in <module>
    tf_out = model.predict(inp)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/models.py", line 965, in predict
    self.build()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/models.py", line 578, in build
    self.model = Model(self.inputs, self.outputs[0], name=self.name + '_model')
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/topology.py", line 678, in __init__
    super(Network, self).__init__(inputs, outputs, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/layers/network.py", line 341, in __init__
    '(thus holding past layer metadata). Found: ' + str(x))
ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: Tensor("activation_4/Softmax:0", shape=(?, 10), dtype=float32)

有什么想法可以在仍然使用Keras进行大部分工作的同时导出TensorFlow模型?

Any idea of how I can export a TensorFlow model while still using Keras for most of the work?

推荐答案

我遇到了同样的问题.经过大量搜索并测试了许多解决方案之后, 我可以在opencv中导入keras分类训练模型. 首先,您必须用tensorflow.python.keras替换keras. 只需从代码行中删除keras并将其替换为tensorflow.python.keras 其次,您必须使用以下代码替换平坦的躺椅: 只需移除扁平化的图层并复制并粘贴以下内容即可:

I had same problem .finally after searching a lot and testing many solutions , i could to import keras classification trained model in opencv. Firstly you must to replace keras with tensorflow.python.keras. Just remove keras from your code lines and replace it with tensorflow.python.keras Secondly you must to replace flatten layares with this codes : just remove flatten layers and copy and past this:

a,b,c,d = model.output_shape
a = b*c*d
model.add(K.layers.Permute([1, 2, 3]))  # Indicate NHWC data layout
model.add(K.layers.Reshape((a,)))

现在,您可以重新训练模型. 训练完成后,将您的模型保存为.h5格式. 现在您必须将keras .h5模型转换为tensorflow .pb模型. 为了转换模型,我使用了以下解决方法: https://stackoverflow.com/a/53386325/5208522 现在,您可以轻松地在opencv中导入.pb模型,并且可以完美运行.

Now you can retrain your model. After training finished , save your model in .h5 format. Now you must convert keras .h5 model to tensorflow .pb model. In order to converting models i used this workaround : https://stackoverflow.com/a/53386325/5208522 Now you can easily import .pb model in opencv and it works perfectly.

这篇关于将Keras模型与OpenCV 3中的Flatten层一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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