Skip to content

Instantly share code, notes, and snippets.

@michelkana
Created July 18, 2019 21:48
Show Gist options
  • Select an option

  • Save michelkana/d483df2eb77b801b70060d13707dc79f to your computer and use it in GitHub Desktop.

Select an option

Save michelkana/d483df2eb77b801b70060d13707dc79f to your computer and use it in GitHub Desktop.
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
import matplotlib.pyplot as plt
def get_ae_cnn(nb_filter=32, kernel_size=3, max_pooling_size=2 ):
input_img = Input(shape=(28, 28, 1))
x = Conv2D(nb_filter, (kernel_size, kernel_size), activation='relu', padding='same')(input_img)
x = MaxPooling2D((max_pooling_size, max_pooling_size), padding='same')(x)
x = Conv2D(nb_filter, (kernel_size, kernel_size), activation='relu', padding='same')(x)
encoded = MaxPooling2D((max_pooling_size, max_pooling_size), padding='same')(x)
encoder = Model(input_img,encoded)
x = Conv2D(nb_filter, (kernel_size, kernel_size), activation='relu', padding='same')(encoded)
x = UpSampling2D((max_pooling_size, max_pooling_size))(x)
x = Conv2D(nb_filter, (kernel_size, kernel_size), activation='relu', padding='same')(x)
x = UpSampling2D((max_pooling_size, max_pooling_size))(x)
decoded = Conv2D(1, (kernel_size, kernel_size), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
return autoencoder, encoder
def fit_ae_cnn(ae, x_train_noisy, x_train, x_test_noisy, x_test, epochs=5, batch_size=128):
ae.compile(optimizer='adadelta', loss='binary_crossentropy')
early_stopping = EarlyStopping(monitor='val_loss',min_delta=0,patience=5,verbose=1, mode='auto')
filepath = "saved-model-{epoch:02d}-{val_loss:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_weights_only=True, save_best_only=False, mode='max')
history = ae.fit(x_train_noisy, x_train,
epochs=epochs,
batch_size=batch_size,
shuffle=True,
validation_data=(x_test_noisy, x_test),
callbacks=[early_stopping, checkpoint])
return history
def plot_training_loss(history):
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss) + 1)
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
autoencoder_32, encoder_32 = get_ae_cnn(nb_filter=32)
history_32 = fit_ae_cnn(autoencoder_32, x_train_noisy, x_train, x_test_noisy, x_test, epochs=20)
plot_training_loss(history)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment