Мне не удалось воспроизвести ошибку.
Возможно, вы забыли закрыть файл или изменили содержимое h5 во время выполнения.
Также вы можете использовать print h5_file.items()
для проверки содержимого вашего файла h5
Проверенный код:
import h5py
import numpy as np
h5_file = h5py.File('test.h5', 'w')
# bogus data with the correct size
data_x = np.random.rand(16,8,8)
label = np.random.randint(100, size=(1,1),dtype='uint8')
#
h5_file.create_dataset('data', data=data_x, compression='gzip', compression_opts=4, dtype='float32')
h5_file.create_dataset('label', data=label, compression='gzip', compression_opts=1, dtype='uint8')
h5_file.close()
h5_file = h5py.File('test.h5', 'r')
f = h5_file
print f.items()
data_h5 = f['data'][...]
label_h5 = f['label'][...]
print (data_h5, label_h5)
h5_file.close()
Производит
[(u'data', <HDF5 dataset "data": shape (16, 8, 8), type "<f4">), (u'label', <HDF5 dataset "label": shape (1, 1), type "|u1">)]
(array([[[4.36837107e-01, 8.05664659e-01, 3.34415197e-01, ...,
8.89135897e-01, 1.84097692e-01, 3.60782951e-01],
[8.86442482e-01, 6.07181549e-01, 2.42844030e-01, ...,
[4.24369454e-01, 6.04596496e-01, 5.56676507e-01, ...,
7.22884715e-01, 2.45932683e-01, 9.18777227e-01]]], dtype=float32), array([[25]], dtype=uint8))