Skip to content

Instantly share code, notes, and snippets.

@Attila94
Last active August 7, 2023 07:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Attila94/fb917e03b04035f3737cc8860d9e9f9b to your computer and use it in GitHub Desktop.
Save Attila94/fb917e03b04035f3737cc8860d9e9f9b to your computer and use it in GitHub Desktop.
Function to read saved Keras (tensorflow) weights from hdf5 file. Returns dict of layer names and weights.
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 11 10:31:43 2019
@author: Attila Lengyel
"""
import h5py
def read_hdf5(path):
weights = {}
keys = []
with h5py.File(path, 'r') as f: # open file
f.visit(keys.append) # append all keys to list
for key in keys:
if ':' in key: # contains data if ':' in key
print(f[key].name)
weights[f[key].name] = f[key].value
return weights
if __name__ == '__main__':
filename = 'comp_named.hdf5'
weights = read_hdf5(filename)
@zubi9
Copy link

zubi9 commented Feb 27, 2022

image

please check this.

@vmpyr
Copy link

vmpyr commented Jun 20, 2023

.value is deprecated as far as I know.

in line 12, instead do this:

weights[f[key].name] = f[key][()]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment