Thank you.
I have implemented dataset_h5. It runs quite well when I set the “num_workers” as 1 or 2. I meet some problem when I set “num_worker” bigger than 2. It seems related to the h5py version. My h5py now is 2.7.1.

File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 281, in next
return self._process_next_batch(batch)
File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 301, in _process_next_batch
raise batch.exc_type(batch.exc_msg)
KeyError: ‘Traceback (most recent call last):\n  File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 55, in _worker_loop\n    samples = collate_fn([dataset[i] for i in batch_indices])\n  File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 55, in \n    samples = collate_fn([dataset[i] for i in batch_indices])\n  File “/home/titan/code/res-pytorch/AdobeData/AdobeData.py”, line 595, in getitem\n    fgimg = self.fgfile[‘img’][index, …]\n  File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapper\n  File “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapper\n  File “/home/titan/anaconda3/lib/python3.6/site-packages/h5py/_hl/group.py”, line 167, in getitem\n    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)\n  File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapper\n  File “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapper\n  File “h5py/h5o.pyx”, line 190, in h5py.h5o.open\nKeyError: ‘Unable to open object (bad object header version number)’\n’

My codes are like

class AdobePatchDataHDF5(data.Dataset):
def __init__(self, root, cropsize = 256, outputsize = 256):

    fgfile = h5py.File(root, 'r')

    self.root = root
    self.fgfile = fgfile
    self.cropsize = cropsize
    self.outputsize = outputsize


def __getitem__(self, index):
    # read image
    fgimg = self.fgfile['img'][index, ...]

    # random crop and resize, random flip with cv2

    # toTensors
    fgimg = fgimg.astype(np.float32) / 255.0 
    fgimg = torch.from_numpy(fg.transpose((2, 0, 1)))

    # norm [0, 1] to [-1, 1]

    return fgimg, label

def __len__(self):
    return self.fgfile['img'].shape[0]


My train loader is like
    train_loader = torch.utils.data.DataLoader(
        AdobePatchDataHDF5(path), batch_size=256, shuffle= True
        num_workers=8, pin_memory=True, sampler=None)