MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / load_model_from_hdf5

Function load_model_from_hdf5

tensorflow/python/keras/saving/hdf5_format.py:117–205  ·  view source on GitHub ↗

Loads a model saved via `save_model_to_hdf5`. Arguments: filepath: One of the following: - String, path to the saved model - `h5py.File` object from which to load the model custom_objects: Optional dictionary mapping names (strings) to custom classes or f

(filepath, custom_objects=None, compile=True)

Source from the content-addressed store, hash-verified

115
116
117def load_model_from_hdf5(filepath, custom_objects=None, compile=True): # pylint: disable=redefined-builtin
118 """Loads a model saved via `save_model_to_hdf5`.
119
120 Arguments:
121 filepath: One of the following:
122 - String, path to the saved model
123 - `h5py.File` object from which to load the model
124 custom_objects: Optional dictionary mapping names
125 (strings) to custom classes or functions to be
126 considered during deserialization.
127 compile: Boolean, whether to compile the model
128 after loading.
129
130 Returns:
131 A Keras model instance. If an optimizer was found
132 as part of the saved model, the model is already
133 compiled. Otherwise, the model is uncompiled and
134 a warning will be displayed. When `compile` is set
135 to False, the compilation is omitted without any
136 warning.
137
138 Raises:
139 ImportError: if h5py is not available.
140 ValueError: In case of an invalid savefile.
141 """
142 if h5py is None:
143 raise ImportError('`load_model` requires h5py.')
144
145 if not custom_objects:
146 custom_objects = {}
147
148 opened_new_file = not isinstance(filepath, h5py.File)
149 if opened_new_file:
150 f = h5py.File(filepath, mode='r')
151 else:
152 f = filepath
153
154 model = None
155 try:
156 # instantiate model
157 model_config = f.attrs.get('model_config')
158 if model_config is None:
159 raise ValueError('No model found in config file.')
160 model_config = json.loads(model_config)
161 model = model_config_lib.model_from_config(model_config,
162 custom_objects=custom_objects)
163
164 # set weights
165 load_weights_from_hdf5_group(f['model_weights'], model.layers)
166
167 if compile:
168 # instantiate optimizer
169 training_config = f.attrs.get('training_config')
170 if training_config is None:
171 logging.warning('No training configuration found in save file: '
172 'the model was *not* compiled. Compile it manually.')
173 return model
174 training_config = json.loads(training_config)

Callers

nothing calls this directly

Calls 7

closeMethod · 0.65
getMethod · 0.45
compileMethod · 0.45
_make_train_functionMethod · 0.45
set_weightsMethod · 0.45

Tested by

no test coverage detected