Giter VIP home page Giter VIP logo

pytorch-generative-model-collections's Introduction

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.

pytorch-generative-model-collections's People

Contributors

ssnl avatar znxlwm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-generative-model-collections's Issues

question about wgan-gp

 Today I double check the original paper proposing wgan-gp.I found your model architecture is quietly different from original paper's where it is called cifar-10 ResNet architecture.

What's the most obvious is that there is no BN layer and and ReLu activation is used instead of Leaky-Relu activation.
Could you tell me why you use your custom-defined architecture rather than cifar-10 ResNet architecture?
Thanks!

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

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 !

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'  

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.

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 #18 (comment)

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

bug for infoGAN when change data to celebA

infogan line 102 when I try to apply it on celebA with size (channel,height,width) as (3,180,180)
as the code below, the data from dataloader size is (batch,channel,height,width), but when choose the [0] the data size become (3,180,180), so at the

self.D = discriminator(input_dim=data.shape[1]

the input_dim become 180 rather than 3,I remove the [0] to fix it

        data = self.data_loader.__iter__().__next__()[0]
        #  networks init
        self.G = generator(input_dim=self.z_dim, output_dim=data.shape[1], input_size=self.input_size, len_discrete_code=self.len_discrete_code, len_continuous_code=self.len_continuous_code)
        self.D = discriminator(input_dim=data.shape[1], output_dim=1, input_size=self.input_size, len_discrete_code=self.len_discrete_code, len_continuous_code=self.len_continuous_code)

Questions about training ACGAN

Thanks for your contribution!
A problem occurs when I am training ACGAN without tuning any parameters which is shown as follows:
As the training process goes on, the loss of D is declining, but the loss of G is increasing on the contrary...and the gap between two of them is pretty large. I do not know how to solve it...
image

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!

How can I use the celebA dataset?

The option of dataset doesn't include celebA, but there is a function named load_celebA in utils.py.
How can I use the celebA dataset?

InfoGAN can not run because of the pytorch version problem

/home/yangkaixing/miniconda3/envs/GASDA/lib/python3.6/site-packages/torch/nn/functional.py:1806: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
Traceback (most recent call last):
File "main.py", line 112, in
main()
File "main.py", line 104, in main
gan.train()
File "/data/yangkaixing/Generative_Model_Collections/infoGAN.py", line 230, in train
info_loss.backward()
File "/home/yangkaixing/miniconda3/envs/GASDA/lib/python3.6/site-packages/torch/_tensor.py", line 307, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
File "/home/yangkaixing/miniconda3/envs/GASDA/lib/python3.6/site-packages/torch/autograd/init.py", line 156, in backward
allow_unreachable=True, accumulate_grad=True) # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [64, 3, 4, 4]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

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

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))])" .

EBGAN.py give error raise TypeError('iteration over a 0-d tensor') TypeError: iteration over a 0-d tensor

if list(self.margin-D_fake_err.data)[0] > 0: D_loss = D_real_err + (self.margin - D_fake_err) else: D_loss = D_real_err self.train_hist['D_loss'].append(D_loss.data[0])

The error is in the above line no 190 .... Below Code worked for me .. am i Correct ? I removed word list and [0]
if (self.margin-D_fake_err.data) > 0: D_loss = D_real_err + (self.margin - D_fake_err) else: D_loss = D_real_err self.train_hist['D_loss'].append(D_loss.data[0])

critic iteration on DS iteration

Are you iterating on all the DS in each D iteration? I don't see this is the case here.

Could you show your loss outout please ? I'm having very weird output

started at D: -31699812.0 G: -1.2754488e+21
and now fixed at D: 0.25 G: 6.751009
I'm checking gradient and it is zero every where .

Btw I'm not normalizing my DS do you think it is the reason ?

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!

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

When I run your CGAN program ,the following error message has occured:
D:\ProgramData\Anaconda3\python.exe I:/gupaocode/pytorch-generative-model-collections-master/main.py --dataset mnist --gan_type CGAN --epoch 50 --batch_size 64
Traceback (most recent call last):
File "I:/gupaocode/pytorch-generative-model-collections-master/main.py", line 111, in
main()
File "I:/gupaocode/pytorch-generative-model-collections-master/main.py", line 82, in main
gan = CGAN(args)
File "I:\gupaocode\pytorch-generative-model-collections-master\CGAN.py", line 94, in init
data = self.data_loader.iter().next()[0]
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 517, in next
data = self._next_data()
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py", line 557, in _next_data
data = self.dataset_fetcher.fetch(index) # may raise StopIteration
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data_utils\fetch.py", line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "D:\ProgramData\Anaconda3\lib\site-packages\torchvision\datasets\mnist.py", line 106, in getitem
img = self.transform(img)
File "D:\ProgramData\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 60, in call
img = t(img)
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py", line 889, in call_impl
result = self.forward(*input, **kwargs)
File "D:\ProgramData\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 221, in forward
return F.normalize(tensor, self.mean, self.std, self.inplace)
File "D:\ProgramData\Anaconda3\lib\site-packages\torchvision\transforms\functional.py", line 336, in normalize
tensor.sub
(mean).div
(std)
RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

alpha for each example in WGAN_GP

In the WGAN_GP paper ArXiv, alpha is sampled for each example. However, it's sampled element-wise referred to WGAN_GP.

Suppose we have a tensor of the size (64, 3, 15, 15), I thought the correct way to sample is alpha = torch.rand(x_.size()[0]), which returns a (64, ) tensor.

Is there something I miss or I misunderstand the paper?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.