用多处理方式读取HDF5文件

2024-04-24 04:56:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在训练一个神经网络,它的数据太大,无法存储在内存中。我把数据分成HDF5格式的块,一次读取一个,然后把它们变成一个生成器,然后输入神经网络。我注意到数据块的读取也是一个瓶颈,为了让这个更快,当网络在训练一块数据时,我尝试使用另一个进程来读取下一块数据。我读取数据的功能如下:

import h5py 
def get_data(filename):
    current_file = h5py.File(filename, 'r')
    images = current_file['images'].value 
    coords = current_file['coords'].value
    current_file.close()
    return images, coords

当我直接应用此函数时,它工作正常,大约45秒后读取数据:

^{2}$

但是,当我尝试使用多处理工作程序时,它在10分钟后没有完成,我杀死了它:

import time
from multiprocessing import Pool
start = time()
pool = Pool(1)
res = pool.apply_async(get_data, ('Single-Cell-Image-Chunks/0.hdf5',))
images, coords = res.get()
print(time() - start)

Tags: 数据importdatagettimevalue神经网络coords