This repository contains an implementation of the DCGAN architecture described in the paper "Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks" by Radford et al. (2015).
DCGANs are a class of Convolutional Neural Networks that learn to generate realistic images through adversarial training. This implementation follows the architectural guidelines specified in the paper, including:
- Using strided convolutions (discriminator) and fractional-strided convolutions (generator) instead of pooling layers
- Batch normalization in both the generator and discriminator
- Removing fully connected hidden layers
- Using ReLU activation in generator (except for output, which uses Tanh)
- Using LeakyReLU activation in the discriminator
- Python 3.6+
- PyTorch 1.7+
- torchvision
- numpy
- matplotlib
- tqdm
- PIL (Pillow)
- argparse
Install all dependencies with:
pip install torch torchvision tqdm matplotlib pillow argparseThis implementation supports training on two datasets:
The CelebA dataset contains over 200,000 celebrity face images with various attributes.
To prepare the CelebA dataset:
- Download the dataset from here
- Extract the images to a directory
- Organize the directory structure as follows:
celeba/ └── img_align_celeba/ ├── 000001.jpg ├── 000002.jpg └── ...
The LSUN bedroom dataset contains around 3 million bedroom images.
To prepare the LSUN dataset:
- Download the bedroom class data using the LSUN download script
- Extract the data to a directory
- Organize the directory structure as follows:
lsun/ └── bedroom_train/ ├── data.mdb └── lock.mdb
Both datasets are preprocessed during training with the following steps:
- Resize images to the desired size (default: 64x64)
- Center crop to ensure square dimensions
- Convert to tensors
- Normalize to the range [-1, 1]
To train the model on the CelebA dataset:
python train.py --dataset celeba --data_dir path/to/celeba --output_dir results_celebaTo train the model on the LSUN bedrooms dataset:
python train.py --dataset lsun --data_dir path/to/lsun --output_dir results_lsunAdditional training options:
--n_epochs: Number of training epochs (default: 200)
--batch_size: Batch size (default: 128)
--latent_dim: Dimension of the latent space (default: 100)
--lr: Learning rate (default: 0.0002)
--beta1: Beta1 parameter for Adam optimizer (default: 0.5)
--img_size: Image size (default: 64)
--sample_interval: Interval between image sampling (default: 500)
--save_interval: Interval between model saving (default: 10)
--seed: Random seed for reproducibility (default: 42)
--num_workers: Number of worker threads for dataloader (default: 4)
--no_cuda: Flag to avoid using CUDA even when available
To generate images using a trained generator:
python generate.py --model_path path/to/generator_model.pth --output_dir generated_imagesGeneration options:
--latent_dim: Dimension of the latent space (default: 100)
--channels: Number of image channels (default: 3)
--img_size: Image size (default: 64)
--batch_size: Number of images to generate in each batch (default: 64)
--n_batches: Number of batches to generate (default: 1)
--seed: Random seed for reproducibility
--no_cuda: Flag to avoid using CUDA even when available
To visualize the progress of the generator during training:
python visualization.py progress --image_dir path/to/sample/images --output_path progress.pngTo generate images by interpolating between points in the latent space:
python visualization.py interpolate --model_path path/to/generator_model.pth --output_path interpolation.png- Regular checkpoints of the generator and discriminator models
- Sample images showing training progress
- Runtime information about losses and training speed
- Convincing bedroom scenes (if trained on LSUN) or facial images (if trained on CelebA)
- The generated images should have the following characteristics:
- For LSUN: Recognizable bedroom layouts with furniture, windows, and other bedroom elements
- For CelebA: Realistic faces with proper features, though they