【数据处理】之读取hdf5文件

hdf5文件

HDF5是一种常见的跨平台数据存储文件,可以存储不同类型的图像和数码数据,并且可以在不同类型的机器上传输,同时还有统一处理这种格式的函数库

HDF5文件一般以.h5和.hdf5作为后缀名,hdf5文件结构中有2个主要对象:Groups和Datasets,

  • Groups:类似于文件夹,每个hdf5文件其实就是根目录group
  • Datasets:类似于Numpy中的数组

hdf5读取

参考

#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Created by WW on Jan. 26, 2020
# All rights reserved.
#

import h5py
import numpy as np

def main():
	#===========================================================================
	# Create a HDF5 file.
	f = h5py.File("h5py_example.hdf5", "w")    # mode = {
    
    'w', 'r', 'a'}

	# Create two groups under root '/'.
	g1 = f.create_group("bar1")
	g2 = f.create_group("bar2")

	# Create a dataset under root '/'.
	d = f.create_dataset("dset", data=np.arange(16).reshape([4, 4]))

	# Add two attributes to dataset 'dset'
	d.attrs["myAttr1"] = [100, 200]
	d.attrs["myAttr2"] = "Hello, world!"

	# Create a group and a dataset under group "bar1".
	c1 = g1.create_group("car1")
	d1 = g1.create_dataset("dset1", data=np.arange(10))

	# Create a group and a dataset under group "bar2".
	c2 = g2.create_group("car2")
	d2 = g2.create_dataset("dset2", data=np.arange(10))

	# Save and exit the file.
	f.close()

	''' h5py_example.hdf5 file structure
	+-- '/'
	|   +--	group "bar1"
	|   |   +-- group "car1"
	|   |   |   +-- None
	|   |   |   
	|   |   +-- dataset "dset1"
	|   |
	|   +-- group "bar2"
	|   |   +-- group "car2"
	|   |   |   +-- None
	|   |   |
	|   |   +-- dataset "dset2"
	|   |   
	|   +-- dataset "dset"
	|   |   +-- attribute "myAttr1"
	|   |   +-- attribute "myAttr2"
	|   |   
	|   
	'''

	#===========================================================================
	# Read HDF5 file.
	f = h5py.File("h5py_example.hdf5", "r")    # mode = {
    
    'w', 'r', 'a'}

	# Print the keys of groups and datasets under '/'.
	print(f.filename, ":")
	print([key for key in f.keys()], "\n")  

	#===================================================
	# Read dataset 'dset' under '/'.
	d = f["dset"]

	# Print the data of 'dset'.
	print(d.name, ":")
	print(d[:])

	# Print the attributes of dataset 'dset'.
	for key in d.attrs.keys():
		print(key, ":", d.attrs[key])

	print()

	#===================================================
	# Read group 'bar1'.
	g = f["bar1"]

	# Print the keys of groups and datasets under group 'bar1'.
	print([key for key in g.keys()])

	# Three methods to print the data of 'dset1'.
	print(f["/bar1/dset1"][:])		# 1. absolute path

	print(f["bar1"]["dset1"][:])	# 2. relative path: file[][]

	print(g['dset1'][:])		# 3. relative path: group[]



	# Delete a database.
	# Notice: the mode should be 'a' when you read a file.
	'''
	del g["dset1"]
	'''

	# Save and exit the file
	f.close()

if __name__ == "__main__":
    main()

读取bottom-up-features-fixed

train36.hdf5中文件有如下键:
['image_adj_matrix', 'image_bb', 'image_features', 'semantic_adj_matrix', 'spatial_features']

键名 含义 维度 数值只显示[0][0]数据
image_adj_matrix 空间相邻矩阵 (82783, 36, 36) [ 3. 5. 4. … 11. 10. 5.]
image_bb 边界框特征 (82783, 36, 4) [ 60.647038 263.72934 581.98047 296.65677 ]
image_features 图像特征 (82783, 36, 2048) [0. 2.90207 0. … 0. 2.7414305 0. ]
semantic_adj_matrix 语义的相邻矩阵 (82783, 36, 36) [0. 0. 0. … 0. 0. 0.]
spatial_features 空间特征 (82783, 36, 6) [0.094761 0.6190829 0.9093445 0.69637734 0.8145835 0.07729443]

猜你喜欢

转载自blog.csdn.net/snow_maple521/article/details/109693522
今日推荐