# Get top 5 Predictions in Image Classifier (Solved)

Hi all
I trained an image classifier using resnet50 on the fruits360 dataset. I’m trying to get the top 5 predictions but sadly

learn.predict(img)

Only returns the top class with the highest confidence.
I’ve delved into the learn.predict function but i couldn’t figure out a way to get it done.

Just to be clear i’m trying to get the top 5 predictions for one image.

If anyone knows how to do this. Any and all help will be greatly appreciated.

I’m on a phone right now so I can’t give you code, but predict returns a tuple of class, label, probabilities (for all classes).

There’s bound to be a very easy way to zip and sort your fruit classes with the probs from predict but right now I’ve barely got enough signal to post.

3 Likes

Thanks so much. That’s what i thought too. But i can’t seem to figure out how to extract the probabilities from the tuple. If you could post code whenever you get access to your pc then that’d be great.

I’ll also try it on my end. And if i do accomplish it then i’ll post the code here too. thanks

Will do.

1 Like

@joedockrill your idea worked. Thanks so Much.

There’s a very convenient method to get the top 5 confidences and their respective indices from the learn.predict output tuple

I used the following code to first get the prediction tuple

``````pred_class, pred_idxs, outputs = learn.predict(img2)
top_5_conf, i = outputs.topk(5)
``````

The key to everything here is the topk function. It comes with pytorch tensors. Since outputs was a tensor the topk function could be used to get the top 5 confidences and their respective indices. The said indices can then be cross referenced with the classes list.For Example:

``````itr = 0
classes = data.classes
for x in i:
print(classes[x.item()], top_5_conf[itr].item())
itr=itr+1
``````

This will print your top 5 confidences and their respective labels. Kudos and Thanks

7 Likes

i was thinking of something like the following, which would be a more generic way to do it if there hadn’t been a topk() function for you to use.

it’s worth knowing these functions anyway if you’re new to python:

``````probs = [0.003, 0.1, 0.874, 0.00046, 0.67, 0.02]
c = ["kiwi", "grapefruit", "apple", "star-fruit", "mango", "mandarin"]

zipped = list(zip(probs, c))
srtd = sorted(zipped, key=lambda t: t[0], reverse=True)
top3 = srtd[:3]

for t in top3:
prob, label = t
print(prob, label)``````
2 Likes

This was the approach i was going for initially. It makes sense. Until i discovered that tensor objects in pytorch have the topk() function.
Thanks for the help @joedockrill

1 Like

It seems now pred_idx from learn_inf.predict(img) gives us only the index of the top class.

Is there another way now?
I am getting this

NameError: name ‘data’ is not defined

with this approach, maybe something change from v1 and v2

I solved with this dls.vocab

1 Like

For anyone that comes to this thread after searching how to get the top 5 classes,
Here is the current (2022) code:

``````cls, v, preds = learn.predict(img)
val, idx = preds.topk(5)
learn.dls.vocab[idx]
``````

It returns L object with top 5 classes, here is an example from my model

``````(#5) ['dischidia-onanthe-white-diamond','zamioculcas-raven','tradescantia-purple','scindapsus-pictus-argyraeus','calathea-white-fusion']
``````