-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
487 lines (376 loc) · 17.8 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# basic imports
import os
import cv2
import numpy as np
import pandas as pd
from tqdm import tqdm
import matplotlib.pyplot as plt
from collections import namedtuple
# DL library imports
import torch
import torch.nn as nn
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
from torch.optim.lr_scheduler import _LRScheduler
# For dice loss function
import segmentation_models_pytorch as smp
# for interactive widgets
import IPython.display as Disp
from ipywidgets import widgets
###################################
# FILE CONSTANTS
###################################
# Convert to torch tensor and normalize images using Imagenet values
preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.56, 0.406), std=(0.229, 0.224, 0.225))
])
# when using torch datasets we defined earlier, the output image
# is normalized. So we're defining an inverse transformation to
# transform to normal RGB format
inverse_transform = transforms.Compose([
transforms.Normalize((-0.485/0.229, -0.456/0.224, -0.406/0.225), (1/0.229, 1/0.224, 1/0.225))
])
# Constants for Standard color mapping
# reference : https://github.com/bdd100k/bdd100k/blob/master/bdd100k/label/label.py
Label = namedtuple( "Label", [ "name", "train_id", "color"])
drivables = [
Label("direct", 0, (219, 94, 86)), # red
Label("alternative", 1, (86, 211, 219)), # cyan
Label("background", 2, (0, 0, 0)), # black
]
train_id_to_color = [c.color for c in drivables if (c.train_id != -1 and c.train_id != 255)]
train_id_to_color = np.array(train_id_to_color)
#####################################
### ROI SELECT CLASS DEFINITION ##
#####################################
class roi_select():
def __init__(self,im, figsize=(12,6), line_color=(255,0,0)):
# class variables
self.im = im
self.selected_points = []
self.line_color = line_color
# plot input image, store figure handles
self.fig,ax = plt.subplots(figsize=figsize)
self.img = ax.imshow(self.im.copy())
plt.suptitle('Select Rectangular ROI')
# connect event handlers
self.ka = self.fig.canvas.mpl_connect('button_press_event', self.onclick)
disconnect_button = widgets.Button(description="Disconnect mpl")
Disp.display(disconnect_button)
disconnect_button.on_click(self.disconnect_mpl)
def draw_roi_on_img(self,img,pts):
# plot polygon edges in single color
pts = np.array(pts, np.int32)
pts = pts.reshape((-1,2))
# cv2.polylines(img, [pts], True, self.line_color, 5)
cv2.rectangle(img, pts[0], pts[1], self.line_color, 5)
return img
def onclick(self, event):
self.selected_points.append([event.xdata,event.ydata])
if len(self.selected_points) == 2:
self.img.set_data(self.draw_roi_on_img(self.im.copy(),self.selected_points))
self.disconnect_mpl(None)
def disconnect_mpl(self,_):
self.fig.canvas.mpl_disconnect(self.ka)
def get_bbox_indices(self, scale_factor = None):
pts = np.array(self.selected_points)
if(scale_factor is not None):
pts = pts * scale_factor
# bounding box coordinates as indices
# min_x, min_y is top left index
# max_x, max_y is bottom right index
roi_indices = pts.astype(int)
indices = {}
indices['min_x'], indices['min_y'] = np.min(roi_indices, axis=0)
indices['max_x'], indices['max_y'] = np.max(roi_indices, axis=0)
return indices
#####################################
### TORCH DATASET CLASS DEFINITION ##
#####################################
class BDD100k_dataset(Dataset):
def __init__(self, images, labels, tf=None):
"""Dataset class for BDD100k_dataset drivable / segmentation data """
self.images = images
self.labels = labels
self.tf = tf
def __len__(self):
return self.images.shape[0]
def __getitem__(self, index):
# read source image and convert to RGB, apply transform
rgb_image = self.images[index]
if self.tf is not None:
rgb_image = self.tf(rgb_image)
# read label image and convert to torch tensor
label_image = torch.from_numpy(self.labels[index]).long()
return rgb_image, label_image
###################################
# FUNCTION TO GET TORCH DATASET #
###################################
def get_datasets(images, labels):
data = BDD100k_dataset(images, labels, tf=preprocess)
# split train data into train, validation and test sets
total_count = len(data)
train_count = int(0.7 * total_count)
valid_count = int(0.2 * total_count)
test_count = total_count - train_count - valid_count
train_set, val_set, test_set = torch.utils.data.random_split(data,
(train_count, valid_count, test_count), generator=torch.Generator().manual_seed(1))
return train_set, val_set, test_set
###################################
# FUNCTION TO GET TORCH DATALOADER #
###################################
def get_dataloaders(train_set, val_set, test_set):
train_dataloader = DataLoader(train_set, batch_size=8,drop_last=True)
val_dataloader = DataLoader(val_set, batch_size=8)
test_dataloader = DataLoader(test_set, batch_size=8)
return train_dataloader, val_dataloader, test_dataloader
###################################
# METRIC CLASS DEFINITION
###################################
class meanIoU:
""" Class to find the mean IoU using confusion matrix approach """
def __init__(self, num_classes):
self.iou_metric = 0.0
self.num_classes = num_classes
# placeholder for confusion matrix on entire dataset
self.confusion_matrix = np.zeros((self.num_classes, self.num_classes))
def update(self, y_preds, labels):
""" Function finds the IoU for the input batch
and add batch metrics to overall metrics """
predicted_labels = torch.argmax(y_preds, dim=1)
batch_confusion_matrix = self._fast_hist(labels.numpy().flatten(), predicted_labels.numpy().flatten())
self.confusion_matrix += batch_confusion_matrix
def _fast_hist(self, label_true, label_pred):
""" Function to calculate confusion matrix on single batch """
mask = (label_true >= 0) & (label_true < self.num_classes)
hist = np.bincount(
self.num_classes * label_true[mask].astype(int) + label_pred[mask],
minlength=self.num_classes ** 2,
).reshape(self.num_classes, self.num_classes)
return hist
def compute(self):
""" Computes overall meanIoU metric from confusion matrix data """
hist = self.confusion_matrix
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
return mean_iu
def reset(self):
self.iou_metric = 0.0
self.confusion_matrix = np.zeros((self.num_classes, self.num_classes))
###################################
# PSPNET LOSS FUNCTION DEFINITION
###################################
class pspnet_loss(nn.Module):
def __init__(self, num_classes, aux_weight):
super(pspnet_loss, self).__init__()
self.aux_weight = aux_weight
self.loss_fn = smp.losses.DiceLoss('multiclass',
classes=np.arange(num_classes).tolist(), log_loss = True, smooth=1.0)
def forward(self, preds, labels):
# if input predictions is in dict format
# calculate total loss as weighted sum of
# main and auxiliary losses
if(isinstance(preds, dict) == True):
main_loss = self.loss_fn(preds['main'], labels)
aux_loss = self.loss_fn(preds['aux'], labels)
loss = (1 - self.aux_weight) * main_loss + self.aux_weight * aux_loss
else:
loss = self.loss_fn(preds, labels)
return loss
###################################
# POLY LR DECAY SCHEDULER DEFINITION
###################################
class polynomial_lr_decay(_LRScheduler):
"""Polynomial learning rate decay until step reach to max_decay_step
Args:
optimizer (Optimizer): Wrapped optimizer.
max_decay_steps: after this step, we stop decreasing learning rate
end_learning_rate: scheduler stoping learning rate decay, value of learning rate must be this value
power: The power of the polynomial.
Reference:
https://github.com/cmpark0126/pytorch-polynomial-lr-decay
"""
def __init__(self, optimizer, max_decay_steps, end_learning_rate=0.0001, power=1.0):
if max_decay_steps <= 1.:
raise ValueError('max_decay_steps should be greater than 1.')
self.max_decay_steps = max_decay_steps
self.end_learning_rate = end_learning_rate
self.power = power
self.last_step = 0
super().__init__(optimizer)
def get_lr(self):
if self.last_step > self.max_decay_steps:
return [self.end_learning_rate for _ in self.base_lrs]
return [(base_lr - self.end_learning_rate) *
((1 - self.last_step / self.max_decay_steps) ** (self.power)) +
self.end_learning_rate for base_lr in self.base_lrs]
def step(self, step=None):
if step is None:
step = self.last_step + 1
self.last_step = step if step != 0 else 1
if self.last_step <= self.max_decay_steps:
decay_lrs = [(base_lr - self.end_learning_rate) *
((1 - self.last_step / self.max_decay_steps) ** (self.power)) +
self.end_learning_rate for base_lr in self.base_lrs]
for param_group, lr in zip(self.optimizer.param_groups, decay_lrs):
param_group['lr'] = lr
###################################
# FUNCTION TO PLOT TRAINING, VALIDATION CURVES
###################################
def plot_training_results(df, model_name):
fig, ax1 = plt.subplots(figsize=(10,4))
ax1.set_ylabel('trainLoss', color='tab:red')
ax1.plot(df['epoch'].values, df['trainLoss'].values, color='tab:red')
ax1.tick_params(axis='y', labelcolor='tab:red')
ax2 = ax1.twinx()
ax2.set_ylabel('validationLoss', color='tab:blue')
ax2.plot(df['epoch'].values, df['validationLoss'].values, color='tab:blue')
ax2.tick_params(axis='y', labelcolor='tab:blue')
plt.suptitle(f'{model_name} Training, Validation Curves')
plt.show()
###################################
# FUNCTION TO EVALUATE MODEL ON DATALOADER
###################################
def evaluate_model(model, dataloader, criterion, metric_class, num_classes, device):
model.eval()
total_loss = 0.0
metric_object = metric_class(num_classes)
with torch.no_grad():
for inputs, labels in tqdm(dataloader, total=len(dataloader)):
inputs = inputs.to(device)
labels = labels.to(device)
y_preds = model(inputs)
# calculate loss
loss = criterion(y_preds, labels)
total_loss += loss.item()
# update batch metric information
metric_object.update(y_preds.cpu().detach(), labels.cpu().detach())
evaluation_loss = total_loss / len(dataloader)
evaluation_metric = metric_object.compute()
return evaluation_loss, evaluation_metric
###################################
# FUNCTION TO TRAIN, VALIDATE MODEL ON DATALOADER
###################################
def train_validate_model(model, num_epochs, model_name, criterion, optimizer,
device, dataloader_train, dataloader_valid,
metric_class, metric_name, num_classes, lr_scheduler = None,
output_path = '.'):
# initialize placeholders for running values
results = []
min_val_loss = np.Inf
len_train_loader = len(dataloader_train)
# move model to device
model.to(device)
for epoch in range(num_epochs):
print(f"Starting {epoch + 1} epoch ...")
# Training
model.train()
train_loss = 0.0
for inputs, labels in tqdm(dataloader_train, total=len_train_loader):
inputs = inputs.to(device)
labels = labels.to(device)
# Forward pass
y_preds = model(inputs)
loss = criterion(y_preds, labels)
train_loss += loss.item()
# Backward pass
loss.backward()
optimizer.step()
optimizer.zero_grad()
# adjust learning rate
if lr_scheduler is not None:
lr_scheduler.step()
# compute per batch losses, metric value
train_loss = train_loss / len(dataloader_train)
validation_loss, validation_metric = evaluate_model(
model, dataloader_valid, criterion, metric_class, num_classes, device)
print(f'Epoch: {epoch+1}, trainLoss:{train_loss:6.5f}, validationLoss:{validation_loss:6.5f}, {metric_name}:{validation_metric: 4.2f}')
# store results
results.append({'epoch': epoch,
'trainLoss': train_loss,
'validationLoss': validation_loss,
f'{metric_name}': validation_metric})
# if validation loss has decreased, save model and reset variable
if validation_loss <= min_val_loss:
min_val_loss = validation_loss
torch.save(model.state_dict(), f"{output_path}/{model_name}.pt")
# torch.jit.save(torch.jit.script(model), f"{output_path}/{model_name}.pt")
# plot results
results = pd.DataFrame(results)
plot_training_results(results, model_name)
return results
###################################
# FUNCTION TO VISUALIZE MODEL PREDICTIONS
###################################
def visualize_predictions(model : torch.nn.Module, dataSet : Dataset,
axes, device :torch.device, numTestSamples : int,
id_to_color : np.ndarray = train_id_to_color):
"""Function visualizes predictions of input model on samples from
cityscapes dataset provided
Args:
model (torch.nn.Module): model whose output we're to visualize
dataSet (Dataset): dataset to take samples from
device (torch.device): compute device as in GPU, CPU etc
numTestSamples (int): number of samples to plot
id_to_color (np.ndarray) : array to map class to colormap
"""
model.to(device=device)
model.eval()
# predictions on random samples
testSamples = np.random.choice(len(dataSet), numTestSamples).tolist()
# _, axes = plt.subplots(numTestSamples, 3, figsize=(3*6, numTestSamples * 4))
for i, sampleID in enumerate(testSamples):
inputImage, gt = dataSet[sampleID]
# input rgb image
inputImage = inputImage.to(device)
landscape = inverse_transform(inputImage).permute(1, 2, 0).cpu().detach().numpy()
axes[i, 0].imshow(landscape)
axes[i, 0].set_title("Landscape")
# groundtruth label image
label_class = gt.cpu().detach().numpy()
axes[i, 1].imshow(id_to_color[label_class])
axes[i, 1].set_title("Groudtruth Label")
# predicted label image
y_pred = torch.argmax(model(inputImage.unsqueeze(0)), dim=1).squeeze(0)
label_class_predicted = y_pred.cpu().detach().numpy()
axes[i, 2].imshow(id_to_color[label_class_predicted])
axes[i, 2].set_title("Predicted Label")
plt.show()
###################################
# FUNCTION TO VISUALIZE MODEL
# PREDICTIONS ON TEST VIDEO
###################################
def predict_video(model, model_name, input_video_path, output_dir,
target_width, target_height, device):
file_name = input_video_path.split(os.sep)[-1].split('.')[0]
output_filename = f'{file_name}_{model_name}_output.avi'
output_video_path = os.path.join(output_dir, *[output_filename])
# handles for input output videos
input_handle = cv2.VideoCapture(input_video_path)
output_handle = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'DIVX'), \
30, (target_width, target_height))
# create progress bar
num_frames = int(input_handle.get(cv2.CAP_PROP_FRAME_COUNT))
pbar = tqdm(total = num_frames, position=0, leave=True)
while(input_handle.isOpened()):
ret, frame = input_handle.read()
if ret == True:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# create torch tensor to give as input to model
pt_image = preprocess(frame)
pt_image = pt_image.to(device)
# get model prediction and convert to corresponding color
y_pred = torch.argmax(model(pt_image.unsqueeze(0)), dim=1).squeeze(0)
predicted_labels = y_pred.cpu().detach().numpy()
cm_labels = (train_id_to_color[predicted_labels]).astype(np.uint8)
# overlay prediction over input frame
overlay_image = cv2.addWeighted(frame, 1, cm_labels, 0.25, 0)
overlay_image = cv2.cvtColor(overlay_image, cv2.COLOR_RGB2BGR)
# write output result and update progress
output_handle.write(overlay_image)
pbar.update(1)
else:
break
output_handle.release()
input_handle.release()