xref: /aosp_15_r20/external/tensorflow/tensorflow/python/saved_model/simple_save.py (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""SavedModel simple save functionality."""
16
17from tensorflow.python.framework import ops
18from tensorflow.python.saved_model import builder
19from tensorflow.python.saved_model import signature_constants
20from tensorflow.python.saved_model import signature_def_utils
21from tensorflow.python.saved_model import tag_constants
22from tensorflow.python.util import deprecation
23from tensorflow.python.util.tf_export import tf_export
24
25
26@tf_export(v1=['saved_model.simple_save'])
27@deprecation.deprecated(
28    None,
29    'This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate '
30    'for instructions on how to migrate your code to TensorFlow v2.')
31def simple_save(session, export_dir, inputs, outputs, legacy_init_op=None):
32  """Convenience function to build a SavedModel suitable for serving.
33
34  In many common cases, saving models for serving will be as simple as:
35
36      simple_save(session,
37                  export_dir,
38                  inputs={"x": x, "y": y},
39                  outputs={"z": z})
40
41  Although in many cases it's not necessary to understand all of the many ways
42      to configure a SavedModel, this method has a few practical implications:
43    - It will be treated as a graph for inference / serving (i.e. uses the tag
44      `saved_model.SERVING`)
45    - The SavedModel will load in TensorFlow Serving and supports the
46      [Predict
47      API](https://github.com/tensorflow/serving/blob/master/tensorflow_serving/apis/predict.proto).
48      To use the Classify, Regress, or MultiInference APIs, please
49      use either
50      [tf.Estimator](https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator)
51      or the lower level
52      [SavedModel
53      APIs](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md).
54    - Some TensorFlow ops depend on information on disk or other information
55      called "assets". These are generally handled automatically by adding the
56      assets to the `GraphKeys.ASSET_FILEPATHS` collection. Only assets in that
57      collection are exported; if you need more custom behavior, you'll need to
58      use the
59      [SavedModelBuilder](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/builder.py).
60
61  More information about SavedModel and signatures can be found here:
62  https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md.
63
64  Args:
65    session: The TensorFlow session from which to save the meta graph and
66        variables.
67    export_dir: The path to which the SavedModel will be stored.
68    inputs: dict mapping string input names to tensors. These are added
69        to the SignatureDef as the inputs.
70    outputs:  dict mapping string output names to tensors. These are added
71        to the SignatureDef as the outputs.
72    legacy_init_op: Legacy support for op or group of ops to execute after the
73        restore op upon a load.
74  """
75  signature_def_map = {
76      signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
77          signature_def_utils.predict_signature_def(inputs, outputs)
78  }
79  b = builder.SavedModelBuilder(export_dir)
80  b.add_meta_graph_and_variables(
81      session,
82      tags=[tag_constants.SERVING],
83      signature_def_map=signature_def_map,
84      assets_collection=ops.get_collection(ops.GraphKeys.ASSET_FILEPATHS),
85      main_op=legacy_init_op,
86      clear_devices=True)
87  b.save()
88