How to find classification accuracy of the discriminator of a GAN model?

Hello. I am training a GAN model for style transfer. I have used 20 different styles for the training. My discriminator is a classifier that tries to classify these 20 styles. I have saved the discriminator as a .pth.tar model and I want to test the classification accuracy of it. Is it possible to do that in pytorch? I can’t find a suitable method. Please help.

You can load your saved weights with, make sure your model weights are in same directory as your code…

model = Model()
PATH = "./model_weights.pth"

Then you can get prediction from trained model:

prediction = model.forward(x)

Thank you for your reply. Can you please clarify what is ‘x’ in model.forward(x)?

It is your input data

firstly, some additions to @Samuel_Bachorik answer: to get the classifiation accuracy, I would apply a softmax to the output layer and then choose the max. value of the retrieved probabilies.
Secondly and most importantly, the approach that you have choosen might not be the most flexible one. If you want a more flexible approach that allows you to control the discriminator and the classification loss, you could seperate the the loss generating part of your architecture into the Discriminator and a separate model that is used to classify the different styles based on the latent space. @shrbrh Are you using styleGAN?


I have used the StarGAN discriminator and I did add a softmax layer to its output layer. The output layers of StarGAN discriminator is coded as:

self.conv1 = nn.Conv2d(curr_dim, 1, kernel_size=3, stride=1, padding=1, bias=False)
self.conv2 = nn.Conv2d(curr_dim, c_dim, kernel_size=kernel_size, bias=False)

I added a softmax layer after self.conv2 where c_dim = 20 are for the 20 styles. I trained the GAN model and saved the models for both the generator and the discriminator. Now I want to load my saved discriminator model and find its classification accuracy for a particular stylized test image. So how do I retrieve the probabilities and max. value from that?

x in a GAN should be just x=torch.rand(output_dims). Let’s say your model gives outputs of print(output.size()) and that gives you torch.size(batch_size, channels, Height, Width), for example, (10, 3, 224, 224). Just make a random tensor of that size for input.

Regarding to your original question, I am not familiar with any metric “accuracy” for a GAN. Usually, just the loss of the descriminator and generator is considered. There are several proposed loss functions:

But you should probably just stick with one type for training and analysis.

1 Like

So, do you want to load a specific plain image, sytle it and then test the classification acc. of specific styles?

Yes. I want to take a content image and style image and use the generator of the GAN model to produce a stylized image. Then I want the discriminator to classify the stylized image and I want to test its classification accuracy.

As shown in Figure 3 of the StarGAN paper it appears to me that you give both to your Generator which produces a fake image. This fake image is then passed to the discriminator which then returns True or False for the Discriminator thinkining the image is “Real” or “Fake” and a domain classification score. Have you already tried to use this score?