NVlabs / stylegan3

Official PyTorch implementation of StyleGAN3
Other
6.38k stars 1.12k forks source link

The pre-trained sytleGAN2 discriminator can not discriminate fake and real images #180

Open chobao opened 2 years ago

chobao commented 2 years ago

Hi, I came across the problem when using pre-trained styleGAN2 discriminator. I use discriminator of stylegan2-car-config-f.pklas the supervision for training another image generation network. However, the pre-trained discriminator can not distinguish the real and fake images.

Here are my test codes:

def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('--network', help='Network pickle filename', dest='network_pkl', required=True)
    parser.add_argument('--image_dir', required=True)

    args = parser.parse_args()
    network_pkl=args.network_pkl
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)

    image_paths = sorted(glob.glob(args.image_dir))

    for path in image_paths:
        image = Image.open(path)
        image = image.resize((512,512))
        image = np.transpose(np.asarray(image), (2,0,1))[None]
        labels = np.array([]).reshape(1,0)
        image = tf.convert_to_tensor(image, dtype=tf.float32)
        labels = tf.convert_to_tensor(labels)
        images = misc.adjust_dynamic_range(image, [0, 255], [-1, 1])
        score = _D.get_output_for(images, labels,is_training=False)
        probability = tf.math.softplus(score)
        print("probability:{} ({})".format(probability.eval(),score.eval()))

Here are my test results: real image, probability: 0.00916453, score:-4.6878304 pexels-photo-170811

real image, probability: 0.004, score:-5.490464 05

fake image, probability: 0.006, score:-5.0358 screenshot_7

fake image, probability: 0.006, score:-4.994 screenshot_6

PDillis commented 2 years ago

There are two things to note here: first, during a training of a GAN, you want to reach the saddle point where the Discriminator cannot distinguish between a real and fake image. Secondly, the output of StyleGAN is no longer a probability or anything related to it, but a score ever since WGAN, so the probabilities you obtained (via passing them through a sigmoid function) are incorrect.

In the end, I think you won't get what you want from the Discriminator of a fully trained GAN, perhaps of one at the beginning of training, sure, as then the distinction between a real and a fake image is much more apparent. Hope this helped!

chobao commented 2 years ago

Oh, Thanks @PDillis for replying to my naive question. I am new to styleGAN. As you commented, the output of styleGAN discriminator is just a score that the higher one is real and the lower one is fake. Besides, it seems that the value x to distinguish between real and fake (the score higher than $x$ is real, otherwise is fake) differs in each model and is not obvious to obtain.

Therefore, pre-trained styleGAN discriminator is useless and I have to train my own discriminator to supervise my image generation network.

PDillis commented 2 years ago

No problem. However, I disagree with D being useless: after all, you are trying to use a network trained for one purpose for another completely different one. What you could do to use D is to use its intermediate layers as these most likely contain key features of what makes up a car. This is what I do here, though the code needs a bit of revision.

Omri-CG commented 1 year ago

@PDillis I am not sure that the Discriminator of a fully trained GAN cannot distinguish between a real and fake image. The fact that the Discriminator can not distinguish fake at the end of the training should be that the Generator produces images that are realistic, and therefore the Discriminator cannot tell the difference. However, it will still be useful for "fake" images as shown in this thread