icon

Read HDF5 (H5) File

Last modified: 2023-03-26

HDF5 is a file format of the HDF (Hierarchical Data Format) which is designed to store and organize large amounts of data.

TensorFlow

import tensorflow as tf

model = tf.keras.models.load_model("example.h5")
model.summary()

h5py

h5py is the Python library to read and write HDF5 files.

Installation

pip3 install h5py

Read HDF5 (H5)

Then run the following script.

import h5py

with h5py.File('example.hdf5', 'r') as f:
	# Get all keys
	print("All keys: %s" % f.keys())
	# Get an object
	print("Object: " % f["key_name"])
	print("Object keys: " % f["key_name"].keys())
	print("Sub object: " % f["key_name"]["sub_key_name"])

Write HDF5 (H5)

import h5py
import numpy as np

data_matrix = np.random.uniform(-1, 1, size=(10, 3))

with h5py.File('example.hdf5', 'w') as f:
	f.create_dataset("dataset_name", data=data_matrix)