Project Name: qubvel+segmentation_models.pytorch
Language: Python
Statement Type: API

Source Code:
def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:

        assert y_true.size(0) == y_pred.size(0)

        if self.from_logits:
            # Apply activations to get [0..1] class probabilities
            # Using Log-Exp as this gives more numerically stable result and does not cause vanishing gradient on
            # extreme values 0 and 1
            if self.mode == MULTICLASS_MODE:
                y_pred = y_pred.log_softmax(dim=1).exp()
            else:
                y_pred = F.logsigmoid(y_pred).exp()

        bs = y_true.size(0)
        num_classes = y_pred.size(1)
        dims = (0, 2)

        if self.mode == BINARY_MODE:
            y_true = y_true.view(bs, 1, -1)
            y_pred = y_pred.view(bs, 1, -1)

            if self.ignore_index is not None:
                mask = y_true != self.ignore_index
                y_pred = y_pred * mask
                y_true = y_true * mask

        if self.mode == MULTICLASS_MODE:
            y_true = y_true.view(bs, -1)
            y_pred = y_pred.view(bs, num_classes, -1)

            if self.ignore_index is not None:
                mask = y_true != self.ignore_index
                y_pred = y_pred * mask.unsqueeze(1)

                y_true = F.one_hot((y_true * mask).to(torch.long), num_classes)  # N,H*W -> N,H*W, C
                y_true = y_true.permute(0, 2, 1) * mask.unsqueeze(1)  # N, C, H*W
            else:
                y_true = F.one_hot(y_true, num_classes)  # N,H*W -> N,H*W, C
                y_true = y_true.permute(0, 2, 1)  # N, C, H*W

        if self.mode == MULTILABEL_MODE:
            y_true = y_true.view(bs, num_classes, -1)
            y_pred = y_pred.view(bs, num_classes, -1)

            if self.ignore_index is not None:
                mask = y_true != self.ignore_index
                y_pred = y_pred * mask
                y_true = y_true * mask

        scores = self.compute_score(y_pred, y_true.type_as(y_pred), smooth=self.smooth, eps=self.eps, dims=dims)

        if self.log_loss:
            loss = -torch.log(scores.clamp_min(self.eps))
        else:
            loss = 1.0 - scores

        # Dice loss is undefined for non-empty classes
        # So we zero contribution of channel that does not have true pixels
        # NOTE: A better workaround would be to use loss term `mean(y_pred)`
        # for this case, however it will be a modified jaccard loss

        mask = y_true.sum(dims) > 0
        loss *= mask.to(loss.dtype)

        if self.classes is not None:
            loss = loss[self.classes]

        return self.aggregate_loss(loss)

forward(self=DiceLoss(), y_pred=tensor([[[[1., 1., 1.]]]]), y_true=tensor([[[[1, 1, 1]]]]), self._backward_hooks=OrderedDict(), self._backward_pre_hooks=OrderedDict(), self._buffers=OrderedDict(), self._forward_hooks=OrderedDict(), self._forward_hooks_always_called=OrderedDict(), self._forward_hooks_with_kwargs=OrderedDict(), self._forward_pre_hooks=OrderedDict(), self._forward_pre_hooks_with_kwargs=OrderedDict(), self._is_full_backward_hook=None, self._load_state_dict_post_hooks=OrderedDict(), self._load_state_dict_pre_hooks=OrderedDict(), self._modules=OrderedDict(), self._non_persistent_buffers_set=set(), self._parameters=OrderedDict(), self._state_dict_hooks=OrderedDict(), self._state_dict_pre_hooks=OrderedDict(), self.classes=None, self.eps=1e-07, self.from_logits=False, self.ignore_index=None, self.log_loss=False, self.mode='binary', self.reduction='mean', self.smooth=0.0, self.training=True)

Selected Statement: mask = y_true.sum(dims) > 0

Function Input:
self: DiceLoss()
y_pred: tensor([[[[1., 1., 1.]]]])
y_true: tensor([[[[1, 1, 1]]]])
self._backward_hooks: OrderedDict()
self._backward_pre_hooks: OrderedDict()
self._buffers: OrderedDict()
self._forward_hooks: OrderedDict()
self._forward_hooks_always_called: OrderedDict()
self._forward_hooks_with_kwargs: OrderedDict()
self._forward_pre_hooks: OrderedDict()
self._forward_pre_hooks_with_kwargs: OrderedDict()
self._is_full_backward_hook: None
self._load_state_dict_post_hooks: OrderedDict()
self._load_state_dict_pre_hooks: OrderedDict()
self._modules: OrderedDict()
self._non_persistent_buffers_set: set()
self._parameters: OrderedDict()
self._state_dict_hooks: OrderedDict()
self._state_dict_pre_hooks: OrderedDict()
self.classes: None
self.eps: 1e-07
self.from_logits: False
self.ignore_index: None
self.log_loss: False
self.mode: 'binary'
self.reduction: 'mean'
self.smooth: 0.0
self.training: True

Variable Values Before Statement:
dims: (0, 2)

Value After Statement Execution: tensor([True])

Variable States During Runtime (line, value):
self: 1
DiceLoss()
y_pred: 1
tensor([[[[1., 1., 1.]]]])
20
tensor([[[1., 1., 1.]]])
y_true: 1
tensor([[[[1, 1, 1]]]])
19
tensor([[[1, 1, 1]]])
self._backward_hooks: 1
OrderedDict()
self._backward_pre_hooks: 1
OrderedDict()
self._buffers: 1
OrderedDict()
self._forward_hooks: 1
OrderedDict()
self._forward_hooks_always_called: 1
OrderedDict()
self._forward_hooks_with_kwargs: 1
OrderedDict()
self._forward_pre_hooks: 1
OrderedDict()
self._forward_pre_hooks_with_kwargs: 1
OrderedDict()
self._is_full_backward_hook: 1
None
self._load_state_dict_post_hooks: 1
OrderedDict()
self._load_state_dict_pre_hooks: 1
OrderedDict()
self._modules: 1
OrderedDict()
self._non_persistent_buffers_set: 1
set()
self._parameters: 1
OrderedDict()
self._state_dict_hooks: 1
OrderedDict()
self._state_dict_pre_hooks: 1
OrderedDict()
self.classes: 1
None
self.eps: 1
1e-07
self.from_logits: 1
False
self.ignore_index: 1
None
self.log_loss: 1
False
self.mode: 1
'binary'
self.reduction: 1
'mean'
self.smooth: 1
0.0
self.training: 1
True
bs: 14
1
num_classes: 15
1
dims: 16
(0, 2)
scores: 50
tensor([1.])
loss: 55
tensor([0.])
mask: 62
tensor([True])