I have created an autoencoder using a separate encoder and decoder as described in this link.
Split autoencoder on encoder and decoder keras
I am checkpointing my autoencoder as followed. How do I save the encoder and decoder separately corresponding to the autoencoder? Alternatively, can I extract deep encoder and decoder from my save autoencoder?
JavaScript
x
12
12
1
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose = 1, save_best_only=True, mode='max')
2
callbacks_list = [checkpoint]
3
4
autoencoder.fit(
5
x=x_train,
6
y=x_train,
7
epochs=10,
8
batch_size=128,
9
shuffle=True,
10
validation_data=(x_test, x_test),
11
callbacks=callbacks_list
12
)
Advertisement
Answer
You could try to overwrite the autoencoder’s save function, which the ModelCheckpoint uses, to have it save the encoder and decoder Models separately instead.
JavaScript
1
14
14
1
def custom_save(filepath, *args, **kwargs):
2
""" Overwrite save function to save the two sub-models """
3
global encoder, decoder
4
5
# fix name
6
path, ext = os.path.splitext(filepath)
7
8
# save encoder/decoder separately
9
encoder.save(path + '-encoder.h5', *args, **kwargs)
10
decoder.save(path + '-decoder.h5', *args, **kwargs)
11
12
auto_encoder = Model(auto_input, decoded)
13
setattr(auto_encoder, 'save', custom_save)
14
Make sure to set the save function BEFORE fit.