Unhashable type 'list' in .label_from_func()

Hello fellow fastai users,

a friend of mine and I want to do some bounding box prediction on the new OpenImageV5 dataset. We are however struggling to create the databunch to train our model.

This minimum working example:

from fastai.vision import *
from pathlib import Path
PATH = Path('path')

paths = [PATH/'to/train/file1.jpg', PATH/'to/train/file2.jpg', PATH/'to/train/file3.jpg']
bboxes = [
    [[[32, 1, 4, 5], [89, 5, 37, 5]], ['Footwear', 'Person']],
    [[[32, 2, 14, 35]], ['Table']],
    [[[2, 132, 41, 51], [9, 25, 39, 3]], ['Cup', 'Plate']],
]

paths = [str(item) for item in paths]
img2bbox = dict(zip(paths, bboxes))
img2bbox.keys()

get_y_func = lambda o: img2bbox[str(o)]

df = pd.DataFrame({"Path": ['to/train/file1.jpg', 'to/train/file2.jpg', 'to/train/file3.jpg'], 
                   "ID": [item.split('/')[-1].split('.')[0] for item in paths]}, columns=["Path", "ID"])

def get_data(bs, size):
    src = ImageList.from_df(df, path=PATH)
    src = src.split_none()
    src = src.label_from_func(get_y_func)
    return src.databunch(path=PATH, bs=bs, collate_fn=bb_pad_collate)

data = get_data(bs=1, size=128)

reproduces the error we encounter:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-78c0fbf390e6> in <module>()
     25     return src.databunch(path=PATH, bs=bs, collate_fn=bb_pad_collate)
     26 
---> 27 data = get_data(bs=1, size=128)

<ipython-input-1-78c0fbf390e6> in get_data(bs, size)
     22     src = ImageList.from_df(df, path=PATH)
     23     src = src.split_none()
---> 24     src = src.label_from_func(get_y_func)
     25     return src.databunch(path=PATH, bs=bs, collate_fn=bb_pad_collate)
     26 

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/data_block.py in _inner(*args, **kwargs)
    466             self.valid = fv(*args, from_item_lists=True, **kwargs)
    467             self.__class__ = LabelLists
--> 468             self.process()
    469             return self
    470         return _inner

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/data_block.py in process(self)
    520         "Process the inner datasets."
    521         xp,yp = self.get_processors()
--> 522         for ds,n in zip(self.lists, ['train','valid','test']): ds.process(xp, yp, name=n)
    523         #progress_bar clear the outputs so in some case warnings issued during processing disappear.
    524         for ds in self.lists:

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/data_block.py in process(self, xp, yp, name)
    683     def process(self, xp:PreProcessor=None, yp:PreProcessor=None, name:str=None):
    684         "Launch the processing on `self.x` and `self.y` with `xp` and `yp`."
--> 685         self.y.process(yp)
    686         if getattr(self.y, 'filter_missing_y', False):
    687             filt = array([o is None for o in self.y.items])

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/data_block.py in process(self, processor)
     73         if processor is not None: self.processor = processor
     74         self.processor = listify(self.processor)
---> 75         for p in self.processor: p.process(self)
     76         return self
     77 

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/data_block.py in process(self, ds)
    334 
    335     def process(self, ds):
--> 336         if self.classes is None: self.create_classes(self.generate_classes(ds.items))
    337         ds.classes = self.classes
    338         ds.c2i = self.c2i

~/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/fastai/data_block.py in generate_classes(self, items)
    389         "Generate classes from `items` by taking the sorted unique values."
    390         classes = set()
--> 391         for c in items: classes = classes.union(set(c))
    392         classes = list(classes)
    393         classes.sort()

TypeError: unhashable type: 'list'

We tried around a lot but somehow this error keeps popping up. We have been looking at the Pascal-notebook since it seems to be doing mostly the same thing except that it works…

1 Like

You need to use an ObjectItemList, not an ImageList.

2 Likes

Worked! Thanks a lot :slight_smile: