示例#1
0
文件: test.py 项目: johndpope/SAN-1
def validate(val_loader, model, criterion):
    logger.info('>>>>>>>>>>>>>>>> Start Evaluation >>>>>>>>>>>>>>>>')
    batch_time = AverageMeter()
    data_time = AverageMeter()
    loss_meter = AverageMeter()
    intersection_meter = AverageMeter()
    union_meter = AverageMeter()
    target_meter = AverageMeter()
    top1_meter = AverageMeter()
    top5_meter = AverageMeter()

    model.eval()
    end = time.time()
    for i, (input, target) in enumerate(val_loader):
        data_time.update(time.time() - end)
        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)

        with torch.no_grad():
            output = model(input)
        loss = criterion(output, target)
        top1, top5 = cal_accuracy(output, target, topk=(1, 5))
        n = input.size(0)
        loss_meter.update(loss.item(), n), top1_meter.update(top1.item(), n), top5_meter.update(top5.item(), n)

        output = output.max(1)[1]
        intersection, union, target = intersectionAndUnionGPU(output, target, args.classes, args.ignore_label)
        intersection, union, target = intersection.cpu().numpy(), union.cpu().numpy(), target.cpu().numpy()
        intersection_meter.update(intersection), union_meter.update(union), target_meter.update(target)

        accuracy = sum(intersection_meter.val) / (sum(target_meter.val) + 1e-10)
        batch_time.update(time.time() - end)
        end = time.time()

        if (i + 1) % args.print_freq == 0:
            logger.info('Test: [{}/{}] '
                        'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                        'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}) '
                        'Loss {loss_meter.val:.4f} ({loss_meter.avg:.4f}) '
                        'Accuracy {accuracy:.4f} '
                        'Acc@1 {top1.val:.3f} ({top1.avg:.3f}) '
                        'Acc@5 {top5.val:.3f} ({top5.avg:.3f}).'.format(i + 1, len(val_loader),
                                                                        data_time=data_time,
                                                                        batch_time=batch_time,
                                                                        loss_meter=loss_meter,
                                                                        accuracy=accuracy,
                                                                        top1=top1_meter,
                                                                        top5=top5_meter))

    iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
    accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
    mIoU = np.mean(iou_class)
    mAcc = np.mean(accuracy_class)
    allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

    logger.info('Val result: mIoU/mAcc/allAcc/top1/top5 {:.4f}/{:.4f}/{:.4f}/{:.4f}/{:.4f}.'.format(mIoU, mAcc, allAcc, top1_meter.avg, top5_meter.avg))
    for i in range(args.classes):
        logger.info('Class_{} Result: iou/accuracy {:.4f}/{:.4f}.'.format(i, iou_class[i], accuracy_class[i]))
    logger.info('<<<<<<<<<<<<<<<<< End Evaluation <<<<<<<<<<<<<<<<<')
    return loss_meter.avg, mIoU, mAcc, allAcc, top1_meter.avg, top5_meter.avg
示例#2
0
def triplet_train(train_loader, model, criterion, optimizer, epoch,
                  classif_criterion):
    iter_time = AverageMeter()
    data_time = AverageMeter()
    transfer_time = AverageMeter()
    batch_time = AverageMeter()
    metric_time = AverageMeter()
    loss_meter = AverageMeter()
    loss_sketch_meter = AverageMeter()
    loss_positive_meter = AverageMeter()
    loss_negative_meter = AverageMeter()
    loss_triplet_meter = AverageMeter()
    top1_meter_sketch = AverageMeter()
    top1_meter_positive = AverageMeter()
    top1_meter_negative = AverageMeter()

    model.train()
    end = time.time()
    sum_epochs = args.contras_epochs + args.triplet_epochs
    max_iter = sum_epochs * len(train_loader)
    for i, (sketch_input, positive_input, negative_input, sketch_target,
            negative_target) in enumerate(train_loader):
        data_time.update(time.time() - end)
        if args.time_breakdown:
            last = time.time()

        sketch_input = sketch_input.cuda(non_blocking=True)
        positive_input = positive_input.cuda(non_blocking=True)
        negative_input = negative_input.cuda(non_blocking=True)
        sketch_target = sketch_target.cuda(non_blocking=True)
        negative_target = negative_target.cuda(non_blocking=True)
        if args.time_breakdown:
            torch.cuda.synchronize()
            transfer_time.update(time.time() - last)
            last = time.time()

        (sketchFeatVec, positiveFeatVec,
         negativeFeatVec), (sketch_output, positive_output,
                            negative_output) = model(sketch_input,
                                                     positive_input,
                                                     negative_input)
        loss_sketch = smooth_loss(
            sketch_output, sketch_target, args.label_smoothing
        ) if args.label_smoothing else classif_criterion(
            sketch_output, sketch_target)
        loss_positive = smooth_loss(
            positive_output, sketch_target, args.label_smoothing
        ) if args.label_smoothing else classif_criterion(
            positive_output, sketch_target)
        loss_negative = smooth_loss(
            negative_output, negative_target, args.label_smoothing
        ) if args.label_smoothing else classif_criterion(
            negative_output, negative_target)
        sketchFeatVec = F.normalize(sketchFeatVec)
        positiveFeatVec = F.normalize(positiveFeatVec)
        negativeFeatVec = F.normalize(negativeFeatVec)
        loss_triplet = criterion(sketchFeatVec, positiveFeatVec,
                                 negativeFeatVec)

        loss_triplet, loss_sketch, loss_positive, loss_negative = loss_triplet, loss_sketch * 0.5, loss_positive * 0.25, loss_negative * 0.25
        classif_decay = 0.5 * (1 + math.cos(epoch * math.pi / sum_epochs))
        loss = 2.0 * loss_triplet * epoch + classif_decay * (
            loss_sketch + loss_positive + loss_negative)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if args.time_breakdown:
            torch.cuda.synchronize()
            batch_time.update(time.time() - last)
            last = time.time()

        n = sketch_input.size(0)
        if args.multiprocessing_distributed:
            with torch.no_grad():
                loss, loss_triplet, loss_sketch, loss_positive, loss_negative = loss.detach(
                ) * n, loss_triplet.detach() * n, loss_sketch.detach(
                ) * n, loss_positive.detach() * n, loss_negative.detach() * n
                count = sketch_target.new_tensor([n], dtype=torch.long)
                dist.all_reduce(loss), dist.all_reduce(count), dist.all_reduce(
                    loss_triplet), dist.all_reduce(
                        loss_sketch), dist.all_reduce(
                            loss_positive), dist.all_reduce(loss_negative)
                n = count.item()
                loss, loss_triplet, loss_sketch, loss_positive, loss_negative = loss / n, loss_triplet / n, loss_sketch / n, loss_positive / n, loss_negative / n
        loss_meter.update(loss.item(), n)
        loss_triplet_meter.update(loss_triplet.item(), n)
        loss_sketch_meter.update(loss_sketch.item(), n)
        loss_positive_meter.update(loss_positive.item(), n)
        loss_negative_meter.update(loss_negative.item(), n)

        # classification metrics
        top1_s, = cal_accuracy(sketch_output, sketch_target, topk=(1, ))
        top1_p, = cal_accuracy(positive_output, sketch_target, topk=(1, ))
        top1_n, = cal_accuracy(negative_output, negative_target, topk=(1, ))
        n = sketch_input.size(0)
        if args.multiprocessing_distributed:
            with torch.no_grad():
                top1_s = top1_s * n
                top1_p = top1_p * n
                top1_n = top1_n * n
                count = sketch_target.new_tensor([n], dtype=torch.long)
                dist.all_reduce(top1_s), dist.all_reduce(
                    top1_p), dist.all_reduce(top1_n), dist.all_reduce(count)
                n = count.item()
                top1_s = top1_s / n
                top1_p = top1_p / n
                top1_n = top1_n / n
        top1_meter_sketch.update(top1_s.item(), n), top1_meter_positive.update(
            top1_p.item(), n), top1_meter_negative.update(top1_n.item(), n)

        if args.time_breakdown:
            torch.cuda.synchronize()
            metric_time.update(time.time() - last)
        iter_time.update(time.time() - end)
        end = time.time()

        # calculate remain time
        current_iter = epoch * len(train_loader) + i + 1
        remain_iter = max_iter - current_iter
        remain_time = remain_iter * iter_time.avg
        t_m, t_s = divmod(remain_time, 60)
        t_h, t_m = divmod(t_m, 60)
        remain_time = '{:02d}:{:02d}:{:02d}'.format(int(t_h), int(t_m),
                                                    int(t_s))

        if ((i + 1) % args.print_freq == 0) and main_process():
            logger.info((
                'Epoch: [{}/{}][{}/{}] '
                'Time {iter_time.val:.3f} ({iter_time.avg:.3f}) '
                'Data {data_time.val:.3f} ({data_time.avg:.3f}) ' +
                ('Transfer {transfer_time.val:.3f} ({transfer_time.avg:.3f}) '
                 'Batch {batch_time.val:.4f} ({batch_time.avg:.4f}) '
                 'Metric {metric_time.val:.3f} ({metric_time.avg:.3f}) '
                 if args.time_breakdown else '') + 'Remain {remain_time} '
                'Loss total/triplet/classifSketch/classifPos/classifNeg '
                '{loss_meter.val:.4f}/{triplet_loss_meter.val:.4f}/{sketch_loss_meter.val:.4f}/{positive_loss_meter.val:.4f}/{negative_loss_meter.val:.4f} '
                'Top1classif sketch/positive/negative {top1_s.val:.3f}/{top1_p.val:.3f}/{top1_n.val:.3f}'
            ).format(epoch + 1,
                     sum_epochs,
                     i + 1,
                     len(train_loader),
                     iter_time=iter_time,
                     data_time=data_time,
                     transfer_time=transfer_time,
                     batch_time=batch_time,
                     metric_time=metric_time,
                     remain_time=remain_time,
                     triplet_loss_meter=loss_triplet_meter,
                     sketch_loss_meter=loss_sketch_meter,
                     positive_loss_meter=loss_positive_meter,
                     negative_loss_meter=loss_negative_meter,
                     loss_meter=loss_meter,
                     top1_s=top1_meter_sketch,
                     top1_p=top1_meter_positive,
                     top1_n=top1_meter_negative))

    if main_process():
        logger.info(
            'Train result at epoch [{}/{}]: Loss total/triplet/classifSketch/classifPos/classifNeg {:.4f}/{:.4f}/{:.4f}/{:.4f}/{:.4f}. '
            'Top1classif sketch/positive/negative {top1_s.avg:.3f}/{top1_p.avg:.3f}/{top1_n.avg:.3f}'
            .format(epoch + 1,
                    sum_epochs,
                    loss_meter.avg,
                    loss_triplet_meter.avg,
                    loss_sketch_meter.avg,
                    loss_positive_meter.avg,
                    loss_negative_meter.avg,
                    top1_s=top1_meter_sketch,
                    top1_p=top1_meter_positive,
                    top1_n=top1_meter_negative))
    return loss_meter.avg
示例#3
0
def train(train_loader, model, criterion, optimizer, epoch):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    loss_meter = AverageMeter()
    intersection_meter = AverageMeter()
    union_meter = AverageMeter()
    target_meter = AverageMeter()
    top1_meter = AverageMeter()
    top5_meter = AverageMeter()

    model.train()
    # print(get_parameter_number(model))
    # exit(0)
    end = time.time()
    max_iter = args.epochs * len(train_loader)
    for i, (input, target) in enumerate(train_loader):
        data_time.update(time.time() - end)
        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)
        if args.mixup_alpha:
            eps = args.label_smoothing if args.label_smoothing else 0.0
            input, target_a, target_b, lam = mixup_data(
                input, target, args.mixup_alpha)
            output = model(input)
            loss = mixup_loss(output, target_a, target_b, lam, eps)
        else:
            output = model(input)
            loss = smooth_loss(
                output, target,
                args.label_smoothing) if args.label_smoothing else criterion(
                    output, target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        top1, top5 = cal_accuracy(output, target, topk=(1, 5))
        n = input.size(0)
        if args.multiprocessing_distributed:
            with torch.no_grad():
                loss, top1, top5 = loss.detach() * n, top1 * n, top5 * n
                count = target.new_tensor([n], dtype=torch.long)
                dist.all_reduce(loss), dist.all_reduce(top1), dist.all_reduce(
                    top5), dist.all_reduce(count)
                n = count.item()
                loss, top1, top5 = loss / n, top1 / n, top5 / n
        loss_meter.update(loss.item(), n), top1_meter.update(
            top1.item(), n), top5_meter.update(top5.item(), n)

        output = output.max(1)[1]
        intersection, union, target = intersectionAndUnionGPU(
            output, target, args.classes, args.ignore_label)
        if args.multiprocessing_distributed:
            dist.all_reduce(intersection), dist.all_reduce(
                union), dist.all_reduce(target)
        intersection, union, target = intersection.cpu().numpy(), union.cpu(
        ).numpy(), target.cpu().numpy()
        intersection_meter.update(intersection), union_meter.update(
            union), target_meter.update(target)
        accuracy = sum(
            intersection_meter.val) / (sum(target_meter.val) + 1e-10)
        batch_time.update(time.time() - end)
        end = time.time()

        # calculate remain time
        current_iter = epoch * len(train_loader) + i + 1
        remain_iter = max_iter - current_iter
        remain_time = remain_iter * batch_time.avg
        t_m, t_s = divmod(remain_time, 60)
        t_h, t_m = divmod(t_m, 60)
        remain_time = '{:02d}:{:02d}:{:02d}'.format(int(t_h), int(t_m),
                                                    int(t_s))

        if ((i + 1) % args.print_freq == 0) and main_process():
            logger.info('Epoch: [{}/{}][{}/{}] '
                        'Data {data_time.val:.3f} ({data_time.avg:.3f}) '
                        'Batch {batch_time.val:.3f} ({batch_time.avg:.3f}) '
                        'Remain {remain_time} '
                        'Loss {loss_meter.val:.4f} '
                        'Accuracy {accuracy:.4f} '
                        'Acc@1 {top1.val:.3f} ({top1.avg:.3f}) '
                        'Acc@5 {top5.val:.3f} ({top5.avg:.3f}).'.format(
                            epoch + 1,
                            args.epochs,
                            i + 1,
                            len(train_loader),
                            data_time=data_time,
                            batch_time=batch_time,
                            remain_time=remain_time,
                            loss_meter=loss_meter,
                            accuracy=accuracy,
                            top1=top1_meter,
                            top5=top5_meter))
        if main_process():
            writer.add_scalar('train/loss', loss_meter.val, current_iter)
            writer.add_scalar('train/mIoU',
                              np.mean(intersection / (union + 1e-10)),
                              current_iter)
            writer.add_scalar('train/mAcc',
                              np.mean(intersection / (target + 1e-10)),
                              current_iter)
            writer.add_scalar('train/allAcc', accuracy, current_iter)
            writer.add_scalar('train/top1', top1, current_iter)
            writer.add_scalar('train/top5', top5, current_iter)

    iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
    accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
    mIoU = np.mean(iou_class)
    mAcc = np.mean(accuracy_class)
    allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)
    if main_process():
        logger.info(
            'Train result at epoch [{}/{}]: mIoU/mAcc/allAcc/top1/top5 {:.4f}/{:.4f}/{:.4f}/{:.4f}/{:.4f}.'
            .format(epoch + 1, args.epochs, mIoU, mAcc, allAcc, top1_meter.avg,
                    top5_meter.avg))
    return loss_meter.avg, mIoU, mAcc, allAcc, top1_meter.avg, top5_meter.avg