Bce dice combined loss

From wenmin-wu/ds-skills · View interactive version

Bce dice combined loss is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Combines BCE-with-logits and soft Dice loss with configurable weights for binary and multilabel segmentation training.

How to use this skill

Skill file

BCE + Dice Combined Loss

Overview

BCE loss optimizes per-pixel accuracy but struggles with class imbalance in segmentation (background dominates). Dice loss directly optimizes the Dice coefficient (F1 score) and handles imbalance well, but has noisy gradients. Combining both gives stable training from BCE while pushing toward high Dice overlap. This is the standard loss for medical image segmentation tasks.

Quick Start

import torch
import torch.nn as nn

def dice_loss(logits, targets, smooth=1.0):
    probs = torch.sigmoid(logits)
    iflat = probs.view(-1)
    tflat = targets.view(-1)
    intersection = (iflat * tflat).sum()
    return 1 - (2. * intersection + smooth) / (iflat.sum() + tflat.sum() + smooth)

def bce_dice_loss(logits, targets, bce_weight=1.0, dice_weight=1.0):
    bce = nn.functional.binary_cross_entropy_with_logits(logits, targets)
    dice = dice_loss(logits, targets)
    return (bce_weight * bce + dice_weight * dice) / (bce_weight + dice_weight)

# Usage
loss = bce_dice_loss(model_output, mask_target)

Workflow

  1. Compute BCE with logits loss (handles numerical stability internally)
  2. Compute soft Dice loss from sigmoid probabilities
  3. Combine with configurable weights (default 1:1)
  4. Backpropagate the combined loss

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.