1

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.names. 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.

glinka
  • 337
  • 3
  • 13
  • Please refer previous SO thread discussing similar task and see if it helps. https://stackoverflow.com/questions/59142040/tensorflow-2-0-how-to-change-the-output-signature-while-using-tf-saved-model – mlneural03 Dec 02 '21 at 21:47
  • That works (somewhat) for the input tensor name, `prediction_default_inputs`, but does not help in setting the output name, `StatefulPartitionedCall`. – glinka Dec 02 '21 at 22:16
  • 1
    The answer [here](https://stackoverflow.com/questions/68561245/extract-or-set-input-output-tf-tensor-names-information-from-python-api-instea) shows how to get these values in Python, which doesn't answer the question but is enough for my purposes. – glinka Dec 03 '21 at 15:25
  • Did you solve this? @glinka – Preshen Jun 12 '22 at 21:43
  • No, frustratingly. We manually identify them using `saved_model_cli`. – glinka Jun 13 '22 at 20:01

0 Answers0