devit icon indicating copy to clipboard operation
devit copied to clipboard

division by zero if no masks

Open londumas opened this issue 2 years ago • 0 comments

If there are no masks in the crop or image, then the current code divides by zero:

https://github.com/mlzxy/devit/blob/cfd0e0e28c8cdc8c0deff9b3f9dba7dcc1a08e61/detectron2/modeling/meta_arch/devit.py#L180

The simple solution is:

    loss = loss.sum()
    if num_masks != 0:
        loss /= num_masks
        
    return loss

Same here: https://github.com/mlzxy/devit/blob/cfd0e0e28c8cdc8c0deff9b3f9dba7dcc1a08e61/detectron2/modeling/meta_arch/devit.py#L199 The solution is then:

    loss = loss.mean(1).sum()
    if num_masks != 0:
        loss /= num_masks

    return loss

And also there: https://github.com/mlzxy/devit/blob/cfd0e0e28c8cdc8c0deff9b3f9dba7dcc1a08e61/detectron2/modeling/meta_arch/devit.py#L1321

And the fix is:

def fixed_l1_loss(input, target):
    
    res = F.l1_loss(input, target)
    
    if input.shape[0] == 0:
        res = torch.zeros_like(res)
    
    return res
loss_dict[f"rg_l1_loss_{i}"] = fixed_l1_loss(
                        pred_region_coords, gt_region_coords
                    )

londumas avatar Mar 18 '24 10:03 londumas