本文整理匯總了Python中utils.calculate_accuracy方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.calculate_accuracy方法的具體用法?Python utils.calculate_accuracy怎麽用?Python utils.calculate_accuracy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils
的用法示例。
在下文中一共展示了utils.calculate_accuracy方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import calculate_accuracy [as 別名]
def test(data_loader, model, args, device):
batch_time = AverageMeter()
data_time = AverageMeter()
top1 = AverageMeter()
top3 = AverageMeter()
# switch to evaluate mode
model.eval()
end_time = time.time()
for i, (input, target) in enumerate(data_loader):
# measure data loading time
data_time.update(time.time() - end_time)
input = input.to(device)
target = target.to(device)
# compute output and loss
output = model(input)
# measure accuracy and record loss
prec1, prec3 = calculate_accuracy(output, target, topk=(1, 3))
# prec1[0]: convert torch.Size([1]) to torch.Size([])
top1.update(prec1[0].item(), input.size(0))
top3.update(prec3[0].item(), input.size(0))
# measure elapsed time
batch_time.update(time.time() - end_time)
end_time = time.time()
if (i + 1) % args.log_interval == 0:
print('Test Iter [{0}/{1}]\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Prec@3 {top3.val:.3f} ({top3.avg:.3f})\t'
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data Time {data_time.val:.3f} ({data_time.avg:.3f})'.format(
i + 1,
len(data_loader),
top1=top1,
top3=top3,
batch_time=batch_time,
data_time=data_time))
print(' * Prec@1 {top1.avg:.2f}% | Prec@3 {top3.avg:.2f}%'.format(
top1=top1, top3=top3))
return top1.avg
示例2: val_epoch
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import calculate_accuracy [as 別名]
def val_epoch(epoch, data_loader, model, criterion, opt, logger):
print('validation at epoch {}'.format(epoch))
model.eval()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
accuracies = AverageMeter()
end_time = time.time()
for i, (inputs, targets) in enumerate(data_loader):
data_time.update(time.time() - end_time)
if not opt.no_cuda:
targets = targets.cuda(async=True)
inputs = Variable(inputs, volatile=True)
targets = Variable(targets, volatile=True)
outputs = model(inputs)
loss = criterion(outputs, targets)
acc = calculate_accuracy(outputs, targets)
losses.update(loss.data[0], inputs.size(0))
accuracies.update(acc, inputs.size(0))
batch_time.update(time.time() - end_time)
end_time = time.time()
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc {acc.val:.3f} ({acc.avg:.3f})'.format(
epoch, i + 1, len(data_loader), batch_time=batch_time,
data_time=data_time, loss=losses, acc=accuracies))
logger.log({
'epoch': epoch,
'loss': losses.avg,
'acc': accuracies.avg
})
return losses.avg