-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathevaluation.py
124 lines (101 loc) · 5.01 KB
/
evaluation.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
import torch
import torch.nn as nn
from torch.autograd import Variable
from options.test_options import TestOptions
from data import CreateTrgDataLoader
from PIL import Image
import json
import os.path as osp
import os
import numpy as np
from model import CreateModel
palette = [128, 64, 128, 244, 35, 232, 70, 70, 70, 102, 102, 156, 190, 153, 153, 153, 153, 153, 250, 170, 30,
220, 220, 0, 107, 142, 35, 152, 251, 152, 70, 130, 180, 220, 20, 60, 255, 0, 0, 0, 0, 142, 0, 0, 70,
0, 60, 100, 0, 80, 100, 0, 0, 230, 119, 11, 32]
zero_pad = 256 * 3 - len(palette)
for i in range(zero_pad):
palette.append(0)
def colorize_mask(mask):
# mask: numpy array of the mask
new_mask = Image.fromarray(mask.astype(np.uint8)).convert('P')
new_mask.putpalette(palette)
return new_mask
def fast_hist(a, b, n):
k = (a >= 0) & (a < n)
return np.bincount(n * a[k].astype(int) + b[k], minlength=n ** 2).reshape(n, n)
def per_class_iu(hist):
return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))
def label_mapping(input, mapping):
output = np.copy(input)
for ind in range(len(mapping)):
output[input == mapping[ind][0]] = mapping[ind][1]
return np.array(output, dtype=np.int64)
def compute_mIoU(gt_dir, pred_dir, devkit_dir='', restore_from=''):
with open(osp.join(devkit_dir, 'info.json'), 'r') as fp:
info = json.load(fp)
num_classes = np.int(info['classes'])
print('Num classes', num_classes)
name_classes = np.array(info['label'], dtype=np.str)
mapping = np.array(info['label2train'], dtype=np.int)
hist = np.zeros((num_classes, num_classes))
image_path_list = osp.join(devkit_dir, 'val.txt')
label_path_list = osp.join(devkit_dir, 'label.txt')
gt_imgs = open(label_path_list, 'r').read().splitlines()
gt_imgs = [osp.join(gt_dir, x) for x in gt_imgs]
pred_imgs = open(image_path_list, 'r').read().splitlines()
pred_imgs = [osp.join(pred_dir, x.split('/')[-1]) for x in pred_imgs]
for ind in range(len(gt_imgs)):
pred = np.array(Image.open(pred_imgs[ind]))
label = np.array(Image.open(gt_imgs[ind]))
label = label_mapping(label, mapping)
if len(label.flatten()) != len(pred.flatten()):
print('Skipping: len(gt) = {:d}, len(pred) = {:d}, {:s}, {:s}'.format(len(label.flatten()), len(pred.flatten()), gt_imgs[ind], pred_imgs[ind]))
continue
hist += fast_hist(label.flatten(), pred.flatten(), num_classes)
if ind > 0 and ind % 10 == 0:
with open(restore_from+'_mIoU.txt', 'a') as f:
f.write('{:d} / {:d}: {:0.2f}\n'.format(ind, len(gt_imgs), 100*np.mean(per_class_iu(hist))))
print('{:d} / {:d}: {:0.2f}'.format(ind, len(gt_imgs), 100*np.mean(per_class_iu(hist))))
hist2 = np.zeros((19, 19))
for i in range(19):
hist2[i] = hist[i] / np.sum(hist[i])
mIoUs = per_class_iu(hist)
for ind_class in range(num_classes):
with open(restore_from+'_mIoU.txt', 'a') as f:
f.write('===>' + name_classes[ind_class] + ':\t' + str(round(mIoUs[ind_class] * 100, 2)) + '\n')
print('===>' + name_classes[ind_class] + ':\t' + str(round(mIoUs[ind_class] * 100, 2)))
with open(restore_from+'_mIoU.txt', 'a') as f:
f.write('===> mIoU: ' + str(round(np.nanmean(mIoUs) * 100, 2)) + '\n')
print('===> mIoU19: ' + str(round(np.nanmean(mIoUs) * 100, 2)))
print('===> mIoU16: ' + str(round(np.mean(mIoUs[[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18]]) * 100, 2)))
print('===> mIoU13: ' + str(round(np.mean(mIoUs[[0, 1, 2, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18]]) * 100, 2)))
def main():
opt = TestOptions()
args = opt.initialize()
if not os.path.exists(args.save):
os.makedirs(args.save)
model = CreateModel(args)
model.eval()
model.cuda()
targetloader = CreateTrgDataLoader(args)
for index, batch in enumerate(targetloader):
if index % 100 == 0:
print '%d processd' % index
image, _, name = batch
output = model(Variable(image).cuda())
output = nn.functional.softmax(output, dim=1)
output = nn.functional.upsample(output, (1024, 2048), mode='bilinear', align_corners=True).cpu().data[0].numpy()
output = output.transpose(1,2,0)
output_nomask = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
output_col = colorize_mask(output_nomask)
output_nomask = Image.fromarray(output_nomask)
name = name[0].split('/')[-1]
output_nomask.save('%s/%s' % (args.save, name))
output_col.save('%s/%s_color.png' % (args.save, name.split('.')[0]))
compute_mIoU(args.gt_dir, args.save, args.devkit_dir, args.restore_from)
if __name__ == '__main__':
os.system('nvidia-smi -q -d Memory |grep -A4 GPU|grep Free >tmp')
memory_gpu=[int(x.split()[2]) for x in open('tmp','r').readlines()]
os.system('rm tmp')
os.environ["CUDA_VISIBLE_DEVICES"] = str(np.argmax(memory_gpu))
main()