aitorzip / pytorch-cyclegan Goto Github PK
View Code? Open in Web Editor NEWA clean and readable Pytorch implementation of CycleGAN
Home Page: https://arxiv.org/abs/1703.10593
License: GNU General Public License v3.0
A clean and readable Pytorch implementation of CycleGAN
Home Page: https://arxiv.org/abs/1703.10593
License: GNU General Public License v3.0
w/o ReplayBuffer, is there any data/paper to support ReplayBuffer?
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?
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?
Thank you so much for implementing CycleGAN in pytorch more readable!
I would like to know which part is PatchGAN??
Could you please share your model? I don`t have a proper GPU.Thanks!
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!
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 ?
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))
When I want to train, there is a problem:“Setting up a new session...”
I checked the problem seems to be visdom, but I can't solve it, do you have any good advice?
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()
Traceback (most recent call last):
File "./train", line 6, in
import torchvision.transforms as transforms
ImportError: No module named 'torchvision'
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~
What is the reason for adding 1 and multiplying by 0.5 here when generating our "fake" outputs. Is this to reverse the effects of transforms.Normalize()
?
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.
"This wil likely lead to incorrect results due to broadcasting. Please ensure they have the same size"
Does anybody know how to fix this issue?
In = 64
out = in*2
<-- so its 128
We will loop this twice and reassign "out" to "in" so..
in = out
<-- in = 128
`out=in * 2'< -- out = 256
The output is consistently getting bigger and bigger why is this labeled #DownSampling?
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.
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.
Line 163 in 67da8f9
RuntimeError: The expanded size of the tensor (1224) must match the existing size (370) at non-singleton dimension 2
When I am executing the test.py file, there is a runtime error, how can I solve this problem?
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?
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
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 ?
Pls tell me why derive the discriminator loss function w.r.t. the label rather than the input data.
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
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
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?
/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
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?
the picture size must 256 *256 ? can change the size?
I couldn't help but notice that the discriminator did not have a sigmoid activation. Is there any reason for this? (
Line 92 in 67da8f9
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.