2

我有一个图像生成器,它从 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 模式。

我可以从这些功能之一中受益吗?

4

1 回答 1

1

这不是一个确定的答案,但是对于压缩数据,我今天遇到了问题,并在寻找此修复程序时发现了您的问题:Giving a python file object to h5py instead of a filename, you can bypass some the questions and read compressed data via multiprocessing

    # using the python file-open overcomes some complex problem
    with h5py.File( open(self.src, "rb"), "r" ) as hfile:
        # grab the data from hfile
        groups = list(h['/']) # etc

据我所知,hdf 正在尝试为压缩(分块)数据“优化”磁盘 IO。如果多个进程试图读取相同的块,您可能不想为每个进程解压缩它们。这会造成混乱。使用 python 文件对象,我们可以希望库不再知道进程正在查看相同的数据并且将停止尝试提供帮助。

于 2021-02-07T22:33:55.683 回答