Search code examples
pythontensorflowkerasdeep-learningautoencoder

how to save best weights and best model using keras


Experts, I'm new to the machine learning and using Keras API with TensorFlow back-end to train a machine learning model. I'm using Model-checkpoint to save best weights and best model in .json and .h5 file independently.in so far I tried to write a code as below but i am not getting any model or weights saved. Hope i will get good solution.Thanks in advance.

    filepath1="best_weights.h5"
    filepath2="best_model.json"
    checkpoint = ModelCheckpoint(filepath1, monitor='val_acc', verbose=1, save_best_only=True,  mode='max')
    callbacks_list = [checkpoint]
    history = model.fit_generator(train_generator,steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, callbacks=callbacks_list, validation_steps=nb_validation_samples // batch_size, verbose=1)
    

Solution

  • Solution 1 (at the end of your training):

    You can try using the below snippet, at the end of your training to save the weights and the model architecture separately.

    from tensorflow.keras.models import model_from_json
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
    model.save_weights("model.h5")
    

    Solution 2 (during the training):

    We can observe that the model architecture does not change during the training, only the weights. Therefore, you can use this checkpoint to save only the best weights during the training, and at the beginning/end of the training save only the model_from_json.

    checkpoint = ModelCheckpoint(filepath1, 
                                 monitor='val_acc',
                                 verbose=1,
                                 save_best_only=True, 
                                 save_weights_only=True, 
                                 mode='max')
    
    ....training runs.....
    ......................
    ....training ends.....
    
    from tensorflow.keras.models import model_from_json
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
    

    If there is nothing saved, ensure you have the correct filepath1.