This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vae_decoder_input, vae_decoder_output, vae_decoder = build_decoder(input_dim = Z_DIM, | |
| shape_before_flattening = shape_before_flattening, | |
| conv_filters = [64,64,32,3], | |
| conv_kernel_size = [3,3,3,3], | |
| conv_strides = [2,2,2,2] | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ENCODER | |
| def build_vae_encoder(input_dim, output_dim, conv_filters, conv_kernel_size, | |
| conv_strides, use_batch_norm = False, use_dropout = False): | |
| # Clear tensorflow session to reset layer index numbers to 0 for LeakyRelu, | |
| # BatchNormalization and Dropout. | |
| # Otherwise, the names of above mentioned layers in the model | |
| # would be inconsistent | |
| global K | |
| K.clear_session() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def generate_images_from_noise(n_to_show = 10): | |
| reconst_images = decoder.predict(np.random.normal(0,1,size=(n_to_show,Z_DIM))) | |
| fig = plt.figure(figsize=(15, 3)) | |
| fig.subplots_adjust(hspace=0.4, wspace=0.4) | |
| for i in range(n_to_show): | |
| img = reconst_images[i].squeeze() | |
| sub = fig.add_subplot(2, n_to_show, i+1) | |
| sub.axis('off') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| plot_compare(images = example_images, add_noise = True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| example_batch = next(data_flow) | |
| example_batch = example_batch[0] | |
| example_images = example_batch[:10] | |
| def plot_compare(images=None, add_noise=False): | |
| if images is None: | |
| example_batch = next(data_flow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LEARNING_RATE = 0.0005 | |
| N_EPOCHS = 10 | |
| optimizer = Adam(lr = LEARNING_RATE) | |
| def r_loss(y_true, y_pred): | |
| return K.mean(K.square(y_true - y_pred), axis = [1,2,3]) | |
| simple_autoencoder.compile(optimizer=optimizer, loss = r_loss) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The input to the model will be the image fed to the encoder. | |
| simple_autoencoder_input = encoder_input | |
| # The output will be the output of the decoder. The term - decoder(encoder_output) | |
| # combines the model by passing the encoder output to the input of the decoder. | |
| simple_autoencoder_output = decoder(encoder_output) | |
| # Input to the combined model will be the input to the encoder. | |
| # Output of the combined model will be the output of the decoder. | |
| simple_autoencoder = Model(simple_autoencoder_input, simple_autoencoder_output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Decoder | |
| def build_decoder(input_dim, shape_before_flattening, conv_filters, conv_kernel_size, | |
| conv_strides): | |
| # Number of Conv layers | |
| n_layers = len(conv_filters) | |
| # Define model input | |
| decoder_input = Input(shape = (input_dim,) , name = 'decoder_input') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ENCODER | |
| def build_encoder(input_dim, output_dim, conv_filters, conv_kernel_size, | |
| conv_strides): | |
| # Clear tensorflow session to reset layer index numbers to 0 for LeakyRelu, | |
| # BatchNormalization and Dropout. | |
| # Otherwise, the names of above mentioned layers in the model | |
| # would be inconsistent | |
| global K | |
| K.clear_session() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| filenames = np.array(glob(os.path.join(DATA_FOLDER, '*/*.jpg'))) | |
| NUM_IMAGES = len(filenames) | |
| print("Total number of images : " + str(NUM_IMAGES)) | |
| # prints : Total number of images : 202599 | |
| INPUT_DIM = (128,128,3) # Image dimension | |
| BATCH_SIZE = 512 | |
| Z_DIM = 200 # Dimension of the latent vector (z) |