Home > Mobile >  How to find score probability of classification model result in pytorch?
How to find score probability of classification model result in pytorch?

Time:05-11

I'm new to pytorch using this I've trained a image classification model, when I test the model with the image I only get label , if I want to get probability of prediction of that class how can I get that ?

 test_image = test_image_tensor.view(1,3,300,300)
 model.eval()
 out = model(test_image)
 ps = torch.exp(out)
 topk,topclass = ps.topk(1,dim=1)
 class_name = idx_to_class[topclass.cpu().numpy()[0][0]]

I'm using above code for prediction which gives only class name , if I want label score of prediction how can I get it?

Any help or suggestion on this will be appreciated

CodePudding user response:

The probabilities are the softmax of the predictions:

class_prob = torch.softmax(out, dim=1)
# get most probable class and its probability:
class_prob, topclass = torch.max(class_prob, dim=1)
# get class names
class_name = idx_to_class[topclass.cpu().numpy()[0][0]]
  • Related