Skip to content

Instantly share code, notes, and snippets.

@RodrigoCMoraes
Last active December 1, 2018 18:49
Show Gist options
  • Save RodrigoCMoraes/d76890fb8149182e2522014f308660ae to your computer and use it in GitHub Desktop.
Save RodrigoCMoraes/d76890fb8149182e2522014f308660ae to your computer and use it in GitHub Desktop.
Read keras model from architecture and weights file, json and h5 respectively
# https://gist.github.com/RodrigoCMoraes/d76890fb8149182e2522014f308660ae
import keras
def read_model(arch_file, weights_file, optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']):
"""Read custom keras model from files
Args:
arch_file(str): json file with model architecture
weights_file(str): h5 file with weights of model
Returns:
model: model read from file
"""
with open(arch_file) as fl:
model = keras.models.model_from_json(fl.read())
model.load_weights(weights_file, by_name=False)
model.compile(optimizer=optimizer, loss=loss, metrics=metrics)
return model
@DiasIuri
Copy link

DiasIuri commented Jul 3, 2018

Interesting.

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