All Projects → gordicaleksa → Pytorch Gans

gordicaleksa / Pytorch Gans

Licence: mit
My implementation of various GAN (generative adversarial networks) architectures like vanilla GAN (Goodfellow et al.), cGAN (Mirza et al.), DCGAN (Radford et al.), etc.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pytorch Gans

Mimicry
[CVPR 2020 Workshop] A PyTorch GAN library that reproduces research results for popular GANs.
Stars: ✭ 458 (+69%)
Mutual labels:  gans, generative-adversarial-networks
Torchgan
Research Framework for easy and efficient training of GANs based on Pytorch
Stars: ✭ 1,156 (+326.57%)
Mutual labels:  gans, generative-adversarial-networks
Gansformer
Generative Adversarial Transformers
Stars: ✭ 421 (+55.35%)
Mutual labels:  gans, generative-adversarial-networks
3d Sdn
[NeurIPS 2018] 3D-Aware Scene Manipulation via Inverse Graphics
Stars: ✭ 256 (-5.54%)
Mutual labels:  gans, generative-adversarial-networks
Generative adversarial networks 101
Keras implementations of Generative Adversarial Networks. GANs, DCGAN, CGAN, CCGAN, WGAN and LSGAN models with MNIST and CIFAR-10 datasets.
Stars: ✭ 138 (-49.08%)
Mutual labels:  gans, generative-adversarial-networks
Ylg
[CVPR 2020] Official Implementation: "Your Local GAN: Designing Two Dimensional Local Attention Mechanisms for Generative Models".
Stars: ✭ 109 (-59.78%)
Mutual labels:  gans, generative-adversarial-networks
Delving Deep Into Gans
Generative Adversarial Networks (GANs) resources sorted by citations
Stars: ✭ 834 (+207.75%)
Mutual labels:  gans, generative-adversarial-networks
stylegan-v
[CVPR 2022] StyleGAN-V: A Continuous Video Generator with the Price, Image Quality and Perks of StyleGAN2
Stars: ✭ 136 (-49.82%)
Mutual labels:  gans, generative-adversarial-networks
Gdwct
Official PyTorch implementation of GDWCT (CVPR 2019, oral)
Stars: ✭ 122 (-54.98%)
Mutual labels:  gans, generative-adversarial-networks
Energy based generative models
PyTorch code accompanying our paper on Maximum Entropy Generators for Energy-Based Models
Stars: ✭ 114 (-57.93%)
Mutual labels:  gans, generative-adversarial-networks
gan-vae-pretrained-pytorch
Pretrained GANs + VAEs + classifiers for MNIST/CIFAR in pytorch.
Stars: ✭ 134 (-50.55%)
Mutual labels:  gans, generative-adversarial-networks
generative deep learning
Generative Deep Learning Sessions led by Anugraha Sinha (Machine Learning Tokyo)
Stars: ✭ 24 (-91.14%)
Mutual labels:  gans, generative-adversarial-networks
Deep-Learning
It contains the coursework and the practice I have done while learning Deep Learning.🚀 👨‍💻💥 🚩🌈
Stars: ✭ 21 (-92.25%)
Mutual labels:  gans
HistoGAN
Reference code for the paper HistoGAN: Controlling Colors of GAN-Generated and Real Images via Color Histograms (CVPR 2021).
Stars: ✭ 158 (-41.7%)
Mutual labels:  gans
Personalised-aesthetic-assessment-using-residual-adapters
Jupyter notebooks used as supporting material for an msc thesis about personalised aesthetic assessment using residual adapters.
Stars: ✭ 19 (-92.99%)
Mutual labels:  deep-learning-tutorial
skip-thought-gan
Generating Text through Adversarial Training(GAN) using Skip-Thought Vectors
Stars: ✭ 44 (-83.76%)
Mutual labels:  gans
EigenGAN-Tensorflow
EigenGAN: Layer-Wise Eigen-Learning for GANs (ICCV 2021)
Stars: ✭ 294 (+8.49%)
Mutual labels:  gans
PaintsTensorFlow
line drawing colorization using TensorFlow
Stars: ✭ 47 (-82.66%)
Mutual labels:  gans
gans-collection.torch
Torch implementation of various types of GAN (e.g. DCGAN, ALI, Context-encoder, DiscoGAN, CycleGAN, EBGAN, LSGAN)
Stars: ✭ 53 (-80.44%)
Mutual labels:  gans
Deep-Object-Removal
Using cGANs to remove objects from a photo
Stars: ✭ 82 (-69.74%)
Mutual labels:  gans

PyTorch GANs 💻 vs 💻 = ❤️

This repo contains PyTorch implementation of various GAN architectures.
It's aimed at making it easy for beginners to start playing and learning about GANs.

All of the repos I found do obscure things like setting bias in some network layer to False without explaining
why certain design decisions were made. This repo makes every design decision transparent.

Table of Contents

What are GANs?

GANs were originally proposed by Ian Goodfellow et al. in a seminal paper called Generative Adversarial Nets.

GANs are a framework where 2 models (usually neural networks), called generator (G) and discriminator (D), play a minimax game against each other. The generator is trying to learn the distribution of real data and is the network which we're usually interested in. During the game the goal of the generator is to trick the discriminator into "thinking" that the data it generates is real. The goal of the discriminator, on the other hand, is to correctly discriminate between the generated (fake) images and real images coming from some dataset (e.g. MNIST).

Setup

  1. git clone https://github.com/gordicaleksa/pytorch-gans
  2. Open Anaconda console and navigate into project directory cd path_to_repo
  3. Run conda env create from project directory (this will create a brand new conda environment).
  4. Run activate pytorch-gans (for running scripts from your console or set the interpreter in your IDE)

That's it! It should work out-of-the-box executing environment.yml file which deals with dependencies.


PyTorch package will pull some version of CUDA with it, but it is highly recommended that you install system-wide CUDA beforehand, mostly because of GPU drivers. I also recommend using Miniconda installer as a way to get conda on your system.

Follow through points 1 and 2 of this setup and use the most up-to-date versions of Miniconda and CUDA/cuDNN.

Implementations

Important note: you don't need to train the GANs to use this project I've checked-in pre-trained models.
You can just use the generate_imagery.py script to play with the models.

Vanilla GAN

Vanilla GAN is my implementation of the original GAN paper (Goodfellow et al.) with certain modifications mostly in the model architecture, like the usage of LeakyReLU and 1D batch normalization (it didn't even exist back then) instead of the maxout activation and dropout.

Examples

GAN was trained on data from MNIST dataset. Here is how the digits from the dataset look like:

You can see how the network is slowly learning to capture the data distribution during training:

After the generator is trained we can use it to generate all 10 digits! Looks like it's coming directly from MNIST, right!?

We can also pick 2 generated numbers that we like, save their latent vectors, and subsequently linearly or spherically
interpolate between them to generate new images and understand how the latent space (z-space) is structured:

We can see how the number 4 is slowly morphing into 9 and then into the number 3.

The idea behind spherical interpolation is super easy - instead of moving over the shortest possible path
(line i.e. linear interpolation) from the first vector (p0) to the second (p1), you take the sphere's arc path:

Usage

Option 1: Jupyter Notebook

Just run jupyter notebook from you Anaconda console and it will open the session in your default browser.
Open Vanilla GAN (PyTorch).py and you're ready to play!

If you created the env before I added jupyter just do pip install jupyter==1.0.0 and you're ready.


Note: if you get DLL load failed while importing win32api: The specified module could not be found
Just do pip uninstall pywin32 and then either pip install pywin32 or conda install pywin32 should fix it!

Option 2: Use your IDE of choice

Training

It's really easy to kick-off new training just run this:
python train_vanilla_gan.py --batch_size <number which won't break your GPU's VRAM>

The code is well commented so you can exactly understand how the training itself works.

The script will:

  • Dump checkpoint *.pth models into models/checkpoints/
  • Dump the final *.pth model into models/binaries/
  • Dump intermediate generated imagery into data/debug_imagery/
  • Download MNIST (~100 MB) the first time you run it and place it into data/MNIST/
  • Dump tensorboard data into runs/, just run tensorboard --logdir=runs from your Anaconda

And that's it you can track the training both visually (dumped imagery) and through G's and D's loss progress.

Tracking loss can be helpful but I mostly relied on visually analyzing intermediate imagery.

Note1: also make sure to check out playground.py file if you're having problems understanding adversarial loss.
Note2: Images are dumped both to the file system data/debug_imagery/ but also to tensorboard.

Generating imagery and interpolating

To generate a single image just run the script with defaults:
python generate_imagery.py

It will display and dump the generated image into data/generated_imagery/ using checked-in generator model.

Make sure to change the --model_name param to your model's name (once you train your own model).


If you want to play with interpolation, just set the --generation_mode to GenerationMode.INTERPOLATION.
And optionally set --slerp to true if you want to use spherical interpolation.

The first time you run it in this mode the script will start generating images,
and ask you to pick 2 images you like by entering 'y' into the console.

Finally it will start displaying interpolated imagery and dump the results to data/interpolated_imagery.

Conditional GAN

Conditional GAN (cGAN) is my implementation of the cGAN paper (Mehdi et al.).
It basically just adds conditioning vectors (one hot encoding of digit labels) to the vanilla GAN above.

Examples

In addition to everything that we could do with the original GAN, here we can exactly control which digit we want to generate! We make it dump 10x10 grid where each column is a single digit and this is how the learning proceeds:

Usage

For training just check out vanilla GAN (just make sure to use train_cgan.py instead).

Generating imagery

Same as for vanilla GAN but you can additionally set cgan_digit to a number between 0 and 9 to generate that exact digit! There is no interpolation support for cGAN, it's the same as for vanilla GAN feel free to use that.

Note: make sure to set --model_name to either CGAN_000000.pth (pre-trained and checked-in) or your own model.

DCGAN

DCGAN is my implementation of the DCGAN paper (Radford et al.).
The main contribution of the paper was that they were the first who made CNNs successfully work in the GAN setup.
Batch normalization was invented in the meanwhile and that's what got CNNs to work basically.

Examples

I trained DCGAN on preprocessed CelebA dataset. Here are some samples from the dataset:

Again, you can see how the network is slowly learning to capture the data distribution during training:

After the generator is trained we can use it to generate new faces! This problem is much harder than generating MNIST digits, so generated faces are not indistinguishable from the real ones.

Some SOTA GAN papers did a much better job at generating faces, currently the best model is StyleGAN2.

Similarly we can explore the structure of the latent space via interpolations:

We can see how the man's face is slowly morphing into woman's face and also the skin tan is changing gradually.

Finally, because the latent space has some nice properties (linear structure) we can do some interesting things.
Subtracting neutral woman's latent vector from smiling woman's latent vector gives us the "smile vector".
Adding that vector to neutral man's latent vector, we hopefully get smiling man's latent vector. And so it is!

You can also create the "sunglasses vector" and use it to add sunglasses to other faces, etc.

Note: I've created an interactive script so you can play with this check out GenerationMode.VECTOR_ARITHMETIC.

Usage

For training just check out vanilla GAN (just make sure to use train_dcgan.py instead).
The only difference is that this script will download pre-processed CelebA dataset instead of MNIST.

Generating imagery

Again just use the generate_imagery.py script.

You have 3 options you can set the generation_mode to:

  • GenerationMode.SINGLE_IMAGE <- generate a single face image
  • GenerationMode.INTERPOLATION <- pick 2 face images you like and script will interpolate between them
  • GenerationMode.VECTOR_ARITHMETIC <- pick 9 images and script will do vector arithmetic

GenerationMode.VECTOR_ARITHMETIC will give you an interactive matplotlib plot to pick 9 images.

Note: make sure to set --model_name to either DCGAN_000000.pth (pre-trained and checked-in) or your own model.

Acknowledgements

I found these repos useful (while developing this one):

Citation

If you find this code useful for your research, please cite the following:

@misc{Gordić2020PyTorchGANs,
  author = {Gordić, Aleksa},
  title = {pytorch-gans},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/gordicaleksa/pytorch-gans}},
}

Connect with me

If you'd love to have some more AI-related content in your life 🤓, consider:

Licence

License: MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].