Lesson 1: Practical Deep Learning for Coders. Probabilities output are incorrect

I am new to Kaggle, Python, AI/ML

I created my first Notebook which downloads front/side/rear views of various brands of SUVs in India. I selected the categories
‘toyota’,‘hyundai’, ‘mahindra’, ‘suzuki’

When I run the prediction the category detection is correct. But the probabilities are incorrect. What is the order of probabilities in output? or What could be wrong?

This is a: hyundai.
tensor(0.9976) tensor(0.0005) tensor(0.0006) tensor(0.0012)
Probability it’s a hyundai: 0.0005
Probability it’s a mahindra: 0.0006
Probability it’s a suzuki: 0.0012
Probability it’s a toyota: 0.9976

The last example may have felt I had the order wrong. But here is a better example output which I am unable to understand

This is a: toyota.
tensor(0.1104) tensor(0.1755) tensor(0.2406) tensor(0.4735)
Probability it’s a hyundai: 0.1755
Probability it’s a mahindra: 0.2406
Probability it’s a suzuki: 0.4735
Probability it’s a toyota: 0.1104

It actually is a Toyota but the probability wise it is a Suzuki.

Any pointers?

I have looked at your notebook and I noticed that when you specify the search, you list them like this:

'toyota','hyundai', 'mahindra', 'suzuki'

But in your output, the order is different. What is happening here (I think) is that this line

learn.predict(PILImage.create('/kaggle/working/which_suv/hyundai/64c8727d-fa34-45c4-8a4f-be54be0f3030.jpg'))  

is giving you the correct prediction, because it picks the label there itself, but you have labeled the probabilities in the wrong order. You can find the order of your labels with

learn.data.classes

See: this function

1 Like

OK, thanks.
I will have a look at learn.data.classes and let you know how it goes.

Thanks @Liannnnn – that helped me.
I had to do a little hunting as I was using fastai v2 – the equivalent to learn.data.classes is learn.dls.vocab – as found in For a loaded learner, where are the list of labels stored? - #5 by sgugger

I had the same problem. In the chapter 1 sample code they get probs[0], while you should get probs[pred_class]. Note that in the sample code pred_class output is ignored using ‘_’

My code now looks as follows.

result,pred_class,probs = learn.predict(PILImage.create(o))
print(f"Probability it's a {result}: {probs[pred_class]:.4f}")
1 Like