0

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

1

1 Answer 1

1

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]] 
Sign up to request clarification or add additional context in comments.

2 Comments

it gives tensor values as tensor([[0.00567,0.0433]],device='cuda:0') from this how can I get max prob?
@godzillabeast please see my edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.