question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

the weights file will not be closed after called load_weights function.

See original GitHub issue

hi, 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fcholletcommented, Dec 4, 2016

Fixed.

0reactions
bjtho08commented, Jul 2, 2019

I made a small addition to the load_weights() method as shown below. I’ll submit a PR momentarily.

def load_weights(self, filepath, by_name=False,
                     skip_mismatch=False, reshape=False):
       """[docstring omitted for clarity]
       """
        
        if h5py is None:
            raise ImportError('`load_weights` requires h5py.')
        with h5py.File(filepath, mode='r') as f:
            if 'layer_names' not in f.attrs and 'model_weights' in f:
                f = f['model_weights']
            if by_name:
                saving.load_weights_from_hdf5_group_by_name(
                    f, self.layers, skip_mismatch=skip_mismatch,
                    reshape=reshape)
            else:
                saving.load_weights_from_hdf5_group(
                    f, self.layers, reshape=reshape)
            if hasattr(f, 'close'):
                f.close()
            elif hasattr(f.file, 'close'):
                f.file.close()
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found