python - 在 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)"```
解决方案
事实证明,虽然使用load_weights
之前有效,但我实际上保存了整个模型,并且对于某些保存的 .hdf5 文件它不起作用。更改为使用load_model
正确加载所有这些。
推荐阅读
- sql - 如何用 WHERE 条件和超过 x 个条目来制定 SQL 查询?
- javascript - 基本图片上传不起作用:“405 Method not allowed”
- php - PHP访问方括号中的嵌套json元素
- javascript - 对象作为 React 子级无效(找到:带键的对象)。如果您打算渲染一组孩子,请改用数组
- r - 如何在一张图中绘制 2 个直方图(不同的行长)(ggplot)
- csvhelper - 如何将特定时区的日期时间解析为 UTC?
- gnuplot - 在gnuplot中用`front`键交换对象的层
- javascript - 加载资源失败:net::ERR_CACHE_OPERATION_NOT_SUPPORTED
- osgi - 使用 STATIC 策略绑定 OSGi 声明式服务的顺序
- python - Apache 无法找到烧瓶应用程序路径