Beginner: Basics of fastai, PyTorch, numpy, etc ✅

Thanks!
I was very close to giving up.

1 Like

Seems like the newest release no longer supports PIL images being passed as parameter.

Instead, the predict method takes care of all of that for you. You can just pass the name of the file:

img_filename = 'bird.jpg'
is_bird,_,probs = learn.predict(image_filename)
14 Likes

Hi, to everyone.

I am a PhD student in Greece and i would like to learn and use fastai. I would like to train various timeseries models based on Agrometerorological stations and spatial data in order to predict some agricultural indexes.

Can i please be a part of your forum to ask my question ?

Best regards
Chris Koliopanos

1 Like

Alternative work around for 2.7.11 . Thanks to @ bwarner in Fastai discord.

Try

Replace this line:

is_bird,_,probs = learn.predict(PILImage.create('bird.jpg'))

with this:

is_bird,_,probs = learn.predict('bird.jpg')
7 Likes

Can i please be a part of your forum to ask my question ?

Yes, and welcome.

I can confirm this works! Thanks @zerotosingularity

2 Likes

yeah just dropped the PR to fix that → Fix error AttributeError: read by drsmog · Pull Request #52 · fastai/course22 · GitHub

1 Like

Not sure if the cause of this is permanent, or just a result of an unintentional library change in the latest release (2.7.11)

1 Like

For the following code block:

#hide
# For the book, we can't actually click an upload button, so we fake it
uploader = SimpleNamespace(data = ['dog.jpg'])

#img = 'images/chapter1_cat_example.jpg' #PILImage.create('images/chapter1_cat_example.jpg')
is_cat,_,probs = learn.predict('dog.jpg')
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")

I get the following error:


FileNotFoundError Traceback (most recent call last)
in
1 #img = ‘images/chapter1_cat_example.jpg’ #PILImage.create(‘images/chapter1_cat_example.jpg’)
----> 2 is_cat,_,probs = learn.predict(‘dog.jpg’)
3 print(f"Is this a cat?: {is_cat}.“)
4 print(f"Probability it’s a cat: {probs[1].item():.6f}”)

25 frames
/usr/local/lib/python3.9/dist-packages/PIL/Image.py in open(fp, mode, formats)
2973
2974 if filename:
→ 2975 fp = builtins.open(filename, “rb”)
2976 exclusive_fp = True
2977

FileNotFoundError: [Errno 2] No such file or directory: ‘dog.jpg’

No such file or directory: ‘dog.jpg’

In the folder you are running this code, is there such file ‘dog’jpg’?
Try…

!ls 'dog.jpg' 

Hello all !
I’m also having a similar problem with the last step in Lesson 1. I have tried the fix mentioned further above, which says to remove the PILImage portion of that line of code with simply ‘bird.jpg’
This does indeed get rid of the error, but (at least for me) does not actually work.
For example, if I save an image of a car in the directory and call it ‘bird.jpg’, the notebook still says it is 100% probability of being a bird.
If I replace the bird image with a human cartoon, it says its 100% probability of being a bird.
In summary, the fix mentioned above seems to get rid of the error, but the last step is not able to differentiate between a bird and things that are not birds.

Ok, I’ve managed to figure out the answer to my own question. I’ll post it here in case it helps others (and hopefully I’m right in what I’m saying.)
What I said above is true. If I feed the notebook a picture of a coffee or a car or a beer, it says it is a bird. But I think I understand now that we haven’t really trained (fine-tuned) the model, to recognize birds (so the xkcd cartoon at the beginning of the lecture is a little bit misleading)…but rather, what we’ve done is taught the model to be able to tell the difference between a bird and a forest. That’s a significant difference.
I expanded the loop in the notebook to also get pictures of ‘car’ and of ‘beer’. Then I tested the notebook with new pictures of birds, cars, beers, and forests. It was very accurate in recognizing each new picture! But if I give it something new, like a cooking pan with some stuff in it, it says its a bird.
So I think what this notebook does is to teach a model to distinguish items from each other, but not in general.

1 Like

Indeed, if you train a classifier on pictures of birds or forests, it will always output either one of these 2 classes. Even if you give it a picture of a car.

What you could do is use multi-label classification, in principle that should be able to also classify “nothing” in case you run inference on an image of a class it wasn’t trained on.

1 Like

Hello everyone,

As I’m a total beginner, I still feel a bit overwhelmed browsing the https://docs.fast.ai docs, especially when I need to look for a very specific piece of information.

In the lesson 1 of the course, the method fine_tune is called on a Vision Learner, as in =>

learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(3) 

What is the integer param passed to the fine_tune method ?

Thanks in advance !

Hi Yacine,

You pass the number of epochs you want the model to train, aka the number of times the models gets to improve using the entire dataset.

For more details, have a look at my answer here: "fine_tune" vs. "fit_one_cycle" - #7 by zerotosingularity

1 Like

thanks a lot ! checking your linked answer right now

Small update: in v2.7.12 (PyTorch 2.0.0) you can use the PILImage again to make a prediction:

img_file = "my_image.jpg"
img = PILImage.create(img_file)
learn.predict(img)

or

learn.predict(img_file)
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}")

For this last cell in Lesson 1’s code, how do we know which index of probs is referring to bird? I tried a very simple change in the notebook by changing all the ‘bird’ to ‘dog’ but to get the relevant prediction I had to use ‘probs[1]’ instead of ‘probs[0]’. What gives?

To match the probability with the corresponding index, change the first line to

is_bird, index, probs = learn.predict(PILImage.create('bird.jpg'))

and in the last line, use that index as {probs[index]:.4f}`.

It will take care of the issue itself without us bothering what to put!

Here is what happens behind the scenes.

2 Likes

I want to create an instance of ResNet for 3D, 1-channel images (MRIs). All the examples and libraries seem to assume 2D and/or 3-channel images.

How do I an instance of ResNet for 3D, 1-channel images?