-
Notifications
You must be signed in to change notification settings - Fork 10
/
eval_sem_seg.py
45 lines (36 loc) · 1.52 KB
/
eval_sem_seg.py
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
import numpy as np
import os
from chainercv.datasets import VOCSemanticSegmentationDataset
from chainercv.evaluations import calc_semantic_segmentation_confusion
import imageio
import argparse
def run(args):
dataset = VOCSemanticSegmentationDataset(split=args.chainer_eval_set, data_dir=args.voc12_root)
preds = []
labels = []
n_img = 0
for i, id in enumerate(dataset.ids):
cls_labels = imageio.imread(os.path.join(args.sem_seg_out_dir, id + '.png')).astype(np.uint8)
preds.append(cls_labels.copy())
labels.append(dataset.get_example_by_keys(i, (1,))[0])
n_img += 1
confusion = calc_semantic_segmentation_confusion(preds, labels)[:21, :21]
gtj = confusion.sum(axis=1)
resj = confusion.sum(axis=0)
gtjresj = np.diag(confusion)
denominator = gtj + resj - gtjresj
fp = 1. - gtj / denominator
fn = 1. - resj / denominator
iou = gtjresj / denominator
print("total images", n_img)
print(fp[0], fn[0])
print(np.mean(fp[1:]), np.mean(fn[1:]))
print({'iou': iou, 'miou': np.nanmean(iou)})
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--voc12_root", default='../PascalVOC2012/VOCdevkit/VOC2012', type=str)
parser.add_argument("--gt_dir", default='../PascalVOC2012/VOCdevkit/VOC2012/SegmentationClass', type=str)
parser.add_argument("--chainer_eval_set", default="train", type=str)
parser.add_argument("--sem_seg_out_dir", default="exp/pseudo_label", type=str)
args = parser.parse_args()
run(args)