Hello All, I have an encode/decoder model based on Conv2D and Conv2DTranspose layers which I am able to train, but then I need to freeze it to be able to open it in OpenCvSharp.DNN.
I use freezing method which works fine with image classification models but have an issue when I use Conv2DTranspose layer in a model. I am getting exception “Input layer not found: sequential/conv2d_transpose/stack/1” when I load the model with CvDnn.ReadNetFromTensorflow(“path to model”). So, I inspect the model in Netron and I see there “Pack” element expecting inputs:
- sequential/conv2d_transpose/strided_slice
- sequential/conv2d_transpose/stack/1
- sequential/conv2d_transpose/stack/2
- sequential/conv2d_transpose/stack/3
while the only one preceeding element “StridedSlice” have only one output
- sequential/conv2d_transpose/strided_slice
Freezing procedure looks like this:
def Freeze(model, modelFolder):
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
x=tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
logdir=modelFolder,
name="fmodel.pb",
as_text=False)
del full_model
del frozen_func
Input model construction looks like this:
model = Sequential([
layers.Rescaling(1./255, input_shape=(trans_h, trans_w, 3))
])
model.add(layers.Conv2D(filters=256, kernel_size=3, strides=2, activation='relu', padding='same'))
model.add(layers.Conv2D(filters=128, kernel_size=3, strides=2, activation='relu', padding='same'))
model.add(layers.Conv2D(filters=64, kernel_size=3, strides=2, activation='relu', padding='same'))
model.add(layers.Conv2DTranspose(filters=64, kernel_size=3, strides=2, activation='relu', padding='same'))
model.add(layers.Conv2DTranspose(filters=128, kernel_size=3, strides=2, activation='relu', padding='same'))
model.add(layers.Conv2DTranspose(filters=256, kernel_size=3, strides=2, activation='relu', padding='same'))
model.add(layers.Conv2DTranspose(1, kernel_size=(3, 3), padding='same'))
I found freezing code on stackoverflow and do not understand all steps, because of I am bit Python and TF/Keras newbie . Is there anyone here who can help me to solve the issue?
Thank you
Milan B.