2

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

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

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;
    }
}

I get this error:

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

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

Thanks for your help.

maccN
  • 189
  • 1
  • 4
  • 13
  • You can try to replace flatten with reshape layer. Flatten is just to specific for Keras. – Tay2510 Jan 06 '18 at 17:59
  • 1
    Thank for your answer. I changed the code of the net in Keras and, instead of flatten layer, now I have the following line `model.add(Reshape((-1,)))`, but I got the same error on reshape layer: `OpenCV Error: Unspecified error (Unknown layer type Shape in op reshape_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 reshape_1/Shape in function populateNet` – maccN Jan 07 '18 at 08:20

1 Answers1

1

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

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.

Renan Wille
  • 298
  • 2
  • 8