OSError: image file is truncated (38 bytes not processed)

I’m getting following error in midway of second epoch. First epoch ran successfully without any error.

OSError                                   Traceback (most recent call last)
<ipython-input-91-be1ab4476b35> in <module>()
----> 1 learn.fit_one_cycle(5, slice(lr))

/usr/local/lib/python3.6/dist-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, wd, callbacks, **kwargs)
     18     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor,
     19                                         pct_start=pct_start, **kwargs))
---> 20     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
     21 
     22 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, **kwargs:Any):

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
    160         callbacks = [cb(self) for cb in self.callback_fns] + listify(callbacks)
    161         fit(epochs, self.model, self.loss_func, opt=self.opt, data=self.data, metrics=self.metrics,
--> 162             callbacks=self.callbacks+callbacks)
    163 
    164     def create_opt(self, lr:Floats, wd:Floats=0.)->None:

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(epochs, model, loss_func, opt, data, callbacks, metrics)
     92     except Exception as e:
     93         exception = e
---> 94         raise e
     95     finally: cb_handler.on_train_end(exception)
     96 

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(epochs, model, loss_func, opt, data, callbacks, metrics)
     80             cb_handler.on_epoch_begin()
     81 
---> 82             for xb,yb in progress_bar(data.train_dl, parent=pbar):
     83                 xb, yb = cb_handler.on_batch_begin(xb, yb)
     84                 loss = loss_batch(model, xb, yb, loss_func, opt, cb_handler)

/usr/local/lib/python3.6/dist-packages/fastprogress/fastprogress.py in __iter__(self)
     63         self.update(0)
     64         try:
---> 65             for i,o in enumerate(self._gen):
     66                 yield o
     67                 if self.auto_update: self.update(i+1)

/usr/local/lib/python3.6/dist-packages/fastai/basic_data.py in __iter__(self)
     45     def __iter__(self):
     46         "Process and returns items from `DataLoader`."
---> 47         for b in self.dl:
     48             y = b[1][0] if is_listy(b[1]) else b[1]
     49             if not self.skip_size1 or y.size(0) != 1: yield self.proc_batch(b)

/usr/local/lib/python3.6/dist-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

/usr/local/lib/python3.6/dist-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 "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/usr/local/lib/python3.6/dist-packages/fastai/data_block.py", line 383, in __getitem__
    if self.item is None: x,y = self.x[idxs],self.y[idxs]
  File "/usr/local/lib/python3.6/dist-packages/fastai/data_block.py", line 67, in __getitem__
    if isinstance(try_int(idxs), int): return self.get(idxs)
  File "/usr/local/lib/python3.6/dist-packages/fastai/vision/data.py", line 265, in get
    res = super().get(i)
  File "/usr/local/lib/python3.6/dist-packages/fastai/data_block.py", line 43, in get
    return self.create_func(item) if self.create_func else item
  File "/usr/local/lib/python3.6/dist-packages/fastai/vision/image.py", line 422, in open_image
    x = PIL.Image.open(fn).convert('RGB')
  File "/usr/local/lib/python3.6/dist-packages/PIL/Image.py", line 844, in convert
    """
  File "/usr/local/lib/python3.6/dist-packages/PIL/ImageFile.py", line 231, in load
    "(%d bytes not processed)" % len(b))
OSError: image file is truncated (38 bytes not processed)

What could be the reason? I guess it may be due to small size of image but then error should have come in first epoch too.

2 Likes

I know it’s late, but I faced the similar issue recently. Just wanted to post it for others who might encounter this error. This SE answer helped me - https://stackoverflow.com/a/23575424/2650427

Essentially, add this in your notebook

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
9 Likes

I am facing this same problem I tried using “ImageFile” but it didn’t worked out.

1 Like

any other solution? I am stuck at this problem also!

I had a similar error. In my case, the problem was that my image files were corrupted because of a downloading error. I was able to fix my problem by going into the “.fastai/archive” directory, deleting the relevant “.tgz” file, and then deleting the image files in the “.fastai/data” directory. Once I did that, I downloaded the image files again and my code ran without issue.

Using the following code

PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

did get my model to train, but my results were terrible because a number of my image files had not downloaded correctly.

So if you’re having problems with truncated images, I would suggest downloading the images again.