コード例 #1
0
def validate(val_loader, model, criterion, args, indices_in_1k=None):
    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        prefetcher = data_prefetcher(val_loader)
        x, y = prefetcher.next()
        idx = 0
        while x is not None:
            # compute output
            output = model(x)
            if indices_in_1k is not None:
                output = output[:, indices_in_1k]

            loss = criterion(output, y)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, y, topk=(1, 5))

            acc1 = reduce_tensor(acc1)
            acc5 = reduce_tensor(acc5)
            loss = reduce_tensor(loss)

            losses.update(loss.item(), x.size(0))
            top1.update(acc1[0], x.size(0))
            top5.update(acc5[0], x.size(0))

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

            x, y = prefetcher.next()

            if idx % args.print_freq == 0:
                logger.info(
                    f'Test: [{idx}/{len(val_loader)}]\t'
                    f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                    f'Loss {losses.val:.4f} ({losses.avg:.4f})\t'
                    f'Acc@1 {top1.val:.3%} ({top1.avg:.3%})\t'
                    f'Acc@5 {top5.val:.3%} ({top5.avg:.3%})')
            idx = idx + 1

        logger.info(f' * Acc@1 {top1.avg:.3%} Acc@5 {top5.avg:.3%}')

    return top1.avg
コード例 #2
0
def validate(val_loader, model, classifier, criterion, args):
    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()
    top5 = AverageMeter()

    # switch to evaluate mode
    model.eval()
    classifier.eval()

    with torch.no_grad():
        end = time.time()
        for idx, (x, y) in enumerate(val_loader):
            x = x.cuda(non_blocking=True)
            y = y.cuda(non_blocking=True)

            # compute output
            feat = model(x, args.layer)
            output = classifier(feat)
            loss = criterion(output, y)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, y, topk=(1, 5))

            acc1 = reduce_tensor(acc1)
            acc5 = reduce_tensor(acc5)
            loss = reduce_tensor(loss)

            losses.update(loss.item(), x.size(0))
            top1.update(acc1[0], x.size(0))
            top5.update(acc5[0], x.size(0))

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

            if idx % args.print_freq == 0:
                logger.info(
                    f'Test: [{idx}/{len(val_loader)}]\t'
                    f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                    f'Loss {losses.val:.4f} ({losses.avg:.4f})\t'
                    f'Acc@1 {top1.val:.3%} ({top1.avg:.3%})\t'
                    f'Acc@5 {top5.val:.3%} ({top5.avg:.3%})')

        logger.info(f' * Acc@1 {top1.avg:.3%} Acc@5 {top5.avg:.3%}')

    return top1.avg