-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathgen_cityscapes.py
170 lines (143 loc) · 6.04 KB
/
gen_cityscapes.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import numpy as np
import torch
import glob
import cv2
import os
from argparse import ArgumentParser
from cnn import SegmentationModel as net
from torch import nn
#============================================
__author__ = "Sachin Mehta"
__license__ = "MIT"
__maintainer__ = "Sachin Mehta"
#============================================
pallete = [[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],
[0, 0, 0]]
def relabel(img):
'''
This function relabels the predicted labels so that cityscape dataset can process
:param img:
:return:
'''
img[img == 19] = 255
img[img == 18] = 33
img[img == 17] = 32
img[img == 16] = 31
img[img == 15] = 28
img[img == 14] = 27
img[img == 13] = 26
img[img == 12] = 25
img[img == 11] = 24
img[img == 10] = 23
img[img == 9] = 22
img[img == 8] = 21
img[img == 7] = 20
img[img == 6] = 19
img[img == 5] = 17
img[img == 4] = 13
img[img == 3] = 12
img[img == 2] = 11
img[img == 1] = 8
img[img == 0] = 7
img[img == 255] = 0
return img
def evaluateModel(args, model, image_list):
# gloabl mean and std values
mean = [72.3923111, 82.90893555, 73.15840149]
std = [45.3192215, 46.15289307, 44.91483307]
model.eval()
for i, imgName in enumerate(image_list):
img = cv2.imread(imgName)
if args.overlay:
img_orig = np.copy(img)
img = img.astype(np.float32)
for j in range(3):
img[:, :, j] -= mean[j]
for j in range(3):
img[:, :, j] /= std[j]
# resize the image to 1024x512x3
img = cv2.resize(img, (args.inWidth, args.inHeight))
if args.overlay:
img_orig = cv2.resize(img_orig, (args.inWidth, args.inHeight))
img /= 255
img = img.transpose((2, 0, 1))
img_tensor = torch.from_numpy(img)
img_tensor = torch.unsqueeze(img_tensor, 0) # add a batch dimension
if args.gpu:
img_tensor = img_tensor.cuda()
img_out = model(img_tensor)
classMap_numpy = img_out[0].max(0)[1].byte().cpu().data.numpy()
# upsample the feature maps to the same size as the input image using Nearest neighbour interpolation
# upsample the feature map from 1024x512 to 2048x1024
classMap_numpy = cv2.resize(classMap_numpy, (args.inWidth*2, args.inHeight*2), interpolation=cv2.INTER_NEAREST)
if i % 100 == 0 and i > 0:
print('Processed [{}/{}]'.format(i, len(image_list)))
name = imgName.split('/')[-1]
if args.colored:
classMap_numpy_color = np.zeros((img.shape[1], img.shape[2], img.shape[0]), dtype=np.uint8)
for idx in range(len(pallete)):
[r, g, b] = pallete[idx]
classMap_numpy_color[classMap_numpy == idx] = [b, g, r]
cv2.imwrite(args.savedir + os.sep + 'c_' + name.replace(args.img_extn, 'png'), classMap_numpy_color)
if args.overlay:
overlayed = cv2.addWeighted(img_orig, 0.5, classMap_numpy_color, 0.5, 0)
cv2.imwrite(args.savedir + os.sep + 'over_' + name.replace(args.img_extn, 'jpg'), overlayed)
if args.cityFormat:
classMap_numpy = relabel(classMap_numpy.astype(np.uint8))
cv2.imwrite(args.savedir + os.sep + name.replace(args.img_extn, 'png'), classMap_numpy)
def main(args):
# read all the images in the folder
image_list = glob.glob(args.data_dir + os.sep + '*.' + args.img_extn)
modelA = net.EESPNet_Seg(args.classes, s=args.s)
if not os.path.isfile(args.pretrained):
print('Pre-trained model file does not exist. Please check ./pretrained_models folder')
exit(-1)
modelA = nn.DataParallel(modelA)
modelA.load_state_dict(torch.load(args.pretrained))
if args.gpu:
modelA = modelA.cuda()
# set to evaluation mode
modelA.eval()
if not os.path.isdir(args.savedir):
os.mkdir(args.savedir)
evaluateModel(args, modelA, image_list)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--model', default="ESPNetv2", help='Model name')
parser.add_argument('--data_dir', default="./data", help='Data directory')
parser.add_argument('--img_extn', default="png", help='RGB Image format')
parser.add_argument('--inWidth', type=int, default=1024, help='Width of RGB image')
parser.add_argument('--inHeight', type=int, default=512, help='Height of RGB image')
parser.add_argument('--savedir', default='./results', help='directory to save the results')
parser.add_argument('--gpu', default=True, type=bool, help='Run on CPU or GPU. If TRUE, then GPU.')
parser.add_argument('--pretrained', default='', help='Pretrained weights directory.')
parser.add_argument('--s', default=0.5, type=float, help='scale')
parser.add_argument('--cityFormat', default=True, type=bool, help='If you want to convert to cityscape '
'original label ids')
parser.add_argument('--colored', default=False, type=bool, help='If you want to visualize the '
'segmentation masks in color')
parser.add_argument('--overlay', default=False, type=bool, help='If you want to visualize the '
'segmentation masks overlayed on top of RGB image')
parser.add_argument('--classes', default=20, type=int, help='Number of classes in the dataset. 20 for Cityscapes')
args = parser.parse_args()
if args.overlay:
args.colored = True # This has to be true if you want to overlay
main(args)