Git Product home page Git Product logo

aitorzip / pytorch-cyclegan Goto Github PK

View Code? Open in Web Editor NEW
1.1K 1.1K 284.0 547 KB

A clean and readable Pytorch implementation of CycleGAN

Home Page: https://arxiv.org/abs/1703.10593

License: GNU General Public License v3.0

Python 94.61% Shell 5.39%
artificial-intelligence computer-graphics computer-vision cyclegan deep-learning generative-adversarial-network image-generation image-processing pytorch

pytorch-cyclegan's People

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

pytorch-cyclegan's Issues

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

Thanks for providing great codes for cycleGAN.
Firstly, I changed code in models.py
return F.avg_pool2d(x, x.size()[2:]).view(x.size()[0],-1) -> remove -1
because of UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([1, 1])) issue.
After I fix it, I got another issue 'RuntimeError: output with shape [1, 256, 256] doesn't match the broadcast shape [3, 256, 256]'

Could you comment on this issue?

Adversarial Loss not trained against stagnant partner?

I was under the impression that the optimizer and the generator should be training against stagnant counterparts but the step function on the generators are called before the gradients are calculated for the discriminator. Is this standard practice?

Model

Could you please share your model? I don`t have a proper GPU.Thanks!

UserWarning: Argument interpolation should be of type InterpolationMode instead of int. Please, use InterpolationMode enum.

Setting up a new session...
Namespace(batchSize=1, cuda=True, dataroot='C:\Users\MaYu\Desktop\Dachuang\datasets\horse2zebra', decay_epoch=100, epoch=0, input_nc=3, lr=0.0002, n_cpu=6, n_epochs=200, output_nc=3, size=256)
E:\anaconda\anaconda\envs\pytorch\lib\site-packages\torchvision\transforms\transforms.py:257: UserWarning: Argument interpolation should be of type InterpolationMode instead of int. Please, use InterpolationMode enum.
warnings.warn(

help me plz!

Getting some random noise in generated fake images while training after few epochs and not generating quite clear images even after 60 epochs

Hi @aitorzip

I have been training cyclegan on my own dataset of around 500 images of A and 500 images of B.
But after few epochs I see that there’s random noise added to the fake images being generated while training and after 60 epochs now it’s still having some noise in generated images.
But I see that g_loss is minimising well for 60 epochs as of now but also random oscillations in loss_G_GAN
Is there anything that’s going wrong in training ?

7418388E-1B11-410E-A90B-06A34C7632AC

about results

my test images have Artifacts like this, what's the problem and how to sovle it? thx anyway!
1

why discriminator is designed like this?

 loss_D_real = criterion_GAN(pred_real, torch.ones_like(pred_real));i think this should be      loss_D_real = criterion_GAN(pred_real, torch.zeros_like(pred_real))

needs update utils.py by updating PyTorch0.4

Now if I run this utils.py and get this error

➜ python train --dataroot datasets/horse2zebra/
Namespace(batchSize=1, cuda=False, dataroot='datasets/horse2zebra/', decay_epoch=100, epoch=0, input_nc=3, lr=0.0002, n_cpu=8, n_epochs=200, output_nc=3, size=256)
/Users/tk/python_dir/pytorch_dir/Cycle/PyTorch-CycleGAN/utils.py:114: UserWarning: nn.init.normal is now deprecated in favor of nn.init.normal_.
  torch.nn.init.normal(m.weight.data, 0.0, 0.02)
WARNING:root:Setting up a new session...
Epoch 001/200 [0001/1334] -- Traceback (most recent call last):
  File "train", line 176, in <module>
    images={'real_A': real_A, 'real_B': real_B, 'fake_A': fake_A, 'fake_B': fake_B})
  File "/Users/tk/python_dir/pytorch_dir/ramen2zirou/PyTorch-CycleGAN/utils.py", line 39, in log
    self.losses[loss_name] = losses[loss_name].data[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

So utils.py update in line 39 and line 41

self.losses[loss_name] = losses[loss_name].data[0]

to

self.losses[loss_name] = losses[loss_name].item()

Import Error!

Traceback (most recent call last):
File "./train", line 6, in
import torchvision.transforms as transforms
ImportError: No module named 'torchvision'

Can the results same as the official implement?

Hi, friend,

I want to make a knowledge about the results generated by your code and the official implement.
Because recently, I have to reimplement a code which is said that using cycleGAN. The official code is hard to read for me while yours is easy to understand.

If you are free, please give me a hand, thanks~

Regarding Visdom

Hi,
Thank you very much for easy implementation of Cyclgan code.
I am training on the server and couldn't use web browser to visualize the results.
So is there a way that I could stop that part in code as it is continuously giving errorss.

PatchGAN loss calculated incorrectly (perhaps)

Thanks for the great work @aitorzip
I think I've spotted a mistake in the way the GAN loss is calculated.

In the forward of your Discriminator class you average the activations first, and then you calculate the loss based on the resulting value. Actually, I think you should calculate the per neuron loss first then average the result of that. When you do the math one way vs the other you will find the losses are not the same.

I noticed you haven't committed since 2017 but if you see this and agree I'd be happy to make a PR.

why not fix D while updating G?

Hi, thx for your nice work.

I have a question about training. When you train discriminator, you use detach which means avoid updating generator. But when you update generator, you do not fix discriminator. Is this a common strategy? Because I think in the original GAN paper, we fix discriminator too.

pred_fake = netD_B(fake_B.detach())

question for the dataset structur

hi, i have a question for the dataset structure, because i'm new to the GAN, so i want to ask, the images from domain A and B must be one to one matched? another question is the number of images in domain A and domain B must be same?

about identity loss

Hi,
thank you for making this repository and it makes me more easier to learn CycleGAN
But seeing this train code, there is a wandering thing about identity loss

At the paper, there is no identity loss.
So what kind of role does identity loss have in this code?

Thanks

Reason for copying the tensors from the dataloader to input tensors

Hi Aitor. Thanks for the excellent code.

I was trying to make some modifications and use CycleGAN for a project of mine. I am often running into memory issues which I think is due to the copying of tensors from dataloader outputs to the input tensors in train.py

real_A = Variable(input_A.copy_(batch['A']))
real_B = Variable(input_B.copy_(batch['B']))

On watching the GPU usage with smaller batch sizes I notice that the usage increases momentarily and then drops down (probably when doing feed-forward and backprop) which could be the reason behind memory issues with larger batch sizes.

Is there some particular reason behind this copying or can I use the batches from the dataloader directly ?

input and target shapes do not match: input [1 x 1], target [1] at /pytorch/aten/src/THCUNN/generic/MSECriterion.cu:15

real A: torch.Size([1, 3, 256, 256])
real_B: torch.Size([1, 3, 256, 256])
Traceback (most recent call last):
File "train.py", line 118, in
loss_GAN_A2B = criterion_GAN(pred_fake, target_real)
File "/home/z00495072/anaconda3/envs/centernet/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in call
result = self.forward(*input, **kwargs)
File "/home/z00495072/anaconda3/envs/centernet/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 372, in forward
return F.mse_loss(input, target, size_average=self.size_average, reduce=self.reduce)
File "/home/z00495072/anaconda3/envs/centernet/lib/python3.6/site-packages/torch/nn/functional.py", line 1569, in mse_loss
input, target, size_average, reduce)
File "/home/z00495072/anaconda3/envs/centernet/lib/python3.6/site-packages/torch/nn/functional.py", line 1537, in _pointwise_loss
return lambd_optimized(input, target, size_average, reduce)
RuntimeError: input and target shapes do not match: input [1 x 1], target [1] at /pytorch/aten/src/THCUNN/generic/MSECriterion.cu:15

SOLUTION: IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

Base code use old version on pytorch. In new version we must change this code from file utils.py

            if loss_name not in self.losses:
                self.losses[loss_name] = losses[loss_name].data[0]
            else:
                self.losses[loss_name] += losses[loss_name].data[0]

By this code

            if loss_name not in self.losses:
                self.losses[loss_name] = losses[loss_name].data
            else:
                self.losses[loss_name] += losses[loss_name].data

The expanded size of the tensor must match the existing size at non-singleton dimension 0

This error message appears when the total number of training images modulo batch size does not equal zero. For instance, if I had 50 images with a batch-size of 8, it would make 6 batches, but there would be a leftover batch size of 2, which throws off the tensor shape (expected: [8, nc, height, width], observed: [2, nc, height, width]. What's the best way to overcome this issue?

Error in pred_fake shape

/home/shah2/.local/lib/python3.6/site-packages/torch/nn/modules/loss.py:431: UserWarning: Using a target size (torch.Size([1])) that is different to the input size (torch.Size([1, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.mse_loss(input, target, reduction=self.reduction)
torch.Size([1, 1]) torch.Size([1])
Epoch 001/200 [0001/2088] -- Traceback (most recent call last):
File "PyTorch-CycleGAN/train", line 178, in
images={'real_A': real_A, 'real_B': real_B, 'fake_A': fake_A, 'fake_B': fake_B})
File "/workspace/data/uiou_dark_zurich/PyTorch-CycleGAN/utils.py", line 39, in log
self.losses[loss_name] = losses[loss_name].data[0]
IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number

Training did not converge

I got loss as follows

...
2018-10-27 Saturday 02:21:32 : Train Epoch: 1 [213/284 (75%)]   loss_G: 13.778452       loss_G_identity: 4.409110       loss_G_GAN: 0.245768    loss_G_cycle: 9.123573  loss_D: 1.171054
2018-10-27 Saturday 02:21:33 : Train Epoch: 1 [214/284 (75%)]   loss_G: 13.634476       loss_G_identity: 3.904183       loss_G_GAN: 0.687696    loss_G_cycle: 9.042597  loss_D: 0.879381
2018-10-27 Saturday 02:21:33 : Train Epoch: 1 [215/284 (76%)]   loss_G: 22.416130       loss_G_identity: 5.798176       loss_G_GAN: 0.030727    loss_G_cycle: 16.587227 loss_D: 1.798575
2018-10-27 Saturday 02:21:33 : Train Epoch: 1 [216/284 (76%)]   loss_G: 29.573841       loss_G_identity: 6.921859       loss_G_GAN: 0.317474    loss_G_cycle: 22.334511 loss_D: 3.714886
2018-10-27 Saturday 02:21:33 : Train Epoch: 1 [217/284 (76%)]   loss_G: 36.265060       loss_G_identity: 8.680099       loss_G_GAN: 0.797719    loss_G_cycle: 26.787243 loss_D: 3.788987
2018-10-27 Saturday 02:21:33 : Train Epoch: 1 [218/284 (77%)]   loss_G: 35.755966       loss_G_identity: 8.223778       loss_G_GAN: 1.697493    loss_G_cycle: 25.834694 loss_D: 10.536936
2018-10-27 Saturday 02:21:34 : Train Epoch: 1 [219/284 (77%)]   loss_G: 39.954712       loss_G_identity: 8.718156       loss_G_GAN: 3.962042    loss_G_cycle: 27.274513 loss_D: 10.759437
2018-10-27 Saturday 02:21:34 : Train Epoch: 1 [220/284 (77%)]   loss_G: 47.431328       loss_G_identity: 9.417677       loss_G_GAN: 9.492589    loss_G_cycle: 28.521061 loss_D: 16.836094
2018-10-27 Saturday 02:21:34 : Train Epoch: 1 [221/284 (78%)]   loss_G: 68.834702       loss_G_identity: 11.180630      loss_G_GAN: 24.363785   loss_G_cycle: 33.290287 loss_D: 56.229206
2018-10-27 Saturday 02:21:34 : Train Epoch: 1 [222/284 (78%)]   loss_G: 76.460297       loss_G_identity: 8.798868       loss_G_GAN: 39.637287   loss_G_cycle: 28.024141 loss_D: 56.287437
2018-10-27 Saturday 02:21:35 : Train Epoch: 1 [223/284 (79%)]   loss_G: 90.395309       loss_G_identity: 9.412882       loss_G_GAN: 52.389076   loss_G_cycle: 28.593346 loss_D: 93.407372
2018-10-27 Saturday 02:21:35 : Train Epoch: 1 [224/284 (79%)]   loss_G: 124.559311      loss_G_identity: 8.107519       loss_G_GAN: 92.771484   loss_G_cycle: 23.680317 loss_D: 122.070450
2018-10-27 Saturday 02:21:35 : Train Epoch: 1 [225/284 (79%)]   loss_G: 197.886826      loss_G_identity: 10.789768      loss_G_GAN: 154.939728  loss_G_cycle: 32.157345 loss_D: 258.935150
2018-10-27 Saturday 02:21:35 : Train Epoch: 1 [226/284 (80%)]   loss_G: 203.583557      loss_G_identity: 8.484806       loss_G_GAN: 169.029099  loss_G_cycle: 26.069662 loss_D: 365.472229
2018-10-27 Saturday 02:21:36 : Train Epoch: 1 [227/284 (80%)]   loss_G: 277.741608      loss_G_identity: 9.769445       loss_G_GAN: 238.781219  loss_G_cycle: 29.190956 loss_D: 392.631653
2018-10-27 Saturday 02:21:36 : Train Epoch: 1 [228/284 (80%)]   loss_G: 254.521164      loss_G_identity: 11.291607      loss_G_GAN: 215.258072  loss_G_cycle: 27.971478 loss_D: 557.096924
2018-10-27 Saturday 02:21:36 : Train Epoch: 1 [229/284 (81%)]   loss_G: 359.739563      loss_G_identity: 10.633758      loss_G_GAN: 319.721008  loss_G_cycle: 29.384762 loss_D: 647.901001
2018-10-27 Saturday 02:21:36 : Train Epoch: 1 [230/284 (81%)]   loss_G: 419.248474      loss_G_identity: 8.859273       loss_G_GAN: 385.754211  loss_G_cycle: 24.635035 loss_D: 789.995117
2018-10-27 Saturday 02:21:37 : Train Epoch: 1 [231/284 (81%)]   loss_G: 470.745880      loss_G_identity: 10.386059      loss_G_GAN: 431.400543  loss_G_cycle: 28.959274 loss_D: 903.578308
...

Is something wrong? what should I do?

picture size

the picture size must 256 *256 ? can change the size?

question about train

Hello,thanks for your share,when i use your code with the command 'sh ./train --dataroot datasets/horse2zebra/ --cuda',i encounter a problem said './train: 20: ./train: Syntax error: "(" unexpected',could you please help me?

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.