fastbook icon indicating copy to clipboard operation
fastbook copied to clipboard

Chapter 1. CNN classifier predict function fails with exception

Open Agaspher20 opened this issue 2 years ago • 3 comments

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 avatar Mar 30 '23 16:03 Agaspher20

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}")

Agaspher20 avatar Apr 01 '23 10:04 Agaspher20

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

pushpitkamboj avatar Apr 15 '23 18:04 pushpitkamboj

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

Yes, sure

Agaspher20 avatar Apr 16 '23 17:04 Agaspher20