the weights file will not be closed after called load_weights function.
See original GitHub issuehi, I find that if I load weights from a h5 file, the file will not be closed after called load_weights function, so if I want to save the weights to this file again, it will throw an IO error like follows:
File "g:\Anaconda\lib\site-packages\keras\engine\training.py", line 1124, in fit
callback_metrics=callback_metrics)
File "g:\Anaconda\lib\site-packages\keras\engine\training.py", line 862, in _fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "g:\Anaconda\lib\site-packages\keras\callbacks.py", line 42, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "g:\Anaconda\lib\site-packages\keras\callbacks.py", line 298, in on_epoch_end
self.model.save(filepath, overwrite=True)
File "g:\Anaconda\lib\site-packages\keras\engine\topology.py", line 2423, in save
save_model(self, filepath, overwrite)
File "g:\Anaconda\lib\site-packages\keras\models.py", line 48, in save_model
f = h5py.File(filepath, 'w')
File "g:\Anaconda\lib\site-packages\h5py\_hl\files.py", line 222, in __init__
fid = make_fid(name, mode, userblock_size, fapl)
File "g:\Anaconda\lib\site-packages\h5py\_hl\files.py", line 85, in make_fid
fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
File "h5f.pyx", line 90, in h5py.h5f.create (h5py\h5f.c:1998)
IOError: Unable to create file (Unable to truncate a file which is already open)
I check the load_weights function code in keras\engine\topology.py is like follows:
def load_weights(self, filepath, by_name=False):
import h5py
f = h5py.File(filepath, mode='r')
if 'layer_names' not in f.attrs and 'model_weights' in f:
f = f['model_weights']
if by_name:
self.load_weights_from_hdf5_group_by_name(f)
else:
self.load_weights_from_hdf5_group(f)
if hasattr(f, 'close'):
f.close()
it will call f = f[‘model_weights’] if ‘model_weights’ in f and change f to a HDF5 group, which have no ‘close’ attribute. I think this is a bug.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
the weights file will not be closed after called load_weights function.
I find that if I load weights from a h5 file, the file will not be closed after called load_weights function, so if...
Read more >keras tensorflow load_weights fails - Stack Overflow
I have a function that loads a pre-calibrated model from json and then loads its weights from a hdf5 file. def load(): model...
Read more >How to Checkpoint Deep Learning Models in Keras
When training deep learning models, the checkpoint is at the weights of the model. These weights can be used to make predictions as...
Read more >A quick complete tutorial to save and restore Tensorflow models
In this quick Tensorflow tutorial, you shall learn what's a Tensorflow model and how to save and restore Tensorflow models for fine-tuning and...
Read more >MATLAB importKerasNetwork - MathWorks
Import network architecture and import the weights from separate files. The .json file does not have an output layer or information on the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Fixed.
I made a small addition to the
load_weights()
method as shown below. I’ll submit a PR momentarily.