• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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"""Classes and functions implementing to Model SavedModel serialization."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21from tensorflow.python.keras.saving import saving_utils
22from tensorflow.python.keras.saving.saved_model import constants
23from tensorflow.python.keras.saving.saved_model import layer_serialization
24from tensorflow.python.keras.saving.saved_model import save_impl
25
26
27class ModelSavedModelSaver(layer_serialization.LayerSavedModelSaver):
28  """Model SavedModel serialization."""
29
30  @property
31  def object_identifier(self):
32    return constants.MODEL_IDENTIFIER
33
34  def _python_properties_internal(self):
35    metadata = super(ModelSavedModelSaver, self)._python_properties_internal()
36    # Network stateful property is dependent on the child layers.
37    metadata.pop('stateful')
38    metadata['is_graph_network'] = self.obj._is_graph_network  # pylint: disable=protected-access
39    metadata['save_spec'] = self.obj._get_save_spec(dynamic_batch=False)  # pylint: disable=protected-access
40
41    metadata.update(
42        saving_utils.model_metadata(
43            self.obj, include_optimizer=True, require_config=False))
44    return metadata
45
46  def _get_serialized_attributes_internal(self, serialization_cache):
47    default_signature = None
48
49    # Create a default signature function if this is the only object in the
50    # cache (i.e. this is the root level object).
51    if len(serialization_cache[constants.KERAS_CACHE_KEY]) == 1:
52      default_signature = save_impl.default_save_signature(self.obj)
53
54    # Other than the default signature function, all other attributes match with
55    # the ones serialized by Layer.
56    objects, functions = (
57        super(ModelSavedModelSaver, self)._get_serialized_attributes_internal(
58            serialization_cache))
59    functions['_default_save_signature'] = default_signature
60    return objects, functions
61
62
63class SequentialSavedModelSaver(ModelSavedModelSaver):
64
65  @property
66  def object_identifier(self):
67    return constants.SEQUENTIAL_IDENTIFIER
68