NVlabs / stylegan3

Official PyTorch implementation of StyleGAN3
Other
6.43k stars 1.13k forks source link

Why does Discriminator return negative value? #94

Closed QsROg8320 closed 2 years ago

QsROg8320 commented 2 years ago

Why does Discriminator return negative value? I think that Discriminator should return a value between 0 and 1.

torch.cuda.empty_cache()
script_dir = os.path.abspath('') #<-- absolute dir the script is in
rel_path = 'stylegan3-r-afhqv2-512x512.pkl'
abs_file_path = os.path.join(script_dir, rel_path)
with open(abs_file_path, 'rb') as f:
    D = pickle.load(f)['D']  # torch.nn.Module
with open(abs_file_path, 'rb') as f:
    G = pickle.load(f)['G_ema'] 
z = torch.randn([1, G.z_dim])  # latent codes
c = None                                # class labels (not used in this example)
img = G(z, c)                           # NCHW, float32, dynamic range [-1, +1], no truncation

d_out = D(img,c)
print(d_out)
#tensor([[-2.0559]])
PDillis commented 2 years ago

The Discriminator's output is not a probability/classification of real or fake ever since Progressive Growing of GANs; I recommend you read about Wasserstein GAN as it's the basis of most modern GANs, but it does exactly what you are asking: the output layer is a linear/fully connected layer, which can give a real number. A summary on WGAN loss can be found here.