【问题标题】:Why can't I open a .h5 file in Python?为什么我不能在 Python 中打开 .h5 文件?
【发布时间】:2021-09-01 13:20:08
【问题描述】:

我正在尝试打开一个 .h5 文件,但遇到操作系统错误。

import sys
sys.path.append('..') 
from unet3d.training import load_old_model
import tables
from train_model import config


model_file=config["model_file"] #config["model_file"] = os.path.abspath("mc_seg_model.h5")
hdf5_file=config["val_data_file"] #config['val_data_file'] = os.path.abspath("../data/val_data.h5")

model = load_old_model(model_file)

load_model函数如下:

import math
from functools import partial
import pdb
from keras import backend as K
from keras.callbacks import ModelCheckpoint, CSVLogger, LearningRateScheduler, ReduceLROnPlateau, EarlyStopping
from keras.models import load_model
import tensorflow_addons as tfa

def load_old_model(model_file):
#     pdb.set_trace()
    print("Loading pre-trained model")
    custom_objects = {'dice_coefficient_loss': dice_coefficient_loss, 'dice_coefficient': dice_coefficient,
                      'weighted_dice_coefficient': weighted_dice_coefficient,
                      'weighted_dice_coefficient_loss': weighted_dice_coefficient_loss}
    try:
        #from keras_contrib.layers import InstanceNormalization
        from tfa.layers import InstanceNormalization
        custom_objects["InstanceNormalization"] = InstanceNormalization
    except ImportError:
        pass
    try:
        return load_model(model_file, custom_objects=custom_objects)
    except ValueError as error:
        if 'InstanceNormalization' in str(error):
            raise ValueError(str(error) + "\n\nPlease install keras-contrib to use InstanceNormalization:\n"
                                          "'pip install git+https://www.github.com/keras-team/keras-contrib.git'")
        else:
            raise error

当我尝试加载模型时,它会引发以下操作系统错误,并且是“输入/输出错误”。

2021-06-16 14:31:38.354199: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
Traceback (most recent call last):
  File "draft.py", line 35, in <module>
    model = load_old_model(model_file)
  File "../unet3d/training.py", line 50, in load_old_model
    return load_model(model_file, custom_objects=custom_objects)
  File "/share/apps/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 182, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/share/apps/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 173, in load_model_from_hdf5
    model_config = f.attrs.get('model_config')
  File "/share/apps/anaconda3/lib/python3.7/_collections_abc.py", line 660, in get
    return self[key]
  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 "/share/apps/anaconda3/lib/python3.7/site-packages/h5py/_hl/attrs.py", line 81, in __getitem__
    attr.read(arr, mtype=htype)
  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/h5a.pyx", line 355, in h5py.h5a.AttrID.read
  File "h5py/_proxy.pyx", line 58, in h5py._proxy.attr_rw
OSError: Unable to read attribute (file read failed: time = Wed Jun 16 14:31:42 2021
, filename = '/data/kfernando/brats20/demo_task3_mcmc/mc_seg_model.h5', file descriptor = 4, errno = 5, error message = 'Input/output error', buf = 0x56126c096440, total read size = 30352, bytes this sub-read = 30352, bytes actually read = 18446744073709551615, offset = 16384)

谁能告诉我是什么导致了这个错误?

【问题讨论】:

  • 可以用hdf5工具从命令行打开吗?喜欢 h5dump file.h5?
  • 文件路径是否正确 - /data/kfer... 你知道程序认为它是从哪个目录运行的吗?打印 os.getcwd()。它是你认为应该在的地方吗?
  • 使用上面的h5dump 等简单测试进行调试,或使用 HDFView 打开文件。如果可行,请在 python 中使用:import h5py 进行测试; h5=h5py.File(model_file,'r') ; h5.close() 如果前 2 次测试中有 1 次失败,则说明您的文件有问题。如果它们有效,则说明您的代码有错误。
  • aalways 将代码、数据和完整的错误消息作为文本(不是屏幕截图,不是链接)放在有问题的地方(不是评论)。
  • @tobias 和@kcw78,当打开文件import h5py ; h5=h5py.File(model_file,'r') ; h5.close() 这工作时,我不知道为什么我的功能不起作用。 @cup 文件路径正确。 @furas 我编辑了问题并将完整的错误消息作为文本包含在内。

标签: python tensorflow operating-system hdf5 h5py


【解决方案1】:

根据您关于成功打开/关闭 h5py 的 cmets,您似乎拥有一个有效的 HDF5 文件。还有 2 个问题需要调查:1) 读取属性数据的问题,或 2) TensorFlow load_model() 函数中的错误。我对 TF 无能为力。然而,这里有一些代码递归地降低数据层次结构并输出所有属性和值。见下文:

def get_all_attrs(name, h5_obj):
    
    if isinstance(h5_obj,h5py.Group):
        print('\n{} is a Group'.format(name))
    elif isinstance(h5_obj,h5py.Dataset):
        print('\n{} is a Dataset'.format(name))
    
    print('number of attributes:',len( h5_obj.attrs.keys() ))
    for k in h5_obj.attrs.keys():
        print('{} => {}'.format(k, h5_obj.attrs[k]))

with h5py.File(file_path, 'r') as h5r:
    print('number of root level attributes:',len( h5r.attrs.keys() ))
    for k in h5r.attrs.keys():
        print('{} => {}'.format(k, h5r.attrs[k]))        
    h5r.visititems(get_all_attrs)

用你的 TF 文件运行它。它可能会在读取其中一个属性时发现错误。我的测试文件的示例输出如下所示:

number of root level attributes: 2
OS => Windows
User => Me

Base_Group is a Group
number of attributes: 2
Date => today
Time => now

Base_Group/default is a Dataset
number of attributes: 2
attr1 => 1.0
attr2 => 22.2

Group1 is a Group
number of attributes: 0

Group1/default1 is a Dataset
number of attributes: 0

这应该有助于确定错误的来源。如果 h5py 可以读取属性,则需要研究 TF load_data() 函数。如果您在读取属性时遇到错误……嗯,那是您的问题,但我不知道如何确定根本原因。

【讨论】:

  • 感谢您的解释。我尝试使用 tf.keras 保存 .h5 文件,但它再次引发相同的操作系统错误:RuntimeError: Problems closing file (file write failed: time = Wed Jun 16 16:43:28 2021 , filename = 'model0.h5', file descriptor = 92, errno = 5, error message = 'Input/output error', buf = 0x55babb2ea540, total write size = 3360, bytes this sub-write = 3360, bytes actually written = 18446744073709551615, offset = 277200) /opt/gridengine/default/spool/GPU12/job_scripts/18456: line 15: 5905 Segmentation fault (core dumped) /share/apps/anaconda3/bin/python3 cv.py &gt; cv.txt
  • 我认为除了 HDF5 问题之外,还有更多需要诊断的内容。您的原始帖子是关于加载(读取)一个 .h5 文件。此注释在 保存(写入).h5 文件时出错。它说:bytes actually written = 18_446_744_073_709_551_615。您是否有足够大的驱动器来容纳这么大的文件?
  • 是的,因为我无法加载 .h5 文件,所以我尝试保存另一个 .h5 文件,这时就出现了上述错误。我正在远程服务器(来自大学)上运行文件,正如你所说,我可能已经超过了配额。我提出了要求增加配额并等待答复的请求。希望它会起作用。
猜你喜欢
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多