Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

python - Error loading model weights from .hdf5 in Keras

I'm trying to load model weights from an hdf5 file to evaluate on my test set. When I try and load the weights, I get the following error:

"Unable to open object (file read failed: time = Sat Jan 9 18:02:20 2021 , filename = '/content/drive/My Drive/Training Checkpoints/training_vgg16/Augmented/01-1.6986_preprocessed_unfrozen.hdf5', file descriptor = 203, errno = 5, error message = 'Input/output error', buf = 0x2d4ae840, total read size = 328, bytes this sub-read = 328, bytes actually read = 18446744073709551615, offset = 134448512)"

And the code I'm using is below:

weights_path = '/content/drive/My Drive/Training Checkpoints/training_vgg16/Augmented/'

for weight in os.listdir(weights_path):
    print(weight)
    weight_path = weights_path + weight
    model.load_weights(weight_path)
    evaluate_model()

The same process was working fine yesterday, but today I'm getting this error. Any help would be very much appreciated!

EDIT: after restarting the Colab runtime and rerunning this is the error stack trace I get:

KeyError                                  Traceback (most recent call last)
<ipython-input-51-0c9304b73f08> in <module>()
      7     print(weight)
      8     weight_path = weights_path + weight
----> 9     model.load_weights(weight_path)
     10     evaluate_model()

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

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

/usr/local/lib/python3.6/dist-packages/h5py/_hl/group.py in __getitem__(self, name)
    262                 raise ValueError("Invalid HDF5 object reference")
    263         else:
--> 264             oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
    265 
    266         otype = h5i.get_type(oid)

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

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

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

KeyError: "Unable to open object (file read failed: time = Sat Jan  9 20:30:57 2021
, filename = '/content/drive/My Drive/Training Checkpoints/training_vgg16/Unaugmented/03-1.5748_1_frozen.hdf5', file descriptor = 85, errno = 22, error message = 'Invalid argument', buf = 0x2b2af360, total read size = 160, bytes this sub-read = 160, bytes actually read = 18446744073709551615, offset = 49486272)"```
question from:https://stackoverflow.com/questions/65646104/error-loading-model-weights-from-hdf5-in-keras

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Turns out, although using load_weights worked before, I was actually saving the entire model, and for some of the saved .hdf5 files it didn't work. Changing to using load_model loads all of them correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...