I have a Keras (sequential) model that could be saved with custom signature defs in Tensorflow 1.13 as follows:
from tensorflow.saved_model.utils import build_tensor_info
from tensorflow.saved_model.signature_def_utils import predict_signature_def, build_signature_def
model = Sequential() // with some layers
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
score_signature = predict_signature_def(
inputs={'waveform': model.input},
outputs={'scores': model.output})
metadata = build_signature_def(
outputs={'other_variable': build_tensor_info(tf.constant(1234, dtype=tf.int64))})
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tf.saved_model.tag_constants.SERVING],
signature_def_map={'score': score_signature, 'metadata': metadata})
builder.save()
Migrating the model to TF2 keras was cool :), but I can't figure out how to save the model with the same signature as above. Should I be using the new tf.saved_model.save()
or tf.keras.experimental.export_saved_model()
? How should the above code be written in TF2?
Key requirements:
- The model has a score signature and a metadata signature
- The metadata signature contains 1 or more constants