AliaksandrSiarohin / gan

Gan implimentation in keras
2 stars 3 forks source link

confused about train_one_step when loading data for discriminator #2

Open 7color94 opened 5 years ago

7color94 commented 5 years ago

In train_one_step, when training discriminator, it loads discrimiantor_batch and generator_batch as follows:

discrimiantor_batch = self.dataset.next_discriminator_sample()
generator_batch = self.dataset.next_generator_sample()

, but the question is that the batch images in discrimiantor_batch and generator_batch are totally different, which means that the discriminator computes the true loss (using discrimiantor_batch) and fake loss (using generator_batch) using different images. It seems uncommon when training the discriminator. Could you please explain it? Thanks.

AliaksandrSiarohin commented 5 years ago

Not sure if I understand your question. First of all you refer to conditional generation based on labels, image2image or unconditional generation based on noise?

7color94 commented 5 years ago

Sorry for my poor descriptions. I am reading your pose-gan project, which is a image2image problem. But in train_one_step, it implement the training phase of unconditional generation based on noise in this paper, right? And one more question is that why use y_pred (instead of y_true) to compute true loss for discriminator ?

AliaksandrSiarohin commented 5 years ago

Initially, I tried to create this gan sub-module to being easily adapted for arbitrary gan (image2image, conditional and staff). But I have to admit that this was not very good decision, and because of this some parts is hard to understand and it is not perfectly optimized. 1) In train_one_step, when training discriminator, it loads discrimiantor_batch and generator_batch as follows:

Indeed you a right the usual way to implement it in image2image: take a batch of pairs (image_from_A, image_from_B) forward images_from_A through generator and obtain generated_from_B. Then forward (image_from_a, image_from_B) and (image_from_a, generated_from_B) through discriminator. In this way you can save some computational time (one generator pass), but on the other hand iid assumption for discriminator breaks.

In my implementation I separated discriminator step from generator step. In details I train discriminator with some data, then sample different data for training generator. This is less efficient, but more theoretically correct. And it was also easier to implement, when I need to do multiple discriminator steps, e.g (training_ratio > 1).

2) But in train_one_step, it implement the training phase of unconditional generation based on noise in this paper, right?

As I said above I tried to implement everything in this function. Both unconditional, conditional and image2image.

3) And one more question is that why use y_pred (instead of y_true) to compute true loss for discriminator ?

This is another consequence of poor initial decisions. Loss in keras should always have the following signature loss(y_true, y_pred). Because of that in order to have the loss that depends only on predictions (like in Discriminator loss for real images, or loss for fake images) I need to pass dummy labels: https://github.com/AliaksandrSiarohin/gan/blob/a4aa9a38792e89480ffbb15dc7e9beba8513e893/train.py#L62.

In later version of this module I fix this staff, and it seems more clear.

Hope this clarify some of your doubts.