Issue
In the above article, they are creating both JSON and HDF5 files,
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
and, later... they are reloading JSON and HDF5 and combining them to create the model:
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
Must I save a JSON model along with HDF5 to re-construct a Keras model?
Can I save the model only in HDF5 format and reload it later?
Solution
The article you linked is pretty old and does not use the best practices.
You can save the whole model (including architecture, weights, and even optimizer state) into a single HDF5 file using model.save('something.h5')
, and then load it using
model = keras.models.load_model('something.h5')
This has been standard for many years in Keras. There is no need to use JSON files to store the architecture.
Answered By - Dr. Snoopy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.