Skip to content Skip to sidebar Skip to footer

Opencv: Can't Create Layer "flatten_1/shape" Of Type "shape"

I've tried to implement a tensorflow model in opencv dnn. This is the error I've got: OpenCV: Can't create layer 'flatten_1/Shape' of type 'Shape' I used keras to build my model

Solution 1:

Try to change Flatten to below:

#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

Solution 2:

I found that OpenCV dnn only allow inference, so the model need to be optimized for inference. I use graph transform tool from tensorflow to do that.

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'
            ])

Post a Comment for "Opencv: Can't Create Layer "flatten_1/shape" Of Type "shape""