<jupyter_start><jupyter_text>Generating images using VAENow that we have understood how Variational Autoencoders(VAE) works, in this section we will learn how to use VAE to generate images.  Import the required libraries<jupyter_code>import warnings
warnings.filterwarnings('ignore')

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

from tensorflow.keras.layers import Input, Dense, Lambda
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
from tensorflow.keras import metrics
from tensorflow.keras.datasets import mnist

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)<jupyter_output><empty_output><jupyter_text>Prepare the datasetLoad the MNIST dataset:<jupyter_code>(x_train, _), (x_test, _) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>Normalize the dataset:<jupyter_code>x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.<jupyter_output><empty_output><jupyter_text>Reshape the dataset:<jupyter_code>x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))<jupyter_output><empty_output><jupyter_text>Now the shape of data would become:<jupyter_code>print(x_train.shape, x_test.shape)<jupyter_output>((60000, 784), (10000, 784))<jupyter_text>Now, let's define some important parameters:<jupyter_code>batch_size = 100
original_dim = 784
latent_dim = 2
intermediate_dim = 256
epochs = 10
epsilon_std = 1.0<jupyter_output><empty_output><jupyter_text>Defining EncoderDefine the input:<jupyter_code>x = Input(shape=(original_dim,))<jupyter_output><empty_output><jupyter_text>Define the Encoder hidden layer:<jupyter_code>h = Dense(intermediate_dim, activation='relu')(x)<jupyter_output><empty_output><jupyter_text>Compute mean and variance:<jupyter_code>z_mean = Dense(latent_dim)(h)
z_log_var = Dense(latent_dim)(h)<jupyter_output><empty_output><jupyter_text>Define the sampling operationDefine the sampling operation with a reparameterization trick which samples the latent vector form the encoder's distribution:<jupyter_code>def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0., stddev=epsilon_std)
    
    return z_mean + K.exp(z_log_var / 2) * epsilon<jupyter_output><empty_output><jupyter_text>Sample the latent vector z from the mean and variance of the encoder's distribution:<jupyter_code>z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])<jupyter_output><empty_output><jupyter_text>Define the DecoderWe define the decoder with two layers:<jupyter_code>decoder_hidden = Dense(intermediate_dim, activation='relu')
decoder_reconstruct = Dense(original_dim, activation='sigmoid')<jupyter_output><empty_output><jupyter_text>Reconstruct images using the decoder:<jupyter_code>decoded = decoder_hidden(z)
reconstructed = decoder_reconstruct(decoded)<jupyter_output><empty_output><jupyter_text>Build the model<jupyter_code>vae = Model(x, reconstructed)<jupyter_output><empty_output><jupyter_text>Define the lossAs we learned we use two types of loss: Reconstruction loss and KL divergence loss Reconstuction loss is defined as:<jupyter_code>Reconstruction_loss = original_dim * metrics.binary_crossentropy(x, reconstructed)<jupyter_output><empty_output><jupyter_text>KL diveregnce loss is defined as:<jupyter_code>kl_divergence_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)<jupyter_output><empty_output><jupyter_text>Thus our total loss becomes:<jupyter_code>total_loss = K.mean(Reconstruction_loss + kl_divergence_loss)<jupyter_output><empty_output><jupyter_text>Add loss and compile the model<jupyter_code>vae.add_loss(total_loss)
vae.compile(optimizer='rmsprop')<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>vae.fit(x_train,shuffle=True, epochs=epochs, batch_size=batch_size,verbose=2, validation_data=(x_test, None))<jupyter_output>Train on 60000 samples, validate on 10000 samples
Epoch 1/10
 - 7s - loss: 189.6828 - val_loss: 171.2307
Epoch 2/10
 - 6s - loss: 169.0377 - val_loss: 167.7445
Epoch 3/10
 - 6s - loss: 166.3774 - val_loss: 165.3534
Epoch 4/10
 - 6s - loss: 164.3107 - val_loss: 164.0418
Epoch 5/10
 - 7s - loss: 162.7984 - val_loss: 162.3211
Epoch 6/10
 - 7s - loss: 161.6029 - val_loss: 161.3738
Epoch 7/10
 - 7s - loss: 160.5524 - val_loss: 160.5113
Epoch 8/10
 - 7s - loss: 159.5963 - val_loss: 159.8307
Epoch 9/10
 - 7s - loss: 158.7382 - val_loss: 158.5780
Epoch 10/10
 - 7s - loss: 157.9101 - val_loss: 157.8535<jupyter_text>Defining GeneratorNow, we define the generator that samples from the learned distribution and generates an image. Decoder is bascially our generator as it generates the images. Define the two layers of decoder:<jupyter_code>decoder_input = Input(shape=(latent_dim,))
_decoded = decoder_hidden(decoder_input)<jupyter_output><empty_output><jupyter_text>Define the generator:<jupyter_code>_reconstructed = decoder_reconstruct(_decoded)
generator = Model(decoder_input, _reconstructed)<jupyter_output><empty_output><jupyter_text>Plotting generated imagesNow, we plot the image generated by the generator<jupyter_code>n = 7 
digit_size = 28
figure = np.zeros((digit_size * n, digit_size * n))

grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
grid_y = norm.ppf(np.linspace(0.05, 0.95, n))

for i, yi in enumerate(grid_x):
    for j, xi in enumerate(grid_y):
        z_sample = np.array([[xi, yi]])
        x_decoded = generator.predict(z_sample)
        digit = x_decoded[0].reshape(digit_size, digit_size)
        figure[i * digit_size: (i + 1) * digit_size,
               j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(4, 4), dpi=100)
plt.imshow(figure, cmap='Greys_r')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11076923076923077, 'fraction_numerical': 0.04512820512820513, 'mean_word_length': 6.874831763122477, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '424589', 'license': 'MIT', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2125, 'n_words': 439, 'path': 'Chapter10/10.13 Generating images using VAE.ipynb', 'repo_name': 'bharlow058/Packt-Deep-Learning', 'size': '5850'}