SVHN数据集中hdf5组的两种访问方式有什么区别?

What is the difference between the two ways of accessing the hdf5 group in SVHN dataset?

我需要读取 SVHN 数据集并试图读取第一张图像的文件名。

我有点难以理解 HDF5 的结构,尤其是理解 SVHN 数据集的 hierarchy/structure

这两种读取图片名称的方式有什么区别?

我在 getName() 函数的定义中遇到了此脚本中的方法 1:https://github.com/bdiesel/tensorflow-svhn/blob/master/digit_struct.py

我尝试了 hdf5 格式文件,并在尝试显示相同结果的不同事物时想出了方法 2。

# Both these methods read the first character of the name of the 1st
# image in svhn dataset
f = h5py.File(path_to_svhn_dataset,'r')

# method 1 
f[f['digitStruct']['name'][0][0]].value

# method 2
f[f['digitStruct']['name'].value[0].item()].value[0][0]

第一个图像是文件名为“1.png”的文件。上面提到的获取文件名第一个字符的两种方法都会给我们 int 等效于 ascii '1'-> 49

首先,您的 2 种方法的输出略有不同。
方法 1:returns 完整数组(编码文件名)
方法二:只return数组的第一个元素(字符)

让我们解构您的代码以了解您拥有的内容。
第一部分处理 h5py 个数据对象。

f['digitStruct'] -> returns a h5py group object
f['digitStruct']['name'] -> returns 一个 h5py 数据集 对象
f['digitStruct']['name'].name -> returns 数据集对象的名称(路径)

注:
/digitStruct/name 数据集包含 "Object References"。每个数组条目都是指向另一个 h5py 对象(在本例中是另一个数据集)的指针。 例如(用于描述 2 个对象引用的空格):
f[ f['digitStruct']['name'][0][0] ] -> returns 在 [0][0]
处引用的对象 因此,外部 f[ obj_ref ] 就像其他对象引用一样工作。

f['digitStruct']['name'][0][0]的情况下,这是一个指向数据集/#refs#/b的对象 换句话说,f['digitStruct']['name'][0][0] 引用相同的对象: f['#refs#']['b']f['/#refs#/b']

h5py 对象引用到此为止。
让我们继续使用方法1.

从此对象引用中获取数据

f[f['digitStruct']['name'][0][0]].value -> return 将整个 /#refs#/b 数据集作为 NumPy 数组。

但是,dataset.value 已弃用,首选 NumPy 索引,如下所示: f[f['digitStruct']['name'][0][0]][:](获取整个数组)

注意:这两个return都是编码字符的整个数组。 此时,获取名称是 Python 和 NumPy 功能。 使用它来 return 文件名作为字符串:
f[f['digitStruct']['name'][0][0]][:].tostring().decode('ascii')

现在让我们解构您用于方法 2 的对象引用。

f['digitStruct']['name'].value -> return 将整个 /digitStruct/name 数据集作为 NumPy 数组。 它有 13,068 行对象引用

f['digitStruct']['name'].value[0] -> 是第一行

f['digitStruct']['name'].value[0].item() -> 将该数组元素复制到 python 标量

所以所有这些都指向同一个对象:
方法一:f['digitStruct']['name'][0][0]
方法二:f['digitStruct']['name'].value[0].item()
并且与此示例中的 f['#refs#']['b']f['/#refs#/b'] 相同。

与方法 1 一样,获取字符串是 Python 和 NumPy 函数。

f[f['digitStruct']['name'].value[0].item()][:].tostring().decode('ascii')

是的,对象引用很复杂....
我的推荐:
使用 NumPy 索引而不是 .value 从对象中提取 NumPy 数组(如上面的修改方法 1 所示)。

完整性示例代码。用于显示正在发生的事情的中间打印语句。

import h5py

# Both of these methods read the name of the 1st
# image in svhn dataset
f = h5py.File('test_digitStruct.mat','r')
print (f['digitStruct'])
print (f['digitStruct']['name'])
print (f['digitStruct']['name'].name)

# method 1
print('\ntest method 1')
print (f[f['digitStruct']['name'][0][0]])
print (f[f['digitStruct']['name'][0][0]].name)
#  both of these get the entire array / filename:
print (f[f['digitStruct']['name'][0][0]].value)
print (f[f['digitStruct']['name'][0][0]][:]) # same as .value above
print (f[f['digitStruct']['name'][0][0]][:].tostring().decode('ascii'))

# method 2
print('\ntest method 2')
print (f[f['digitStruct']['name'].value[0].item()]) 
print (f[f['digitStruct']['name'].value[0].item()].name) 

# this only gets the first array member / character:
print (f[f['digitStruct']['name'].value[0].item()].value[0][0])
print (f[f['digitStruct']['name'].value[0].item()].value[0][0].tostring().decode('ascii'))
#  this gets the entire array / filename:
print (f[f['digitStruct']['name'].value[0].item()][:])
print (f[f['digitStruct']['name'].value[0].item()][:].tostring().decode('ascii'))

每种方法的最后 2 个打印语句的输出是相同的:

[[ 49]
 [ 46]
 [112]
 [110]
 [103]]
1.png