1# Copyright 2020 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 Metrics SavedModel serialization.""" 16 17from tensorflow.python.keras.saving.saved_model import constants 18from tensorflow.python.keras.saving.saved_model import layer_serialization 19from tensorflow.python.keras.utils import generic_utils 20from tensorflow.python.training.tracking import data_structures 21 22 23class MetricSavedModelSaver(layer_serialization.LayerSavedModelSaver): 24 """Metric serialization.""" 25 26 @property 27 def object_identifier(self): 28 return constants.METRIC_IDENTIFIER 29 30 def _python_properties_internal(self): 31 metadata = dict( 32 class_name=generic_utils.get_registered_name(type(self.obj)), 33 name=self.obj.name, 34 dtype=self.obj.dtype) 35 metadata.update(layer_serialization.get_serialized(self.obj)) 36 if self.obj._build_input_shape is not None: # pylint: disable=protected-access 37 metadata['build_input_shape'] = self.obj._build_input_shape # pylint: disable=protected-access 38 return metadata 39 40 def _get_serialized_attributes_internal(self, unused_serialization_cache): 41 return (dict(variables=data_structures.wrap_or_unwrap(self.obj.variables)), 42 dict()) # TODO(b/135550038): save functions to enable saving 43 # custom metrics. 44