我用 Keras 创建了一个 CNN。网络代码是:
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'))
然后,使用这个脚本 https://github.com/amir-abdi/keras_to_tensorflow,我已将 Keras 创建的 .h5 文件转换为 .pb。
现在我想使用OpenCV(3.4)导入模型,但是当我执行以下代码时
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;
}
}
我收到此错误:
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
看来 OpenCV 无法处理扁平化层,对吗?有没有办法导入我的网络?
感谢您的帮助。