Keras load weights: OSError: Unable to open file (Truncated file: eof = 41091072)

Viewed 7882

When I try to load weights using keras' model.load_weights I get OSError: Unable to open file (Truncated file: eof = 41091072) Here is the full error trace:

model.load_weights(get_file(fname, "E:/Work/Practical\ Deep\ Learning\ Course/my\ notebooks/lesson1/vgg16.h5", cache_subdir='models'))
    141 
    142 

C:\....\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py in load_weights(self, filepath, by_name)
   2700         """
   2701         import h5py
-> 2702         f = h5py.File(filepath, mode='r')
   2703         if 'layer_names' not in f.attrs and 'model_weights' in f:
   2704             f = f['model_weights']

C:\....\Anaconda3\envs\tensorflow\lib\site-packages\h5py\_hl\files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
    269 
    270                 fapl = make_fapl(driver, libver, **kwds)
--> 271                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    272 
    273                 if swmr_support:

C:\....\Anaconda3\envs\tensorflow\lib\site-packages\h5py\_hl\files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
     99         if swmr and swmr_support:
    100             flags |= h5f.ACC_SWMR_READ
--> 101         fid = h5f.open(name, flags, fapl=fapl)
    102     elif mode == 'r+':
    103         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.7.0\h5py\_objects.c:2853)()

h5py\_objects.pyx in h5py._objects.with_phil.wrapper (D:\Build\h5py\h5py-2.7.0\h5py\_objects.c:2811)()

h5py\h5f.pyx in h5py.h5f.open (D:\Build\h5py\h5py-2.7.0\h5py\h5f.c:2130)()

OSError: Unable to open file (Truncated file: eof = 41091072, sblock->base_addr = 0, stored_eoa = 553482496)

How can I handle this?

4 Answers

mine is windows 10. This solved my error- go to this path C:\Users\you_name\ .keras\models\vggface and delete the h5 file and rerun the code.

I solved it by adding this code before load h5 file.

from keras import models    
model = models.Sequential()

This error is saying that for whatever reason the model weights that are stored in your file cannot be loaded into the model object you have generated. There are multiple potential causes for this error.

  1. No model object has been created to load weights to. As Frightera stated, this error could be caused by not having created a model object in the first place to map weights onto. When this is the cause, the error can be fixed if the weights come from a Sequential model by creating a model object with the following code:

    from keras import models
    model = models.Sequential()

  2. There is a mismatch between the model object structure and the structure of the weights you are trying to load. This error can also be caused by trying to load model weights onto a model that does not have the same number and types of hyper-parameters of the saved weights. To check for this, make sure there is a match between the number of hyper-parameters in your model and the saved weights.

  3. The saved weights are corrupted. As the user qed noted, the weight file that you are trying to load into the model may be corrupted or not fully downloaded. Attempt to redownload the weights, and if that fails consider retraining the model.

This is by no means an exhaustive list. If people come up with more ideas for potential causes and ways of resolving this error, please comment on this answer and I will try to add it to this answer.

oh!I just found the way,delete the pre-train model '**.h5',and re-download it.

Related