Mari
Mari

Reputation: 69

keras dosen't load the model and weights when using checkpoint

I'm using keras to build a deep autoencoder. I used its checkpointer to load the model and the weights but the result is always None which I think it means that the checkpoint dosen't work correctly and is not saving weights. Here is the code how I proceed:

checkpointer = ModelCheckpoint(filepath="weights.best.h5",
                               verbose=0,
                               save_best_only=True)
tensorboard = TensorBoard(log_dir='/tmp/autoencoder',
                          histogram_freq=0,
                          write_graph=True,
                          write_images=True)
input_enc = Input(shape=(input_size,))
hidden_1 = Dense(hidden_size1, activation='relu')(input_enc)
hidden_11 = Dense(hidden_size2, activation='relu')(hidden_1)
code = Dense(code_size, activation='relu')(hidden_11)
hidden_22 = Dense(hidden_size2, activation='relu')(code)
hidden_2 = Dense(hidden_size1, activation='relu')(hidden_22)
output_enc = Dense(input_size, activation='tanh')(hidden_2)
autoencoder_yes = Model(input_enc, output_enc)

autoencoder_yes.compile(optimizer='adam',
                         loss='mean_squared_error', 
                         metrics=['accuracy'])
history_yes = autoencoder_yes.fit(df_noyau_norm_y, df_noyau_norm_y,
                               epochs=200,
                                batch_size=batch_size,
                                shuffle = True,
                                validation_data=(df_test_norm_y, df_test_norm_y),
                                verbose=1, 
                                callbacks=[checkpointer, tensorboard]).history

autoencoder_yes.save_weights("weights.best.h5")
print(autoencoder_yes.load_weights("weights.best.h5"))

Can somebody help me find out a way to resolve the problem? Thanks

Upvotes: 3

Views: 2761

Answers (3)

Vivek Kalyanarangan
Vivek Kalyanarangan

Reputation: 9081

This is expected behavior not an error. The autoencoder_yes.load_weights("weights.best.h5") doesn't actually return anything, so if you try to print the output of this function you will get None as output.

Expected behavior

In the code that you have provided, you have trained the model and saved the weights. So, the autoencoder_yes is a keras.Model object that has the fine-tuned weights.

In the same script if you load the saved weights once again, nothing is supposed to happen, the weights that you saved will get loaded again.

For clarity

Start with another fresh script, build the same model architecture and reload the weights from the h5 file and then do some predictions. In that case it will silently load the pre-trained weights and do the predictions according to that.

Upvotes: 0

Dr. Snoopy
Dr. Snoopy

Reputation: 56387

No, your interpretation of load_weights returning None is not correct. Load weights is a procedure, it does not return anything, and if you assign the return value of a procedure to a variable, it will get the value of None.

So weight saving is probably working fine, its just your interpretation that is wrong.

Upvotes: 5

Ioannis Nasios
Ioannis Nasios

Reputation: 8527

you should use save_weights_only=True. Without this the whole model is saved not just the weights. To be able to load weights you must save weights like this:

checkpointer = ModelCheckpoint(filepath="weights.best.h5",
                           verbose=0, save_weights_only=True,
                           save_best_only=True)

Upvotes: 3

Related Questions