OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor

this is the error
[OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 10606]
i’m getting as soon as i run this code
learn = ConvLearner.pretrained(resnet34, data)
i thought may be opencv is not installed that why i’m getting this problem but l checked everything was installed
then i tried different sz=224 and sz=299
but still i got the same error

does anyone faced this error before ,is anyone out there who can help me in resolving this issue

and as always thanks in advance :wink:

1 Like

Sounds like you have something in there that isn’t an image.

but,i myself has put every image into the folder. uploaded to aws from jupyter notebook

does i have to manually check every folder again to find the culprit ,or is there any technique which can guide
me to the folder which is stopping me to get the required results

Immediately after the exception type %debug in a cell to open the python debugger. There you can find out what is the issue. Here’s some tips on how to use the debugger after it’s popped up https://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/

1 Like

This error sometimes occurs because of other reasons too - Check that your Folder Names (in Train or Validation) have NO spaces in them.

I check the sizes of Images, by opening each image and checking its size as below. If this is useful, I can create a pull request to act as an utility Function.

@naveenmanwani - You can use this for now to see if it helps you find the image that’s the problem.

1 Like

@jeremy and @ramesh
when i did % debug i got this as a result

and i checked names of folders of valid and test and train set,they don’t consist of any spaces

Hey…Please Don’t @ people unless they are the only ones who know this issue (I certainly don’t know). Because then others who may have answers might not respond to your question :slight_smile:

Also run the code I put here to check the image sizes.

1 Like

oh ,i’m extremely sorry for committing such mistake.i’ll not do it again in future
when i tried from glob import glob
from collections import defaultdict
from pprint import pprint

image_sizes=defaultdict(int)
for img_fname in glob(f"{PATH}valid//.jpg"):
try:
img=Image.open(img_fname)
except Exception:
print(f"Error opening Image File:{img_fname}")
image_sizes[img.size]+=1
i got this error
Error opening Image File:data/idlisamosa/valid/idli/images (69).jpg

NameError Traceback (most recent call last)
in ()
9 except Exception:
10 print(f"Error opening Image File:{img_fname}")
—> 11 image_sizes[img.size]+=1
12
13

NameError: name ‘img’ is not defined

i have ran 8 times this block of code and everytime when this code runs it point out [error opening image]
,i delete that image from the set .
my question is upto what point i have to keep on running this code
actually this is the way i have given name to my images
[‘images (56).jpg’,
‘images (57).jpg’,
‘images (58).jpg’,
‘images (59).jpg’,
‘images (60).jpg’,
‘images (61).jpg’,
‘images (62).jpg’,
‘images (63).jpg’,
‘images (64).jpg’,
‘images (65).jpg’]

check indentation and try running that code… and in terms of image names, I am not sure but try removing the space in the name.

Try this
Go to the file dataset.py under fastai and do

print (fn)

Before the return
You will learn a lot about the data that your are trying to invest to the model.
After you figure out rollback the change or do a

git pull to bring a fresh copy.

@jeremy I think some of the methods need to include verbosity levels to print out some of the detail for debugging.

thanks for the advice .i removed all the spaces from the image name .i got results for train set ,valid set and but when i run this same code for test set, i got this as a result defaultdict(int, {})
any clue why i got this result

sir are you saying that just before this line
return [(o[mask],o[~mask]) for o in a]
i should put print(fn)
after that sir how should i run it .

Put the change after line 118 and before 119 on the file
dataset.py


or try to investigate the value of variable fn on using the %debug keyword.

sir as you said i putted the print(fn ),then i got this as a result

this is the full list
Epoch
0% 0/3 [00:00<?, ?it/s]

0%| | 0/3 [00:00<?, ?it/s]data/idlisamosa/train/idli/images(8).jpg
data/idlisamosa/train/samosa/download11.jpg
data/idlisamosa/train/samosa/images64.jpgdata/idlisamosa/train/samosa/images17.jpg
data/idlisamosa/train/idli/images(54).jpg

data/idlisamosa/train/samosa/download2.jpg
data/idlisamosa/train/idli/images(6).jpg
data/idlisamosa/train/idli/images(31).jpg
data/idlisamosa/train/idli/images(44).jpg
data/idlisamosa/train/samosa/download7.jpg
data/idlisamosa/train/idli/images(27).jpg
data/idlisamosa/train/idli/images(36).jpg
data/idlisamosa/train/idli/images(33).jpg
data/idlisamosa/train/idli/images(15).jpg
data/idlisamosa/train/idli/images(23).jpg
data/idlisamosa/train/idli/download(11).jpg
data/idlisamosa/train/idli/images(50).jpg
data/idlisamosa/train/idli/download(5).jpg
data/idlisamosa/train/idli/images(48).jpg
data/idlisamosa/train/idli/images(30).jpg
data/idlisamosa/train/idli/images(52).jpg
data/idlisamosa/train/samosa/images22.jpg
data/idlisamosa/train/samosa/images53.jpg
data/idlisamosa/train/idli/images(49).jpg
data/idlisamosa/train/samosa/images61.jpg
data/idlisamosa/train/idli/images(24).jpg
data/idlisamosa/train/idli/images(5).jpg
data/idlisamosa/train/samosa/images37.jpg
data/idlisamosa/train/samosa/images23.jpg
data/idlisamosa/train/samosa/download9.jpg
data/idlisamosa/train/idli/images(47).jpg
data/idlisamosa/train/idli/download.jpg
data/idlisamosa/train/idli/images(51).jpg
data/idlisamosa/train/idli/download(9).jpg
data/idlisamosa/train/samosa/download1.jpg
data/idlisamosa/train/samosa/download.jpg
data/idlisamosa/train/idli/images(46).jpg
data/idlisamosa/train/samosa/images49.jpg
data/idlisamosa/train/samosa/images69.jpg
data/idlisamosa/train/idli/images(40).jpg
data/idlisamosa/train/idli/images(16).jpg
data/idlisamosa/train/samosa/images52.jpg
data/idlisamosa/train/idli/images(28).jpg
data/idlisamosa/train/idli/images(13).jpg
data/idlisamosa/train/idli/download(12).jpg
data/idlisamosa/train/samosa/download6.jpg
data/idlisamosa/train/idli/images(37).jpg
data/idlisamosa/train/idli/download(8).jpg
data/idlisamosa/train/samosa/images19.jpg
data/idlisamosa/train/idli/images(43).jpg
data/idlisamosa/train/samosa/images54.jpg
data/idlisamosa/train/samosa/download5.jpg
data/idlisamosa/train/idli/images(21).jpg
data/idlisamosa/train/samosa/images31.jpg
data/idlisamosa/train/samosa/download3.jpg
data/idlisamosa/train/samosa/images20.jpg
data/idlisamosa/train/idli/download(7).jpg
data/idlisamosa/train/samosa/download14.jpg
data/idlisamosa/train/samosa/images47.jpg
data/idlisamosa/train/idli/images(19).jpg
data/idlisamosa/train/samosa/images41.jpg
data/idlisamosa/train/samosa/images50.jpg
data/idlisamosa/train/samosa/images56.jpg
data/idlisamosa/train/idli/images(34).jpg
data/idlisamosa/train/idli/images(20).jpg
data/idlisamosa/train/idli/images(4).jpg
data/idlisamosa/train/samosa/images70.jpgdata/idlisamosa/train/samosa/images33.jpg

data/idlisamosa/train/idli/images(39).jpg
data/idlisamosa/train/idli/images(2).jpg
data/idlisamosa/train/samosa/images48.jpg
data/idlisamosa/train/samosa/images45.jpg
data/idlisamosa/train/samosa/images62.jpg
data/idlisamosa/train/samosa/images57.jpg
data/idlisamosa/train/samosa/images55.jpg
data/idlisamosa/train/samosa/images32.jpg
data/idlisamosa/train/idli/images(53).jpg
data/idlisamosa/train/samosa/images34.jpg
data/idlisamosa/train/idli/images(26).jpg
data/idlisamosa/train/idli/download(13).jpg
data/idlisamosa/train/idli/download(2).jpg
data/idlisamosa/train/samosa/images30.jpg
data/idlisamosa/train/samosa/.ipynb_checkpoints
data/idlisamosa/train/samosa/images68.jpg
data/idlisamosa/train/samosa/images42.jpg
data/idlisamosa/train/samosa/images58.jpg
data/idlisamosa/train/idli/images(1).jpg
data/idlisamosa/train/idli/images(22).jpg
data/idlisamosa/train/samosa/images65.jpg
data/idlisamosa/train/idli/images(25).jpg
data/idlisamosa/train/samosa/images51.jpg
data/idlisamosa/train/idli/images(11).jpg
data/idlisamosa/train/samosa/images15.jpg
data/idlisamosa/train/idli/idli_625x350_51464604752.jpg
data/idlisamosa/train/idli/images(42).jpg
data/idlisamosa/train/idli/images(10).jpg
data/idlisamosa/train/idli/images(55).jpg
data/idlisamosa/train/samosa/download10.jpg
data/idlisamosa/train/idli/images(17).jpg
data/idlisamosa/train/samosa/download13.jpg
data/idlisamosa/train/idli/images(9).jpg
data/idlisamosa/train/idli/images(45).jpg
data/idlisamosa/train/samosa/images59.jpg
data/idlisamosa/train/samosa/images66.jpg
data/idlisamosa/train/samosa/download4.jpg
data/idlisamosa/train/samosa/images67.jpg
data/idlisamosa/train/idli/download(3).jpg
data/idlisamosa/train/idli/images(18).jpg
data/idlisamosa/train/samosa/images35.jpg
data/idlisamosa/train/samosa/images44.jpg
data/idlisamosa/train/samosa/images26.jpg

does it mean i have to remove these images from my dataset

Can you verify if the files exists?

sir i randomly looked for 11 images .and all those images do exist in my training folder