fastai / fastbook

The fastai book, published as Jupyter Notebooks
Other
21.41k stars 8.3k forks source link

ImageClassifierCleaner throws KeyError #606

Open couzhei opened 1 year ago

couzhei commented 1 year ago

I was doing everything fine, in 02_production.ipynb, until I reach this cell:

cleaner = ImageClassifierCleaner(learn) cleaner

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File ~/miniconda3/envs/fastbook/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:639, in _save(im, fp, filename)
    638 try:
--> 639     rawmode = RAWMODE[im.mode]
    640 except KeyError as e:

The only thing I changed was using DuckDuckGo Image Search instead of Microsoft Azure, any help would be appreciated!

ed-kung commented 1 year ago

I have the same problem. The problem is that im.mode='RGBA' which throws a KeyError in JpegImagePlugin.py:

KeyError                                  Traceback (most recent call last)
File ~/mambaforge/lib/python3.10/site-packages/PIL/JpegImagePlugin.py:639, in _save(im, fp, filename)
    638 try:
--> 639     rawmode = RAWMODE[im.mode]
    640 except KeyError as e:

KeyError: 'RGBA'

I tried converting the images to RGB and re-saving them, but no luck, so I'm guessing fastai converts them to RGBA somewhere along the way?

fns = get_image_files(path)
for fn in fns:
    im = Image.open(fn)
    im = im.convert('RGB')
    im.save(fn)
kamransoomro84 commented 11 months ago

@couzhei @ed-kung I have this issue too. Did either of you guys figure out a solution?

couzhei commented 11 months ago

I actually dropped this investigation a while ago since I found out @jph00 is onto preparing the second edition of the book while the recent course of fastai didn't cover the type of production fastai API intended (@jph00 changed the focus quite rightfully into another tool called Gradio, which I believe is the new hype around the community, also great advertising came from both deeplearning.ai and Huggingface communities), but out of curiosity about @ed-kung's investigations I ran through it and it is apparently fine now! I don't know what made this happen but I list possible options that might be suspicious, so i t might not be an actual solution, I guess from fastbook import * might make the python management system confused that all the dependencies are provided or don't upgrade your existing packages, or it might relate to the version of your notebook (I downgraded it), also I seemed to have newer version of lxml, httpcore, and httpx compared to the previous environment.

kamransoomro84 commented 11 months ago

Strange. I have the current latest versions of every package so no clue what might be causing it! And yes, I'm using Gradio as well.

kamransoomro84 commented 11 months ago

Ok. I downgraded to pillow 9.5.0 and now it works.

spa-dev commented 10 months ago

I tried converting the images to RGB and re-saving them, but no luck, so I'm guessing fastai converts them to RGBA somewhere along the way?

The open_thumb function of this widget converts to RBGA. I deleted the .convert('RGBA') part of the code below and the widget no longer throws a KeyError. Seems to work, but I have no idea whether that's needed for something else.

def _open_thumb(
    fn:Path|str, # A path of an image
    h:int, # Thumbnail Height
    w:int # Thumbnail Width
) -> Image: # `PIL` image to display
    "Opens an image path and returns the thumbnail of the image"
    return Image.open(fn).to_thumb(h, w).convert('RGBA')

The rest of the code is here: https://github.com/fastai/fastai/blob/master/fastai/vision/widgets.py