I can find the input and output tensor names using saved_model_cli show --dir <my model> --all
, which, for a model defined like this
input_spec = tf.TensorSpec(shape=(None, 28, 28), name='inputs')
model = tf.keras.Sequential()
model.add(tf.keras.Input(type_spec=input_spec))
...
@tf.function
def serve(*args, **kwargs):
outputs = model(*args, **kwargs)
return {'outputs': outputs}
...
model.save(
save_dir,
save_format='tf',
signatures={'prediction_default': serve.get_concrete_function(input_spec)})
shows
signature_def['prediction_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 28, 28)
name: prediction_default_inputs:0
The given SavedModel SignatureDef contains the following output(s):
outputs['outputs'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 10)
name: StatefulPartitionedCall:0
To call the model in C++, I need the input and output tensor_info.name
s. The input name I can hackily identify since it concatenates the signature name and the input tensor name, but how can I (semi) reliably identiy the output name (StatefulPartitionedCall:0
)? Better yet, is there a way to set these myself in Python? Unfortunately, I am limited to TF 2.5.0 and the pre-built C API, so I cannot, for instance, inspect the output of tensorflow::LoadSavedModel
using the C++ API.