MCPcopy Index your code
hub / github.com/OpenGVLab/InternVL / validate

Function validate

classification/main.py:605–672  ·  view source on GitHub ↗
(config, data_loader, model, epoch=None, amp_autocast=suppress)

Source from the content-addressed store, hash-verified

603
604@torch.no_grad()
605def validate(config, data_loader, model, epoch=None, amp_autocast=suppress):
606 criterion = torch.nn.CrossEntropyLoss()
607 model.eval()
608
609 batch_time = AverageMeter()
610 loss_meter = AverageMeter()
611 acc1_meter = AverageMeter()
612 acc5_meter = AverageMeter()
613
614 end = time.time()
615 amp_type = torch.float16 if config.AMP_TYPE == 'float16' else torch.bfloat16
616 for idx, (images, target) in enumerate(data_loader):
617 images = images.cuda(non_blocking=True)
618 target = target.cuda(non_blocking=True)
619 if not obsolete_torch_version(TORCH_VERSION, (1, 9)) and config.AMP_OPT_LEVEL != 'O0':
620 with amp_autocast(dtype=amp_type):
621 output = model(images)
622 else:
623 with amp_autocast():
624 output = model(images)
625
626 # convert 22k to 1k to evaluate
627 if output.size(-1) == 21841:
628 convert_file = './meta_data/map22kto1k.txt'
629 with open(convert_file, 'r') as f:
630 convert_list = [int(line) for line in f.readlines()]
631 output = output[:, convert_list]
632
633 if config.DATA.DATASET == 'imagenet_a':
634 from dataset.imagenet_a_r_indices import imagenet_a_mask
635 output = output[:, imagenet_a_mask]
636 elif config.DATA.DATASET == 'imagenet_r':
637 from dataset.imagenet_a_r_indices import imagenet_r_mask
638 output = output[:, imagenet_r_mask]
639
640 # measure accuracy and record loss
641 loss = criterion(output, target)
642 acc1, acc5 = accuracy(output, target, topk=(1, 5))
643
644 acc1 = reduce_tensor(acc1)
645 acc5 = reduce_tensor(acc5)
646 loss = reduce_tensor(loss)
647
648 loss_meter.update(loss.item(), target.size(0))
649 acc1_meter.update(acc1.item(), target.size(0))
650 acc5_meter.update(acc5.item(), target.size(0))
651
652 # measure elapsed time
653 batch_time.update(time.time() - end)
654 end = time.time()
655
656 if idx % config.PRINT_FREQ == 0:
657 memory_used = torch.cuda.max_memory_allocated() / (1024.0 * 1024.0)
658 logger.info(f'Test: [{idx}/{len(data_loader)}]\t'
659 f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
660 f'Loss {loss_meter.val:.4f} ({loss_meter.avg:.4f})\t'
661 f'Acc@1 {acc1_meter.val:.3f} ({acc1_meter.avg:.3f})\t'
662 f'Acc@5 {acc5_meter.val:.3f} ({acc5_meter.avg:.3f})\t'

Callers 1

mainFunction · 0.85

Calls 4

reduce_tensorFunction · 0.90
obsolete_torch_versionFunction · 0.85
accuracyFunction · 0.85
updateMethod · 0.80

Tested by

no test coverage detected