我有一个图像生成器,它从 HDF5 文件(通过 h5py)中读取批量 3D 张量,它使用 Python 多处理库(继承自 Keras 序列)。
我想了解我是否正确执行此操作,以及是否可以改进。
我有一个 __getitem__由多个进程调用 N 次迭代的方法,每次调用此方法时,我打开一个 HDF5 文件并读取一组给定索引的数据并立即关闭文件(通过上下文管理器)。
def get_dataset_items(self, dataset: str, indices: np.ndarray) -> np.ndarray:
"""Get an h5py dataset items.
Arguments
---------
dataset : str
The dataset key.
indices : ndarray,
The list of current batch indices.
Returns
-------
np.ndarray
An batch of elements.
"""
with h5.File(self.src, 'r') as file:
return file[dataset][indices]
看起来这种方法没有问题,但我真的不确定。我读到从多个进程读取文件时,我们可能会遇到奇怪的东西和损坏的数据。
我看到有 MPI 接口和 SWMR 模式。
我可以从这些功能之一中受益吗?