def eval(args, model, dataloader): print('\nStart Test!') for c in os.listdir(os.path.join("./checkpoints", args.net_work)): pretrained_model_path = os.path.join("./checkpoints", args.net_work, c) # 最后一个模型(最好的) print("Load best model " + '\"' + os.path.abspath(pretrained_model_path) + '\"') checkpoint = torch.load(pretrained_model_path) model.load_state_dict(checkpoint['state_dict']) with torch.no_grad(): model.eval() test_progressor = pb.Test_ProgressBar(total=len(dataloader), model_name=args.net_work) total_Dice = [] total_Acc = [] total_jaccard = [] total_Sensitivity = [] total_Specificity = [] for i, (data, [label, label_path]) in enumerate(dataloader): test_progressor.current = i if torch.cuda.is_available() and args.use_gpu: data = data.cuda() label = label.cuda() # get RGB predict image predict = model(data) Dice, Acc, jaccard, Sensitivity, Specificity = u.eval_single_seg( predict, label) predict = torch.round(torch.sigmoid(predict)).byte() predict_seg = predict.data.cpu().numpy().squeeze() * 255 img = Image.fromarray(predict_seg, mode='L') label_name = label_path[0].split(os.sep)[-1].split('.')[0] + '.png' img.save(args.result_path + label_name) total_Dice += Dice total_Acc += Acc total_jaccard += jaccard total_Sensitivity += Sensitivity total_Specificity += Specificity dice = sum(total_Dice) / len(total_Dice) acc = sum(total_Acc) / len(total_Acc) jac = sum(total_jaccard) / len(total_jaccard) sen = sum(total_Sensitivity) / len(total_Sensitivity) spe = sum(total_Specificity) / len(total_Specificity) test_progressor.val = [dice, acc, jac, sen, spe] test_progressor() test_progressor.done()
def val(args, model, dataloader): print('\n') print('Start Validation!') with torch.no_grad(): model.eval() tbar = tqdm.tqdm(dataloader, desc='\r') total_inter, total_union, total_correct, total_label = 0, 0, 0, 0 total_Dice = [] total_Acc = [] total_jaccard = [] total_Sensitivity = [] total_Specificity = [] for i, (data, label) in enumerate(tbar): # tbar.update() if torch.cuda.is_available() and args.use_gpu: data = data.cuda() label = label.cuda() # get RGB predict image aux_predict, predict = model(data) Dice, Acc, jaccard, Sensitivity, Specificity = u.eval_single_seg( predict, label) total_Dice += Dice total_Acc += Acc total_jaccard += jaccard total_Sensitivity += Sensitivity total_Specificity += Specificity dice = sum(total_Dice) / len(total_Dice) acc = sum(total_Acc) / len(total_Acc) jac = sum(total_jaccard) / len(total_jaccard) sen = sum(total_Sensitivity) / len(total_Sensitivity) spe = sum(total_Specificity) / len(total_Specificity) tbar.set_description( 'Dice: %.3f, Acc: %.3f, Jac: %.3f, Sen: %.3f, Spe: %.3f' % (dice, acc, jac, sen, spe)) print('Dice:', dice) print('Acc:', acc) print('Jac:', jac) print('Sen:', sen) print('Spe:', spe) return dice, acc, jac, sen, spe
def eval(args, model, dataloader): print('\nStart Test!') for c in os.listdir(os.path.join("./checkpoints",args.net_work)): pretrained_model_path = os.path.join("./checkpoints",args.net_work,c) # 最后一个模型(最好的) break print("Load best model "+'\"'+os.path.abspath(pretrained_model_path)+'\"') checkpoint = torch.load(pretrained_model_path) model.load_state_dict(checkpoint['state_dict']) with torch.no_grad(): model.eval() test_progressor = pb.Test_ProgressBar(total=len(dataloader),model_name=args.net_work) total_Dice = [] total_Acc = [] total_jaccard = [] total_Sensitivity = [] total_Specificity = [] for i, (data, label) in enumerate(dataloader): test_progressor.current = i if torch.cuda.is_available() and args.use_gpu: data = data.cuda() label = label.cuda() # get RGB predict image predict = model(data) Dice, Acc, jaccard, Sensitivity, Specificity = u.eval_single_seg(predict, label) total_Dice += Dice total_Acc += Acc total_jaccard += jaccard total_Sensitivity += Sensitivity total_Specificity += Specificity dice = sum(total_Dice) / len(total_Dice) acc = sum(total_Acc) / len(total_Acc) jac = sum(total_jaccard) / len(total_jaccard) sen = sum(total_Sensitivity) / len(total_Sensitivity) spe = sum(total_Specificity) / len(total_Specificity) test_progressor.val=[dice,acc,jac,sen,spe] test_progressor() test_progressor.done()
def val(args, model, dataloader, k_fold, epoch): with torch.no_grad(): model.eval() Dice_m = u.AverageMeter() Acc_m = u.AverageMeter() jaccard_m = u.AverageMeter() Sensitivity_m = u.AverageMeter() Specificity_m = u.AverageMeter() eval_progressor = pb.Val_ProgressBar(mode='val', epoch=epoch + 1, fold=k_fold, model_name=args.net_work, net_index=args.net_index, total=len(dataloader)) for i, (data, label) in enumerate(dataloader): eval_progressor.current = i if torch.cuda.is_available() and args.use_gpu: data = data.cuda() label = label.cuda() aux_predict, predict = model(data) # 获取评价指标 Dice, Acc, jaccard, Sensitivity, Specificity = u.eval_single_seg( predict, label) Dice_m.update(Dice) Acc_m.update(Acc) jaccard_m.update(jaccard) Sensitivity_m.update(Sensitivity) Specificity_m.update(Specificity) dice, acc, jac, sen, spe = Dice_m.avg, Acc_m.avg, jaccard_m.avg, Sensitivity_m.avg, Specificity_m.avg eval_progressor.val = [dice, acc, jac, sen, spe] # 更新进度条 eval_progressor() eval_progressor.done() # print('Dice:', dice) # print('Acc:', acc) # print('Jac:', jac) # print('Sen:', sen) # print('Spe:', spe) return dice, acc, jac, sen, spe
def val(args, model, dataloader): with torch.no_grad(): model.eval() val_progressor = pb.Val_ProgressBar( model_name=args.net_work, total=len(dataloader)) # 验证进度条,用于显示指标 total_Dice = [] total_Acc = [] total_jaccard = [] total_Sensitivity = [] total_Specificity = [] for i, (data, label) in enumerate(dataloader): val_progressor.current = i if torch.cuda.is_available() and args.use_gpu: data = data.cuda() label = label.cuda() # get RGB predict image predict = model(data) Dice, Acc, jaccard, Sensitivity, Specificity = u.eval_single_seg( predict, label) # 每个指标返回的是batchsize长的列表 # 将每个batch的列表相加,在迭代中动态显示指标的平均值(最终值) total_Dice += Dice total_Acc += Acc total_jaccard += jaccard total_Sensitivity += Sensitivity total_Specificity += Specificity # 表示对batchsize个值取平均,len=batchsize dice = sum(total_Dice) / len(total_Dice) acc = sum(total_Acc) / len(total_Acc) jac = sum(total_jaccard) / len(total_jaccard) sen = sum(total_Sensitivity) / len(total_Sensitivity) spe = sum(total_Specificity) / len(total_Specificity) val_progressor.val = [dice, acc, jac, sen, spe] val_progressor() val_progressor.done() return dice, acc, jac, sen, spe