Error loading TF model with OpenCV::DNN

asked 2018-01-20 17:44:54 -0500

Rbt gravatar image

updated 2018-03-11 01:16:03 -0500

I trained the following regression model with Keras:

def regression_model_1(input_shape):
   model = Sequential()
   model.add(Convolution2D(32, kernel_size=5, padding="same", input_shape=input_shape))
   model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
   model.add(Convolution2D(32, padding="valid", kernel_size=4))
   model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
   model.add(Convolution2D(64, padding="valid", kernel_size=5))
   model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
   return model

I convert the trained model in Keras format (.h5) to TF format (.pb) with the following script:

from keras import backend as K
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from keras.models import load_model 
import os.path as osp

weight_file_path = 'model-01.h5' 
output_folder_path = './' ]

net_model = load_model(weight_file_path)
sess = K.get_session()

constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), ['dense_2/BiasAdd'])
graph_io.write_graph(constant_graph, output_folder_path, 'model.pb', as_text=False)
print('saved the constant graph (ready for inference) at: ', osp.join('output_folder_path', 'model.pb'))

When I try to load the trained model with

net_ = cv::dnn::readNetFromTensorflow("model.pb");

I get the following errors:

OpenCV Error: Unspecified error (Input layer not found: conv2d_1/bias) in connect, file /home/programs/opencv-3.3.1/modules/dnn/src/tensorflow/tf_importer.cpp, line 529
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/programs/opencv-3.3.1/modules/dnn/src/tensorflow/tf_importer.cpp:529: error: (-2) Input layer not found: conv2d_1/bias in function connect


Does anyone have a solution?!!!

edit retag flag offensive close merge delete


@Rbt, please try again with master branch of OpenCV.

dkurt gravatar imagedkurt ( 2018-01-22 04:23:30 -0500 )edit

OK, I will do that .... !!! Thank you!!

Rbt gravatar imageRbt ( 2018-01-23 13:31:34 -0500 )edit