Error trying to do a multi-label classification

Hi, I am trying to do a multi-label image classification on an essentially single-class problem (I have a list of categories, only one of which is usually present, so my input csv ONLY has ONE label per image eg. always “label1” or “label2” NEVER “label1 label2” (i am doing it this way as it might happen that NONE is present, so I’d prefer that a high enough threshold).

I’m not understanding something as I’m constantly hitting an error, specifically:

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/ in accuracy_multi(inp, targ, thresh, sigmoid)
166 def accuracy_multi(inp, targ, thresh=0.5, sigmoid=True):
167 “Compute accuracy when inp and targ are the same size.”
–> 168 inp,targ = flatten_check(inp,targ)
169 if sigmoid: inp = inp.sigmoid()
170 return ((inp>thresh)==targ.bool()).float().mean()

AssertionError: ==:

I am using:

dls = ImageDataLoaders.from_path_func(’’, files, label_func,
label_delim=’ ',label_cls=MultiCategoryBlock,item_tfms=RandomResizedCrop(448, min_scale=0.75),batch_tfms=aug_transforms(size=224),bs=128)

learn = cnn_learner(dls, resnet18, metrics=partial(accuracy_multi, thresh=0.5)).to_fp16()

if instead I use
learn = cnn_learner(dls, resnet18, metrics=error_rate).to_fp16()
everything works and trains, but of course, it is not multi-class and I can’t seem to force it to be.

What am I doing wrong? Many thanks.

P.S. I googled the error, no problem, I also looked at the code, and I have a hunch that

def from_path_func(cls, path, fnames, label_func, valid_pct=0.2, seed=None, item_tfms=None, batch_tfms=None, **kwargs):
"Create from list of fnames in paths with label_func"
dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),

in might be responsible, as it seems to hardcode the CategoryBlock for from_path_func


1 Like

Just an update: I forced my data into a df and used from_df and this worked (it detected it is a MultiCategory) so my immediate problem is solved. I am leaving this open as I feel that the helper functions should expose the same functionality (ie. that the _from_df and from_path_func should both support MultiCategoryBlock and that the current approach (hardcoding it) is probably unintentional.