我有一个巨大的训练数据集,无法容纳在RAM中。我尝试将随机批次的图像加载到堆栈中,而不加载整个.h5。我的方法是创建索引列表并对其进行混排,而不是对整个.h5文件进行混排。 假设:
a = np.arange(2000*2000*2000).reshape(2000, 2000, 2000)
idx = np.random.randint(2000, size = 800) #so that I only need to shuffle this idx at the end of epoch
# create this huge data 32GBs > my RAM
with h5py.File('./tmp.h5', 'w') as f:
tmp = f.create_dataset('a', (2000, 2000, 2000))
tmp[:] = a
# read it
with h5py.File('./tmp.h5', 'r') as f:
tensor = f['a'][:][idx] #if I don't do [:] there will be error if I do so it will load whole file which I don't want
有人可以解决吗?
答案 0 :(得分:0)
感谢@ max9111,这是我建议解决的方法:
batch_size = 100
idx = np.arange(2000)
# shuffle
idx = np.random.shuffle(idx)
选择坐标必须按升序给出
阅读前应该先排序
for step in range(epoch_len // batch_size):
try:
with h5py.File(path, 'r') as f:
return f['img'][np.sort(idx[step * batch_size])], f['label'][np.sort(idx[step * batch_size])]
except:
raise('epoch finished and drop the remainder')