Esempio n. 1
0
    def forward(self, outputs, targets):
        dice = metrics.dice(outputs,
                            targets,
                            eps=self.eps,
                            activation=self.activation)

        return 1 - dice
Esempio n. 2
0
def train_iter(train_loader, model, device, criterion, optimizer,
               segmentation_weight):
    train_losses = []
    train_dices = []
    train_size = 0
    train_predicted = 0

    for i, data in enumerate(train_loader, 0):
        input = data["features"]
        targets = data["targets"]
        x = input.to(device)
        mask, season = targets
        mask = mask.to(device)
        season = season.to(device)

        optimizer.zero_grad()
        season_prediction, mask_prediction = model(x)

        segmentation_loss = criterion(mask_prediction, mask)
        season_loss = F.binary_cross_entropy(season_prediction, season)
        loss = segmentation_loss * segmentation_weight + season_loss * (
            1 - segmentation_weight)
        loss.backward()
        optimizer.step()

        season_prediction = season_prediction > 0.5
        train_size += season_prediction.size()[0]
        train_predicted += torch.sum(
            (season_prediction == season.byte())).item()

        dice_score = dice(mask_prediction, mask).item()
        train_losses.append(loss.item())
        train_dices.append(dice_score)

        print(
            "Bce_dice_loss: {:.3f}".format(segmentation_loss.mean().item()),
            '\t',
            "Bce_season: {:.3f}".format(season_loss.mean().item()),
            '\t',
            "Dice: {:.3f}".format(dice_score),
            end='\r',
            flush=True,
        )

    print()
    train_accuracy = train_predicted / train_size

    print("Train:")
    print("Loss:", round(mean(train_losses), 3), "Dice:",
          round(mean(train_dices), 3), "Accuracy:", round(train_accuracy, 3))
Esempio n. 3
0
def segmentation_head_dice(y_pred, y_true):
    """
    Computes the accuracy for a batch of predictions
    
    Args:
        y_pred (torch.Tensor): the logit predictions of the neural network.
        y_true (torch.Tensor): the ground truths.
        
    Returns:
        The average accuracy of the batch.
    """
    y_pred = y_pred[0]
    y_true = y_true[0]
    return dice(y_pred, y_true, 1e-7)
Esempio n. 4
0
def valid_iter(valid_loader, model, device, criterion, segmentation_weight):
    valid_losses = []
    valid_dices = []
    valid_size = 0
    valid_predicted = 0

    with torch.no_grad():
        for valid_idx, valid_data in enumerate(valid_loader, 0):
            input = valid_data["features"]
            targets = valid_data["targets"]
            x = input.to(device)
            mask, season = targets
            mask = mask.to(device)
            season = season.to(device)

            season_prediction, mask_prediction = model(x)

            segmentation_loss = criterion(mask_prediction, mask)
            season_loss = F.binary_cross_entropy(season_prediction, season)
            loss = segmentation_loss * segmentation_weight + season_loss * (
                1 - segmentation_weight)

            season_prediction = season_prediction > 0.5

            valid_size += season_prediction.size()[0]
            valid_predicted += torch.sum(
                (season_prediction == season.byte())).item()

            valid_losses.append(loss.item())
            valid_dices.append(dice(mask_prediction, mask).item())

        dice_mean = round(mean(valid_dices), 3)
        valid_accuracy = valid_predicted / valid_size
        print("Validation:")
        print("Loss:", round(mean(valid_losses), 3), "Dice:", dice_mean,
              "Accuracy:", round(valid_accuracy, 3))

    return dice_mean, valid_accuracy
Esempio n. 5
0
def dice_loss(pred, target, eps=1.):
    return 1 - dice(pred, target, eps)