如何用h5py编辑h5文件?

时间:2017-09-08 16:49:49

标签: python arrays numpy 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)

上面的代码返回:

['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'])会返回错误。

每个块(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']

这会正确返回,但我无法保存修改后的h5文件,因为我不知道如何在python 3中引用它。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我之前没有看到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">
相关问题