Converts a checkpoint and graph to a servable for TensorFlow Serving. Use TF's `SavedModelBuilder` to export a trained model without tensorpack dependency. Args: filename (str): path for export directory tags (tuple): tuple of user specified tags. De
(self, filename,
tags=None,
signature_name='prediction_pipeline')
| 89 | logger.info("Output graph written to {}.".format(filename)) |
| 90 | |
| 91 | def export_serving(self, filename, |
| 92 | tags=None, |
| 93 | signature_name='prediction_pipeline'): |
| 94 | """ |
| 95 | Converts a checkpoint and graph to a servable for TensorFlow Serving. |
| 96 | Use TF's `SavedModelBuilder` to export a trained model without tensorpack dependency. |
| 97 | |
| 98 | Args: |
| 99 | filename (str): path for export directory |
| 100 | tags (tuple): tuple of user specified tags. Defaults to just "SERVING". |
| 101 | signature_name (str): name of signature for prediction |
| 102 | |
| 103 | Note: |
| 104 | This produces |
| 105 | |
| 106 | .. code-block:: none |
| 107 | |
| 108 | variables/ # output from the vanilla Saver |
| 109 | variables.data-?????-of-????? |
| 110 | variables.index |
| 111 | saved_model.pb # a `SavedModel` protobuf |
| 112 | |
| 113 | Currently, we only support a single signature, which is the general PredictSignatureDef: |
| 114 | https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/signature_defs.md |
| 115 | """ |
| 116 | if tags is None: |
| 117 | tags = (tf.saved_model.SERVING if get_tf_version_tuple() >= (1, 12) |
| 118 | else tf.saved_model.tag_constants.SERVING, ) |
| 119 | |
| 120 | self.graph = self.config._maybe_create_graph() |
| 121 | with self.graph.as_default(): |
| 122 | input = PlaceholderInput() |
| 123 | input.setup(self.config.input_signature) |
| 124 | with PredictTowerContext(''): |
| 125 | self.config.tower_func(*input.get_input_tensors()) |
| 126 | |
| 127 | input_tensors = get_tensors_by_names(self.config.input_names) |
| 128 | saved_model = tfv1.saved_model.utils |
| 129 | inputs_signatures = {t.name: saved_model.build_tensor_info(t) for t in input_tensors} |
| 130 | output_tensors = get_tensors_by_names(self.config.output_names) |
| 131 | outputs_signatures = {t.name: saved_model.build_tensor_info(t) for t in output_tensors} |
| 132 | |
| 133 | self.config.session_init._setup_graph() |
| 134 | # we cannot use "self.config.session_creator.create_session()" here since it finalizes the graph |
| 135 | sess = tfv1.Session(config=tfv1.ConfigProto(allow_soft_placement=True)) |
| 136 | self.config.session_init._run_init(sess) |
| 137 | |
| 138 | builder = tfv1.saved_model.builder.SavedModelBuilder(filename) |
| 139 | |
| 140 | prediction_signature = tfv1.saved_model.signature_def_utils.build_signature_def( |
| 141 | inputs=inputs_signatures, |
| 142 | outputs=outputs_signatures, |
| 143 | method_name=tfv1.saved_model.signature_constants.PREDICT_METHOD_NAME) |
| 144 | |
| 145 | builder.add_meta_graph_and_variables( |
| 146 | sess, list(tags), |
| 147 | signature_def_map={signature_name: prediction_signature}) |
| 148 | builder.save() |
no test coverage detected