PyTorch 模型评估代码:计算 mAP
for idx, (images, target, path) in enumerate(data_loader):
images = images.cuda(non_blocking=True)
target = target.cuda(non_blocking=True)
output = model(images)
# 测量特定准确率
if config.EVAL_MODE:
_, predicted = torch.max(output, 1)
labels.extend(target.cpu().numpy())
preds.extend(predicted.cpu().numpy())
paths.extend(path)
# 测量准确率并记录损失
loss = criterion(output, target)
acc1, acc5 = accuracy(output, target, topk=(1, 5))
acc1 = reduce_tensor(acc1)
acc5 = reduce_tensor(acc5)
loss = reduce_tensor(loss)
loss_meter.update(loss.item(), target.size(0))
acc1_meter.update(acc1.item(), target.size(0))
acc5_meter.update(acc5.item(), target.size(0))
# 测量经过时间
batch_time.update(time.time() - end)
end = time.time()
# 测量 mAP
if config.EVAL_MODE:
if idx % config.PRINT_FREQ == 0:
print('Computing mAP...')
ap = compute_mAP(labels, preds, paths)
print('mAP: {:.4f}'.format(ap))
请注意,上述代码中的 compute_mAP 函数是用于计算 mAP 的自定义函数,你需要根据你的实际情况来实现该函数。
原文地址: https://www.cveoy.top/t/topic/qmXN 著作权归作者所有。请勿转载和采集!