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