-
Notifications
You must be signed in to change notification settings - Fork 93
/
eval_post.py
133 lines (98 loc) · 3.55 KB
/
eval_post.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
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
import numpy as np
from PIL import Image
import progressbar
from util.compute_boundary_acc import compute_boundary_acc
from util.file_buffer import FileBuffer
from argparse import ArgumentParser
import os
import re
parser = ArgumentParser()
parser.add_argument('--dir', help='Directory with image, gt, and mask')
parser.add_argument('--output', help='Output of temp results',
default=None)
args = parser.parse_args()
def get_iu(seg, gt):
intersection = np.count_nonzero(seg & gt)
union = np.count_nonzero(seg | gt)
return intersection, union
total_new_i = 0
total_new_u = 0
total_old_i = 0
total_old_u = 0
total_old_correct_pixels = 0
total_new_correct_pixels = 0
total_num_pixels = 0
total_num_images = 0
total_seg_acc = 0
total_mask_acc = 0
small_objects = 0
all_h = 0
all_w = 0
all_max = 0
all_gts = [gt for gt in os.listdir(args.dir) if '_gt.png' in gt]
file_buffer = FileBuffer(os.path.join(args.dir, 'results_post.txt'))
if args.output is not None:
os.makedirs(args.output, exist_ok=True)
for gt_name in progressbar.progressbar(all_gts):
gt = np.array(Image.open(os.path.join(args.dir, gt_name)
).convert('L'))
seg = np.array(Image.open(os.path.join(args.dir, gt_name.replace('_gt', '_seg'))
).convert('L'))
mask_im = Image.open(os.path.join(args.dir, gt_name.replace('_gt', '_mask'))
).convert('L')
mask = seg.copy()
this_class = int(re.search(r'\d+', gt_name[::-1]).group()[::-1]) - 1
rmin = cmin = 0
rmax, cmax = seg.shape
all_h += rmax
all_w += cmax
all_max += max(rmax, cmax)
mask_h, mask_w = mask.shape
if mask_h != cmax:
mask = np.array(mask_im.resize((cmax, rmax), Image.BILINEAR))
if seg.sum() < 32*32:
# Reject small objects, just copy input
small_objects += 1
else:
if (cmax==cmin) or (rmax==rmin):
# Should not happen. Check the input in this case.
print(gt_name, this_class)
continue
class_mask_prob = np.array(mask_im.resize((cmax-cmin, rmax-rmin), Image.BILINEAR))
mask[rmin:rmax, cmin:cmax] = class_mask_prob
"""
Compute IoU and boundary accuracy
"""
gt = gt > 128
seg = seg > 128
mask = mask > 128
old_i, old_u = get_iu(gt, seg)
new_i, new_u = get_iu(gt, mask)
total_new_i += new_i
total_new_u += new_u
total_old_i += old_i
total_old_u += old_u
seg_acc, mask_acc = compute_boundary_acc(gt, seg, mask)
total_seg_acc += seg_acc
total_mask_acc += mask_acc
total_num_images += 1
if args.output is not None:
gt = Image.fromarray(gt)
seg = Image.fromarray(seg)
mask = Image.fromarray(mask)
gt.save(os.path.join(args.output, gt_name))
seg.save(os.path.join(args.output, gt_name.replace('_gt.png', '_seg.png')))
mask.save(os.path.join(args.output, gt_name.replace('_gt.png', '_mask.png')))
new_iou = total_new_i/total_new_u
old_iou = total_old_i/total_old_u
new_mba = total_mask_acc/total_num_images
old_mba = total_seg_acc/total_num_images
file_buffer.write('New IoU : ', new_iou)
file_buffer.write('Old IoU : ', old_iou)
file_buffer.write('IoU Delta: ', new_iou-old_iou)
file_buffer.write('New mBA : ', new_mba)
file_buffer.write('Old mBA : ', old_mba)
file_buffer.write('mBA Delta: ', new_mba-old_mba)
file_buffer.write('Avg. H+W : ', (all_h+all_w)/total_num_images)
file_buffer.write('Avg. Max(H,W) : ', all_max/total_num_images)
file_buffer.write('Number of small objects: ', small_objects)