val_get.py 1.4 KB

123456789101112131415161718192021222324252627282930
  1. import tqdm
  2. import torch
  3. from block.metric_get import metric
  4. def val_get(args, val_dataloader, model, loss, ema, data_len):
  5. tqdm_len = (data_len - 1) // (args.batch // args.device_number) + 1
  6. tqdm_show = tqdm.tqdm(total=tqdm_len)
  7. with torch.no_grad():
  8. model = ema.ema if args.ema else model.eval()
  9. pred_all = [] # 记录所有预测
  10. true_all = [] # 记录所有标签
  11. for index, (image_batch, true_batch) in enumerate(val_dataloader):
  12. image_batch = image_batch.to(args.device, non_blocking=args.latch)
  13. pred_batch = model(image_batch).detach().cpu()
  14. loss_batch = loss(pred_batch, true_batch)
  15. pred_all.extend(pred_batch)
  16. true_all.extend(true_batch)
  17. tqdm_show.set_postfix({'val_loss': loss_batch.item()}) # 添加显示
  18. tqdm_show.update(1) # 更新进度条
  19. # tqdm
  20. tqdm_show.close()
  21. # 计算指标
  22. pred_all = torch.stack(pred_all, dim=0)
  23. true_all = torch.stack(true_all, dim=0)
  24. loss_all = loss(pred_all, true_all).item()
  25. accuracy, precision, recall, m_ap = metric(pred_all, true_all, args.class_threshold)
  26. print(f'\n| 验证 | val_loss:{loss_all:.4f} | 阈值:{args.class_threshold:.2f} | val_accuracy:{accuracy:.4f} |'
  27. f' val_precision:{precision:.4f} | val_recall:{recall:.4f} | val_m_ap:{m_ap:.4f} |')
  28. return loss_all, accuracy, precision, recall, m_ap