def validate(loader, num_classes, device, net, scheduler, criterion):
    num_samples = 0
    running_loss = 0

    metrics = Metrics(range(num_classes))

    net.eval()

    for images, masks in tqdm.tqdm(loader):
        images = images.to(device, dtype=torch.float)
        masks = masks.to(device)

        num_samples += int(images.size(0))

        outputs = net(images)

        loss = criterion(outputs, masks)

        running_loss += loss.item()

        for mask, output in zip(masks, outputs):
            metrics.add(mask, output)

    assert num_samples > 0, "dataset contains validation images and labels"

    scheduler.step(metrics.get_miou())  # update learning rate

    return {
        "loss": running_loss / num_samples,
        "miou": metrics.get_miou(),
        "fg_iou": metrics.get_fg_iou(),
        "mcc": metrics.get_mcc(),
    }
def train(loader, num_classes, device, net, optimizer, criterion):
    num_samples = 0
    running_loss = 0

    metrics = Metrics(range(num_classes))

    net.train()
    for images, masks in tqdm.tqdm(loader):
        images = torch.squeeze(images.to(device, dtype=torch.float))
        masks = torch.squeeze(masks.to(device))
        # print("images'size:{},masks'size:{}".format(images.size(),masks.size()))

        assert images.size()[2:] == masks.size(
        )[1:], "resolutions for images and masks are in sync"

        num_samples += int(images.size(0))

        optimizer.zero_grad()
        outputs0, outputs1, outputs2, outputs3, outputs0_2, outputs1_2, outputs2_2, outputs3_2 = net(
            images)
        loss0 = criterion(outputs0, masks)
        loss1 = criterion(outputs1, masks)
        loss2 = criterion(outputs2, masks)
        loss3 = criterion(outputs3, masks)
        loss0_2 = criterion(outputs0_2, masks)
        loss1_2 = criterion(outputs1_2, masks)
        loss2_2 = criterion(outputs2_2, masks)
        loss3_2 = criterion(outputs3_2, masks)
        loss = loss0 + loss1 + loss2 + loss3 + loss0_2 + loss1_2 + loss2_2 + loss3_2
        loss.backward()
        batch_loss = loss.item()
        optimizer.step()

        running_loss += batch_loss
        outputs = (outputs0_2 + outputs1_2 + outputs2_2 + outputs3_2) / 4
        for mask, output in zip(masks, outputs):
            prediction = output.detach()
            metrics.add(mask, prediction)

    assert num_samples > 0, "dataset contains training images and labels"

    return {
        "loss": running_loss / num_samples,
        "miou": metrics.get_miou(),
        "fg_iou": metrics.get_fg_iou(),
        "mcc": metrics.get_mcc(),
    }
Exemple #3
0
def train(loader, num_classes, device, net, optimizer, criterion):
    global global_step

    num_samples = 0
    running_loss = 0

    metrics = Metrics(range(num_classes))

    net.train()
    for images, masks, dwm in tqdm.tqdm(loader):
        images = images.to(device, dtype=torch.float)
        masks = masks.to(device)
        dwm = torch.squeeze(dwm.to(device))
        # print("images'size:{},masks'size:{}".format(images.size(),masks.size()))

        num_samples += int(images.size(0))

        optimizer.zero_grad()
        outputs, dsv4, dsv3, dsv2 = net(images)

        l2 = criterion(dsv2, masks, dwm)
        l3 = criterion(dsv3, masks, dwm)
        l4 = criterion(dsv4, masks, dwm)
        loss_fuse = criterion(outputs, masks, dwm)
        loss = (l2 + l3 + l4 + loss_fuse) / 4

        loss.backward()
        batch_loss = loss.item()
        optimizer.step()

        global_step = global_step + 1

        running_loss += batch_loss

        for mask, output in zip(masks, outputs):
            prediction = output.detach()
            metrics.add(mask, prediction)

    assert num_samples > 0, "dataset contains training images and labels"

    return {
        "loss": running_loss / num_samples,
        "miou": metrics.get_miou(),
        "fg_iou": metrics.get_fg_iou(),
        "mcc": metrics.get_mcc(),
    }
def validate(loader, num_classes, device, net, scheduler, criterion):
    num_samples = 0
    running_loss = 0

    metrics = Metrics(range(num_classes))

    net.eval()

    for images, masks in tqdm.tqdm(loader):
        images = torch.squeeze(images.to(device, dtype=torch.float))
        masks = torch.squeeze(masks.to(device).long())

        assert images.size()[2:] == masks.size(
        )[1:], "resolutions for images and masks are in sync"

        num_samples += int(images.size(0))

        outputs0, outputs1, outputs2, outputs3, outputs0_2, outputs1_2, outputs2_2, outputs3_2 = net(
            images)
        loss0 = criterion(outputs0, masks)
        loss1 = criterion(outputs1, masks)
        loss2 = criterion(outputs2, masks)
        loss3 = criterion(outputs3, masks)
        loss0_2 = criterion(outputs0_2, masks)
        loss1_2 = criterion(outputs1_2, masks)
        loss2_2 = criterion(outputs2_2, masks)
        loss3_2 = criterion(outputs3_2, masks)
        loss = loss0 + loss1 + loss2 + loss3 + loss0_2 + loss1_2 + loss2_2 + loss3_2
        running_loss += loss.item()

        outputs = (outputs0_2 + outputs1_2 + outputs2_2 + outputs3_2) / 4
        for mask, output in zip(masks, outputs):
            metrics.add(mask, output)

    assert num_samples > 0, "dataset contains validation images and labels"

    scheduler.step(metrics.get_miou())  # update learning rate

    return {
        "loss": running_loss / num_samples,
        "miou": metrics.get_miou(),
        "fg_iou": metrics.get_fg_iou(),
        "mcc": metrics.get_mcc(),
    }
def train(loader, num_classes, device, net, optimizer, criterion):
    num_samples = 0
    running_loss = 0

    metrics = Metrics(range(num_classes))

    net.train()
    for images, masks in tqdm.tqdm(loader):
        images = images.to(device)
        masks = masks.to(device)

        assert images.size()[2:] == masks.size(
        )[1:], "resolutions for images and masks are in sync"

        num_samples += int(images.size(0))

        optimizer.zero_grad()
        outputs = net(images)

        assert outputs.size()[2:] == masks.size(
        )[1:], "resolutions for predictions and masks are in sync"
        assert outputs.size(
        )[1] == num_classes, "classes for predictions and dataset are in sync"

        loss = criterion(outputs, masks)
        loss.backward()

        optimizer.step()

        running_loss += loss.item()

        for mask, output in zip(masks, outputs):
            prediction = output.detach()
            metrics.add(mask, prediction)

    assert num_samples > 0, "dataset contains training images and labels"

    return {
        "loss": running_loss / num_samples,
        "miou": metrics.get_miou(),
        "fg_iou": metrics.get_fg_iou(),
        "mcc": metrics.get_mcc(),
    }
def validate(loader, num_classes, device, net, criterion):
    num_samples = 0
    running_loss = 0

    metrics = Metrics(range(num_classes))

    net.eval()

    for images, masks, in tqdm.tqdm(loader):
        images = images.to(device)
        masks = masks.to(device)

        assert images.size()[2:] == masks.size(
        )[1:], "resolutions for images and masks are in sync"

        num_samples += int(images.size(0))

        outputs = net(images)

        assert outputs.size()[2:] == masks.size(
        )[1:], "resolutions for predictions and masks are in sync"
        assert outputs.size(
        )[1] == num_classes, "classes for predictions and dataset are in sync"

        loss = criterion(outputs, masks)

        running_loss += loss.item()

        for mask, output in zip(masks, outputs):
            metrics.add(mask, output)

    assert num_samples > 0, "dataset contains validation images and labels"

    return {
        "loss": running_loss / num_samples,
        "miou": metrics.get_miou(),
        "fg_iou": metrics.get_fg_iou(),
        "mcc": metrics.get_mcc(),
    }