I have a class to load hdf5 likes
class H5Loader(data.Dataset):
def __init__(self):
self.hdf5_files = ['1.h5', '2.h5', '3.h5']
self.data_lst = []
for ind in range (len(self.hdf5_list)):
h5_file = h5py.File(self.hdf5_list[ind])
data_ = h5_file.get('data')
self.data_lst.append(data_)
#h5_file.close()
def __getitem__(self, index):
self.data = np.asarray(self.data_lst[index])
return torch.from_numpy(self.data).float()
def __len__(self):
return len(self.hdf5_list)
It worked. However, the hdf5 does not close after __init__
function. Hence, I closed it using h5_file.close()
(uncommend line above). But it show the error
File "h5loader.py", line 30, in __getitem__
self.data = np.asarray(self.data_lst[index])
File "/home/john/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py", line 501, in asarray
return array(a, dtype, copy=False, order=order)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "/home/john/anaconda3/lib/python3.6/site-packages/h5py/_hl/dataset.py", line 690, in __array__
arr = numpy.empty(self.shape, dtype=self.dtype if dtype is None else dtype)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "/home/john/anaconda3/lib/python3.6/site-packages/h5py/_hl/dataset.py", line 225, in shape
return self.id.shape
File "h5py/h5d.pyx", line 131, in h5py.h5d.DatasetID.shape.__get__
File "h5py/h5d.pyx", line 132, in h5py.h5d.DatasetID.shape.__get__
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5d.pyx", line 288, in h5py.h5d.DatasetID.get_space
ValueError: Not a dataset (not a dataset)
Any suggestion to close h5 file?