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/metrics.py in accuracy_multi(inp, targ, thresh, sigmoid)
166 def accuracy_multi(inp, targ, thresh=0.5, sigmoid=True):
167 “Compute accuracy when
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()
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
dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),
in data.py might be responsible, as it seems to hardcode the CategoryBlock for from_path_func