コード例 #1
0
def validate(valid_loader, model, criterion, epoch):
    start = time.strftime("%H:%M:%S")
    print("\n")
    print(f"Starting epoch: {epoch} | phase: Valid | ⏰: {start}")
    batch_time = AverageMeter()
    losses = AverageMeter()
    dices = AverageMeter()

    # switch to evaluate mode
    model.eval()

    end = time.time()
    for it, iter_data in enumerate(valid_loader, 0):
        images, masks = iter_data
        images, masks = images.to(device), masks.to(device)

        outputs = model(images)
        #if lossfnc != None:
        #    loss = criterion(outputs, masks, epoch=epoch)
        #else:
        loss = criterion(outputs, masks)
        probs = F.sigmoid(outputs)
        dice = metric(probs, masks)

        losses.update(loss.item())
        dices.update(dice.item())

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

    return losses.avg, dices.avg
コード例 #2
0
def train(train_loader, model, criterion, optimizer, epoch, lr=1e-5):
    start = time.strftime("%H:%M:%S")
    print("\n")
    print(f"Starting epoch: {epoch} | phase: Train | ⏰: {start}")   
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    dices = AverageMeter()
    # switch to train mode
    model.train()
    num_its = len(train_loader)
    end = time.time()
    iter = 0
    print_freq = 1
    # start = time.strftime("%H:%M:%S")
    # print(f"Starting epoch: {epoch} | phase: {phase} | ⏰: {start}")   
    for iter, iter_data in enumerate(train_loader, 0):
        # measure data loading time
        data_time.update(time.time() - end)
        images, masks = iter_data
        images, masks = images.to(device), masks.to(device)
        outputs = model(images)
        #if lossfnc != None:
        #    loss = criterion(outputs, masks, epoch=epoch)
        #else:
        loss = criterion(outputs, masks)
        
        losses.update(loss.item())
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
        probs = F.sigmoid(outputs)
        dice = metric(probs, masks)
        dices.update(dice.item())
        if (iter + 1) % print_freq == 0 or iter == 0 or (iter + 1) == num_its:
            print('\r%5.1f   %5d    %0.6f   |  %0.4f  %0.4f  | ... ' % \
                  (epoch - 1 + (iter + 1) / num_its, iter + 1, lr, losses.avg, dices.avg), \
                  end='', flush=True)
    return iter, losses.avg, dices.avg
コード例 #3
0
def validate(valid_loader, model, criterion, epoch):
    start = time.strftime("%H:%M:%S")
    print("\n")
    print(f"Starting epoch: {epoch} | phase: Valid | ⏰: {start}")
    batch_time = AverageMeter()
    losses = AverageMeter()
    dices = AverageMeter()
    # switch to evaluate mode
    model.eval()
    end = time.time()

    num_its = len(valid_loader)
    print_freq = 1
    for iter, iter_data in enumerate(valid_loader, 0):
        images, masks = iter_data
        images, masks = images.to(device), masks.to(device)

        outputs = model(images)
        if lossfnc == "SymmetricLovaszLoss":
            loss = criterion(outputs, masks, epoch=epoch)
        else:
            loss = criterion(outputs, masks)
        probs = F.sigmoid(outputs)
        dice = metric(probs, masks)

        losses.update(loss.item())
        dices.update(dice.item())

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()
        if (iter + 1) % print_freq == 0 or iter == 0 or (iter + 1) == num_its:
            print('\r%5.1f   %5d            |  %0.4f  %0.4f  | ... ' % \
                  (epoch - 1 + (iter + 1) / num_its, iter + 1, losses.avg, dices.avg), \
                  end='', flush=True)
    return losses.avg, dices.avg