GaParmar / img2img-turbo

One-step image-to-image with Stable Diffusion turbo: sketch2image, day2night, and more
MIT License
1.32k stars 142 forks source link

Is there a bug about cyclegan? #56

Open onepeachbiubiubiu opened 2 weeks ago

onepeachbiubiubiu commented 2 weeks ago

I noticed that the training code does not set ‘requires_grad=False’ for the discriminator when training generators, which will conduct gradients to discriminators and force the discriminator to treat the fake image as a 'true image'.

GaParmar commented 2 weeks ago

I am a little confused by this. Could you point to the relevant line in the training code?

Here, the fake image is detached which should stop the flow of gradients: https://github.com/GaParmar/img2img-turbo/blob/main/src/train_cyclegan_turbo.py#L232

-Gaurav

onepeachbiubiubiu commented 2 weeks ago

When training the generator, the discriminator's 'requires_grad' is Ture. So https://github.com/GaParmar/img2img-turbo/blob/main/src/train_cyclegan_turbo.py#L205 will produce gradients for the discriminator. The gradients will update to the discriminator's parameter in https://github.com/GaParmar/img2img-turbo/blob/main/src/train_cyclegan_turbo.py#L238.

yahshibu commented 2 weeks ago

I think that is the case for train_pix2pix_turbo.py.

Gradients for the discriminator will be produced at https://github.com/GaParmar/img2img-turbo/blob/573dfdd3b1cb6caf6a3c69dabe137200eab26f3c/src/train_pix2pix_turbo.py#L200 . And, the gradients won't be deleted until https://github.com/GaParmar/img2img-turbo/blob/573dfdd3b1cb6caf6a3c69dabe137200eab26f3c/src/train_pix2pix_turbo.py#L217 .

I think accelerator.unwrap_model(net_disc).requires_grad_(False) and accelerator.unwrap_model(net_disc).requires_grad_(True) should be called at appropriate places.

Thank you for finding this! @onepeachbiubiubiu

GaParmar commented 5 days ago

Good catch! Feel free to make a PR changing this, I will merge it!