Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

division by zero if no masks #51

Open
londumas opened this issue Mar 18, 2024 · 0 comments
Open

division by zero if no masks #51

londumas opened this issue Mar 18, 2024 · 0 comments

Comments

@londumas
Copy link

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

return loss.sum() / num_masks

The simple solution is:

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

Same here:

return loss.mean(1).sum() / num_masks

The solution is then:

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

    return loss

And also there:

loss_dict[f'rg_l1_loss_{i}'] = F.l1_loss(pred_region_coords, gt_region_coords)

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
                    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant