def _v1_multi_metagraph_saved_model(self): export_graph = ops.Graph() with export_graph.as_default(): start = array_ops.placeholder( shape=[None], dtype=dtypes.float32, name="start") v = resource_variable_ops.ResourceVariable(21.) first_output = array_ops.identity(start * v, name="first_output") second_output = array_ops.identity(v, name="second_output") with session_lib.Session() as session: session.run(v.initializer) path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) builder = builder_impl.SavedModelBuilder(path) builder.add_meta_graph_and_variables( session, tags=["first"], signature_def_map={ "first_key": signature_def_utils.build_signature_def( {"first_start": utils_impl.build_tensor_info(start)}, {"first_output": utils_impl.build_tensor_info( first_output)})}) builder.add_meta_graph( tags=["second"], signature_def_map={ "second_key": signature_def_utils.build_signature_def( {"second_start": utils_impl.build_tensor_info(start)}, {"second_output": utils_impl.build_tensor_info( second_output)})}) builder.save() return path
def _v1_multi_input_saved_model(self): export_graph = ops.Graph() with export_graph.as_default(): input1 = array_ops.placeholder(shape=[None], dtype=dtypes.float32, name="input1") input2 = array_ops.placeholder(shape=[None], dtype=dtypes.float32, name="input2") v = resource_variable_ops.ResourceVariable(21.) output = array_ops.identity(input1 * v + input2, name="output") with session_lib.Session() as session: session.run(v.initializer) path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) builder = builder_impl.SavedModelBuilder(path) builder.add_meta_graph_and_variables( session, tags=[tag_constants.SERVING], signature_def_map={ "serving_default": signature_def_utils.build_signature_def( { "input1": utils_impl.build_tensor_info(input1), "input2": utils_impl.build_tensor_info(input2) }, {"output": utils_impl.build_tensor_info(output)}) }) builder.save() return path
def _v1_multi_metagraph_saved_model(self): export_graph = ops.Graph() with export_graph.as_default(): start = array_ops.placeholder( shape=[None], dtype=dtypes.float32, name="start") v = resource_variable_ops.ResourceVariable(21.) first_output = array_ops.identity(start * v, name="first_output") second_output = array_ops.identity(v, name="second_output") with session_lib.Session() as session: session.run(v.initializer) path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) builder = builder_impl.SavedModelBuilder(path) builder.add_meta_graph_and_variables( session, tags=["first"], signature_def_map={ "first_key": signature_def_utils.build_signature_def( {"first_start": utils_impl.build_tensor_info(start)}, {"first_output": utils_impl.build_tensor_info( first_output)})}) builder.add_meta_graph( tags=["second"], signature_def_map={ "second_key": signature_def_utils.build_signature_def( {"second_start": utils_impl.build_tensor_info(start)}, {"second_output": utils_impl.build_tensor_info( second_output)})}) builder.save() return path
def predict_signature_def(inputs, outputs): """Creates prediction signature from given inputs and outputs. This function produces signatures intended for use with the TensorFlow Serving Predict API (tensorflow_serving/apis/prediction_service.proto). This API imposes no constraints on the input and output types. Args: inputs: dict of string to `Tensor`. outputs: dict of string to `Tensor`. Returns: A prediction-flavored signature_def. Raises: ValueError: If inputs or outputs is `None`. """ if inputs is None or not inputs: raise ValueError('Prediction inputs cannot be None or empty.') if outputs is None or not outputs: raise ValueError('Prediction outputs cannot be None or empty.') signature_inputs = {key: utils.build_tensor_info(tensor) for key, tensor in inputs.items()} signature_outputs = {key: utils.build_tensor_info(tensor) for key, tensor in outputs.items()} signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.PREDICT_METHOD_NAME) return signature_def
def predict_signature_def(inputs, outputs): """Creates prediction signature from given inputs and outputs. This function produces signatures intended for use with the TensorFlow Serving Predict API (tensorflow_serving/apis/prediction_service.proto). This API imposes no constraints on the input and output types. Args: inputs: dict of string to `Tensor`. outputs: dict of string to `Tensor`. Returns: A prediction-flavored signature_def. Raises: ValueError: If inputs or outputs is `None`. """ if inputs is None or not inputs: raise ValueError('Prediction inputs cannot be None or empty.') if outputs is None or not outputs: raise ValueError('Prediction outputs cannot be None or empty.') signature_inputs = {key: utils.build_tensor_info(tensor) for key, tensor in inputs.items()} signature_outputs = {key: utils.build_tensor_info(tensor) for key, tensor in outputs.items()} signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.PREDICT_METHOD_NAME) return signature_def
def classification_signature_def(examples, classes, scores): """Creates classification signature from given examples and predictions. This function produces signatures intended for use with the TensorFlow Serving Classify API (tensorflow_serving/apis/prediction_service.proto), and so constrains the input and output types to those allowed by TensorFlow Serving. Args: examples: A string `Tensor`, expected to accept serialized tf.Examples. classes: A string `Tensor`. Note that the ClassificationResponse message requires that class labels are strings, not integers or anything else. scores: a float `Tensor`. Returns: A classification-flavored signature_def. Raises: ValueError: If examples is `None`. """ if examples is None: raise ValueError('Classification `examples` cannot be None.') if not isinstance(examples, ops.Tensor): raise ValueError('Classification `examples` must be a string Tensor. ' f'Found `examples` of type {type(examples)}.') if classes is None and scores is None: raise ValueError( 'Classification `classes` and `scores` cannot both be ' 'None.') input_tensor_info = utils.build_tensor_info(examples) if input_tensor_info.dtype != types_pb2.DT_STRING: raise ValueError( 'Classification input tensors must be of type string. ' f'Found tensors of type {input_tensor_info.dtype}') signature_inputs = {signature_constants.CLASSIFY_INPUTS: input_tensor_info} signature_outputs = {} if classes is not None: classes_tensor_info = utils.build_tensor_info(classes) if classes_tensor_info.dtype != types_pb2.DT_STRING: raise ValueError( 'Classification classes must be of type string Tensor. ' f'Found tensors of type {classes_tensor_info.dtype}.`') signature_outputs[signature_constants.CLASSIFY_OUTPUT_CLASSES] = ( classes_tensor_info) if scores is not None: scores_tensor_info = utils.build_tensor_info(scores) if scores_tensor_info.dtype != types_pb2.DT_FLOAT: raise ValueError('Classification scores must be a float Tensor.') signature_outputs[signature_constants.CLASSIFY_OUTPUT_SCORES] = ( scores_tensor_info) signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.CLASSIFY_METHOD_NAME) return signature_def
def classification_signature_def(examples, classes, scores): """Creates classification signature from given examples and predictions. This function produces signatures intended for use with the TensorFlow Serving Classify API (tensorflow_serving/apis/prediction_service.proto), and so constrains the input and output types to those allowed by TensorFlow Serving. Args: examples: A string `Tensor`, expected to accept serialized tf.Examples. classes: A string `Tensor`. Note that the ClassificationResponse message requires that class labels are strings, not integers or anything else. scores: a float `Tensor`. Returns: A classification-flavored signature_def. Raises: ValueError: If examples is `None`. """ if examples is None: raise ValueError('Classification examples cannot be None.') if not isinstance(examples, ops.Tensor): raise ValueError('Classification examples must be a string Tensor.') if classes is None and scores is None: raise ValueError('Classification classes and scores cannot both be None.') input_tensor_info = utils.build_tensor_info(examples) if input_tensor_info.dtype != types_pb2.DT_STRING: raise ValueError('Classification examples must be a string Tensor.') signature_inputs = {signature_constants.CLASSIFY_INPUTS: input_tensor_info} signature_outputs = {} if classes is not None: classes_tensor_info = utils.build_tensor_info(classes) if classes_tensor_info.dtype != types_pb2.DT_STRING: raise ValueError('Classification classes must be a string Tensor.') signature_outputs[signature_constants.CLASSIFY_OUTPUT_CLASSES] = ( classes_tensor_info) if scores is not None: scores_tensor_info = utils.build_tensor_info(scores) if scores_tensor_info.dtype != types_pb2.DT_FLOAT: raise ValueError('Classification scores must be a float Tensor.') signature_outputs[signature_constants.CLASSIFY_OUTPUT_SCORES] = ( scores_tensor_info) signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.CLASSIFY_METHOD_NAME) return signature_def
def _supervised_signature_def(method_name, inputs, loss=None, predictions=None, metrics=None): """Creates a signature for training and eval data. This function produces signatures that describe the inputs and outputs of a supervised process, such as training or evaluation, that results in loss, metrics, and the like. Note that this function only requires inputs to be not None. Args: method_name: Method name of the SignatureDef as a string. inputs: dict of string to `Tensor`. loss: dict of string to `Tensor` representing computed loss. predictions: dict of string to `Tensor` representing the output predictions. metrics: dict of string to `Tensor` representing metric ops. Returns: A train- or eval-flavored signature_def. Raises: ValueError: If inputs or outputs is `None`. """ if inputs is None or not inputs: raise ValueError( '{} inputs cannot be None or empty.'.format(method_name)) signature_inputs = { key: utils.build_tensor_info(tensor) for key, tensor in inputs.items() } signature_outputs = {} for output_set in (loss, predictions, metrics): if output_set is not None: sig_out = { key: utils.build_tensor_info(tensor) for key, tensor in output_set.items() } signature_outputs.update(sig_out) signature_def = build_signature_def(signature_inputs, signature_outputs, method_name) return signature_def
def regression_signature_def(examples, predictions): """Creates regression signature from given examples and predictions. This function produces signatures intended for use with the TensorFlow Serving Regress API (tensorflow_serving/apis/prediction_service.proto), and so constrains the input and output types to those allowed by TensorFlow Serving. Args: examples: A string `Tensor`, expected to accept serialized tf.Examples. predictions: A float `Tensor`. Returns: A regression-flavored signature_def. Raises: ValueError: If examples is `None`. """ if examples is None: raise ValueError('Regression `examples` cannot be None.') if not isinstance(examples, ops.Tensor): raise ValueError( 'Expected regression `examples` to be of type Tensor. ' f'Found `examples` of type {type(examples)}.') if predictions is None: raise ValueError('Regression `predictions` cannot be None.') input_tensor_info = utils.build_tensor_info(examples) if input_tensor_info.dtype != types_pb2.DT_STRING: raise ValueError('Regression input tensors must be of type string. ' f'Found tensors with type {input_tensor_info.dtype}.') signature_inputs = {signature_constants.REGRESS_INPUTS: input_tensor_info} output_tensor_info = utils.build_tensor_info(predictions) if output_tensor_info.dtype != types_pb2.DT_FLOAT: raise ValueError( 'Regression output tensors must be of type float. ' f'Found tensors with type {output_tensor_info.dtype}.') signature_outputs = { signature_constants.REGRESS_OUTPUTS: output_tensor_info } signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.REGRESS_METHOD_NAME) return signature_def
def _v1_output_shape_saved_model(self): export_graph = ops.Graph() with export_graph.as_default(): start = array_ops.placeholder( shape=[None], dtype=dtypes.float32, name="start") output = array_ops.identity(start, name="output") output.set_shape([1]) # Ok to use [1] because shape is only informational with session_lib.Session() as session: path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) builder = builder_impl.SavedModelBuilder(path) builder.add_meta_graph_and_variables( session, tags=[tag_constants.SERVING], signature_def_map={ "serving_default": signature_def_utils.build_signature_def( {"start": utils_impl.build_tensor_info(start)}, {"output": utils_impl.build_tensor_info(output)}) }) builder.save() return path
def _v1_output_shape_saved_model(self): export_graph = ops.Graph() with export_graph.as_default(): start = array_ops.placeholder( shape=[None], dtype=dtypes.float32, name="start") output = array_ops.identity(start, name="output") output.set_shape([1]) # Ok to use [1] because shape is only informational with session_lib.Session() as session: path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) builder = builder_impl.SavedModelBuilder(path) builder.add_meta_graph_and_variables( session, tags=[tag_constants.SERVING], signature_def_map={ "serving_default": signature_def_utils.build_signature_def( {"start": utils_impl.build_tensor_info(start)}, {"output": utils_impl.build_tensor_info(output)}) }) builder.save() return path
def _supervised_signature_def( method_name, inputs, loss=None, predictions=None, metrics=None): """Creates a signature for training and eval data. This function produces signatures that describe the inputs and outputs of a supervised process, such as training or evaluation, that results in loss, metrics, and the like. Note that this function only requires inputs to be not None. Args: method_name: Method name of the SignatureDef as a string. inputs: dict of string to `Tensor`. loss: dict of string to `Tensor` representing computed loss. predictions: dict of string to `Tensor` representing the output predictions. metrics: dict of string to `Tensor` representing metric ops. Returns: A train- or eval-flavored signature_def. Raises: ValueError: If inputs or outputs is `None`. """ if inputs is None or not inputs: raise ValueError('{} inputs cannot be None or empty.'.format(method_name)) signature_inputs = {key: utils.build_tensor_info(tensor) for key, tensor in inputs.items()} signature_outputs = {} for output_set in (loss, predictions, metrics): if output_set is not None: sig_out = {key: utils.build_tensor_info(tensor) for key, tensor in output_set.items()} signature_outputs.update(sig_out) signature_def = build_signature_def( signature_inputs, signature_outputs, method_name) return signature_def
def regression_signature_def(examples, predictions): """Creates regression signature from given examples and predictions. This function produces signatures intended for use with the TensorFlow Serving Regress API (tensorflow_serving/apis/prediction_service.proto), and so constrains the input and output types to those allowed by TensorFlow Serving. Args: examples: A string `Tensor`, expected to accept serialized tf.Examples. predictions: A float `Tensor`. Returns: A regression-flavored signature_def. Raises: ValueError: If examples is `None`. """ if examples is None: raise ValueError('Regression examples cannot be None.') if not isinstance(examples, ops.Tensor): raise ValueError('Regression examples must be a string Tensor.') if predictions is None: raise ValueError('Regression predictions cannot be None.') input_tensor_info = utils.build_tensor_info(examples) if input_tensor_info.dtype != types_pb2.DT_STRING: raise ValueError('Regression examples must be a string Tensor.') signature_inputs = {signature_constants.REGRESS_INPUTS: input_tensor_info} output_tensor_info = utils.build_tensor_info(predictions) if output_tensor_info.dtype != types_pb2.DT_FLOAT: raise ValueError('Regression output must be a float Tensor.') signature_outputs = {signature_constants.REGRESS_OUTPUTS: output_tensor_info} signature_def = build_signature_def( signature_inputs, signature_outputs, signature_constants.REGRESS_METHOD_NAME) return signature_def
def _unfed_placeholder_signature(self): export_graph = ops.Graph() with export_graph.as_default(): x = array_ops.placeholder(name="x", shape=[], dtype=dtypes.float32) output = x * random_ops.random_normal([2]) with session_lib.Session() as session: path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) b = builder_impl.SavedModelBuilder(path) b.add_meta_graph_and_variables( session, tags=[tag_constants.SERVING], signature_def_map={ "key": signature_def_utils.build_signature_def( {}, dict(value=utils_impl.build_tensor_info(output)))}) b.save() return path
def _signature_with_no_inputs(self): export_graph = ops.Graph() with export_graph.as_default(): array_ops.placeholder(name="x", shape=[], dtype=dtypes.float32) output = random_ops.random_normal([2]) with session_lib.Session() as session: path = os.path.join(self.get_temp_dir(), "saved_model", str(ops.uid())) b = builder_impl.SavedModelBuilder(path) b.add_meta_graph_and_variables( session, tags=[tag_constants.SERVING], signature_def_map={ "key": signature_def_utils.build_signature_def( {}, dict(value=utils_impl.build_tensor_info(output)))}) b.save() return path
def _tensor_dict_to_tensorinfo(tensor_dict): return { key: utils_impl.build_tensor_info(value) for key, value in tensor_dict.items() }
def _tensor_dict_to_tensorinfo(tensor_dict): return {key: utils_impl.build_tensor_info(value) for key, value in tensor_dict.items()}