本文整理匯總了Python中utils.accuracy方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.accuracy方法的具體用法?Python utils.accuracy怎麽用?Python utils.accuracy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils
的用法示例。
在下文中一共展示了utils.accuracy方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: base_val
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def base_val(base_train_loader, model_E, criterion, epoch):
losses = AverageMeter()
top1 = AverageMeter()
model_E.eval()
with torch.no_grad():
for batch_idx, (input, target) in enumerate(base_train_loader):
input = input.cuda()
target = target.cuda(non_blocking=True)
# compute output
_, output = model_E(input)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
model_E.weight_norm()
if (batch_idx+1)%250==0:
print('base_test:', batch_idx+1, 'loss:', losses.avg, 'acc:', top1.avg)
return (losses.avg, top1.avg)
示例2: epoch_test_multiple_crops
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def epoch_test_multiple_crops(model, loader, xp, cuda):
metrics = xp.get_metric(tag=loader.tag, name='parent')
xp.Temperature.update()
for batch_idx, (data, target) in tqdm(enumerate(loader), desc='Test Epoch',
leave=False, total=len(loader)):
avg = 0
for img in data:
img, target = data_to_var(img, target, cuda)
output = model(img)
# cross-entropy
if xp.temperature == -1:
avg += nn.functional.softmax(output).data
# smooth-svm
else:
avg += output.data
# avg += torch.exp(output.data / xp.temperature)
prec1 = accuracy(avg, target.data, topk=1)
preck = accuracy(avg, target.data, topk=xp.config['topk'])
metrics.update(acck=float(preck), acc1=float(prec1), n=target.size(0))
示例3: child_valid
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def child_valid(valid_queue, model, arch_pool, criterion):
valid_acc_list = []
with torch.no_grad():
model.eval()
for i, arch in enumerate(arch_pool):
# for step, (input, target) in enumerate(valid_queue):
inputs, targets = next(iter(valid_queue))
inputs = inputs.cuda()
targets = targets.cuda()
logits, _ = model(inputs, arch, bn_train=True)
loss = criterion(logits, targets)
prec1, prec5 = utils.accuracy(logits, targets, topk=(1, 5))
valid_acc_list.append(prec1.data/100)
if (i+1) % 100 == 0:
logging.info('Valid arch %s\n loss %.2f top1 %f top5 %f', ' '.join(map(str, arch[0] + arch[1])), loss, prec1, prec5)
return valid_acc_list
示例4: valid
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def valid(valid_queue, model, criterion):
objs = utils.AvgrageMeter()
top1 = utils.AvgrageMeter()
top5 = utils.AvgrageMeter()
with torch.no_grad():
model.eval()
for step, (input, target) in enumerate(valid_queue):
input = input.cuda()
target = target.cuda()
logits, _ = model(input)
loss = criterion(logits, target)
prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
n = input.size(0)
objs.update(loss.data, n)
top1.update(prec1.data, n)
top5.update(prec5.data, n)
if (step+1) % 100 == 0:
logging.info('valid %03d %e %f %f', step+1, objs.avg, top1.avg, top5.avg)
return top1.avg, top5.avg, objs.avg
示例5: valid
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def valid(valid_queue, model, criterion):
objs = utils.AvgrageMeter()
top1 = utils.AvgrageMeter()
top5 = utils.AvgrageMeter()
with torch.no_grad():
model.eval()
for step, (input, target) in enumerate(valid_queue):
input = input.cuda()
target = target.cuda()
logits, _ = model(input)
loss = criterion(logits, target)
prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
n = input.size(0)
objs.update(loss.data, n)
top1.update(prec1.data, n)
top5.update(prec5.data, n)
if (step+1) % 100 == 0:
logging.info('valid %03d %e %f %f', step+1, objs.avg, top1.avg, top5.avg)
return top1.avg, objs.avg
示例6: child_valid
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def child_valid(valid_queue, model, arch_pool, criterion):
valid_acc_list = []
with torch.no_grad():
model.eval()
for i, arch in enumerate(arch_pool):
#for step, (inputs, targets) in enumerate(valid_queue):
inputs, targets = next(iter(valid_queue))
inputs = inputs.cuda()
targets = targets.cuda()
logits, _ = model(inputs, arch, bn_train=True)
loss = criterion(logits, targets)
prec1, prec5 = utils.accuracy(logits, targets, topk=(1, 5))
valid_acc_list.append(prec1.data/100)
if (i+1) % 100 == 0:
logging.info('Valid arch %s\n loss %.2f top1 %f top5 %f', ' '.join(map(str, arch[0] + arch[1])), loss, prec1, prec5)
return valid_acc_list
示例7: train
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def train(epoch):
model.train()
global iteration
st = time.time()
for batch_idx, (data, target) in enumerate(train_loader):
iteration += 1
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
prec1, = accuracy(output, target)
loss = criterion(output, target)
loss.backward()
optimizer.step()
if batch_idx % args.print_freq == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}, Accuracy: {:.2f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item(), prec1.item()))
writer.add_scalar('Loss/Train', loss.item(), iteration)
writer.add_scalar('Accuracy/Train', prec1, iteration)
epoch_time = time.time() - st
print('Epoch time:{:0.2f}s'.format(epoch_time))
scheduler.step()
示例8: _summarize_shared_train
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def _summarize_shared_train(curr_step, total_loss, raw_total_loss, acc=0, acc_5=0, lr=0.0, epoch_steps=1, writer=None):
"""Logs a set of training steps."""
cur_loss = utils.to_item(total_loss) / epoch_steps
cur_raw_loss = utils.to_item(raw_total_loss) / epoch_steps
logging.info(f'| step {curr_step:3d} '
f'| lr {lr:4.2f} '
f'| raw loss {cur_raw_loss:.2f} '
f'| loss {cur_loss:.2f} '
f'| acc {acc:8.2f}'
f'| acc-5 {acc_5: 8.2f}')
# Tensorboard
if writer is not None:
writer.scalar_summary('shared/loss',
cur_loss,
epoch_steps)
writer.scalar_summary('shared/accuracy',
acc,
epoch_steps)
示例9: nao_model_validation
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def nao_model_validation(valid_queue, model, arch_pool, criterion):
valid_acc_list = []
with torch.no_grad():
model.eval()
for i, arch in enumerate(arch_pool):
# for step, (inputs, targets) in enumerate(valid_queue):
inputs, targets = next(iter(valid_queue))
inputs = inputs.cuda()
targets = targets.cuda()
logits, _ = model(inputs, arch, bn_train=True)
loss = criterion(logits, targets)
prec1, prec5 = nao_utils.accuracy(logits, targets, topk=(1, 5))
valid_acc_list.append(prec1.data / 100)
if (i + 1) % 100 == 0:
logging.info('Valid arch %s\n loss %.2f top1 %f top5 %f', ' '.join(map(str, arch[0] + arch[1])), loss,
prec1, prec5)
return valid_acc_list
示例10: infer
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def infer(valid_queue, model, criterion):
objs = utils.AvgrageMeter()
top1 = utils.AvgrageMeter()
top5 = utils.AvgrageMeter()
model.eval()
for step, (input, target) in enumerate(valid_queue):
input = Variable(input, volatile=True).cuda()
target = Variable(target, volatile=True).cuda(async=True)
logits, _ = model(input)
loss = criterion(logits, target)
prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
n = input.size(0)
objs.update(loss.data[0], n)
top1.update(prec1.data[0], n)
top5.update(prec5.data[0], n)
if step % args.report_freq == 0:
logging.info('valid %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
return top1.avg, top5.avg, objs.avg
示例11: infer
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def infer(test_queue, model, criterion):
objs = utils.AvgrageMeter()
top1 = utils.AvgrageMeter()
top5 = utils.AvgrageMeter()
model.eval()
for step, (input, target) in enumerate(test_queue):
input = Variable(input, volatile=True).cuda()
target = Variable(target, volatile=True).cuda(async=True)
logits, _ = model(input)
loss = criterion(logits, target)
prec1, prec5 = utils.accuracy(logits, target, topk=(1, 5))
n = input.size(0)
objs.update(loss.data[0], n)
top1.update(prec1.data[0], n)
top5.update(prec5.data[0], n)
if step % args.report_freq == 0:
logging.info('test %03d %e %f %f', step, objs.avg, top1.avg, top5.avg)
return top1.avg, objs.avg
示例12: base_train
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def base_train(base_train_loader, model_E, criterion, optimizer, epoch):
losses = AverageMeter()
top1 = AverageMeter()
model_E.train()
# for param in model_E.parameters():
# param.requires_grad = True
for batch_idx, (input, target) in enumerate(base_train_loader):
# print(target)
input = input.cuda()
target = target.cuda(non_blocking=True)
# compute output
_, output = model_E(input)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
model_E.weight_norm()
if (batch_idx+1)%250==0:
print('base_train:', batch_idx+1, 'loss:', losses.avg, 'acc:', top1.avg)
return (losses.avg, top1.avg)
示例13: test
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def test(model, loader, args, xp):
model.eval()
if loader.tag == 'val':
xp_group = xp.val
else:
xp_group = xp.test
for metric in xp_group.metrics():
metric.reset()
for x, y in tqdm(loader, disable=not args.tqdm,
desc='{} Epoch'.format(loader.tag.title()),
leave=False, total=len(loader)):
(x, y) = (x.cuda(), y.cuda()) if args.cuda else (x, y)
scores = model(x)
xp_group.acc.update(accuracy(scores, y), weighting=x.size(0))
xp_group.timer.update()
print('Epoch: [{0}] ({tag})\t'
'({timer:.2f}s) \t'
'Obj ----\t'
'Loss ----\t'
'Acc {acc:.2f}% \t'
.format(int(xp.epoch.value),
tag=loader.tag.title(),
timer=xp_group.timer.value,
acc=xp_group.acc.value))
if loader.tag == 'val':
xp.max_val.update(xp.val.acc.value).log(time=xp.epoch.value)
for metric in xp_group.metrics():
metric.log(time=xp.epoch.value)
示例14: train
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def train(model, loss, optimizer, loader, xp, args):
if not len(loader):
return 0
model.train()
xp.Parent_Train.reset()
for batch_idx, (data, target) in tqdm(enumerate(loader), desc='Train Epoch',
leave=False, total=len(loader)):
data, target = data_to_var(data, target, args.cuda)
output = model(data)
obj = loss(output, target)
optimizer.zero_grad()
obj.backward()
optimizer.step()
prec1 = accuracy(output.data, target.data, topk=1)
preck = accuracy(output.data, target.data, topk=xp.config['topk'])
xp.Parent_Train.update(loss=float(obj), acck=float(preck), acc1=float(prec1), n=data.size(0))
# compute objective function (including regularization)
obj = xp.Loss_Train.get() + regularization(model, xp.mu)
xp.Obj_Train.update(float(obj))
# measure elapsed time
xp.Timer_Train.update()
xp.log_with_tag('train')
if args.verbosity:
print_stats(xp, 'train')
示例15: epoch_test
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import accuracy [as 別名]
def epoch_test(model, loader, xp, cuda):
metrics = xp.get_metric(tag=loader.tag, name='parent')
for batch_idx, (data, target) in tqdm(enumerate(loader), desc='Test Epoch',
leave=False, total=len(loader)):
data, target = data_to_var(data, target, cuda)
output = model(data)
prec1 = accuracy(output.data, target.data, topk=1)
preck = accuracy(output.data, target.data, topk=xp.config['topk'])
metrics.update(acck=float(preck), acc1=float(prec1), n=data.size(0))