OpenCV error scn ==3 || scn ==4

Hi,

I took the image classification model from lesson 1, and modified the get data method to use the from_csv instead of paths(Similar to how jeremy mentioned in lesson2 ) on my data set that I have at my work. I got that to work and was got some good results. But later on I have pulled the latest updates from fast ai github . Since then I am encountering these opencv related issues. To test my theory I did a git checkout f0df3a476c3bf18ccf1caed6e0ca4318b681d0de (Nov 15th commit. This is when I last ran my successful model) and the opencv issue went away. Due to my lack of knowledge of opencv , I am not able to figure out the root cause. Please help me if you guys have encountered this issue before.

model = resnet34
bs=128
def get_data(sz):
tfms = tfms_from_model(model, sz, aug_tfms=None, max_zoom=1.1)
return ImageClassifierData.from_csv(PATH, ‘sample_images’, label_csv,bs=bs, tfms=tfms, val_idxs=val_idxs,suffix=’’, test_name=‘sample_images’, num_workers=8)

I am getting the following opencv error when I try to run the learning rate finder / fit the model .

0%| | 0/619 [00:00<?, ?it/s]
0%| | 0/619 [00:00<?, ?it/s, loss=3.72]
0%| | 1/619 [00:00<06:24, 1.61it/s, loss=3.72]
0%| | 1/619 [00:00<07:49, 1.32it/s, loss=3.7]
0%| | 2/619 [00:00<03:57, 2.59it/s, loss=3.7]
0%| | 2/619 [00:00<04:38, 2.22it/s, loss=3.67]
0%| | 3/619 [00:00<03:07, 3.29it/s, loss=3.67]
0%| | 3/619 [00:01<03:34, 2.87it/s, loss=3.69]
1%| | 4/619 [00:01<02:42, 3.79it/s, loss=3.69]
1%| | 4/619 [00:01<03:04, 3.34it/s, loss=3.64]
1%| | 5/619 [00:01<02:27, 4.15it/s, loss=3.64]
1%| | 5/619 [00:01<02:45, 3.72it/s, loss=3.65]
1%| | 6/619 [00:01<02:18, 4.42it/s, loss=3.65]
1%| | 6/619 [00:01<02:33, 4.00it/s, loss=3.65]
1%| | 7/619 [00:01<02:12, 4.63it/s, loss=3.65]
1%| | 7/619 [00:01<02:24, 4.22it/s, loss=3.63]
1%|▏ | 8/619 [00:01<02:07, 4.80it/s, loss=3.63]
1%|▏ | 8/619 [00:01<02:18, 4.42it/s, loss=3.62]
1%|▏ | 9/619 [00:01<02:03, 4.94it/s, loss=3.62]
1%|▏ | 9/619 [00:01<02:12, 4.60it/s, loss=3.61]
2%|▏ | 10/619 [00:01<01:59, 5.09it/s, loss=3.61]
2%|▏ | 10/619 [00:02<02:07, 4.76it/s, loss=3.61]
2%|▏ | 11/619 [00:02<01:56, 5.22it/s, loss=3.61]
2%|▏ | 11/619 [00:02<02:03, 4.91it/s, loss=3.6]
2%|▏ | 12/619 [00:02<01:53, 5.33it/s, loss=3.6]
2%|▏ | 12/619 [00:02<02:00, 5.03it/s, loss=3.6]
2%|▏ | 13/619 [00:02<01:51, 5.42it/s, loss=3.6]
2%|▏ | 13/619 [00:02<01:57, 5.14it/s, loss=3.6]
2%|▏ | 14/619 [00:02<01:49, 5.52it/s, loss=3.6]
2%|▏ | 14/619 [00:02<01:55, 5.25it/s, loss=3.58]
2%|▏ | 15/619 [00:02<01:47, 5.60it/s, loss=3.58]
2%|▏ | 15/619 [00:02<01:52, 5.35it/s, loss=3.55]
3%|▎ | 16/619 [00:02<01:46, 5.68it/s, loss=3.55]
3%|▎ | 16/619 [00:02<01:50, 5.44it/s, loss=3.54]
3%|▎ | 17/619 [00:02<01:44, 5.76it/s, loss=3.54]
3%|▎ | 17/619 [00:03<01:49, 5.52it/s, loss=3.52]
3%|▎ | 18/619 [00:03<01:43, 5.83it/s, loss=3.52]

error Traceback (most recent call last)
in ()
1 lr = 0.1
----> 2 learn.fit(lr, 3, cycle_len=1, cycle_mult=2)

~/playground/fastai/courses/dl1/fastai/learner.py in fit(self, lrs, n_cycle, wds, **kwargs)
97 self.sched = None
98 layer_opt = self.get_layer_opt(lrs, wds)
—> 99 self.fit_gen(self.model, self.data, layer_opt, n_cycle, **kwargs)
100
101 def lr_find(self, start_lr=1e-5, end_lr=10, wds=None):

~/playground/fastai/courses/dl1/fastai/learner.py in fit_gen(self, model, data, layer_opt, n_cycle, cycle_len, cycle_mult, cycle_save_name, metrics, callbacks, **kwargs)
87 n_epoch = sum_geom(cycle_len if cycle_len else 1, cycle_mult, n_cycle)
88 fit(model, data, n_epoch, layer_opt.opt, self.crit,
—> 89 metrics=metrics, callbacks=callbacks, reg_fn=self.reg_fn, clip=self.clip, **kwargs)
90
91 def get_layer_groups(self): return self.models.get_layer_groups()

~/playground/fastai/courses/dl1/fastai/model.py in fit(model, data, epochs, opt, crit, metrics, callbacks, **kwargs)
80 stepper.reset(True)
81 t = tqdm(iter(data.trn_dl), leave=False, total=len(data.trn_dl))
—> 82 for (*x,y) in t:
83 batch_num += 1
84 loss = stepper.step(V(x),V(y))

~/anaconda3/lib/python3.6/site-packages/tqdm/_tqdm.py in iter(self)
951 “”", fp_write=getattr(self.fp, ‘write’, sys.stderr.write))
952
–> 953 for obj in iterable:
954 yield obj
955 # Update and possibly print the progressbar.

~/playground/fastai/courses/dl1/fastai/dataset.py in next(self)
227 if self.i>=len(self.dl): raise StopIteration
228 self.i+=1
–> 229 return next(self.it)
230
231 @property

~/playground/fastai/courses/dl1/fastai/dataloader.py in iter(self)
64 def iter(self):
65 with ThreadPoolExecutor(max_workers=self.num_workers) as e:
—> 66 for batch in e.map(self.get_batch, iter(self.batch_sampler)):
67 yield get_tensor(batch, self.pin_memory)
68

~/anaconda3/lib/python3.6/concurrent/futures/_base.py in result_iterator()
584 # Careful not to keep a reference to the popped future
585 if timeout is None:
–> 586 yield fs.pop().result()
587 else:
588 yield fs.pop().result(end_time - time.time())

~/anaconda3/lib/python3.6/concurrent/futures/_base.py in result(self, timeout)
423 raise CancelledError()
424 elif self._state == FINISHED:
–> 425 return self.__get_result()
426
427 self._condition.wait(timeout)

~/anaconda3/lib/python3.6/concurrent/futures/_base.py in __get_result(self)
382 def __get_result(self):
383 if self._exception:
–> 384 raise self._exception
385 else:
386 return self._result

~/anaconda3/lib/python3.6/concurrent/futures/thread.py in run(self)
54
55 try:
—> 56 result = self.fn(*self.args, **self.kwargs)
57 except BaseException as exc:
58 self.future.set_exception(exc)

~/playground/fastai/courses/dl1/fastai/dataloader.py in get_batch(self, indices)
60 def len(self): return len(self.batch_sampler)
61
—> 62 def get_batch(self, indices): return self.collate_fn([self.dataset[i] for i in indices])
63
64 def iter(self):

~/playground/fastai/courses/dl1/fastai/dataloader.py in (.0)
60 def len(self): return len(self.batch_sampler)
61
—> 62 def get_batch(self, indices): return self.collate_fn([self.dataset[i] for i in indices])
63
64 def iter(self):

~/playground/fastai/courses/dl1/fastai/dataset.py in getitem(self, idx)
92
93 def getitem(self, idx):
—> 94 x,y = self.get_x(idx),self.get_y(idx)
95 return self.get(self.transform, x, y)
96

~/playground/fastai/courses/dl1/fastai/dataset.py in get_x(self, i)
130 fn = os.path.join(self.path, self.fnames[i])
131 #print('Opening file name: ',fn)
–> 132 return cv2.cvtColor(cv2.imread(fn, flags), cv2.COLOR_BGR2RGB).astype(np.float32)/255
133
134 def resize_imgs(self, targ, new_path):

error: /io/opencv/modules/imgproc/src/color.cpp:10606: error: (-215) scn == 3 || scn == 4 in function cvtColor

I have read online about this error, and people have mentioned that opencv raises this if the path to the image is not absolute , or if we are trying to read the image in grayscale mode like cv2.imread(‘image.jpg’,0) but when I looked at the fastai dataset.py, looks like we are not reading the image with that flag and the filename is actually an absolute path.

I have also tried deleting my images folder to eliminate if there are any corrupt images. But nothing worked out so far. Please give me some pointers to resolve this issue.

Environment: Custom DL Box (Ubuntu)
Python 3 (conda)
GPU : P100

Thanks,
Abilash

Can you share a df.head() of the CSV file…

If the images already have the suffixes attached, then we neednot to pass any suffix…

Atleast that fixed the error in my case…

Here’s the thread…

Thanks @ecdrid for the response. I have looked at the previous discussion. Yes the file names have .jpg as the suffix . So I did not pass in any suffix, the default in fast ai dataset is also set to suffix=’’

Here is the head on my csv file.
0049132.jpg,9
0058891.jpg,11
0084687.jpg,16
0042064.jpg,8
0069265.jpg,13
0032953.jpg,6
0086394.jpg,17
0063201.jpg,12

i’m also facing the same issue

did anyone know how to resolve it

After you get an exception, enter %debug in a cell to open the python debugger. There you can see what’s going on to debug your problem.

3 Likes

Hi,
I come across the same problem.Could you tell how you managed to find the solution of the problem?
Thanks.