Example #1
0
def train_epoch(model, optimizer, loader, alpha=0.75):
    model.train()
    epoch_loss, correct, total = 0, 0, 0
    res_name, res_prob, res_label = [], [], []
    for i, sample in enumerate(loader):
        name, image, feats, label = sample[0], sample[1], sample[2], sample[3]
        image, feats, label = image.cuda(), feats.cuda(), label.cuda()
        label = label.unsqueeze(1).float()
        out = model(image, feats)
        loss = sigmoid_focal_loss(out,
                                  label,
                                  alpha,
                                  gamma=2.0,
                                  reduction="mean")
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        epoch_loss += loss.item()
        total += label.size(0)
        out = torch.sigmoid(out)
        correct += ((out > 0.5).int() == label).sum().item()
        res_prob += out.detach().cpu().numpy().tolist()
        res_label += label.detach().cpu().numpy().tolist()
    y_true = np.array(res_label)
    y_pred = np.array(res_prob)
    auc = roc_auc_score(y_true, y_pred)
    ap = average_precision_score(y_true, y_pred)
    acc = balanced_accuracy_score(y_true, (y_pred > 0.5).astype(int))
    print("Train Loss: {}, Accuracy: {}, AUC: {}".format(
        round(epoch_loss, 4), round(acc, 4), round(auc, 4)))
    return epoch_loss, image
Example #2
0
def val_epoch(model, loader, alpha=0.75):
    model.eval()
    with torch.no_grad():
        epoch_loss, correct, total = 0, 0, 0
        y_name, y_pred, y_true = [], [], []
        for i, sample in enumerate(loader):
            name, image, feats, label = sample[0], sample[1].cuda(
            ), sample[2].cuda(), sample[3].cuda()
            label = label.unsqueeze(1).float()
            if USE_TTA:
                batch_size, n_crops, c, h, w = image.size()
                image = image.view(-1, c, h, w)
                if FEATS:
                    _, n_feats = feats.size()
                    feats = feats.repeat(1, n_crops).view(-1, n_feats)
                out = model(image, feats)
                out = out.view(batch_size, n_crops, -1).mean(1)
            else:
                out = model(image, feats)
            loss = sigmoid_focal_loss(out,
                                      label,
                                      alpha,
                                      gamma=2.0,
                                      reduction="mean")
            epoch_loss += loss.item()
            total += label.size(0)
            out = torch.sigmoid(out)
            correct += ((out > 0.5).int() == label).sum().item()
            y_pred += out.detach().cpu().numpy().tolist()
            y_true += label.detach().cpu().numpy().tolist()
            y_name += name
        y_pred = np.array([x[0] for x in y_pred])
        y_true = np.array([x[0] for x in y_true])
        auc = roc_auc_score(y_true, y_pred)
        ap = average_precision_score(y_true, y_pred)
        acc = balanced_accuracy_score(y_true, (y_pred > 0.5).astype(int))
        print("Val Loss: {}, Accuracy: {}, AUC: {}".format(
            round(epoch_loss, 4), round(acc, 4), round(auc, 4)))
    return y_pred, y_true, y_name, auc, acc, image
Example #3
0
        correct = 0
        total = 0
        res_name = []
        res_prob = []
        res_label = []
        for i, sample in enumerate(train_loader):
            images, names, labels = sample[0], sample[1], sample[2]
            #print(images.shape, labels.shape)
            images = images.cuda()
            labels = labels.cuda()
            labels = labels.unsqueeze(1).float()
            out = model(images)
            #loss = criterion(out, labels)
            loss = sigmoid_focal_loss(out,
                                      labels,
                                      alpha=alpha,
                                      gamma=gamma,
                                      reduction="mean")

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            running_loss += loss.item()

            total += labels.size(0)
            out = torch.sigmoid(out)
            correct += ((out > 0.5).int() == labels).sum().item()
            print("iter: {}, Loss: {}".format(i, loss.item()))

            res_prob += out.detach().cpu().numpy().tolist()
            res_label += labels.detach().cpu().numpy().tolist()
                target_a = labels
                target_b = labels[rand_index]
                features_a = features
                features_b = features[rand_index]
                bbx1, bby1, bbx2, bby2 = rand_bbox(images.size(), lam)
                images[:, :, bbx1:bbx2, bby1:bby2] = images[rand_index, :, bbx1:bbx2, bby1:bby2]
                # adjust lambda to exactly match pixel ratio
                lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (images.size()[-1] * images.size()[-2]))
                features = features_a * lam + features_b * (1. - lam)
                # compute output
                if FEATURES:
                    out = model(images, features)
                else:
                    out = model(images)
                #loss = criterion(out, target_a) * lam + criterion(out, target_b) * (1. - lam)
                loss = sigmoid_focal_loss(out, target_a, alpha, gamma, reduction="mean") * lam + \
                       sigmoid_focal_loss(out, target_b, alpha, gamma, reduction="mean") * (1. - lam)

            else:
                if FEATURES:
                    out = model(images, features)
                else:
                    out = model(images)
                #loss = criterion(out, labels)
                loss = sigmoid_focal_loss(out, labels, alpha=alpha, gamma=gamma, reduction="mean")

            optimizer.zero_grad()
            loss.backward()

            optimizer.step()
            running_loss += loss.item()
Example #5
0
                rand_index = torch.randperm(image1.size()[0]).cuda()
                target_a = labels
                target_b = labels[rand_index]
                features_a = features
                features_b = features[rand_index]
                bbx1, bby1, bbx2, bby2 = rand_bbox(image1.size(), lam)
                image1[:, :, bbx1:bbx2,
                       bby1:bby2] = image1[rand_index, :, bbx1:bbx2, bby1:bby2]
                # adjust lambda to exactly match pixel ratio
                lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) /
                           (image1.size()[-1] * image1.size()[-2]))
                features = features_a * lam + features_b * (1. - lam)
                # compute output
                out = model(image1, feats1, image2, feats2)
                #loss = criterion(out, target_a) * lam + criterion(out, target_b) * (1. - lam)
                loss = sigmoid_focal_loss(out, target_a, ALPHA, GAMMA, reduction="mean") * lam + \
                       sigmoid_focal_loss(out, target_b, ALPHA, GAMMA, reduction="mean") * (1. - lam)

            else:
                out = model(image1, feats1, image2, feats2)
                #loss = criterion(out, labels)
                loss = sigmoid_focal_loss(out,
                                          labels,
                                          alpha=ALPHA,
                                          gamma=GAMMA,
                                          reduction="mean")

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            running_loss += loss.item()