簡體   English   中英

用 h5py 創建的 HDF5 文件不能被 h5py 打開

[英]HDF5 file created with h5py can't be opened by h5py

我在 Ubuntu 12.04(32 位版本)下創建了一個 HDF5 文件,顯然沒有任何問題,使用 Anaconda 作為 Python 分發和編寫 ipython 筆記本。 底層數據都是numpy arrays。 例如,

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

但是,如果我嘗試從新的 iython 筆記本打開此文件,則會收到一條錯誤消息:

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)

你能告訴我那個丟失的文件簽名是什么嗎? 創建文件時我錯過了什么嗎?

由於我們在對我的問題的評論中解決了這個問題,我在這里寫出結果以將其標記為已解決。

主要問題是我在創建文件后忘記關閉文件。 會有兩個簡單的選擇,或者:

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

或者,我最喜歡的,因為文件自動關閉:

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

我正在使用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

上面的解決方案也適用於我。 我隨機中斷了訓練,因此.hdf5文件沒有正確關閉,之后無法打開。

  File "S2_network_training.py", line 31, in <module>
    f = h5py.File(dataset_path,'r')
  File "/home/vislab-002/anaconda3/envs/XYZNet/lib/python3.7/site-packages/h5py/_hl/files.py", line 427, in __init__
    swmr=swmr)
  File "/home/vislab-002/anaconda3/envs/XYZNet/lib/python3.7/site-packages/h5py/_hl/files.py", line 190, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  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/h5f.pyx", line 96, in h5py.h5f.open
OSError: Unable to open file (bad object header version number)

我檢查以確保我關閉了文件,但我仍然收到錯誤消息。 請幫忙

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM