Exporting the trained model

The trained model can be saved by using tf.saved_model.builder.SavedModelBuilder as a protocol buffer object. First, we create the builder, input, and output tensor. The pseudocode that follows is to illustrate the procedure. You have to replace the corresponding variables enclosed by <> with the respective variables:

bldr = tf.saved_model.builder.SavedModelBuilder(<directory_to_export>)
tensor_inputs = tf.saved_model.utils.build_tensor_info(<inputs>)
tensor_outputs = tf.saved_model.utils.build_tensor_info(<outputs>)

The TensorFlow utils.build_tensor_info utility function helps us to create the necessary input and output protocol buffers. Next, we will create the signature for the inference protocol buffer:

inference_signature = (
tf.saved_model.signature_def_utils.build_signature_def(inputs={'inputs': tensor_inputs},
outputs={‘predictions’: tensor_outputs},
method_name= tf.saved_model.signature_constants.PREDICT_METHOD_NAME)

The TensorFlow signature_def_utils.build_signature_def function can be used to create the signature definition protocol buffer. After creating the inference signature, we will export it and save it by using the add_meta_graph_and_variables function of the model builder:

bldr.add_meta_graph_and_variables(tflow_s,[tf.saved_model.tag_constants.SERVING],
signature_def_map={ 'inference_results': inference_signature},
legacy_init_op=<op_init>)
bldr.save()

This will save the model as a protocol buffer in the export directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset