【问题标题】:OpenCV: Can't create layer "flatten_1/Shape" of type "Shape"OpenCV:无法创建“Shape”类型的图层“flatten_1/Shape”
【发布时间】:2019-06-03 18:20:36
【问题描述】:

我尝试在 opencv dnn 中实现一个 tensorflow 模型。这是我遇到的错误:

OpenCV:无法创建类型为“Shape”的图层“flatten_1/Shape”

我使用 keras 构建模型

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape = (32,32,1), activation = 'relu'))

model.add(Conv2D(32, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(Conv2D(64, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())#<== this is the layer that opencv doesnt support

model.add(Dense(units = 128, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = num_classes, activation = 'softmax'))

我已经尝试过this:

from tensorflow.python.keras.layers.core import Reshape

model.add(Reshape((-1,)))

但是又报错了

TypeError:添加的层必须是类Layer的实例。找到:tensorflow.python.keras.layers.core.Reshape 对象位于 0x000001D21EF1A630>

从那里我还没有找到任何解决方案。我的问题是在 keras 中是否可以替换 Flatten()

【问题讨论】:

标签: python opencv tensorflow keras


【解决方案1】:

我发现 OpenCV dnn 只允许推理,所以模型需要针对推理进行优化。我使用 tensorflow 的图形转换工具来做到这一点。

from tensorflow.tools.graph_transforms import TransformGraph

graph = TransformGraph(graph,
            ["input_1"], # inputs nodes
            ["dense_2/Softmax"], # outputs nodes
            ['fold_constants()',
            'strip_unused_nodes(type=float, shape="None,32,32,1")',
            'remove_nodes(op=Identity, op=CheckNumerics)',
            'fold_batch_norms',
            'fold_old_batch_norms'
            ])

【讨论】:

  • 在 TensorFlow 工具中在 1.14.0 版本中没有属性“graph_transforms”
【解决方案2】:

尝试将 Flatten 更改为以下:

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

https://github.com/opencv/opencv/issues/10135

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
猜你喜欢
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 2020-12-15
  • 2012-10-16
  • 2016-02-12
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多