Converting keras model to opencv. Problem with flatten layer.

asked 2019-10-08 08:16:42 -0500

qwersd gravatar image

Opencv version: 4.1.0 (using opencv plugin for unity)

Operating system: Windows 10

I'm trying to port to Unity keras model that detect emotions from human faces I'm using Unity opencv plugin that has already example that detects face from picture, so all I have to do is feed the square with face to the deep neural network.

I've tried converting the model to pb file using this ( but after plugging it to the program I got errors like:

dnn::forward_11() : OpenCV(4.1.0-dev) C:\Users\satoo\Desktop\opencv\modules\dnn\src\dnn.cpp:524: error: (-2:Unspecified error) Can't create layer "flatten_1/Shape" of type "Shape" in function 'cv::dnn::dnn4_v20190122::LayerData::getLayerInstance'

I've tried using script to remove incompatible nodes, but it looks like it makes incorrect network. I couldn't make the updated network work neither in unity nor original python program.

Finally I've tried removing nodes manually from the graph following instructions from here and here but it doesn't work. Trying to load the network from pb model and pbtext file results in Unity just crashing - it most likely means that it fails to load the network from files.

Is there a way to remove incompatible nodes from the network? Preferrably not manually.

edit retag flag offensive close merge delete


Uhmm this is a bit complex - if i understand you correctly you have the following pipeline: keras model ---> tensorflow model --> open cv

Well only hint is to test each step seperatly(test keras model wih keras, then test tensorflow model in tensorflow, then test tensorflow model in opencv) to find where it breaks. Its just too complex this way to tell the root cause.

I am also a bit confused - isnt keras a top level api for tensorflow - why is a conversion necessary?

holger gravatar imageholger ( 2019-10-08 18:22:28 -0500 )edit

Also look here - Looks like the same problem.

holger gravatar imageholger ( 2019-10-08 18:26:51 -0500 )edit