Coco - Data_block Too many args for loss function

I’m trying to use data_block with bboxes similar to Coco. I used the code in the docs and fed the data into a learner. When I run it, I get

     23     if not loss_func: return to_detach(out), yb[0].detach()
---> 24     loss = loss_func(out, *yb)
     26     if opt is not None:

TypeError: __call__() takes 3 positional arguments but 4 were given
coco = untar_data(URLs.COCO_TINY)
images, lbl_bbox = get_annotations(coco/'train.json')
img2bbox = dict(zip(images, lbl_bbox))
get_y_func = lambda o:img2bbox[]

data = (ObjectItemList.from_folder(coco)
        #Where are the images? -> in coco
        #How to split in train/valid? -> randomly with the default 20% in valid
        #How to find the labels? -> use get_y_func
        .transform(get_transforms(), tfm_y=True)
        #Data augmentation? -> Standard transforms with tfm_y=True
        .databunch(bs=16, collate_fn=bb_pad_collate))   
        #Finally we convert to a DataBunch and we use bb_pad_collate

learn = create_cnn(data, models.resnet34)

Not sure what I’m doing wrong. Any help would be appreciated.

There is no model for object detection in fastai yet, so create_cnn and then the default loss function can’t work with object detection.

Ah, thanks! I tried all the loss_functions but it looks like it will need a custom loss function.

This notebook of @sebderhy might point you in the right direction …it´s not fully working yet though :wink:

Were you able to create a loss function that takes into account multi object detection?

I am using @radek’s awesome fluke detection notebook but I have trouble making it work with multiple objects.
This is the loss function he uses for single objects:

def loss_fn(preds, targs, class_idxs):
    return L1Loss()(preds, targs.squeeze())

Hi, I have the same problems with COCO dataset. I tried the suggested loss function but it doesn’t work because it’s been used in a context with just one label. What I got is a mismatch between tensors. @astein did you manage to implement a working custom loss function?

1 Like

@sgugger can you share how to create a “learner” and custom loss function for bboxes in COCO?

There is no fastai easy function for that, no. You need to write your own PyTorch model/loss function.

@sgugger Thanks for the Information.
Can we use the RetinaNetFocalLoss as a loss function?
Can we use this implementation of RetinaNet is correct?

@astein did find a solution for this?