seqcode/cross-species-domain-adaptation

Error loading models with keras.models.load_model in 0_generate_predictions_for_other_notebooks

sbresnahan opened this issue · 0 comments

I have installed the versions of all dependancies listed in the README.md.

2_train_and_test_models/train.py calls the ModelSaveCallback class from callbacks.py, which saves model files in new directories via model.save(). However, in 3_manuscript_figure_and_table_notebooks/0_generate_predictions_for_other_notebooks.ipynb, load_keras_model() calls keras.models.load_model(), which looks for HDF5 files. Thus, I am getting an error when attempting to run the final code block ("# loop over mouse and human, the two species to evaluate models in"):

`
===== CTCF mm10 test, mm10 trained =====

Loading /storage/home/stb5321/scratch/domain_adaptation/models/CTCF/mm10_trained/basic_model/2023-11-02_15-07-52_run1_best.model.

OSError Traceback (most recent call last)
/tmp/ipykernel_2854427/986113624.py in
9
10 # load the 5 independently trained models for the given tf and training species
---> 11 models = get_models_all_runs(tf, train_species)
12
13 # generate predictions for all 5 independent model runs on human data

/tmp/ipykernel_2854427/2539472171.py in get_models_all_runs(tf, train_species, runs)
54 for run in range(runs):
55 model_file = get_model_file(tf, train_species, run + 1)
---> 56 models.append(load_keras_model(model_file, DA = train_species == "DA"))
57 return models

/tmp/ipykernel_2854427/2539472171.py in load_keras_model(model_file, DA)
45 custom_objects = {"GradientReversal":GradientReversal,
46 "custom_loss":custom_loss})
---> 47 return keras.models.load_model(model_file)
48
49

/storage/work/stb5321/mlearn/lib/python3.7/site-packages/keras/engine/saving.py in load_wrapper(*args, **kwargs)
490 os.remove(tmp_filepath)
491 return res
--> 492 return load_function(*args, **kwargs)
493
494 return load_wrapper

/storage/work/stb5321/mlearn/lib/python3.7/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
581
582 if H5Dict.is_supported_type(filepath):
--> 583 with H5Dict(filepath, mode='r') as h5dict:
584 model = _deserialize_model(h5dict, custom_objects, compile)
585 elif hasattr(filepath, 'write') and callable(filepath.write):

/storage/work/stb5321/mlearn/lib/python3.7/site-packages/keras/utils/io_utils.py in init(self, path, mode)
189 self._is_file = False
190 elif isinstance(path, six.string_types) or _is_path_instance(path):
--> 191 self.data = h5py.File(path, mode=mode)
192 self._is_file = True
193 elif isinstance(path, dict):

/storage/work/stb5321/mlearn/lib/python3.7/site-packages/h5py/_hl/files.py in init(self, name, mode, driver, libver, userblock_size, swmr, rdcc_nslots, rdcc_nbytes, rdcc_w0, track_order, **kwds)
406 fid = make_fid(name, mode, userblock_size,
407 fapl, fcpl=make_fcpl(track_order=track_order),
--> 408 swmr=swmr)
409
410 if isinstance(libver, tuple):

/storage/work/stb5321/mlearn/lib/python3.7/site-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
171 if swmr and swmr_support:
172 flags |= h5f.ACC_SWMR_READ
--> 173 fid = h5f.open(name, flags, fapl=fapl)
174 elif mode == 'r+':
175 fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (file read failed: time = Mon Nov 20 12:26:54 2023
, filename = '/storage/home/stb5321/scratch/domain_adaptation/models/CTCF/mm10_trained/basic_model/2023-11-02_15-07-52_run1_best.model', file descriptor = 73, errno = 21, error message = 'Is a directory', buf = 0x7ffc21646dd0, total read size = 8, bytes this sub-read = 8, bytes actually read = 18446744073709551615, offset = 0)
`

Is this a version compatibility issue, or should models have been saved as HDF5 files rather than directories?