Problem with uploader widget and PILImage.create

I’m running through the first lesson, and I downloaded an image of a dog from google, uploaded it with the widget, but now when I run this code:

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

I get this error:

UnidentifiedImageError Traceback (most recent call last)
in
----> 1 img = PILImage.create(uploader.data[0])
2 is_cat,_,probs = learn.predict(img)
3 print(f"Is this a cat?: {is_cat}.")
4 print(f"Probability it’s a cat: {probs[1].item():.6f}")

/opt/conda/lib/python3.7/site-packages/fastai/vision/core.py in create(cls, fn, **kwargs)
108 if isinstance(fn,ndarray): return cls(Image.fromarray(fn))
109 if isinstance(fn,bytes): fn = io.BytesIO(fn)
–> 110 return cls(load_image(fn, **merge(cls._open_args, kwargs)))
111
112 def show(self, ctx=None, **kwargs):

/opt/conda/lib/python3.7/site-packages/fastai/vision/core.py in load_image(fn, mode)
83 def load_image(fn, mode=None):
84 "Open and load a PIL.Image and convert to mode"
—> 85 im = Image.open(fn)
86 im.load()
87 im = im._new(im.im)

/opt/conda/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode)
2929 warnings.warn(message)
2930 raise UnidentifiedImageError(
-> 2931 “cannot identify image file %r” % (filename if filename else fp)
2932 )
2933

UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f93d9bb5410>

Can anyone help me understand why it’s not recognizing the byte data? I printed out uploader.data[0] and it’s a long bytestring, so it should be able to be processed by PILImage.create, from what I understand.

1 Like

Hey jbkjr,

I’ve the exact same problem and i couldnt figure it out

So to give you some content, i’ve been running my stuff on GCP’s AI platform. So i tried just creating a button on google colab and uploaded an image, there is no issue!

Ditto.

  • ‘UnidentifiedImageError: cannot identify image file <_io.BytesIO object at [location varies]>’ when using Google Cloud Platform (using the setup recommended by fast.ai as of Sep 2020). Notebooks otherwise running fine as far as I can tell.
  • Uploader works as expected when I run the same code on my own machine.

Since the uploader is only essential in deployment, which will be done off GCP anyway, my solution is simply to ignore the error during lessons and development on GCP. This obviously isn’t a long-term fix, but it will at least allow you to progress if you are trying to make a first app or something.

1 Like

I am getting an error too. I am running on AI Platform Notebooks on GCP. Here is the error I get -

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-11-37db1363ed76> in <module>
----> 1 img = PILImage.create(uploader.data[0])
      2 is_cat,_,probs = learn.predict(img)
      3 print(f"Is this a cat?: {is_cat}.")
      4 print(f"Probability it's a cat: {probs[1].item():.6f}")

IndexError: list index out of range

I clicked on the Upload button, selected a file, but I am not sure how it actually gets uploaded. I think that is the problem. There is no button to actually click and upload. Not sure if I need to get hung up on this before moving ahead. Sigh.

2 Likes

Hi Srividya,
I’m having the same issue that you outlined. Have you found a solution or any helpful resource?

1 Like

Not yet! :frowning: I am sorry. I am trying…

Here’s the work around from Stackoverflow:

But for me the problem was the image I download couldn’t even be opened on my desktop.
I tried running the code again with an image that could be opened on my desktop and it worked :slight_smile:

Hi Lucas, where are you running your notebooks? On Google Cloud? Thanks!

Hey Srividya, I’m using an Azure VM.

1 Like

We can not click an upload button, so we need to fake it with loading a picture like this:

uploader = SimpleNamespace(data = [’/content/DataML/oxford-iiit-pet/images/Abyssinian_1.jpg’])

and it works :+1:

#sample in the code:

uploader = widgets.FileUpload()

uploader

uploader = SimpleNamespace(data = [’/content/DataML/oxford-iiit-pet/images/Abyssinian_1.jpg’])

img = PILImage.create(uploader.data[0])

I still seem to have the same problem:

Doesn’t work for me in Jupyter notebook. Any fixes?

I just looked at ipywidgets documentation, I have version 8.1.2 so i replaced

img = PILImage.create(uploader.data[0])

with

img = PILImage.create(uploader.value[0].content.tobytes())

and that solved the problem for me :slightly_smiling_face:

1 Like