问题
File "main_finetune_pointConv_torchModel.py", line 484, in <module>
main()
File "main_finetune_pointConv_torchModel.py", line 296, in main
train(train_loader, model, criterion, optimizer, epoch)
File "main_finetune_pointConv_torchModel.py", line 371, in train
writer.add_scalar('train/loss_epoch', losses, epoch)
File "/home/SSD/roth/myProjectEnv/rethinking-network-pruning-conda-py3.6/lib/python3.6/site-packages/tensorboardX/writer.py", line 405, in add_scalar
scalar(tag, scalar_value), global_step, walltime)
File "/home/SSD/roth/myProjectEnv/rethinking-network-pruning-conda-py3.6/lib/python3.6/site-packages/tensorboardX/summary.py", line 146, in scalar
scalar = make_np(scalar)
File "/home/SSD/roth/myProjectEnv/rethinking-network-pruning-conda-py3.6/lib/python3.6/site-packages/tensorboardX/x2num.py", line 34, in make_np
'Got {}, but expected numpy array or torch tensor.'.format(type(x)))
NotImplementedError: Got <class '__main__.AverageMeter'>, but expected numpy array or torch tensor.
代码
def train(train_loader, model, criterion, optimizer, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
end = time.time()
for i, (input, target) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
# target = target.cuda(async=True)
target = target.cuda(non_blocking=True) # python 3.7x
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
# compute output
output = model(input_var)
loss = criterion(output, target_var)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1[0], input.size(0))
top5.update(prec5[0], input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
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'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses, top1=top1, top5=top5))
writer.add_scalar('train/loss_epoch', losses, epoch) # ---------这里报错--------
writer.add_scalar('train/top1_epoch', top1, epoch)
writer.add_scalar('train/top5_epoch', top5, epoch)
解决:
writer.add_scalar('train/loss_epoch', losses.avg, epoch)
writer.add_scalar('train/top1_epoch', top1.avg, epoch)
writer.add_scalar('train/top5_epoch', top5.avg, epoch)