Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Last active May 24, 2018 13:01
Show Gist options
  • Save bzamecnik/d2a9bda7c99d9ba64d1d295ba7a566dc to your computer and use it in GitHub Desktop.
Save bzamecnik/d2a9bda7c99d9ba64d1d295ba7a566dc to your computer and use it in GitHub Desktop.
Prints HDF5 file tree.
"""
Prints HDF5 file tree.
Useful to inspect Keras weighs file.
Example usage:
python hdf5tree.py weights.h5
"""
from __future__ import print_function
import argparse
import h5py
def hdf5tree(path):
with h5py.File(path) as file:
traverse(file)
def traverse(item, keys=[], level=0):
if isinstance(item, h5py.File) or isinstance(item, h5py.Group):
for key in item.keys():
traverse(item[key], keys + [key], level=level + 1)
else:
print(level * '-', '/'.join(keys), item.shape, item.dtype)
def parse_args():
parser = argparse.ArgumentParser(description='Prints HDF5 file tree.')
parser.add_argument('hdf5_file', metavar='HDF5_FILE')
return parser.parse_args()
def main():
args = parse_args()
hdf5tree(args.hdf5_file)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment