Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageClassifierCleaner throws KeyError #606

Open
couzhei opened this issue Jul 28, 2023 · 6 comments
Open

ImageClassifierCleaner throws KeyError #606

couzhei opened this issue Jul 28, 2023 · 6 comments

Comments

@couzhei
Copy link

couzhei commented Jul 28, 2023

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
Copy link

ed-kung commented Aug 1, 2023

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
Copy link

kamransoomro84 commented Sep 1, 2023

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

@couzhei
Copy link
Author

couzhei commented Sep 1, 2023

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
Copy link

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
Copy link

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

@spa-dev
Copy link

spa-dev commented Oct 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants