Model loading: do not return inside `try` block.
@@ -251,6 +251,7 @@ def load_model(filepath, custom_objects=None, compile=True):
else:
f = filepath
+ model = None
try:
# instantiate model
model_config = f.attrs.get('model_config')
@@ -262,15 +263,13 @@ def load_model(filepath, custom_objects=None, compile=True):
# set weights
load_weights_from_hdf5_group(f['model_weights'], model.layers)
- # Early return if compilation is not required.
- if not compile:
- return model
-
+ if compile:
# instantiate optimizer
training_config = f.attrs.get('training_config')
if training_config is None:
warnings.warn('No training configuration found in save file: '
- 'the model was *not* compiled. Compile it manually.')
+ 'the model was *not* compiled. '
+ 'Compile it manually.')
return model
training_config = json.loads(training_config.decode('utf-8'))
optimizer_config = training_config['optimizer_config']
@@ -310,10 +309,10 @@ def load_model(filepath, custom_objects=None, compile=True):
'state. As a result, your model is '
'starting with a freshly initialized '
'optimizer.')
- return model
finally:
if opened_new_file:
f.close()
+ return model
def model_from_config(config, custom_objects=None):
