Why can't Lesson 1 tell that a forest is a forest?

I made a small addition to the lesson 1 notebook

is_bird,_,probs = learn.predict(PILImage.create('bird.jpg'))
print(f"This is a: {is_bird}.")
print(f"Probability it's a bird: {probs[0]:.4f}")

#my added code
is_forest,_,probs = learn.predict(PILImage.create('forest.jpg'))
print(f"This is a: {is_forest}.")
print(f"Probability it's a forest: {probs[0]:.4f}")

The bird is predicted successfully at almost 1.0. The forest is predicted terribly at 0.0000. How do I get this model to work for both the forest and the bird? And if I wanted to scale it to four categories, how would I do that?

This is the notebook but it’s basically identical to the instructor one Is it a bird? Creating a model from your own data | Kaggle

The forest is predicted terribly at 0.0000.

Ah, but it isn’t! :smile:

You have your probs variable. It contains two rows (if it’s not rows, it’s columns then): the first row contains the probability that the image contains a bird, and the second row contains the probability that the image contains a forest.

In your code snippet above, you’re accessing the first row in both cases.

print(f"Probability it's a bird: {probs[0]:.4f}")
Here, you’ve correctly accessed the first row, which tells us the probability of a bird being in the image.

print(f"Probability it's a forest: {probs[0]:.4f}")
Here, you’ve accessed the first row again. The 0.0000 prediction you’re speaking of is the model telling us that there is a 0% chance the image is a bird!

To fix this, simply change the index to access the second row.

As for scaling it to four categories, I think it’s as simple as increasing the number of search terms, searches, in this specific notebook.

1 Like

Brilliant. Thank you!

Where can I read about the documentation of .predict? I though it would be in Vision Learner, becuase of ‘learn = vision_learner(dls, resnet18, metrics=error_rate)’ but it isn’t.

Also, let’s say I have 20 image categories. How would I keep track of which index is which category?

Hey, vision_learner creates a Learner object. So you can read about predict here: https://docs.fast.ai/learner.html#learner.predict

2 Likes

I figured out how the indices work. They are alphabetical order of the folder names. Not documented anywhere AFAIK.

So if I have categories = ‘banana’, ‘cherry’, ‘apple’ then dl the images and run the other ML code

probs[0] is apple
probs[1] is banana
probs[2] is cherry

Code like this is how you get your sorted directories.

def get_subdirectories(directory):
    return [name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name))]

directories=get_subdirectories('lesson1/training_images')

#non-destructive
sorted_dirs = sorted(directories)

#destructive
directories.sort()

1 Like

This is also stored in the DataLoaders object. So you can also use learn.dls.vocab.

learn.dls.vocab
>> ['bird', 'forest']
2 Likes

thanks guys. i was also wondering why my model gave me a prediction of 0.000. changing the index to “1” fixed it for me.