首页 > 解决方案 > 在 Keras 中从 .hdf5 加载模型权重时出错

问题描述

我正在尝试从 hdf5 文件加载模型权重以评估我的测试集。当我尝试加载权重时,出现以下错误:

"Unable to open object (file read failed: time = Sat Jan 9 18:02:20 2021\n, 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)"

我正在使用的代码如下:

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()

昨天同样的过程运行良好,但今天我收到了这个错误。任何帮助将不胜感激!

编辑:重新启动 Colab 运行时并重新运行后,这是我得到的错误堆栈跟踪:

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\n, 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)"```

标签: pythontensorflowkerasconv-neural-network

解决方案


事实证明,虽然使用load_weights之前有效,但我实际上保存了整个模型,并且对于某些保存的 .hdf5 文件它不起作用。更改为使用load_model正确加载所有这些。


推荐阅读