Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/saving/legacy/saved_model/model_serialization.py: 45%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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 keras.src.saving.legacy import saving_utils 

18from keras.src.saving.legacy.saved_model import constants 

19from keras.src.saving.legacy.saved_model import layer_serialization 

20from keras.src.saving.legacy.saved_model import save_impl 

21 

22 

23class ModelSavedModelSaver(layer_serialization.LayerSavedModelSaver): 

24 """Model SavedModel serialization.""" 

25 

26 @property 

27 def object_identifier(self): 

28 return constants.MODEL_IDENTIFIER 

29 

30 def _python_properties_internal(self): 

31 metadata = super()._python_properties_internal() 

32 # Network stateful property is dependent on the child layers. 

33 metadata.pop("stateful") 

34 metadata["is_graph_network"] = self.obj._is_graph_network 

35 spec = self.obj.save_spec(dynamic_batch=False) 

36 metadata["full_save_spec"] = spec 

37 # save_spec is saved for forward compatibility on older TF versions. 

38 metadata["save_spec"] = None if spec is None else spec[0][0] 

39 

40 metadata.update( 

41 saving_utils.model_metadata( 

42 self.obj, include_optimizer=True, require_config=False 

43 ) 

44 ) 

45 return metadata 

46 

47 def _get_serialized_attributes_internal(self, serialization_cache): 

48 default_signature = None 

49 

50 # Create a default signature function if this is the only object in the 

51 # cache (i.e. this is the root level object). 

52 if len(serialization_cache[constants.KERAS_CACHE_KEY]) == 1: 

53 default_signature = save_impl.default_save_signature(self.obj) 

54 

55 # Other than the default signature function, all other attributes match 

56 # with the ones serialized by Layer. 

57 objects, functions = super()._get_serialized_attributes_internal( 

58 serialization_cache 

59 ) 

60 functions["_default_save_signature"] = default_signature 

61 return objects, functions 

62 

63 

64class SequentialSavedModelSaver(ModelSavedModelSaver): 

65 @property 

66 def object_identifier(self): 

67 return constants.SEQUENTIAL_IDENTIFIER 

68