Repository: keras-team/keras

Issue:
the weights file will not be closed after called load_weights function.
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.








Discussion:
I can confirm this - the error here was on a save, and my order is different, but it just failed after 12 hours of training because of too many open file descriptors.

I suspect that it just happened to be the save that broke the camels back, but is most likely a open call that happens in another callback.

```
Traceback (most recent call last):
  File "/home/user/Code/Keras/lstm_text_gen/stateful_hold_lstm.py", line 158, in <module>
  File "/home/user/anaconda3/envs/tensorflow/lib/python3.4/site-packages/keras/models.py", line 882, in fit_generator
  File "/home/user/anaconda3/envs/tensorflow/lib/python3.4/site-packages/keras/engine/training.py", line 1505, in fit_generator
  File "/home/user/anaconda3/envs/tensorflow/lib/python3.4/site-packages/keras/callbacks.py", line 42, in on_epoch_end
  File "/home/user/Code/Keras/lstm_text_gen/stateful_hold_lstm.py", line 116, in on_epoch_end
  File "/home/user/anaconda3/envs/tensorflow/lib/python3.4/site-packages/keras/engine/topology.py", line 2443, in save_weights
  File "/home/user/anaconda3/envs/tensorflow/lib/python3.4/site-packages/h5py/_hl/files.py", line 272, in __init__
  File "/home/user/anaconda3/envs/tensorflow/lib/python3.4/site-packages/h5py/_hl/files.py", line 98, in make_fid
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/work/h5py/_objects.c:2696)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/work/h5py/_objects.c:2654)
  File "h5py/h5f.pyx", line 96, in h5py.h5f.create (/home/ilan/minonda/conda-bld/work/h5py/h5f.c:2109)
OSError: Unable to create file (Unable to open file: name = './out_data/dsp_2_512/tmp/weights.h5', errno = 24, error message = 'too many open files', flags = 13, o_flags = 242)
```
I'm starting to suspect my issue may have been related to TensorBoard rather than load weights. #4533 
Fixed.
solved by changing my hdf5 file name.
  
when i run 
f = h5py.File(os.path.join(data_path, 'train_16.h5'), 'w', compression='blosc:lz4', compression_opts=9)
i get error
TypeError: 'compression' is an invalid keyword argument for this function
kimdly provide some solutions
This has not been fixed. The weight file is simply not closed, meaning that if you keep the kernel running for long enough, loading different weights multiple times, you eventually run into OSError: too many open files.

Resolving patch:
diff --git a/keras/engine/network.py b/keras/engine/network.py
--- a/keras/engine/network.py
+++ b/keras/engine/network.py
@@ -1215,6 +1215,10 @@ def load_weights(self, filepath, by_name=False,
             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()
 
     def _updated_config(self):
         """Util hared between different serialization methods.


Test patch: