MCPcopy Create free account
hub / github.com/SooLab/CGFormer / dice_loss

Function dice_loss

model/segmenter.py:7–24  ·  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, targets)

Source from the content-addressed store, hash-verified

5from bert.modeling_bert import BertModel
6
7def dice_loss(inputs, targets):
8 """
9 Compute the DICE loss, similar to generalized IOU for masks
10 Args:
11 inputs: A float tensor of arbitrary shape.
12 The predictions for each example.
13 targets: A float tensor with the same shape as inputs. Stores the binary
14 classification label for each element in inputs
15 (0 for the negative class and 1 for the positive class).
16 """
17
18 inputs = inputs.sigmoid()
19 inputs = inputs.flatten(1)
20 targets = targets.flatten(1)
21 numerator = 2 * (inputs * targets).sum(1)
22 denominator = inputs.sum(-1) + targets.sum(-1)
23 loss = 1 - (numerator + 1) / (denominator + 1)
24 return loss.mean()
25
26def sigmoid_focal_loss(inputs, targets, alpha: float = 0.25, gamma: float = 2):
27 """

Callers 1

forwardMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected