Closed username123062 closed 2 months ago
Hi @username123062 it looks like you are passing a wrong tensor spec int full_model.get_concrete_function
Hi @username123062 it looks like you are passing a wrong tensor spec int full_model.get_concrete_function
I know. But when I pass the same tensor spec, it also shows the same problem. So I do not know the correct tensor spec. Could you help me?
I need the tensorflow 1.x pd file of clip text encoder, so I have to convert the TF 2.x pb file. Here is the code: ---------------------------------- code start -------------------------
import os import tensorflow as tf from tensorflow import keras from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 import pdb
TF2_model_input = 'CLIP_RN50' # the input path TF115_model_output = 'dir_for_saving_TF1.15_pb'
os.makedirs(TF115_model_output, exist_ok = True)
model = tf.keras.models.load_model(TF2_model_input) # read the model ckpt in TF2.8
full_model = tf.function(lambda serving_default_image,serving_default_text: # change the model to concrete function model(serving_default_image,serving_default_text)) full_model = full_model.get_concrete_function(tf.TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='input/0'),tf.TensorSpec(shape=(None, None, None), dtype=tf.int64, name='input/1')) # Note: if the input information is not in the inherited tf.keras.Model, the Concrete Function is needed to define the input information via TensorSpec.
frozen_func = convert_variables_to_constants_v2(full_model) # change the model parameters to constants
frozen_func.graph.as_graph_def() # change the model graph to graph def
layers = [op.name for op in frozen_func.graph.get_operations()] # debug, check the parameters and parameter names (needed in TF1.15) for layer in layers: print(layer) print("Frozen model inputs: ", frozen_func.inputs) print("Frozen model outputs: ", frozen_func.outputs)
tf.io.write_graph(graph_or_graph_def=frozen_func.graph, # save the Frozen graph using TF1.15 logdir=frozen_out_path, name="tf1.15_frozen_graph_model.pb", as_text=False) ------------------------------------------------- Code ende ------------------------------------------------- but, it gives me an error: =========================== Error ================================== WARNING:tensorflow:No training configuration found in save file, so the model was not compiled. Compile it manually. Traceback (most recent call last): File "convert_tf2pb_to_tf115pb.py", line 19, in
full_model = full_model.get_concrete_function(tf.TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='input/0'),tf.TensorSpec(shape=(None, None, None), dtype=tf.int64, name='input/1'))
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 1264, in get_concrete_function
concrete = self._get_concrete_function_garbage_collected(*args, kwargs)
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 1244, in _get_concrete_function_garbage_collected
self._initialize(args, kwargs, add_initializers_to=initializers)
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 786, in _initialize
*args, *kwds))
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 2983, in _get_concrete_function_internal_garbage_collected
graphfunction, = self._maybe_define_function(args, kwargs)
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 3292, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 3140, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py", line 1161, in func_graph_from_py_func
func_outputs = python_func(func_args, func_kwargs)
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 677, in wrapped_fn
out = weak_wrapped_fn().wrapped(*args, **kwds)
File "/218019043/software/anaconda3/envs/dassl/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py", line 1147, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
===========================Error End==============================
Thanks a lot