File size: 4,507 Bytes
f7f604d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import os
import sys
import tqdm
import pandas as pd
import numpy as np
from PIL import Image
filepath = os.path.split(os.path.abspath(__file__))[0]
repopath = os.path.split(filepath)[0]
sys.path.append(repopath)
from utils.eval_functions import *
from utils.misc import *
BETA = 1.0
def evaluate(opt, args):
if os.path.isdir(opt.Eval.result_path) is False:
os.makedirs(opt.Eval.result_path)
method = os.path.split(opt.Eval.pred_root)[-1]
if args.verbose is True:
print('#' * 20, 'Start Evaluation', '#' * 20)
datasets = tqdm.tqdm(opt.Eval.datasets, desc='Expr - ' + method, total=len(
opt.Eval.datasets), position=0, bar_format='{desc:<30}{percentage:3.0f}%|{bar:50}{r_bar}')
else:
datasets = opt.Eval.datasets
results = []
for dataset in datasets:
pred_root = os.path.join(opt.Eval.pred_root, dataset)
gt_root = os.path.join(opt.Eval.gt_root, dataset, 'masks')
preds = os.listdir(pred_root)
gts = os.listdir(gt_root)
preds = sort(preds)
gts = sort(gts)
preds = [i for i in preds if i in gts]
gts = [i for i in gts if i in preds]
FM = Fmeasure()
WFM = WeightedFmeasure()
SM = Smeasure()
EM = Emeasure()
MAE = Mae()
MSE = Mse()
MBA = BoundaryAccuracy()
IOU = IoU()
BIOU = BIoU()
TIOU = TIoU()
if args.verbose is True:
samples = tqdm.tqdm(enumerate(zip(preds, gts)), desc=dataset + ' - Evaluation', total=len(
preds), position=1, leave=False, bar_format='{desc:<30}{percentage:3.0f}%|{bar:50}{r_bar}')
else:
samples = enumerate(zip(preds, gts))
for i, sample in samples:
pred, gt = sample
pred_mask = np.array(Image.open(os.path.join(pred_root, pred)).convert('L'))
gt_mask = np.array(Image.open(os.path.join(gt_root, gt)).convert('L'))
if len(pred_mask.shape) != 2:
pred_mask = pred_mask[:, :, 0]
if len(gt_mask.shape) != 2:
gt_mask = gt_mask[:, :, 0]
assert pred_mask.shape == gt_mask.shape, print(pred, 'does not match the size of', gt)
# print(gt_mask.max())
FM.step( pred=pred_mask, gt=gt_mask)
WFM.step(pred=pred_mask, gt=gt_mask)
SM.step( pred=pred_mask, gt=gt_mask)
EM.step( pred=pred_mask, gt=gt_mask)
MAE.step(pred=pred_mask, gt=gt_mask)
MSE.step(pred=pred_mask, gt=gt_mask)
MBA.step(pred=pred_mask, gt=gt_mask)
IOU.step(pred=pred_mask, gt=gt_mask)
BIOU.step(pred=pred_mask, gt=gt_mask)
TIOU.step(pred=pred_mask, gt=gt_mask)
result = []
Sm = SM.get_results()["sm"]
wFm = WFM.get_results()["wfm"]
mae = MAE.get_results()["mae"]
mse = MSE.get_results()["mse"]
mBA = MBA.get_results()["mba"]
Fm = FM.get_results()["fm"]
Em = EM.get_results()["em"]
Iou = IOU.get_results()["iou"]
BIou = BIOU.get_results()["biou"]
TIou = TIOU.get_results()["tiou"]
adpEm = Em["adp"]
avgEm = Em["curve"].mean()
maxEm = Em["curve"].max()
adpFm = Fm["adp"]
avgFm = Fm["curve"].mean()
maxFm = Fm["curve"].max()
avgIou = Iou["curve"].mean()
maxIou = Iou["curve"].max()
avgBIou = BIou["curve"].mean()
maxBIou = BIou["curve"].max()
avgTIou = TIou["curve"].mean()
maxTIou = TIou["curve"].max()
out = dict()
for metric in opt.Eval.metrics:
out[metric] = eval(metric)
pkl = os.path.join(opt.Eval.result_path, 'result_' + dataset + '.pkl')
if os.path.isfile(pkl) is True:
result = pd.read_pickle(pkl)
result.loc[method] = out
result.to_pickle(pkl)
else:
result = pd.DataFrame(data=out, index=[method])
result.to_pickle(pkl)
result.to_csv(os.path.join(opt.Eval.result_path, 'result_' + dataset + '.csv'))
results.append(result)
if args.verbose is True:
for dataset, result in zip(datasets, results):
print('###', dataset, '###', '\n', result.sort_index(), '\n')
if __name__ == "__main__":
args = parse_args()
opt = load_config(args.config)
evaluate(opt, args)
|