如何用h5py编辑h5文件?

How to edit h5 files with h5py?

使用h5py覆盖数组的问题没有解决我的问题 我想编辑 VGG16 模型的数组值。

f = h5py.File('C:/Users/yash/.keras/models/vgg16_weights_tf_dim_ordering_tf_kernels_2.h5', mode = 'a')
ab = list(h5py.AttributeManager.keys(f))
print(list(f.attrs.keys()))
print(ab)

上面的代码returns:

['layer_names']


['block1_conv1', 'block1_conv2', 'block1_pool', 'block2_conv1', 'block2_conv2', 'block2_pool', 'block3_conv1', 'block3_conv2', 'block3_conv3', 
'block3_pool', 'block4_conv1', 'block4_conv2', 'block4_conv3', 'block4_pool',
'block5_conv1', 'block5_conv2', 'block5_conv3', 'block5_pool', 'fc1', 'fc2', 

'flatten', 'predictions']

使用此代码后: print(f.attrs['layer_names'])

我得到以下信息:

[b'block1_conv1' b'block1_conv2' b'block1_pool' b'block2_conv1'
 b'block2_conv2' b'block2_pool' b'block3_conv1' b'block3_conv2'
 b'block3_conv3' b'block3_pool' b'block4_conv1' b'block4_conv2'
 b'block4_conv3' b'block4_pool' b'block5_conv1' b'block5_conv2'
 b'block5_conv3' b'block5_pool' b'flatten' b'fc1' b'fc2' b'predictions']

如何更改 f.attrs['layer_names'] 中包含的值?我无法编辑它们主要是因为使用: print(f.attrs['layer_names/block1_conv1']) returns 一个错误。

每个block(n)_conv(n)中都有一个权重和偏置矩阵。

我想更改这些值。

我在 python 3 中执行此操作,但没有文档帮助我编辑这些值。主要是因为我无法在不使用此代码的情况下访问这些内容:

layer = h5py.AttributeManager.get(f, key = str(layerstringlist[i]))
 nplayer = np.asarray(list(layer))

layerstringlist是这样的列表方式:

['block1_conv1/block1_conv1_W_1:0', 'block1_conv1/block1_conv1_b_1:0', .....
'predictions/predictions_W_1:0', 'predictions/predictions_b_1:0']

这个 returns 它是正确的,但是我无法保存修改后的 h5 文件,因为我不知道如何在 python 中引用它 3.

提前致谢!

之前没见过AttributeManager的用法,可能是文档不鼓励使用,http://docs.h5py.org/en/latest/high/attr.html#reference

使用其他 SO 测试遗留下来的文件,我得到:

In [480]: list(h5py.AttributeManager.keys(f))
Out[480]: ['agroup', 'agroup1', 'agroup2', 'arr']
In [481]: list(f.attrs.keys())
Out[481]: []
In [482]: list(f.keys())
Out[482]: ['agroup', 'agroup1', 'agroup2', 'arr']

在这种情况下,我没有为文件分配任何属性,因此 f.attrs.keys() 是空的。您的文件似乎只有一个属性 'layer_names'。它的值是一个名称列表,您用 print(f.attrs['layer_names']).

列出

AttributeManager 列出了组和数据集,而不是 attrs。我得到与 f.keys().

相同的列表

您应该通过以下方式访问这些组或数据集之一:

f['block1_conv1']

如果这是一个组,您需要索引另一层。如果它是数据集,请按照 http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data

中的描述对其进行读写

我认为 f.attrs['layer_names'] 列表对您没有任何用处,因为它具有与 `list(f.keys()).

相同的信息

根据您的评论,f['block1_conv1'] 是一个包含多个数据集的组。这些是索引集合的等效方法:

f['block1_conv1/block1_conv1_W_1:0'] 
f['block1_conv1']['block1_conv1_W_1:0']

在我的测试文件中

In [483]: f['arr']
Out[483]: <HDF5 dataset "arr": shape (3,), type "|V31">

我可以使用 value[:]:

将数据集作为数组加载到内存中
In [485]: f['arr'].value
Out[485]: 
array([(123, 1, 1, 1, 1, 1, 1, 1), (  1, 1, 1, 1, 1, 1, 1, 1),
       (  1, 1, 1, 1, 1, 1, 1, 1)],
      dtype=[('Status', '<u8'), ('Segments', '<u4'), ('Characterized', '<u4'), ('More_Segments', '<u4'), ('ID', '<i4'), ('Releases', '<u2'), ('Type', 'u1'), ('Track', '<i4')])
In [486]: f['arr'][:]
Out[486]: 
array([(123, 1, 1, 1, 1, 1, 1, 1), (  1, 1, 1, 1, 1, 1, 1, 1),
       (  1, 1, 1, 1, 1, 1, 1, 1)],
      dtype=[('Status', '<u8'), ('Segments', '<u4'), ('Characterized', '<u4'), ('More_Segments', '<u4'), ('ID', '<i4'), ('Releases', '<u2'), ('Type', 'u1'), ('Track', '<i4')])

(对不起,这个例子是一个复杂的结构化数组。)

我可以修改这个数据集的值,就像修改相同类型和形状的数组一样

In [487]: f['arr']['Status']
Out[487]: array([123,   1,   1], dtype=uint64)
In [488]: f['arr']['Status'] = [1,2,3]

我无法更换它。 f['arr'] = np.arange(10) 给我一个错误(名字已经存在)。 f['arr'][:] = np.arange(10) 给出不同的错误(关于不兼容的形状)。

我可以创建一个具有不同名称的新数据集

In [492]: f.create_dataset('newarray', np.arange(10))
Out[492]: <HDF5 dataset "newarray": shape (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), type "<f4">
In [493]: list(f.keys())
Out[493]: ['agroup', 'agroup1', 'agroup2', 'arr', 'newarray']

我可以删除数据集:

In [494]: del f['newarray']
In [495]: list(f.keys())
Out[495]: ['agroup', 'agroup1', 'agroup2', 'arr']

并定义一个同名的新的:

In [500]: f.create_dataset('newarray', data=np.ones((3,4)))
Out[500]: <HDF5 dataset "newarray": shape (3, 4), type "<f8">