generated from ntudlcv/DLCV-Fall-2021-HW1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean_iou_evaluate.py
57 lines (47 loc) · 1.76 KB
/
mean_iou_evaluate.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
import numpy as np
import scipy.misc
import imageio
import argparse
import os
def read_masks(filepath):
'''
Read masks from directory and tranform to categorical
'''
file_list = [file for file in os.listdir(filepath) if file.endswith('.png')]
file_list.sort()
n_masks = len(file_list)
masks = np.empty((n_masks, 512, 512))
for i, file in enumerate(file_list):
mask = imageio.imread(os.path.join(filepath, file))
mask = (mask >= 128).astype(int)
mask = 4 * mask[:, :, 0] + 2 * mask[:, :, 1] + mask[:, :, 2]
masks[i, mask == 3] = 0 # (Cyan: 011) Urban land
masks[i, mask == 6] = 1 # (Yellow: 110) Agriculture land
masks[i, mask == 5] = 2 # (Purple: 101) Rangeland
masks[i, mask == 2] = 3 # (Green: 010) Forest land
masks[i, mask == 1] = 4 # (Blue: 001) Water
masks[i, mask == 7] = 5 # (White: 111) Barren land
masks[i, mask == 0] = 6 # (Black: 000) Unknown
return masks
def mean_iou_score(pred, labels):
'''
Compute mean IoU score over 6 classes
'''
mean_iou = 0
for i in range(6):
tp_fp = np.sum(pred == i)
tp_fn = np.sum(labels == i)
tp = np.sum((pred == i) * (labels == i))
iou = tp / (tp_fp + tp_fn - tp)
mean_iou += iou / 6
print('class #%d : %1.5f'%(i, iou))
print('\nmean_iou: %f\n' % mean_iou)
return mean_iou
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-g', '--labels', help='ground truth masks directory', type=str)
parser.add_argument('-p', '--pred', help='prediction masks directory', type=str)
args = parser.parse_args()
pred = read_masks(args.pred)
labels = read_masks(args.labels)
mean_iou_score(pred, labels)