An issue I had when trying to do my own sun-vs-moon dataset to try the classifier was that the learn=...
line would fail with a deep stack trace that eventually boiled down to "Can’t find im.shape
because im
is None
. I traced it back and eventually discovered that some of my images weren’t being read correctly, resulting in None
being passed instead of an array all the way through. In my case I’d used a Google images downloader and hadn’t reviewed the data. There was an .svg
files it couldn’t read, and even trickier than that there was a jpg extension file that wa sactually HTML! It wasn’t obvious which files it was having an issue with, so if anyone else has this issue, here’s how to find them:
Edit the fastai/fastai/dataset.py
file: Find this line: https://github.com/fastai/fastai/blob/e8433d4a76eaf40c29a74a18ee043b23f6c35dbe/fastai/dataset.py#L145
def get_x(self, i): return open_image(os.path.join(self.path, self.fnames[i]))
If that method returns None
, which it will if it can’t open your file as an image, then you’ll eventually get an error when it tries to read its shape. Change it to look like this:
def get_x(self, i):
im = open_image(os.path.join(self.path, self.fnames[i]))
if im is None:
print('Failed to read as image!')
print(os.path.join(self.path, self.fnames[i]))
print(1/0) # Throw an exception now
return im
Now you’ll get a message telling you which file it couldn’t read, and an immediate exception.
I’ll make a similar PR to fastai
tomorrow with a sensible exception, but for now I thought this might help anyone who gets stuck in the same place!
I’ve made a PR to add better error handling to the open_image
call: https://github.com/fastai/fastai/pull/127