Runtime error on running fit.one_cycle

I am trying to run the fit.one_cycle function on an image databunch of around 217,000 images.

When I try to run the learner.fit_one_cycle, I get the error Unidentified Image Error. Provided below is the traceback of the error:
UnidentifiedImageError Traceback (most recent call last)
in
----> 1 learn.fit_one_cycle(4)

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)
         21     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
         22                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
    ---> 23     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
         24 
         25 def fit_fc(learn:Learner, tot_epochs:int=1, lr:float=defaults.lr,  moms:Tuple[float,float]=(0.95,0.85), start_pct:float=0.72,

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
        198         else: self.opt.lr,self.opt.wd = lr,wd
        199         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)
    --> 200         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
        201 
        202     def create_opt(self, lr:Floats, wd:Floats=0.)->None:

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)
         97             cb_handler.set_dl(learn.data.train_dl)
         98             cb_handler.on_epoch_begin()
    ---> 99             for xb,yb in progress_bar(learn.data.train_dl, parent=pbar):
        100                 xb, yb = cb_handler.on_batch_begin(xb, yb)
        101                 loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/fastprogress/fastprogress.py in __iter__(self)
         45         except Exception as e:
         46             self.on_interrupt()
    ---> 47             raise e
         48 
         49     def update(self, val):

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/fastprogress/fastprogress.py in __iter__(self)
         39         if self.total != 0: self.update(0)
         40         try:
    ---> 41             for i,o in enumerate(self.gen):
         42                 if i >= self.total: break
         43                 yield o

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/basic_data.py in __iter__(self)
         73     def __iter__(self):
         74         "Process and returns items from `DataLoader`."
    ---> 75         for b in self.dl: yield self.proc_batch(b)
         76 
         77     @classmethod

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
        343 
        344     def __next__(self):
    --> 345         data = self._next_data()
        346         self._num_yielded += 1
        347         if self._dataset_kind == _DatasetKind.Iterable and \

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
        854             else:
        855                 del self._task_info[idx]
    --> 856                 return self._process_data(data)
        857 
        858     def _try_put_index(self):

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _process_data(self, data)
        879         self._try_put_index()
        880         if isinstance(data, ExceptionWrapper):
    --> 881             data.reraise()
        882         return data
        883 

    ~/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/_utils.py in reraise(self)
        392             # (https://bugs.python.org/issue2651), so we work around it.
        393             msg = KeyErrorMessage(msg)
    --> 394         raise self.exc_type(msg)

    UnidentifiedImageError: Caught UnidentifiedImageError in DataLoader worker process 3.
    Original Traceback (most recent call last):
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
        data = fetcher.fetch(index)
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
        data = [self.dataset[idx] for idx in possibly_batched_index]
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
        data = [self.dataset[idx] for idx in possibly_batched_index]
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/data_block.py", line 651, in __getitem__
        if self.item is None: x,y = self.x[idxs],self.y[idxs]
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/data_block.py", line 120, in __getitem__
        if isinstance(idxs, Integral): return self.get(idxs)
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/vision/data.py", line 271, in get
        res = self.open(fn)
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/vision/data.py", line 267, in open
        return open_image(fn, convert_mode=self.convert_mode, after_open=self.after_open)
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/fastai/vision/image.py", line 398, in open_image
        x = PIL.Image.open(fn).convert(convert_mode)
      File "/home/diliptmonson/anaconda3/envs/my_env/lib/python3.7/site-packages/PIL/Image.py", line 2862, in open
        "cannot identify image file %r" % (filename if filename else fp)
    PIL.UnidentifiedImageError: cannot identify image file '/home/diliptmonson/course-v3/Fastai - Practical Experiments/train/8f17b296-21bc-11ea-a13a-137349068a90.jpg'

That means most likely pillow can’t open it. Can you try opening that particular image with Pillow and see what it gives you? If it’s still an error then that means that image is corrupted or unreadable in some way

Thanks Zachary. It was indeed an issue with pillow unable to open the image. Do you know if there is a way within fit_one_cycle to skip that image and move on to the next image.

Thanks,
Dilip

No you’ll need to clean the dataset yourself. There’s a few scripts among the forums IIRC for checking for corrupted images. And that should also be covered in lesson 2 via verify_images()

1 Like

Hi guys,

I also have a runtime error but related to a pre defined loss_function.
I am trying to use transfer learning to detect multiple object on an image (multiple bounding boxes).
I can load my data without error

data = (ObjectItemList
        .from_folder(train_images_dir)
        .split_by_rand_pct()
        .label_from_func(func=get_y_func)
        .transform(get_transforms(), tfm_y=True, size=(model_input_size, model_input_size), resize_method=ResizeMethod.SQUISH)
        .databunch(bs=batch_size, collate_fn=bb_pad_collate)
        .normalize(imagenet_stats))

but when I try to train with

learner = cnn_learner(data=data, base_arch=models.resnet18, metrics=[accuracy])
learner.fit_one_cycle(1)

I get the following stacktrace

epoch     train_loss  valid_loss  accuracy  time    
Traceback (most recent call last):
  File "/home/habrangi/arhs/dev/workspace/perso/fastai-bounding-boxes/fastai-bounding-boxes/main.py", line 36, in <module>
    learner.fit_one_cycle(1)
  File "/home/habrangi/arhs/dev/workspace/perso/fastai-bounding-boxes/venv/lib/python3.7/site-packages/fastai/train.py", line 23, in fit_one_cycle
    learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
  File "/home/habrangi/arhs/dev/workspace/perso/fastai-bounding-boxes/venv/lib/python3.7/site-packages/fastai/basic_train.py", line 200, in fit
    fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)
  File "/home/habrangi/arhs/dev/workspace/perso/fastai-bounding-boxes/venv/lib/python3.7/site-packages/fastai/basic_train.py", line 101, in fit
    loss = loss_batch(learn.model, xb, yb, learn.loss_func, learn.opt, cb_handler)
  File "/home/habrangi/arhs/dev/workspace/perso/fastai-bounding-boxes/venv/lib/python3.7/site-packages/fastai/basic_train.py", line 30, in loss_batch
    loss = loss_func(out, *yb)
TypeError: __call__() takes 3 positional arguments but 4 were given

Process finished with exit code 1

With the debugger, I could find that the loss_func that is used is FlattenedLoss of BCEWithLogitsLoss()
This Loss function is automatically defined if we use ObjectItemList if I am not mistaken.

I created a sample project where it is just needed to create the virtual env, install the libs and run the main.py script gitlab project if that helps.

Anybody see what I am doing wrong ?

Thanks.