This repository contains a PyTorch implementation of a Deep Convolutional Generative Adversarial Network (DCGAN) for generating realistic images using the CelebA dataset (Its model is taken from Paper1. The GAN consists of a Generator and a Discriminator, which are trained in an adversarial manner to produce high-quality images.
This project implements a DCGAN using PyTorch. The DCGAN is trained on the CelebA dataset to generate realistic face images. The Generator learns to create images from random noise, while the Discriminator learns to distinguish between real and fake images. The two networks are trained simultaneously in a competitive manner.
To run this code, you need the following dependencies:
- Python 3.x
- PyTorch
- Torchvision
- Matplotlib
- NumPy
- Jupyter Notebook (optional, for visualization)
The model is trained on the CelebA dataset, which contains over 200,000 celebrity images. The dataset is provided as a zip file (img_align_celeba.zip
). The code automatically extracts the dataset and applies the necessary transformations (resizing, cropping, normalization).
Update the path to the dataset in the following line of the code:
with zipfile.ZipFile('/content/drive/MyDrive/AI_OWN/dataset/celeba/img_align_celeba.zip', 'r') as zip_ref:
zip_ref.extractall('./data/celeba')
The code is structured as follows:
-
Data Loading and Preprocessing:
- The dataset is loaded and preprocessed using
torchvision.datasets
andtorchvision.transforms
. - Images are resized to 64x64, center-cropped, and normalized.
- The dataset is loaded and preprocessed using
-
Model Architecture:
- Generator: A convolutional neural network that generates images from random noise.
- Discriminator: A convolutional neural network that classifies images as real or fake.
-
Training Loop:
- The Generator and Discriminator are trained alternately using the Adam optimizer.
- Binary Cross-Entropy Loss (BCELoss) is used as the loss function.
-
Visualization:
- Training images and generated images are visualized using Matplotlib.
- An animation is created to show the progression of generated images during training.
To train the model, simply run the provided Python script or Jupyter Notebook. The training process will display the following:
- Loss values for the Generator and Discriminator.
- A grid of real images from the dataset.
- A grid of generated images at different stages of training.
You can adjust the following hyperparameters:
batch_size
: Batch size for training (default: 128).z_dim
: Dimension of the noise vector (default: 100).num_epochs
: Number of training epochs (default: 10).learning_rate
: Learning rate for the optimizer (default: 0.0002).beta1
: Momentum term for the Adam optimizer (default: 0.5).
After training, the model will generate realistic face images. The following visualizations are provided:
- Training Images: A grid of real images from the CelebA dataset.
- Generated Images: A grid of images generated by the trained Generator.
- Training Progress Animation: An animation showing how the generated images improve over time.