tensorflow / gan

Tooling for GANs in TensorFlow
Apache License 2.0
927 stars 246 forks source link

L1 loss problem with GAN if some data has ground-truth and some not #39

Open HymEric opened 3 years ago

HymEric commented 3 years ago

It's a great work.

If I want to use L1 loss between the generated image by generator and the ground-truth image, but some images have ground-truth and some others don't. That is to say, in a batch, some have ground-truth and some not. I only will use l1 loss with the images which have ground-truth.

In this problem, is there a way to address it?
Thank you!

surya1701 commented 3 years ago

Hi @HymEric Maybe you could try using an external function such that g(ground-truth) --> generated, before using L1 loss, as mentioned in https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/293#issuecomment-397740979

aaronsarna commented 3 years ago

Generally the way you would do something like this in Tensorflow is to have some dummy value for the ground truth when it's not available (like an all 0's tensor, for example) and keep a binary mask of shape [batch_size] that has value 1 if there is ground truth and 0 otherwise. You can then use this as a weight applied to the per-sample loss. Your final loss would be something like: loss = tf.reduce_mean(weight_mask * tf.losses.mean_absolute_error(ground_truth, generated)