Skip to content

raphaelsenn/GANs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generative Adversarial Networks

PyTorch reimplementation of the following GAN related papers:

Note: This repository implements early GAN architectures for educational purposes. As these methods are older, the results may be of poor quality.

GAN (MNIST) CGAN (MNIST) DCGAN (CA)
gan_fake_mnist cgan_fake_mnist dcgan_fake_celeba
gan_mnist_loss cgan_mnist_loss dcgan_celeba_loss

GAN and CGAN

This code implements the fully connected generator and discriminator for the MNIST dataset as described in Goodfellow et al. (2014), "Generative Adversarial Networks".

gan_objective Figure 1: The original GAN objective, where G denotes the generator and D the discriminator (Goodfellow et al., 2014).

cgan_objective

Figure 2: The CGAN objective, where G denotes the generator and D the discriminator (Mirza and Osindero, 2014).

Note: Original code and hyperparameters from the paper can be found here.

Training

Just run:

python3 train_gan.py

DCGAN

This code implements the deep convolutional generator and discriminator for the CelebA dataset as described by Radford et al. (2016).

dcgan_generator Figure 3: DCGAN generator used for CelebA (Radford et al., 2016).

loss Figure 4: Loss of the discriminator and generator (Lucic et al., 2018).

Large-scale CelebFaces Attributes (CelebA) Dataset

1. Data collection

Download CelebA from one of the two sources:

Ensure the data folder supports the following tree structure and naming convention:

|-- celeba
|   |-- data  // image folder or prepro_data folder
|   |-- landmarks.csv

2. Preprocessing (optional)

The original CelebA dataset consists of human faces with diverse backgrounds. However, working with human faces and diverse backgrounds makes it difficult for the DCGAN generator. To make his life more easy, I used a pretrained YOLOv8 medium (v0.2) face detector to extract tight face crops from the original CelebA images (see in prepro.py).

Ensure you download the pretrained weights of YOLOv8 medium, and specify the path to the data directory in ./src/utils/prepro.py. After that, you are ready to run:

python3 ./src/utils/prepro.py

NOTE: The preprocessing step using the YOLOv2 model is optional.

3. Training

Modify the trainig script train_dcgan.py to point to your data directory. This script will train both the generator and discriminator on the CelebA dataset.

python3 train_dcgan.py

4. Inference: Generated bedrooms using the DCGAN generator

celeba_fake Figure 3: Image generated by DCGAN generator by follwoing the instructions from this repository.

Manifold Interpolation

Using the trained generator you can easily interpolate two noise samples, and project them using the generator as follows:

# Generator output shape is [1, 3, 64, 64]

noise_start = torch.rand((1, 100))*2 - 1  # shape [1, 100]
noise_end = torch.rand((1, 100))*2 - 1    # shape [1, 100]

n_steps = 16
steps = torch.linspace(0, 1, n_steps)     # shape [n_steps,]

zs = []
for i in range(n_steps):
      alpha = steps[i]
      z = (1-alpha)*noise_start + alpha*noise_end
inter_noise = torch.cat(zs, dim=0)        # shape [n_steps, 100]

fake_imgs = generator(inter_noise)        # shape [n_steps, 3, 64, 64]

celeba_manifold_interpolation Figure 5: Interpolation between a series of 9 noise samples. All noise samples were projected using the trained DCGAN generator (from left-to-right).

Experimental setup

  • OS: Fedora Linux 42 (Workstation Edition) x86_64

  • CPU: AMD Ryzen 5 2600X (12) @ 3.60 GHz

  • GPU: NVIDIA GeForce RTX 3060 ti (8GB VRAM)

  • RAM: 32 GB DDR4 3200 MHz

  • CelebA training time: < 3 hours

Citations

@misc{radford2016unsupervisedrepresentationlearningdeep,
      title={Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks}, 
      author={Alec Radford and Luke Metz and Soumith Chintala},
      year={2016},
      eprint={1511.06434},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      url={https://arxiv.org/abs/1511.06434}, 
}
@misc{goodfellow2014generativeadversarialnetworks,
      title={Generative Adversarial Networks}, 
      author={Ian J. Goodfellow and Jean Pouget-Abadie and Mehdi Mirza and Bing Xu and David Warde-Farley and Sherjil Ozair and Aaron Courville and Yoshua Bengio},
      year={2014},
      eprint={1406.2661},
      archivePrefix={arXiv},
      primaryClass={stat.ML},
      url={https://arxiv.org/abs/1406.2661}, 
}
@misc{lucic2018ganscreatedequallargescale,
      title={Are GANs Created Equal? A Large-Scale Study}, 
      author={Mario Lucic and Karol Kurach and Marcin Michalski and Sylvain Gelly and Olivier Bousquet},
      year={2018},
      eprint={1711.10337},
      archivePrefix={arXiv},
      primaryClass={stat.ML},
      url={https://arxiv.org/abs/1711.10337}, 
}
@inproceedings{liu2015faceattributes,
  title = {Deep Learning Face Attributes in the Wild},
  author = {Liu, Ziwei and Luo, Ping and Wang, Xiaogang and Tang, Xiaoou},
  booktitle = {Proceedings of International Conference on Computer Vision (ICCV)},
  month = {December},
  year = {2015} 
}
@article{yu15lsun,
    Author = {Yu, Fisher and Zhang, Yinda and Song, Shuran and Seff, Ari and Xiao, Jianxiong},
    Title = {LSUN: Construction of a Large-scale Image Dataset using Deep Learning with Humans in the Loop},
    Journal = {arXiv preprint arXiv:1506.03365},
    Year = {2015}
}

About

PyTorch reimplementation of the GAN, CGAN and DCGAN paper.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages