Skip to content

Commit

Permalink
Use isdir() to decide whether to load_model as hdf5 or SavedModel
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 404855256
  • Loading branch information
pcish authored and tensorflower-gardener committed Oct 21, 2021
1 parent 411ab6b commit b78faed
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions keras/saving/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,24 @@ def load_model(filepath, custom_objects=None, compile=True, options=None): # py
with generic_utils.SharedObjectLoadingScope():
with generic_utils.CustomObjectScope(custom_objects or {}):
with load_context.load_context(options):
if (h5py is not None and
(isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
filepath_str = path_to_string(filepath)
if isinstance(filepath_str, str):
if not tf.io.gfile.exists(filepath_str):
raise IOError(f'No file or directory found at {filepath_str}')

if tf.io.gfile.isdir(filepath_str):
return saved_model_load.load(filepath_str, compile, options)
else:
if h5py is None:
raise ImportError(
'Filepath looks like a hdf5 file but h5py is not available.'
f' filepath={filepath}')
return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
compile)
elif h5py is not None and isinstance(filepath, h5py.File):
return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
compile)

filepath = path_to_string(filepath)
if isinstance(filepath, str):
if not tf.io.gfile.exists(filepath):
raise IOError(f'No file or directory found at {filepath}')
if saving_utils.is_hdf5_filepath(filepath) and h5py is None:
raise ImportError(
'Filepath looks like a hdf5 file but h5py is not available.'
f' filepath={filepath}')
return saved_model_load.load(filepath, compile, options)

raise IOError(
'Unable to load model. Filepath is not an hdf5 file (or h5py is not '
f'available) or SavedModel. Received: filepath={filepath}')
Expand Down

0 comments on commit b78faed

Please sign in to comment.