python - 在 python 中访问 SVHN 数据集中的数据
问题描述
我试图从包含 digitStruct.mat 文件的 tar.gz 文件中提取数据。我使用了以下代码片段:
train_dataset = h5py.File('./train/digitStruct.mat')
我想从此对象本身访问 bbox 和名称详细信息。例如:
train_dataset[0]
应该输出如下内容:
{'boxes': [{'height': 219.0,
'label': 1.0,
'left': 246.0,
'top': 77.0,
'width': 81.0},
{'height': 219.0, 'label': 9.0, 'left': 323.0, 'top': 81.0, 'width': 96.0}],
'filename': '1.png'}
我搜索了它并在此链接上找到了一些帮助:
但是上面的链接涉及创建单独的函数 get_box_data(index, hdf5_data) 和 get_name(index, hdf5_data) 来检索相应索引的值。但是,我想直接从变量名 train_dataset[index] 访问它。
解决方案
好的,我想我找到了我在上面的评论中提到的内容。它将 .mat v7.3 格式的 SVHN HDF5 文件转换为更易于使用的文件。文件名输入为dsFileName=
. (我只有 6 个要转换的测试文件,所以没有添加输入机制。)它需要一个名为:的文件yourfilename.mat
并转换为yourfilename.h5
. 第二个文件更容易使用(而且更小更快!)。新的 .h5 文件有一个数据集digitStruct
,每行包含以下记录:
- 名称:字符串(文件名,例如
1.png
) - label : 带数字值的字符串 (0-9)
- left : 左侧图像边界框
- top:图像边界框顶部
- width : 图像边界框宽度
- height:图像边界框高度
注意:这调用了在 github 上共享的代码。URL 和属性包含在下面的源代码中。
import h5py
import numpy as np
import os
import digitStruct
## Note digitStruct.py source found at:
## https://github.com/prijip/Py-Gsvhn-DigitStruct-Reader/blob/master/digitStruct.py
# Main
if __name__ == "__main__":
dsFileName = 'Stanford/extra/digitStruct.mat'
print ('Working on',os.path.split(dsFileName))
print ('Create .h5 called',os.path.splitext(dsFileName)[0]+'.h5')
h5f = h5py.File(os.path.splitext(dsFileName)[0]+'.h5', 'w')
print ('Created',os.path.split(h5f.filename))
# Count number of images in digitStruct.mat file [/name] dataset
mat_f = h5py.File(dsFileName)
num_img = mat_f['/digitStruct/name'].size
mat_f.close()
ds_dtype = np.dtype ( [('name','S16'), ('label','S10'), ('left','f8'),
('top','f8'), ('width','f8'), ('height','f8')] )
ds_recarray = np.recarray ( (10,) , dtype=ds_dtype )
ds_table = h5f.create_dataset('digitStruct', (2*num_img,), dtype=ds_dtype, maxshape=(None,) )
idx_dtype = np.dtype ( [('name','S16'), ('first','i4'), ('length','i4')] )
## idx_recarray = np.recarray ( (1,) , dtype=idx_dtype )
idx_table = h5f.create_dataset('idx_digitStruct', (num_img,), dtype=idx_dtype, maxshape=(None,) )
imgCounter = 0
lblCounter = 0
for dsObj in digitStruct.yieldNextDigitStruct(dsFileName):
if (imgCounter % 1000 == 0) :
print(dsObj.name)
if (idx_table.shape[0] < imgCounter ) : # resize idx_table as needed
idx_table.resize(idx_table.shape[0]+1000, axis=0)
idx_table[imgCounter,'name'] = dsObj.name
idx_table[imgCounter,'first'] = lblCounter
idx_table[imgCounter,'length'] = len(dsObj.bboxList)
raCounter = 0
for bbox in dsObj.bboxList:
ds_recarray[raCounter]['name'] = dsObj.name
ds_recarray[raCounter]['label'] = bbox.label
ds_recarray[raCounter]['left'] = bbox.left
ds_recarray[raCounter]['top'] = bbox.top
ds_recarray[raCounter]['width'] = bbox.width
ds_recarray[raCounter]['height'] = bbox.height
raCounter += 1
lblCounter += 1
if (ds_table.shape[0] < lblCounter ) : # resize ds_table as needed
ds_table.resize(ds_table.shape[0]+1000, axis=0)
ds_table[lblCounter-raCounter:lblCounter] = ds_recarray[0:raCounter]
imgCounter += 1
## if imgCounter >= 2000:
## break
print ('Total images processed:', imgCounter )
print ('Total labels processed:', lblCounter )
ds_table.resize(lblCounter, axis=0)
idx_table.resize(imgCounter, axis=0)
h5f.close()
推荐阅读
- angular - 使用 REST 调用设置 Ag-Grid 许可证密钥
- node-red - 限制数组中的消息
- android - FirebaseRecyclerAdapter 用里面的其他对象填充对象
- python - 无法访问美丽汤中标签中的字符串
- ios - SnapKit 更新约束导致冲突
- ios - iOS 根据设备屏幕尺寸从 Assets 获取图像的实际尺寸
- php - 雄辩的 save() 方法在 for 循环中不起作用
- jsf - BootsFaces 不从 ajax 渲染区域
- matplotlib - 如何使用 matplotlib 绘制柱状图?
- vba - VBA中的验证规则来确定文本的长度