def validate_multi(val_loader, model, ema_model): print("starting validation") Sig = torch.nn.Sigmoid() preds_regular = [] preds_ema = [] targets = [] for i, (input, target) in enumerate(val_loader): target = target # compute output with torch.no_grad(): with autocast(): output_regular = Sig(model(input.cuda())).cpu() output_ema = Sig(ema_model.module(input.cuda())).cpu() # for mAP calculation preds_regular.append(output_regular.cpu().detach()) preds_ema.append(output_ema.cpu().detach()) targets.append(target.cpu().detach()) mAP_score_regular = mAP( torch.cat(targets).numpy(), torch.cat(preds_regular).numpy()) mAP_score_ema = mAP( torch.cat(targets).numpy(), torch.cat(preds_ema).numpy()) print("mAP score regular {:.2f}, mAP score EMA {:.2f}".format( mAP_score_regular, mAP_score_ema)) return max(mAP_score_regular, mAP_score_ema)
def validate_multi(val_loader, model, args): print("starting actuall validation") batch_time = AverageMeter() prec = AverageMeter() rec = AverageMeter() mAP_meter = AverageMeter() Sig = torch.nn.Sigmoid() end = time.time() tp, fp, fn, tn, count = 0, 0, 0, 0, 0 preds = [] targets = [] for i, (input, target) in enumerate(val_loader): target = target target = target.max(dim=1)[0] # compute output with torch.no_grad(): output = Sig(model(input.cuda())).cpu() # for mAP calculation preds.append(output.cpu()) targets.append(target.cpu()) # measure accuracy and record loss pred = output.data.gt(args.thre).long() tp += (pred + target).eq(2).sum(dim=0) fp += (pred - target).eq(1).sum(dim=0) fn += (pred - target).eq(-1).sum(dim=0) tn += (pred + target).eq(0).sum(dim=0) count += input.size(0) this_tp = (pred + target).eq(2).sum() this_fp = (pred - target).eq(1).sum() this_fn = (pred - target).eq(-1).sum() this_tn = (pred + target).eq(0).sum() this_prec = this_tp.float() / ( this_tp + this_fp).float() * 100.0 if this_tp + this_fp != 0 else 0.0 this_rec = this_tp.float() / ( this_tp + this_fn).float() * 100.0 if this_tp + this_fn != 0 else 0.0 prec.update(float(this_prec), input.size(0)) rec.update(float(this_rec), input.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() p_c = [float(tp[i].float() / (tp[i] + fp[i]).float()) * 100.0 if tp[ i] > 0 else 0.0 for i in range(len(tp))] r_c = [float(tp[i].float() / (tp[i] + fn[i]).float()) * 100.0 if tp[ i] > 0 else 0.0 for i in range(len(tp))] f_c = [2 * p_c[i] * r_c[i] / (p_c[i] + r_c[i]) if tp[i] > 0 else 0.0 for i in range(len(tp))] mean_p_c = sum(p_c) / len(p_c) mean_r_c = sum(r_c) / len(r_c) mean_f_c = sum(f_c) / len(f_c) p_o = tp.sum().float() / (tp + fp).sum().float() * 100.0 r_o = tp.sum().float() / (tp + fn).sum().float() * 100.0 f_o = 2 * p_o * r_o / (p_o + r_o) if i % args.print_freq == 0: print('Test: [{0}/{1}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Precision {prec.val:.2f} ({prec.avg:.2f})\t' 'Recall {rec.val:.2f} ({rec.avg:.2f})'.format( i, len(val_loader), batch_time=batch_time, prec=prec, rec=rec)) print( 'P_C {:.2f} R_C {:.2f} F_C {:.2f} P_O {:.2f} R_O {:.2f} F_O {:.2f}' .format(mean_p_c, mean_r_c, mean_f_c, p_o, r_o, f_o)) print( '--------------------------------------------------------------------') print(' * P_C {:.2f} R_C {:.2f} F_C {:.2f} P_O {:.2f} R_O {:.2f} F_O {:.2f}' .format(mean_p_c, mean_r_c, mean_f_c, p_o, r_o, f_o)) mAP_score = mAP(torch.cat(targets).numpy(), torch.cat(preds).numpy()) print("mAP score:", mAP_score) return