MCPcopy Create free account
hub / github.com/pytorch/examples / validate

Function validate

imagenet/main.py:358–426  ·  view source on GitHub ↗
(val_loader, model, criterion, args)

Source from the content-addressed store, hash-verified

356
357
358def validate(val_loader, model, criterion, args):
359
360 use_accel = not args.no_accel and torch.accelerator.is_available()
361
362 def run_validate(loader, base_progress=0):
363
364 if use_accel:
365 device = torch.accelerator.current_accelerator()
366 else:
367 device = torch.device("cpu")
368
369 with torch.no_grad():
370 end = time.time()
371 for i, (images, target) in enumerate(loader):
372 i = base_progress + i
373 if use_accel:
374 if args.gpu is not None and device.type=='cuda':
375 torch.accelerator.set_device_index(args.gpu)
376 images = images.cuda(args.gpu, non_blocking=True)
377 target = target.cuda(args.gpu, non_blocking=True)
378 else:
379 images = images.to(device)
380 target = target.to(device)
381
382 # compute output
383 output = model(images)
384 loss = criterion(output, target)
385
386 # measure accuracy and record loss
387 acc1, acc5 = accuracy(output, target, topk=(1, 5))
388 losses.update(loss.item(), images.size(0))
389 top1.update(acc1[0], images.size(0))
390 top5.update(acc5[0], images.size(0))
391
392 # measure elapsed time
393 batch_time.update(time.time() - end)
394 end = time.time()
395
396 if i % args.print_freq == 0:
397 progress.display(i + 1)
398
399 batch_time = AverageMeter('Time', use_accel, ':6.3f', Summary.NONE)
400 losses = AverageMeter('Loss', use_accel, ':.4e', Summary.NONE)
401 top1 = AverageMeter('Acc@1', use_accel, ':6.2f', Summary.AVERAGE)
402 top5 = AverageMeter('Acc@5', use_accel, ':6.2f', Summary.AVERAGE)
403 progress = ProgressMeter(
404 len(val_loader) + (args.distributed and (len(val_loader.sampler) * args.world_size < len(val_loader.dataset))),
405 [batch_time, losses, top1, top5],
406 prefix='Test: ')
407
408 # switch to evaluate mode
409 model.eval()
410
411 run_validate(val_loader)
412 if args.distributed:
413 top1.all_reduce()
414 top5.all_reduce()
415

Callers 1

main_workerFunction · 0.70

Calls 5

all_reduceMethod · 0.95
display_summaryMethod · 0.95
AverageMeterClass · 0.85
ProgressMeterClass · 0.85
run_validateFunction · 0.85

Tested by

no test coverage detected