fastai / fastbook

The fastai book, published as Jupyter Notebooks
Other
21.51k stars 8.33k forks source link

Chapter 1. CNN classifier predict function fails with exception #573

Open Agaspher20 opened 1 year ago

Agaspher20 commented 1 year ago

In the very beginning of chapter 1 there is a following code which tries to distinguish whether it's cat or dog:

img = PILImage.create(uploader.data[0])
is_cat,_,probs = learn.predict(img)
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")

If I run it I face the following exception:

AttributeError                            Traceback (most recent call last)

[<ipython-input-16-865668ce572a>](https://localhost:8080/#) in <cell line: 1>()
----> 1 is_cat,_,probs = learn.predict(img)
      2 print(f"Is this a cat?: {is_cat}.")
      3 print(f"Probability it's a cat: {probs[1].item():.6f}")

26 frames

[/usr/local/lib/python3.9/dist-packages/PIL/Image.py](https://localhost:8080/#) in __getattr__(self, name)
    544             )
    545             return self._category
--> 546         raise AttributeError(name)
    547 
    548     @property

AttributeError: read

I run this notebook in Google Colab.

Agaspher20 commented 1 year ago

The actual problem that predict function doesn't resize image before actually predicting. Since in previous cells of chapter 1 notebook we trained model on images resized to 224x224 square, code of the cell should be changed to the following:

is_cat,_,probs = learn.predict(img.to_thumb(224))
print(f"Is this a cat?: {is_cat}.")
print(f"Probability it's a cat: {probs[1].item():.6f}")
pushpitkamboj commented 1 year ago

hey can i work on your problem if its not solved still??

Agaspher20 commented 1 year ago

hey can i work on your problem if its not solved still??

Yes, sure