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"""Options for saving SavedModels.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python.util.tf_export import tf_export 22 23 24@tf_export("saved_model.LoadOptions", v1=[]) 25class LoadOptions(object): 26 """Options for loading a SavedModel. 27 28 This function may be used in the `options` argument in functions that 29 load a SavedModel (`tf.saved_model.load`, `tf.keras.models.load_model`). 30 """ 31 32 # Define object attributes in __slots__ for improved memory and performance. 33 __slots__ = ("allow_partial_checkpoint", "experimental_io_device", 34 "experimental_skip_checkpoint") 35 36 def __init__(self, 37 allow_partial_checkpoint=False, 38 experimental_io_device=None, 39 experimental_skip_checkpoint=False): 40 """Creates an object that stores options for SavedModel loading. 41 42 *When to set `allow_partial_checkpoint=True`?* 43 44 This can be used when loading a Keras model (`tf.keras.models.load_model`) 45 with custom objects. When new variables are added to the custom object 46 class, loading will fail the assertion check that all loaded variables have 47 been restored, because the SavedModel checkpoint only contains the variables 48 that were in original the custom object. 49 See the following example: 50 51 ``` 52 class Custom(tf.keras.Model): 53 def __init__(self): 54 super(Custom, self).__init__() 55 self.v = tf.Variable(...) 56 57 def call(self, inputs): 58 return ... 59 60 model = Custom() 61 model.save(...) 62 ``` 63 64 After saving, say that `Custom` is updated to include an additional 65 variable. 66 67 ``` 68 class Custom(tf.keras.Model): 69 def __init__(self): 70 super(Custom, self).__init__() 71 self.v = tf.Variable(...) 72 self.w = tf.Variable(...) 73 74 def call(self, inputs): 75 return ... 76 ``` 77 78 `tf.keras.models.load_model(path, custom_objects={'Custom': Custom})` fails 79 to load since `Custom.w` does not exist in the SavedModel checkpoint. To 80 acknowledge that there are variables that are not restored from the 81 checkpoint and successfully load the model, call: 82 83 ``` 84 tf.keras.models.load_model( 85 path, custom_objects={'Custom': Custom}, 86 options=tf.saved_model.LoadOptions(allow_partial_checkpoint=True)) 87 ``` 88 89 Args: 90 allow_partial_checkpoint: bool. Defaults to `False`. When enabled, allows 91 the SavedModel checkpoint to not entirely match the loaded object. 92 experimental_io_device: string. Applies in a distributed setting. 93 Tensorflow device to use to access the filesystem. If `None` (default) 94 then for each variable the filesystem is accessed from the CPU:0 device 95 of the host where that variable is assigned. If specified, the 96 filesystem is instead accessed from that device for all variables. 97 This is for example useful if you want to load from a local directory, 98 such as "/tmp" when running in a distributed setting. In that case 99 pass a device for the host where the "/tmp" directory is accessible. 100 experimental_skip_checkpoint: bool. Defaults to `False`. If set to `True`, 101 checkpoints will not be restored. Note that this in the majority of 102 cases will generate an unusable model. 103 104 Example: 105 106 load_options = tf.saved_model.LoadOptions(experimental_io_device= 107 '/job:localhost') 108 restoredmodel = tf.keras.models.load_model(saved_model_path, 109 options=load_options) 110 111 """ 112 self.experimental_io_device = experimental_io_device 113 self.allow_partial_checkpoint = allow_partial_checkpoint 114 self.experimental_skip_checkpoint = experimental_skip_checkpoint 115