As part of a project I am currently running a CNN for classifying images. Right now I am training with 50 epochs. I have noticed that after around 20-30 epochs I reach the highest accuracy and then the loss starts to increase significantly. I thought about using Early Stopping but my model loss has a lot of zig-zagging so I think it would be difficult to find the right patience
argument to use. I think it would be easier to just save a model for every epoch so I can manually select the one with the highest accuracy. Is this possible in Keras?
$\begingroup$
$\endgroup$
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
Saving many models definitely possible.
Please find below code snippet:
# 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")
# later...
# 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")
For detailed reference:Save&Load Keras model
Note: Please append epoch value with the file name, so that you have all different trained model.s