1-GAN Mnist.ipynb - Colab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

11/16/24, 8:33 PM 27 GAN Mnist.

ipynb - Colab

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Reshape, Conv2DTranspose, Conv2D, LeakyReLU, Flatten
from tensorflow.keras.optimizers import Adam

# Fetch the Fashion MNIST dataset from scikit-learn


fashion_mnist = fetch_openml("Fashion-MNIST", version=1)
x_data = fashion_mnist.data.values # Pixel values
y_data = fashion_mnist.target # Labels

# Reshape data to (28, 28) and normalize


x_data = x_data.reshape(-1, 28, 28).astype('float32')
x_train = (x_data - 127.5) / 127.5 # Normalize to [-1, 1]
x_train = np.expand_dims(x_train, axis=-1) # Reshape to (28, 28, 1)

# Verify dataset shape


print(f"x_train shape: {x_train.shape}")
print(f"Sample pixel range: {x_train.min()} to {x_train.max()}")

x_train shape: (70000, 28, 28, 1)


Sample pixel range: -1.0 to 1.0

# Visualize some samples from the dataset


fig, axes = plt.subplots(1, 5, figsize=(10, 2))
for i, ax in enumerate(axes):
ax.imshow(x_data[i], cmap='gray')
ax.axis('off')
plt.show()

#1. Data Preprocessing


#Normalization: Scale Fashion MNIST pixel values to the range [-1, 1] for compatibility with the GAN architecture.
x_train = (x_train - 127.5) / 127.5 # Normalize to [-1, 1]
x_train = np.expand_dims(x_train, axis=-1) # Reshape to (28, 28, 1)

2. Model Architecture Generator Network: The generator takes a random noise vector and generates an image similar to Fashion MNIST.

Input Layer: Latent vector (random noise) of size 100. Dense Layer: Fully connected layer to project the latent vector to a larger dimension (e.g.,
7x7x128). Reshape Layer: Reshape the output to a 7x7x128 array. Transposed Convolutional Layers: Layer 1: 128 filters, kernel size 3x3, strides
of 2, ReLU activation, upsample to (14, 14, 128). Layer 2: 64 filters, kernel size 3x3, strides of 2, ReLU activation, upsample to (28, 28, 64). Output
Layer: Transposed convolution to produce a (28, 28, 1) image, with tanh activation for values in range [-1, 1]. python

generator = Sequential([
Dense(7*7*128, input_dim=100),
Reshape((7, 7, 128)),
Conv2DTranspose(128, kernel_size=3, strides=2, padding='same', activation='relu'),
Conv2DTranspose(64, kernel_size=3, strides=2, padding='same', activation='relu'),
Conv2DTranspose(1, kernel_size=3, padding='same', activation='tanh')
])

/usr/local/lib/python3.10/dist-packages/keras/src/layers/core/dense.py:87: UserWarning: Do not pass an `input_shape`/`input_dim` argumen


super().__init__(activity_regularizer=activity_regularizer, **kwargs)

Discriminator Network: The discriminator takes an image (real or generated) and outputs a probability of whether the image is real or fake.

Input Layer: Image of shape (28, 28, 1). Convolutional Layers: Layer 1: 64 filters, kernel size 3x3, Leaky ReLU activation (slope 0.2). Layer 2: 128
filters, kernel size 3x3, Leaky ReLU activation (slope 0.2). Pooling: Optional max pooling or strided convolution for down-sampling. Fully

https://colab.research.google.com/drive/1sdKOFDta2w0c9hxM1aOedLMmtH0gxyqh?authuser=2#scrollTo=vqwBa9F5Bow7&printMode=true 1/4
11/16/24, 8:33 PM 27 GAN Mnist.ipynb - Colab
Connected Layer: Dense layer with a single output node. Output Layer: Sigmoid activation to output a probability score. python

discriminator = Sequential([
Conv2D(64, kernel_size=3, strides=2, padding='same', input_shape=(28, 28, 1)),
LeakyReLU(alpha=0.2),
Conv2D(128, kernel_size=3, strides=2, padding='same'),
LeakyReLU(alpha=0.2),
Flatten(),
Dense(1, activation='sigmoid')
])

/usr/local/lib/python3.10/dist-packages/keras/src/layers/convolutional/base_conv.py:107: UserWarning: Do not pass an `input_shape`/`inpu


super().__init__(activity_regularizer=activity_regularizer, **kwargs)
/usr/local/lib/python3.10/dist-packages/keras/src/layers/activations/leaky_relu.py:41: UserWarning: Argument `alpha` is deprecated. Use
warnings.warn(

3. Training Parameters Optimizers: Use separate Adam optimizers for the generator and discriminator with learning rate 0.0002 and beta_1
of 0.5.

generator_optimizer = Adam(learning_rate=0.0002, beta_1=0.5)


discriminator_optimizer = Adam(learning_rate=0.0002, beta_1=0.5)

Loss Function: Binary Cross-Entropy for both generator and discriminator loss. Epochs: Train for 100 epochs. Batch Size: Set batch size to 64
for stable training. Training Process: For each batch, train the discriminator on real and fake images. Train the generator via the discriminator’s
gradients to improve its ability to generate realistic images.

4. Performance Metrics Discriminator Loss: Track the discriminator loss for both real and fake images. Generator Loss: Track generator loss
during training. Inception Score (optional): Use this advanced metric if available to assess generated image quality.
5. Evaluation Generated Samples: Generate and visualize a grid of images at various training epochs (e.g., every 10 epochs).

# Function to generate sample images from the generator


def generate_images(generator, epoch, examples=25, dim=(5, 5), figsize=(5, 5)):
noise = np.random.normal(0, 1, (examples, 100))
generated_images = generator.predict(noise)
generated_images = (generated_images + 1) / 2.0 # Scale back to [0,1] range for display
fig, axes = plt.subplots(dim[0], dim[1], figsize=figsize)
for i, ax in enumerate(axes.flatten()):
ax.imshow(generated_images[i, :, :, 0], cmap='gray')
ax.axis('off')
plt.tight_layout()
plt.show()

discriminator_losses = []
generator_losses = []

discriminator.compile(loss='binary_crossentropy', optimizer=discriminator_optimizer, metrics=['accuracy'])

batch_size=10
for epoch in range(10):
for batch in range(len(x_train) // batch_size):
# Prepare real and fake data
# Slice x_train and reshape to (batch_size, 28, 28, 1) in one step
# Remove the extra dimension from x_train using squeeze
real_images = x_train[batch * batch_size: (batch + 1) * batch_size].squeeze(axis=-1) # Remove extra dimension

noise = np.random.normal(0, 1, (batch_size, 100))


fake_images = generator.predict(noise)

# Train discriminator
d_loss_real = discriminator.train_on_batch(real_images, np.ones((batch_size, 1)))

d_loss_fake = discriminator.train_on_batch(fake_images, np.zeros((batch_size, 1)))


d_loss = 0.5 * (d_loss_real + d_loss_fake)

# Train generator
noise = np.random.normal(0, 1, (batch_size, 100))

https://colab.research.google.com/drive/1sdKOFDta2w0c9hxM1aOedLMmtH0gxyqh?authuser=2#scrollTo=vqwBa9F5Bow7&printMode=true 2/4
11/16/24, 8:33 PM 27 GAN Mnist.ipynb - Colab
g_loss = gan.train_on_batch(noise, np.ones((batch_size, 1)))

# Append losses
discriminator_losses.append(d_loss)
generator_losses.append(g_loss)

print(f"Epoch {epoch + 1}/{epochs}, Discriminator Loss: {d_loss}, Generator Loss: {g_loss}")

Next steps: Explain error

d_loss_real = discriminator.train_on_batch(real_images, np.ones((batch_size, 1)))

Training Curves: Plot generator and discriminator losses over epochs for performance analysis.

plt.plot(discriminator_losses, label="Discriminator Loss")


plt.plot(generator_losses, label="Generator Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.show()

Next steps: Explain error

6. Result Interpretation Image Quality: Analyze the visual quality and diversity of the generated images over training epochs. Model Behavior:
Discuss notable trends in loss functions, such as mode collapse (where the generator produces similar images repetitively). Address
whether the discriminator is too strong, leading to generator struggling to produce realistic images. Submission Requirements Code:
Submit a well-documented Jupyter Notebook with all code, including comments explaining each step. Report: Describe the model
architecture and reasons for chosen hyperparameters. Analyze the training results, mentioning any observations or issues encountered.
Plots and Visualizations: Ensure all required plots, including generated images, loss curves, and any additional evaluations, are present in
the notebook and report. This assignment will help you understand GAN architecture, train models to generate synthetic data, and
interpret GAN performance metrics and outcomes.

https://colab.research.google.com/drive/1sdKOFDta2w0c9hxM1aOedLMmtH0gxyqh?authuser=2#scrollTo=vqwBa9F5Bow7&printMode=true 3/4
11/16/24, 8:33 PM 27 GAN Mnist.ipynb - Colab

Start coding or generate with AI.

https://colab.research.google.com/drive/1sdKOFDta2w0c9hxM1aOedLMmtH0gxyqh?authuser=2#scrollTo=vqwBa9F5Bow7&printMode=true 4/4

You might also like