How To Get The Class With Highest Confidence From A Tensor?
I want to obtain the class with the highest confidence. Heres the code that performs classification task: names = ['class A', 'class B', 'class C'] def classify_face(image): d
Solution 1:
It seems you will always be looking at the tensor pred[0]
. So let pred
be:
pred = torch.tensor([[212.38568, 117.47020, 339.35773, 266.00513, 0.74144, 2.00000],
[214.60651, 118.50694, 339.90192, 265.91696, 0.94277, 0.00000]])
The index of the prediction with highest confidence is:
i = torch.argmax(pred[:, 4])
Therefore, you just have to get the last value at that index:
pred[i, -1]
And the class name will be names[int(pred[i, -1])]
.
Post a Comment for "How To Get The Class With Highest Confidence From A Tensor?"