Use isdir() to decide whether to load_model as hdf5 or SavedModel
@@ -198,20 +198,23 @@ 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))):
- return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
- compile)
+ 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}')
- 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:
+ 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 saved_model_load.load(filepath, compile, options)
+ 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)
raise IOError(
'Unable to load model. Filepath is not an hdf5 file (or h5py is not '
