在 HDF5 文件中创建和访问数据集

Posted

技术标签:

【中文标题】在 HDF5 文件中创建和访问数据集【英文标题】:Creating and accessing datasets in an HDF5 file 【发布时间】:2019-02-04 05:41:02 【问题描述】:

我正在尝试创建一个包含两个数据集“数据”和“标签”的 HDF5 文件。但是,当我尝试访问上述文件时,出现如下错误:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1664, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/pycharm/Input_Pipeline.py", line 140, in <module>
    data_h5 = f['data'][:]
  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 "C:\Users\u20x47\PycharmProjects\PCL\venv\lib\site-packages\h5py\_hl\group.py", line 177, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  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\h5o.pyx", line 190, in h5py.h5o.open
ValueError: Not a location (invalid object ID)

用于创建数据集的代码:

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')

data_x an array of arrays. Each element in data_x is a 3D array of 1024 elements. 
label is an array of arrays as well. Each element is a 1D array of 1 element. 

访问上述文件的代码:

f = h5_file
data_h5 = f['data'][:]
label_h5 = f['label'][:]
print (data_h5, label_h5)

我该如何解决这个问题?这是语法错误还是逻辑错误?

【问题讨论】:

【参考方案1】:

我无法重现该错误。 也许你忘记关闭文件或者你在执行过程中改变了你的 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))

【讨论】:

嘿。感谢您的回答!我实际上通过为数据和标签声明空列表来解决它。我用数组和标签附加了数据和类号。一旦两个列表都准备好了,我通过相同的行传递它并且它起作用了。文件创建成功! 奇怪的是你必须经历创建空列表。您使用的是什么版本的 h5py 和 HDF5?我使用 HDF5 v1.8.16 和 h5py 2.8.0,我使用 python 2.7.15 我没有单独使用 HDF5。只是 h5py 绑定 2.8.0 和 python 3.6.5。我不确定它是否与您在代码中所做的那样声明 numpy 空数组有什么不同。是吗? 所以也许我不太明白您是如何解决问题的。你能用你如何解决你的问题来编辑你的问题吗?在我的代码中,我直接给出数据(它全为零的事实只是为了举例,我已经编辑了我的代码,所以没有混淆) 更正:我只为标签使用了一个空列表,因为它是一个简单的一维整数列表。对于数据,我还创建了与我尝试附加的 3D 数组大小相同的零。

以上是关于在 HDF5 文件中创建和访问数据集的主要内容,如果未能解决你的问题,请参考以下文章