Problems with Freezing TensorFlow Models

Hi, I have trained a CNN LSTM model, and used ModelCheckpoint to save the model. Then, I used different codes to freeze this model, then input it to OpenVino to convert as IR. However, problems arise as shown below:

First Code

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
import pdb

new_model = tf.keras.models.load_model('/content/drive/MyDrive/cnnlstm1')

full_model = tf.function(lambda x: new_model(x))
full_model = full_model.get_concrete_function(x=tf.TensorSpec((None,20,64,64,3),'float32'))

forzen_func = convert_variables_to_constants_v2(full_model)
forzen_func.graph.as_graph_def()

layers = [op.name for op in forzen_func.graph.get_operations()]
print("-"*50)
print("Frozen model layers:")
for layer in layers:
    print(layer)

print("*"*50)
print("Frozen model input:")
print(forzen_func.inputs)
print("Frozen model output:")
print(forzen_func.outputs)

tf.io.write_graph(
    graph_or_graph_def=forzen_func.graph,
    logdir="./",
    name="testtt.pb",
    as_text=False
)

The error output:

RuntimeError: Check 'false' failed at src/frontends/tensorflow/src/frontend.cpp:168:
FrontEnd API failed with GeneralFailure: :
No input is found for node "sequential_1/lstm_1/PartitionedCall/TensorArrayUnstack/TensorListFromTensor" by port 0

Second code

import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

model = tf.saved_model.load('/content/drive/MyDrive/cnnlstm1')
frozen_func = convert_variables_to_constants_v2(model.signatures['serving_default'], lower_control_flow=False)
graph_def = frozen_func.graph.as_graph_def(add_shapes=True)
tf.io.write_graph(graph_def, './', 'frozen_model.pb', as_text=False)

with the output of

RuntimeError: Check 'translate_map.count(operation_decoder->get_op_type())' failed at src/frontends/tensorflow/src/frontend.cpp:177:
FrontEnd API failed with OpConversionFailure: :
No translator found for TensorListReserve node.

It seems like the froze models from both codes generate error, and I could not find a ‘standardized’ method to freeze the model as well.

I would deeply appreciate any help as a newbie! Thanks in advance.

Hi @Jocelyn_Teh, I have executed your code with a sample CNN LSTM model with Tensorflow 2.12 in colab. I did not face any errors. Could you please provide the model you are loading, and Please let us know which version of tensorflow you are using. Here is the gist that i have executed. Thank You.

Hi Kiran, I am using TensorFlow 2.12 and this is the frozen .pb model link.

The link for the model training is here.

I have request to access the gist you have executed, could you please grant it? Thank you in advance!

Hi @Jocelyn_Teh, I have changed the link you can access it now. Thank You.

Thank you so much for the help.