OSError Unable to Identify the Image Lesson 1

Hi,

I used the google image downloader to downlaod a bunch of cricket and baseball Image.
While plotting the images using show batch function I am getting the following error

OSError: Traceback (most recent call last):
File “/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 138, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File “/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 138, in
samples = collate_fn([dataset[i] for i in batch_indices])
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py”, line 483, in getitem
if self.item is None: x,y = self.x[idxs],self.y[idxs]
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py”, line 92, in getitem
if isinstance(try_int(idxs), int): return self.get(idxs)
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py”, line 266, in get
res = self.open(fn)
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py”, line 262, in open
return open_image(fn, convert_mode=self.convert_mode)
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/image.py”, line 376, in open_image
x = PIL.Image.open(fn).convert(convert_mode)
File “/opt/anaconda3/lib/python3.7/site-packages/PIL/Image.py”, line 2657, in open
% (filename if filename else fp))
OSError: cannot identify image file ‘/home/jupyter/fastai/courses/dl1/data/baseballcricket/train/cricket/0294.jpg’

Any ideas ?

1 Like

It would be good to mention more on the platform you are using and the sharing the code you wrote.

Check this out: How to ask for help

Hi,

I am using Google Cloud to run my code.

I used the tip 1 given in the forum Tips for building large image datasets to download images of cricket and baseball.

I used the above code to load the path where the data has been stored and extract the labels from the path.

Then when I use data.show_batch() I get the following error

data.show_batch(rows=3, figsize=(7,6))

OSError Traceback (most recent call last)
in
----> 1 data.show_batch(rows=3, figsize=(7,6))

/opt/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in show_batch(self, rows, ds_type, **kwargs)
151 def show_batch(self, rows:int=5, ds_type:DatasetType=DatasetType.Train, **kwargs)->None:
152 “Show a batch of data in ds_type on a few rows.”
–> 153 x,y = self.one_batch(ds_type, True, True)
154 if self.train_ds.x._square_show: rows = rows ** 2
155 xs = [self.train_ds.x.reconstruct(grab_idx(x, i, self._batch_first)) for i in range(rows)]

/opt/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in one_batch(self, ds_type, detach, denorm)
134 w = self.num_workers
135 self.num_workers = 0
–> 136 try: x,y = next(iter(dl))
137 finally: self.num_workers = w
138 if detach: x,y = to_detach(x),to_detach(y)

/opt/anaconda3/lib/python3.7/site-packages/fastai/basic_data.py in iter(self)
68 def iter(self):
69 “Process and returns items from DataLoader.”
—> 70 for b in self.dl:
71 y = b[1][0] if is_listy(b[1]) else b[1]
72 yield self.proc_batch(b)

/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in next(self)
635 self.reorder_dict[idx] = batch
636 continue
–> 637 return self._process_next_batch(batch)
638
639 next = next # Python 2 compatibility

/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
656 self._put_indices()
657 if isinstance(batch, ExceptionWrapper):
–> 658 raise batch.exc_type(batch.exc_msg)
659 return batch
660

OSError: Traceback (most recent call last):
File “/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 138, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File “/opt/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 138, in
samples = collate_fn([dataset[i] for i in batch_indices])
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py”, line 483, in getitem
if self.item is None: x,y = self.x[idxs],self.y[idxs]
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/data_block.py”, line 92, in getitem
if isinstance(try_int(idxs), int): return self.get(idxs)
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py”, line 266, in get
res = self.open(fn)
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/data.py”, line 262, in open
return open_image(fn, convert_mode=self.convert_mode)
File “/opt/anaconda3/lib/python3.7/site-packages/fastai/vision/image.py”, line 376, in open_image
x = PIL.Image.open(fn).convert(convert_mode)
File “/opt/anaconda3/lib/python3.7/site-packages/PIL/Image.py”, line 2657, in open
% (filename if filename else fp))
OSError: cannot identify image file ‘/home/jupyter/fastai/courses/dl1/data/baseballcricket/train/cricket/0286.jpg’

Start looking into the image ‘/home/jupyter/fastai/courses/dl1/data/baseballcricket/train/cricket/0286.jpg’.

Some possibilities:

  • It could be a permissions issue; run !ls -l on the file to check whether you have read access (you should see an r in the output). You can add it with chmod a+r
  • The file could be corrupted. Can you download it to your local computer and view it? Can you view it on PIL on your computer? Does everything work if you delete this one file?

Hey @akshaynavalakha I had come across the same error. What is happening is the few images that you downloaded are corrupted and hence cannot be opened for reading. You can check out this script . Basically what the script does is it deletes all the corrupt and non-image files from a given directory. Hope this helps you and anyone with the same error! Please see I am new to python and scripting :grimacing:

2 Likes

I had the same error today. In my case, the image wasn’t corrupted. I used DuckDuckGo search to find images. The image URL ended with “.jpg”, but the actual format was AVIF. It turned out that PIL doesn’t support AVIF by default.
The fix was to install pillow-avif-plugin:
!pip install pillow-avif-plugin
And then import it:
import pillow_avif

I hope this helps.