This project trains a Deep Convolutional GAN (DCGAN) in PyTorch to generate human face images from the CelebA dataset.
The repository currently contains a single notebook:
Face_Generation.ipynb- end-to-end workflow for downloading CelebA, preprocessing images, defining the generator and discriminator, training the model, and visualizing generated samples.
The notebook walks through a standard DCGAN pipeline:
- Downloads the CelebA dataset.
- Extracts images into
data_faces/. - Resizes images to
64x64. - Normalizes images to
[-1, 1]. - Defines a generator and discriminator using convolutional layers.
- Trains both networks with binary cross-entropy loss and Adam optimizers.
- Saves generated image grids during training.
- Image size:
64 - Batch size:
128 - Latent vector size:
100 - Epochs:
5 - Optimizer:
Adam - Learning rate:
0.0002 - Betas:
(0.5, 0.999)
The generator uses ConvTranspose2d -> BatchNorm2d -> ReLU blocks to progressively upsample a 100-dimensional latent vector into a 3 x 64 x 64 RGB image, followed by Tanh at the output layer.
The discriminator uses Conv2d -> BatchNorm2d -> LeakyReLU blocks to downsample an input image and output a single probability through Sigmoid.
The notebook initializes:
- Convolution weights from
N(0, 0.02) - Batch normalization weights from
N(1, 0.02) - Batch normalization bias to
0
Install the Python packages used in the notebook:
pip install torch torchvision matplotlib numpy pillowYou will also need:
- Python 3
- Jupyter Notebook or Google Colab
- A CUDA-enabled GPU if you want faster training
The notebook downloads CelebA with:
wget https://s3-us-west-1.amazonaws.com/udacity-dlnfd/datasets/celeba.zipIt then extracts the archive so the project structure becomes:
data_faces/
└── img_align_celeba/
├── 000001.jpg
├── 000002.jpg
└── ...
Because torchvision.datasets.ImageFolder is used with root='./data_faces', the img_align_celeba directory acts as the single class folder.
- Open
Face_Generation.ipynbin Jupyter or Colab. - Run the dataset download and extraction cells.
- Run the import and preprocessing cells.
- Run the generator and discriminator definition cells.
- Run the training cell.
- View saved samples and plotted outputs.
The notebook saves generated images to:
sample_dir = '/content/sample_data/images'That path is specific to Google Colab. If you run the notebook locally, change it to something inside this repository, for example:
sample_dir = 'samples'
os.makedirs(sample_dir, exist_ok=True)During training, the notebook:
- Tracks generator loss
- Tracks discriminator loss
- Stores generated image grids in memory
- Saves fake images periodically as PNG files
This notebook is a compact educational implementation of DCGAN for face generation, suitable for learning:
- GAN training dynamics
- Image preprocessing in PyTorch
- Generator and discriminator design
- Sampling synthetic faces from random noise
The dataset download link used in the notebook comes from Udacity's deep learning resources, and the notebook is configured to run comfortably in Google Colab.