Last active
August 7, 2023 07:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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) |
.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
please check this.