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)
| 20 | |
| 21 | |
| 22 | def 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 | |
| 40 | batch_dice_loss_jit = torch.jit.script( |
nothing calls this directly
no outgoing calls
no test coverage detected