MCPcopy Create free account
hub / github.com/UX-Decoder/Semantic-SAM / batch_dice_loss

Function batch_dice_loss

semantic_sam/modules/matcher.py:22–37  ·  view source on GitHub ↗

Compute the DICE loss, similar to generalized IOU for masks Args: inputs: A float tensor of arbitrary shape. The predictions for each example. targets: A float tensor with the same shape as inputs. Stores the binary classification label for e

(inputs: torch.Tensor, targets: torch.Tensor)

Source from the content-addressed store, hash-verified

20
21
22def batch_dice_loss(inputs: torch.Tensor, targets: torch.Tensor):
23 """
24 Compute the DICE loss, similar to generalized IOU for masks
25 Args:
26 inputs: A float tensor of arbitrary shape.
27 The predictions for each example.
28 targets: A float tensor with the same shape as inputs. Stores the binary
29 classification label for each element in inputs
30 (0 for the negative class and 1 for the positive class).
31 """
32 inputs = inputs.sigmoid()
33 inputs = inputs.flatten(1)
34 numerator = 2 * torch.einsum("nc,mc->nm", inputs, targets)
35 denominator = inputs.sum(-1)[:, None] + targets.sum(-1)[None, :]
36 loss = 1 - (numerator + 1) / (denominator + 1)
37 return loss
38
39
40batch_dice_loss_jit = torch.jit.script(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected