Python h5py 无法打开用 h5py 创建的 HDF5 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26613570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HDF5 file created with h5py can't be opened by h5py
提问by Lilith-Elina
I created an HDF5 file apparently without any problems, under Ubuntu 12.04 (32bit version), using Anaconda as Python distribution and writing in ipython notebooks. The underlying data are all numpy arrays. For example,
我创建了一个 HDF5 文件,显然没有任何问题,在 Ubuntu 12.04(32 位版本)下,使用 Anaconda 作为 Python 发行版并在 ipython 笔记本中编写。底层数据都是numpy数组。例如,
import numpy as np
import h5py
f = h5py.File('myfile.hdf5','w')
group = f.create_group('a_group')
group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')
If I try to open this file from a new iypthon notebook, though, I get an error message:
但是,如果我尝试从新的 iypthon 笔记本打开此文件,则会收到一条错误消息:
f = h5py.File('myfile.hdf5', "r")
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-4-b64ac5089cd4> in <module>()
----> 1 f = h5py.File(file_name, "r")
/home/sarah/anaconda/lib/python2.7/site-packages/h5py/_hl/files.pyc in __init__(self, name, mode, driver, libver, userblock_size, **kwds)
220
221 fapl = make_fapl(driver, libver, **kwds)
--> 222 fid = make_fid(name, mode, userblock_size, fapl)
223
224 Group.__init__(self, fid)
/home/sarah/anaconda/lib/python2.7/site-packages/h5py/_hl/files.pyc in make_fid(name, mode, userblock_size, fapl, fcpl)
77
78 if mode == 'r':
---> 79 fid = h5f.open(name, h5f.ACC_RDONLY, fapl=fapl)
80 elif mode == 'r+':
81 fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)
/home/sarah/anaconda/lib/python2.7/site-packages/h5py/h5f.so in h5py.h5f.open (h5py/h5f.c:1741)()
IOError: Unable to open file (Unable to find a valid file signature)
Can you tell me what that missing file signature is? Did I miss something when I created the file?
你能告诉我丢失的文件签名是什么吗?我创建文件时是否遗漏了什么?
采纳答案by Lilith-Elina
Since we resolved the issue in the comments on my question, I'm writing the results out here to mark it as solved.
由于我们在对我的问题的评论中解决了该问题,因此我将结果写在此处以将其标记为已解决。
The main problem was that I forgot to close the file after I created it. There would have been two simple options, either:
主要问题是我在创建文件后忘记关闭它。将有两个简单的选择,要么:
import numpy as np
import h5py
f = h5py.File('myfile.hdf5','w')
group = f.create_group('a_group')
group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')
f.close()
or, my favourite because the file is closed automatically:
或者,我最喜欢的,因为文件会自动关闭:
import numpy as np
import h5py
with h5py.File('myfile.hdf5','w') as f:
group = f.create_group('a_group')
group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')
回答by DerOzean
I was working with https://github.com/matterport/Mask_RCNNand produced the same Error:
我正在使用https://github.com/matterport/Mask_RCNN并产生相同的错误:
Traceback (most recent call last):
File "detection.py", line 42, in <module>
model.load_weights(model_path, by_name=True)
File "/home/michael/Bachelor/important/Cable-detection/Mask_RCNN-2.1/samples/cable/mrcnn/model.py", line 2131, in load_weights
f = h5py.File(filepath, mode='r')
File "/home/michael/.local/lib/python3.6/site-packages/h5py/_hl/files.py", line 271, in __init__
fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "/home/michael/.local/lib/python3.6/site-packages/h5py/_hl/files.py", line 101, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip-s_7obrrg-build/h5py/_objects.c:2840)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip-s_7obrrg-build/h5py/_objects.c:2798)
File "h5py/h5f.pyx", line 78, in h5py.h5f.open (/tmp/pip-s_7obrrg-build/h5py/h5f.c:2117)
OSError: Unable to open file (Addr overflow, addr = 800, size=8336, eoa=2144)
HDF5: infinite loop closing library
D,T,F,FD,P,FD,P,FD,P,E,E,SL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL,FL
The solution above worked for me too. I interrupted the training at a random point, therefor the .hdf5file was not closed properly and could not be opened afterward.
上面的解决方案也对我有用。我随机中断了培训,因此.hdf5文件没有正确关闭,之后无法打开。

