Cannot Create an ImageDataBunch From Lists

I am attempting the iWildCam 2019 Animal Classification Challenge on Kaggle using what I’ve learned in Lesson 1.

The data is provided as both a set of images and a train.csv file. The train.csv file contains both the names of images and their corresponding classifications. I created an ImageList from the CSV file using ImageList.from_df.

imgList = ImageList.from_df(df = data, path = train_path, cols = ‘file_name’)

imgList.items[:10]

array([’/kaggle/input/iwildcam-2019-fgvc6/train_images/5998cfa4-23d2-11e8-a6a3-ec086b02610b.jpg’,
‘/kaggle/input/iwildcam-2019-fgvc6/train_images/588a679f-23d2-11e8-a6a3-ec086b02610b.jpg’,
‘/kaggle/input/iwildcam-2019-fgvc6/train_images/59279ce3-23d2-11e8-a6a3-ec086b02610b.jpg’,…], dtype=’<U88’)

But when I try to run the following:

dataBunch = ImageDataBunch.from_lists(path = train_path, fnames = imgList, labels = classification, size = 224)

(train_path = "/kaggle/input/iwildcam-2019-fgvc6/train_images",
classification = The labels associated with each image extracted from the CSV file.)

I get the following error:


TypeError Traceback (most recent call last)
in
1 # labels_ls = list(map(classification, imgList.items))
----> 2 dataBunch = ImageDataBunch.from_lists(path = train_path, fnames = imgList, labels = classification, size = 224)

/opt/conda/lib/python3.6/site-packages/fastai/vision/data.py in from_lists(cls, path, fnames, labels, valid_pct, seed, item_cls, **kwargs)
135 “Create from list of fnames in path.”
136 item_cls = ifnone(item_cls, ImageList)
–> 137 fname2label = {f:l for (f,l) in zip(fnames, labels)}
138 src = (item_cls(fnames, path=path).split_by_rand_pct(valid_pct, seed)
139 .label_from_func(lambda x:fname2label[x]))

/opt/conda/lib/python3.6/site-packages/fastai/vision/data.py in (.0)
135 “Create from list of fnames in path.”
136 item_cls = ifnone(item_cls, ImageList)
–> 137 fname2label = {f:l for (f,l) in zip(fnames, labels)}
138 src = (item_cls(fnames, path=path).split_by_rand_pct(valid_pct, seed)
139 .label_from_func(lambda x:fname2label[x]))

TypeError: unhashable type: 'Image’

I’m not sure what’s causing this error. If somebody could point me in the right direction I’d be grateful. Thank you.

Here’s what’s going on ( I think ):

  1. imgList has an iterator which returns objects of class Image and not the actual file names (which is what is being expected).
  2. The error says that an object of class Image is unhashable. What this means that in a python dictionary, the keys cannot be of the type Image
  3. What you need to do is instead pass a plain python list to the ImageDataBunch function that contains just the names of the images without the rest of the path (which is already coming from train_path)

It’s the first time I’m contributing to these forums, plus I’ve written this on my phone so apologies for any lack of clarity in expression. :sweat_smile:
Hope this helps.

1 Like

Thanks for the suggestion. I’ll try this approach and let you know if it works.

Your solution worked. I had to pass imgList.items instead of just imgList. Thanks for your help. :blush:

Happy to help!