-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
263 lines (195 loc) · 8.93 KB
/
utils.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os
from PIL import Image
import torch
def create_class_mask(img, color_map, is_normalized_img=True, is_normalized_map=False, show_masks=False):
"""
Function to create C matrices from the segmented image, where each of the C matrices is for one class
with all ones at the pixel positions where that class is present
img = The segmented image
color_map = A list with tuples that contains all the RGB values for each color that represents
some class in that image
is_normalized_img = Boolean - Whether the image is normalized or not
If normalized, then the image is multiplied with 255
is_normalized_map = Boolean - Represents whether the color map is normalized or not, if so
then the color map values are multiplied with 255
show_masks = Wherether to show the created masks or not
"""
if is_normalized_img and (not is_normalized_map):
img *= 255
if is_normalized_map and (not is_normalized_img):
img = img / 255
mask = []
hw_tuple = img.shape[:-1]
for color in color_map:
color_img = []
for idx in range(3):
color_img.append(np.ones(hw_tuple) * color[idx])
color_img = np.array(color_img, dtype=np.uint8).transpose(1, 2, 0)
mask.append(np.uint8((color_img == img).sum(axis = -1) == 3))
return np.array(mask)
# Cityscapes dataset Loader
def loader_cscapes(input_path, segmented_path, batch_size, h=1024, w=2048, limited=False):
filenames_t = sorted(glob.glob(input_path + '/**/*.png', recursive=True), key=lambda x : int(x.split('/')[-1].split('_')[1] + x.split('/')[-1].split('_')[2]))
total_files_t = len(filenames_t)
filenames_s = sorted(glob.glob(segmented_path + '/**/*labelIds.png', recursive=True), key=lambda x : int(x.split('/')[-1].split('_')[1] + x.split('/')[-1].split('_')[2]))
total_files_s = len(filenames_s)
assert(total_files_t == total_files_s)
batches = np.random.permutation(np.arange(total_files_s))
idx0 = 0
idx1 = idx0 + batch_size
if str(batch_size).lower() == 'all':
batch_size = total_files_s
idx = 1 if not limited else total_files_s // batch_size + 1
while(idx):
batch = np.arange(idx0, idx1)
# Choosing random indexes of images and labels
batch_idxs = np.random.randint(0, total_files_s, batch_size)
inputs = []
labels = []
for jj in batch_idxs:
# Reading normalized photo
img = np.array(Image.open(filenames_t[jj]))
# Resizing using nearest neighbor method
inputs.append(img)
# Reading semantic image
img = Image.open(filenames_s[jj])
img = np.array(img)
# Resizing using nearest neighbor method
labels.append(img)
inputs = np.stack(inputs, axis=2)
# Changing image format to C x H x W
inputs = torch.tensor(inputs).transpose(0, 2).transpose(1, 3)
labels = torch.tensor(labels)
idx0 = idx1 if idx1 + batch_size < total_files_s else 0
idx1 = idx0 + batch_size
if limited:
idx -= 1
yield inputs, labels
def loader(training_path, segmented_path, batch_size, h=512, w=512):
"""
The Loader to generate inputs and labels from the Image and Segmented Directory
Arguments:
training_path - str - Path to the directory that contains the training images
segmented_path - str - Path to the directory that contains the segmented images
batch_size - int - the batch size
yields inputs and labels of the batch size
"""
filenames_t = os.listdir(training_path)
total_files_t = len(filenames_t)
filenames_s = os.listdir(segmented_path)
total_files_s = len(filenames_s)
assert(total_files_t == total_files_s)
if str(batch_size).lower() == 'all':
batch_size = total_files_s
idx = 0
while(1):
batch_idxs = np.random.randint(0, total_files_s, batch_size)
inputs = []
labels = []
for jj in batch_idxs:
img = plt.imread(training_path + filenames_t[jj])
img = cv2.resize(img, (h, w), cv2.INTER_NEAREST)
inputs.append(img)
img = Image.open(segmented_path + filenames_s[jj])
img = np.array(img)
img = cv2.resize(img, (h, w), cv2.INTER_NEAREST)
labels.append(img)
inputs = np.stack(inputs, axis=2)
inputs = torch.tensor(inputs).transpose(0, 2).transpose(1, 3)
labels = torch.tensor(labels)
yield inputs, labels
def decode_segmap_camvid(image):
Sky = [128, 128, 128]
Building = [128, 0, 0]
Pole = [192, 192, 128]
Road_marking = [255, 69, 0]
Road = [128, 64, 128]
Pavement = [60, 40, 222]
Tree = [128, 128, 0]
SignSymbol = [192, 128, 128]
Fence = [64, 64, 128]
Car = [64, 0, 128]
Pedestrian = [64, 64, 0]
Bicyclist = [0, 128, 192]
label_colors = np.array([Sky, Building, Pole, Road_marking, Road,
Pavement, Tree, SignSymbol, Fence, Car,
Pedestrian, Bicyclist]).astype(np.uint8)
r = np.zeros_like(image).astype(np.uint8)
g = np.zeros_like(image).astype(np.uint8)
b = np.zeros_like(image).astype(np.uint8)
for label in range(len(label_colors)):
r[image == label] = label_colors[label, 0]
g[image == label] = label_colors[label, 1]
b[image == label] = label_colors[label, 2]
rgb = np.zeros((image.shape[0], image.shape[1], 3)).astype(np.uint8)
rgb[:, :, 0] = r
rgb[:, :, 1] = g
rgb[:, :, 2] = b
return rgb
def decode_segmap_cscapes(image, nc=34):
label_colours = np.array([(0, 0, 0), # 0=background
(0, 0, 0), # 1=ego vehicle
(0, 0, 0), # 2=rectification border
(0, 0, 0), # 3=out of toi
(0, 0, 0), # 4=static
# 5=dynamic, 6=ground, 7=road, 8=sidewalk, 9=parking
(111, 74, 0), ( 81, 0, 81), (128, 64,128), (244, 35,232), (250,170,160),
# 10=rail track, 11=building, 12=wall, 13=fence, 14=guard rail
(230,150,140), ( 70, 70, 70), (102,102,156), (190,153,153), (180,165,180),
# 15=bridge, 16=tunnel, 17=pole, 18=pole group, 19=traffic light
(150,100,100), (150,120, 90), (153,153,153), (153,153,153), (250,170, 30),
# 20=traffic sign, 21=vegetation, 22=terrain, 23=sky, 24=person
(220,220, 0), (107,142, 35), (152,251,152), ( 70,130,180), (220, 20, 60),
# 25=rider, 26=car, 27=truck, 28=bus, 29=caravan,
(255, 0, 0), ( 0, 0,142), ( 0, 0, 70), ( 0, 60,100), ( 0, 0, 90),
# 30=trailer, 31=train, 32=motorcycle, 33=bicycle, 34=license plate,
( 0, 0,110), ( 0, 80,100), ( 0, 0,230), (119, 11, 32), ( 0, 0,142),
])
r = np.zeros_like(image).astype(np.uint8)
g = np.zeros_like(image).astype(np.uint8)
b = np.zeros_like(image).astype(np.uint8)
for l in range(0, nc):
r[image == l] = label_colours[l, 0]
g[image == l] = label_colours[l, 1]
b[image == l] = label_colours[l, 2]
rgb = np.zeros((image.shape[0], image.shape[1], 3)).astype(np.uint8)
rgb[:, :, 0] = b
rgb[:, :, 1] = g
rgb[:, :, 2] = r
return rgb
def show_images(images, in_row=True):
'''
Helper function to show 3 images
'''
total_images = len(images)
rc_tuple = (1, total_images)
if not in_row:
rc_tuple = (total_images, 1)
#figure = plt.figure(figsize=(20, 10))
for ii in range(len(images)):
plt.subplot(*rc_tuple, ii+1)
plt.title(images[ii][0])
plt.axis('off')
plt.imshow(images[ii][1])
plt.show()
def get_class_weights(loader, num_classes, c=1.02):
'''
This class return the class weights for each class
Arguments:
- loader : The generator object which return all the labels at one iteration
Do Note: That this class expects all the labels to be returned in
one iteration
- num_classes : The number of classes
Return:
- class_weights : An array equal in length to the number of classes
containing the class weights for each class
'''
_, labels = next(loader)
all_labels = labels.flatten()
each_class = np.bincount(all_labels, minlength=num_classes)
prospensity_score = each_class / len(all_labels)
class_weights = 1 / (np.log(c + prospensity_score))
return class_weights