How to freeze Keras model with Conv2DTranspose layers

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 :frowning: . Is there anyone here who can help me to solve the issue?

Thank you

Milan B.

Hi @Borg_Rootan, you can freeze the entire model using model.trainable = False. Thank You.

Hi @Kiran_Sai_Ramineni, thank you very much for your answer. I see now that my question is not properly formulated. Freezing the model is only one part. I also need to save the model in .pb format to be able to load it into opencv.dnn (especially OpenCvSharp.Dnn).
I am not sure what will happen when I use your advice and set Keras model trainable atribut to false and then skip calling frozen_func = convert_variables_to_constants_v2(full_model), but I will definitely try it. Anyway I am afraid of issue root cause based on getting TF graph from Keras model.
Do you have any idea whether I am doing something wrong while getting TF function from Keras model or there is some bug in implementation of getting TF function from Conv2DTranspose layer?

Thank you