【发布时间】:2020-08-30 18:07:50
【问题描述】:
我正在尝试将图像数据集合并到 H5 文件中。到目前为止,我已经设法创建了该文件,但是当我附加到它时,它只会覆盖已经存在的内容。我查看了其他答案(例如Adding data to existing h5py file along new axis using h5py)并尝试了它们的变体但无济于事。
for i in range(len(files)):
if i == 0:
with h5py.File('input_images.h5', 'w') as f:
img = np.array(Image.open(files[i]))
f.create_dataset('/array', data = img, maxshape = (None), chunks = True, dtype = img.dtype)
else:
with h5py.File('input_images.h5', 'r+') as f:
img = np.array(Image.open(files[i]))
f.require_dataset('/array', data = img, shape = img.shape, dtype = img.dtype)
print(i)
我尝试将 maxshape 设置为 (None, None, None),但这只会产生错误:ValueError: "maxshape" must have same rank as dataset shape
总共有 1000 张图片,每张图片的形状为 2048 到 2048。有人可以告诉我如何修复我的代码吗?
【问题讨论】:
-
查看这个答案。第一步创建一个 HDF5 文件,其中包含与您类似的图像数据集。但是,它会一次性加载所有图像数据。我将尝试创建一个示例,稍后为您逐步完成它。 How get data from hdf5 file
-
这里是另一个使用
maxshape和resize的例子。 way to remove rows from a HDF5 dataset 添加或删除工作相同。这个例子比较简单:数据集和数组的维数相同。