Collection of generative models in Pytorch version.

Overview

pytorch-generative-model-collections

Original : [Tensorflow version]

Pytorch implementation of various GANs.

This repository was re-implemented with reference to tensorflow-generative-model-collections by Hwalsuk Lee

I tried to implement this repository as much as possible with tensorflow-generative-model-collections, But some models are a little different.

This repository is included code for CPU mode Pytorch, but i did not test. I tested only in GPU mode Pytorch.

Dataset

  • MNIST
  • Fashion-MNIST
  • CIFAR10
  • SVHN
  • STL10
  • LSUN-bed

I only tested the code on MNIST and Fashion-MNIST.

Generative Adversarial Networks (GANs)

Lists (Table is borrowed from tensorflow-generative-model-collections)

Name Paper Link Value Function
GAN Arxiv
LSGAN Arxiv
WGAN Arxiv
WGAN_GP Arxiv
DRAGAN Arxiv
CGAN Arxiv
infoGAN Arxiv
ACGAN Arxiv
EBGAN Arxiv
BEGAN Arxiv

Variants of GAN structure (Figures are borrowed from tensorflow-generative-model-collections)

Results for mnist

Network architecture of generator and discriminator is the exaclty sames as in infoGAN paper.
For fair comparison of core ideas in all gan variants, all implementations for network architecture are kept same except EBGAN and BEGAN. Small modification is made for EBGAN/BEGAN, since those adopt auto-encoder strucutre for discriminator. But I tried to keep the capacity of discirminator.

The following results can be reproduced with command:

python main.py --dataset mnist --gan_type <TYPE> --epoch 50 --batch_size 64

Fixed generation

All results are generated from the fixed noise vector.

Name Epoch 1 Epoch 25 Epoch 50 GIF
GAN
LSGAN
WGAN
WGAN_GP
DRAGAN
EBGAN
BEGAN

Conditional generation

Each row has the same noise vector and each column has the same label condition.

Name Epoch 1 Epoch 25 Epoch 50 GIF
CGAN
ACGAN
infoGAN

InfoGAN : Manipulating two continous codes

All results have the same noise vector and label condition, but have different continous vector.

Name Epoch 1 Epoch 25 Epoch 50 GIF
infoGAN

Loss plot

Name Loss
GAN
LSGAN
WGAN
WGAN_GP
DRAGAN
EBGAN
BEGAN
CGAN
ACGAN
infoGAN

Results for fashion-mnist

Comments on network architecture in mnist are also applied to here.
Fashion-mnist is a recently proposed dataset consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes. (T-shirt/top, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, Ankle boot)

The following results can be reproduced with command:

python main.py --dataset fashion-mnist --gan_type <TYPE> --epoch 50 --batch_size 64

Fixed generation

All results are generated from the fixed noise vector.

Name Epoch 1 Epoch 25 Epoch 50 GIF
GAN
LSGAN
WGAN
WGAN_GP
DRAGAN
EBGAN
BEGAN

Conditional generation

Each row has the same noise vector and each column has the same label condition.

Name Epoch 1 Epoch 25 Epoch 50 GIF
CGAN
ACGAN
infoGAN

InfoGAN : Manipulating two continous codes

All results have the same noise vector and label condition, but have different continous vector.

Name Epoch 1 Epoch 25 Epoch 50 GIF
infoGAN

Loss plot

Name Loss
GAN
LSGAN
WGAN
WGAN_GP
DRAGAN
EBGAN
BEGAN
CGAN
ACGAN
infoGAN

Folder structure

The following shows basic folder structure.

├── main.py # gateway
├── data
│   ├── mnist # mnist data (not included in this repo)
│   ├── ...
│   ├── ...
│   └── fashion-mnist # fashion-mnist data (not included in this repo)
│
├── GAN.py # vainilla GAN
├── utils.py # utils
├── dataloader.py # dataloader
├── models # model files to be saved here
└── results # generation results to be saved here

Development Environment

  • Ubuntu 16.04 LTS
  • NVIDIA GTX 1080 ti
  • cuda 9.0
  • Python 3.5.2
  • pytorch 0.4.0
  • torchvision 0.2.1
  • numpy 1.14.3
  • matplotlib 2.2.2
  • imageio 2.3.0
  • scipy 1.1.0

Acknowledgements

This implementation has been based on tensorflow-generative-model-collections and tested with Pytorch 0.4.0 on Ubuntu 16.04 using GPU.

Issues
  • Doubt in ACGAN

    Doubt in ACGAN

    Hi

    Thanks for your wonderful code. I read the ACGAN paper and your implementation. I have a query, in the schematic diagram you have shown that c is used along-with x in the Discriminator D. However, in the code, you have not used the class information c while training the D and G(z) as you have done in CGAN ?

    [1] Is it wrong to use the class information in the discriminator process ? [2] If it is not required then shouldn't the variable y_fill_ removed ? It seems to be redundant in this case.

    Also for the C_fake_loss can we use some random labels or is it required to use those labels that have been sampled from the data_loader (like you have done) ?

    Thanks in advance !

    opened by devraj89 3
  • Add missing comma in argparse option

    Add missing comma in argparse option

    Without the comma, 'WGAN_GP' and 'DRAGAN' options are considered invalid as the parser expects 'WGAN_GPDRAGAN'.

    opened by SsnL 2
  • Can only load mnist dataset

    Can only load mnist dataset

    Thanks for this cool codebase that allows one to use many GANs!

    But it seems that only the mnist dataset can be loaded, because the other functions are missing from the utils module. For example, if I issue this command:

    python main.py --dataset celebA --gan_type WGAN --epoch 25 --batch_size 64
    

    This results in the traceback:

    Traceback (most recent call last):                                                                          │·····
     File "main.py", line 107, in <module>                                                                     │·····
        main()                                                                                                  │·····
      File "main.py", line 86, in main                                                                          │·····
        gan = WGAN(args)                                                                                        │·····
      File "WGAN.py", line 130, in __init__             
    │self.data_loader = utils.load_celebA('data/celebA', transform=transforms.Compose(                       
    │AttributeError: module 'utils' has no attribute 'load_celebA'  
    
    opened by connellybarnes 1
  • Why do we need to call net.train() in ACGAN?

    Why do we need to call net.train() in ACGAN?

    In many places like here and here, we have D.train() and G.train(). Is there any particular reason that we need to explicitly set them as True (over and over again) ? The official Pytorch GAN example doesn't seem to do net.train() at all. Thanks!

    opened by haoyangz 1
  • RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

    RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

    i run the original code,but i get this problem.and i don't know why can you help me thx (👍

    opened by killuaZold 1
  • gradient penalty in WGAN_GP

    gradient penalty in WGAN_GP

    Thanks for your great work! I have a question about the gradient penalty part in WGAN_GP model. From line 189 to line 194, I know that these codes compute the gradients of the pred_hat with respect to x_hat, but I don't know why you add [0] in the end. Would you please do some explanations to that? Thank you very much!

    opened by CuthbertCai 0
  • [bug report] wrong implementation of gradient clip in WGAN?

    [bug report] wrong implementation of gradient clip in WGAN?

    Dear author,

    Thanks for your friendly implementation of popular GANs. I am reading your implementation of Wasserstein GANs, and notice that the gradient clip operation is placed after optimizer.step as seen in this line. Typically, in pytorch, the gradient operation is placed before the optimizer step.

    pls have a check.

    opened by lihuiknight 0
  • can you add hinge loss

    can you add hinge loss

    can you add hinge loss

    opened by shaonianruntu 0
  • How to trian with multiple gpu?

    How to trian with multiple gpu?

    Hello @znxlwm,

    I have noticed that the GAN models are all defined with model.cuda() that use only one device. Is there any way to train them with multiple gpus so that the input size can be increased without decreasing teh batch size? Thank you.

    opened by nuanv 0
  • Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif,.tiff,.webp

    Supported extensions are: .jpg,.jpeg,.png,.ppm,.bmp,.pgm,.tif,.tiff,.webp

    getting this error when training with my own database?

    opened by manvirvirk 1
  • No mode collapse in ACGAN (pytorch)

    No mode collapse in ACGAN (pytorch)

    Hi!

    I am curious why there is no mode in the ACGAN-PyTorch version while it exists in the TensorFlow version given in the link.

    In fact, I am more curious because I ran ACGAN-TensorFlow+Keras (link) and found mode collapse. Now, running Pytorch version (link), and so far, there is no sign of mode collapse. BTW, I ran implementations from same author.

    So, did you find/analyze the reason why it works perfectly in Pytorch, but not in TensorFlow?

    Thanks in advance

    opened by farhodfm 0
  • Size issue from running custom dataset

    Size issue from running custom dataset

    Hello, I followed the instructions for uploading a custom dataset. However, I get this error:

    $ python main.py --dataset ships --gan_type GAN --epoch 50 --batch_size 64 ---------- Networks architecture ------------- generator( (fc): Sequential( (0): Linear(in_features=62, out_features=1024, bias=True) (1): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (2): ReLU() (3): Linear(in_features=1024, out_features=6272, bias=True) (4): BatchNorm1d(6272, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (5): ReLU() ) (deconv): Sequential( (0): ConvTranspose2d(128, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1)) (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (2): ReLU() (3): ConvTranspose2d(64, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1)) (4): Tanh() ) ) Total number of parameters: 6642243 discriminator( (conv): Sequential( (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1)) (1): LeakyReLU(negative_slope=0.2) (2): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1)) (3): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (4): LeakyReLU(negative_slope=0.2) ) (fc): Sequential( (0): Linear(in_features=6272, out_features=1024, bias=True) (1): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) (2): LeakyReLU(negative_slope=0.2) (3): Linear(in_features=1024, out_features=1, bias=True) (4): Sigmoid() ) ) Total number of parameters: 6561217

    training start!! Traceback (most recent call last): File "main.py", line 111, in main() File "main.py", line 103, in main gan.train() File "C:\Users\evdelph\Desktop\NEEC\test\pytorch-generative-model-collections\GAN.py", line 144, in train D_real = self.D(x_) File "C:\Users\evdelph\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\module.py", line 532, in call result = self.forward(*input, **kwargs) File "C:\Users\evdelph\Desktop\NEEC\test\pytorch-generative-model-collections\GAN.py", line 67, in forward x = x.view(-1, 128 * (self.input_size // 4) * (self.input_size // 4)) RuntimeError: shape '[-1, 6272]' is invalid for input of size 3276800

    I am not sure how to fix this error. I am trying to generate images of ships for a school project.

    Any insight would be great. Thanks!

    opened by evdelph 0
  • I managed to fix this issue by placing them in another folder called 1, so my directory is now, data/strawberriesBlack/1/B(1).jpg

    I managed to fix this issue by placing them in another folder called 1, so my directory is now, data/strawberriesBlack/1/B(1).jpg

    I managed to fix this issue by placing them in another folder called 1, so my directory is now, data/strawberriesBlack/1/B(1).jpg

    However, my results are completely blank after 50 epochs on GAN

    image

    My dataset is currently coloured, do you think I should change to grayscale? How big should said dataset actually be?

    Thanks in advance

    Originally posted by @HamzahNizami in https://github.com/znxlwm/pytorch-generative-model-collections/issues/18#issuecomment-476016270

    Have u ever successfully ran this example yet? :)Did you get your strawberries?

    opened by Xshelton 1
  • ACGAN for CIFAR10 Performance

    ACGAN for CIFAR10 Performance

    Hi,

    Thanks for the wonderful code.

    I tried directly using the ACGAN code for training conditional GAN on CIFAR10 but it seems hard to generate high fidelity images. I wonder if you guys feels the same? or is there something special to look at when training CIFAR10?

    Thanks

    opened by jinghuichen 0
  • An error occurs.

    An error occurs.

    When using mnist or fashion-mnist these two datasets, the "transform" of the dataloader.py should be "transforms.Compose([transforms.Resize((input_size, input_size)), transforms.ToTensor(), transforms.Normalize(mean=([0.5]), std=([0.5]))])" ,instead of "transforms.Compose([transforms.Resize((input_size, input_size)), transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))])" .

    opened by liaokaipeng 1
Owner
Hyeonwoo Kang
Hyeonwoo Kang
A resource for learning about ML, DL, PyTorch and TensorFlow. Feedback always appreciated :)

A resource for learning about ML, DL, PyTorch and TensorFlow. Feedback always appreciated :)

Aladdin Persson 2.3k Oct 24, 2021
PyTorch implementations of Generative Adversarial Networks.

This repository has gone stale as I unfortunately do not have the time to maintain it anymore. If you would like to continue the development of it as

Erik Linder-Norén 10.4k Oct 22, 2021
The Incredible PyTorch: a curated list of tutorials, papers, projects, communities and more relating to PyTorch.

This is a curated list of tutorials, projects, libraries, videos, papers, books and anything related to the incredible PyTorch. Feel free to make a pu

Ritchie Ng 8.5k Oct 22, 2021
Official repository for "On Generating Transferable Targeted Perturbations" (ICCV 2021)

On Generating Transferable Targeted Perturbations (ICCV'21) Muzammal Naseer, Salman Khan, Munawar Hayat, Fahad Shahbaz Khan, and Fatih Porikli Paper:

Muzammal Naseer 29 Sep 15, 2021
A collection of resources on GAN Inversion.

This repo is a collection of resources on GAN inversion, as a supplement for our survey

null 357 Oct 16, 2021
Tensors and Dynamic neural networks in Python with strong GPU acceleration

PyTorch is a Python package that provides two high-level features: Tensor computation (like NumPy) with strong GPU acceleration Deep neural networks b

null 51.6k Oct 24, 2021
Tensors and Dynamic neural networks in Python with strong GPU acceleration

PyTorch is a Python package that provides two high-level features: Tensor computation (like NumPy) with strong GPU acceleration Deep neural networks b

null 46.1k Feb 13, 2021
A comprehensive list of published machine learning applications to cosmology

ml-in-cosmology This github attempts to maintain a comprehensive list of published machine learning applications to cosmology, organized by subject ma

George Stein 211 Oct 15, 2021
PyTorch implementation of "Learning to Discover Cross-Domain Relations with Generative Adversarial Networks"

DiscoGAN in PyTorch PyTorch implementation of Learning to Discover Cross-Domain Relations with Generative Adversarial Networks. * All samples in READM

Taehoon Kim 985 Oct 23, 2021
StudioGAN is a Pytorch library providing implementations of representative Generative Adversarial Networks (GANs) for conditional/unconditional image generation.

StudioGAN is a Pytorch library providing implementations of representative Generative Adversarial Networks (GANs) for conditional/unconditional image generation.

null 1.9k Oct 16, 2021
Image-to-Image Translation in PyTorch

CycleGAN and pix2pix in PyTorch New: Please check out contrastive-unpaired-translation (CUT), our new unpaired image-to-image translation model that e

Jun-Yan Zhu 16k Oct 23, 2021
Geometric Deep Learning Extension Library for PyTorch

Documentation | Paper | Colab Notebooks | External Resources | OGB Examples PyTorch Geometric (PyG) is a geometric deep learning extension library for

Matthias Fey 12.8k Oct 22, 2021
The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate.

The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate. Website • Key Features • How To Use • Docs •

Pytorch Lightning 15.8k Oct 22, 2021
High-level library to help with training and evaluating neural networks in PyTorch flexibly and transparently.

TL;DR Ignite is a high-level library to help with training and evaluating neural networks in PyTorch flexibly and transparently. Click on the image to

null 3.7k Oct 24, 2021
The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate.

The lightweight PyTorch wrapper for high-performance AI research. Scale your models, not the boilerplate. Website • Key Features • How To Use • Docs •

Pytorch Lightning 11.9k Feb 13, 2021
Alias-Free Generative Adversarial Networks (StyleGAN3) Official PyTorch implementation

Alias-Free Generative Adversarial Networks (StyleGAN3) Official PyTorch implementation

NVIDIA Research Projects 2.6k Oct 22, 2021
Official code for Score-Based Generative Modeling through Stochastic Differential Equations

Score-Based Generative Modeling through Stochastic Differential Equations This repo contains the official implementation for the paper Score-Based Gen

Yang Song 387 Oct 15, 2021
🍀 Pytorch implementation of various Attention Mechanisms, MLP, Re-parameter, Convolution, which is helpful to further understand papers.⭐⭐⭐

?? Pytorch implementation of various Attention Mechanisms, MLP, Re-parameter, Convolution, which is helpful to further understand papers.⭐⭐⭐

xmu-xiaoma66 2.1k Oct 19, 2021
Collection of generative models, e.g. GAN, VAE in Pytorch and Tensorflow.

Generative Models Collection of generative models, e.g. GAN, VAE in Pytorch and Tensorflow. Also present here are RBM and Helmholtz Machine. Note: Gen

Agustinus Kristiadi 6.7k Oct 15, 2021