-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathutils.py
executable file
·328 lines (249 loc) · 11 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
import random
import os
import logging
import pickle
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import faiss
from data.coco_train_dataset import TrainCOCO
from data.coco_eval_dataset import EvalCOCO
from data.cityscapes_train_dataset import TrainCityscapes
from data.cityscapes_eval_dataset import EvalCityscapes
################################################################################
# General-purpose #
################################################################################
def str_list(l):
return '_'.join([str(x) for x in l])
def set_logger(log_path):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Logging to a file
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(file_handler)
# Logging to console
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(stream_handler)
return logger
class Logger(object):
""" Class to update every epoch to keep trace of the results
Methods:
- log() log and save
"""
def __init__(self, path):
self.path = path
self.data = []
def log(self, train_point):
self.data.append(train_point)
with open(os.path.join(self.path), 'wb') as fp:
pickle.dump(self.data, fp, -1)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def get_datetime(time_delta):
days_delta = time_delta // (24*3600)
time_delta = time_delta % (24*3600)
hour_delta = time_delta // 3600
time_delta = time_delta % 3600
mins_delta = time_delta // 60
time_delta = time_delta % 60
secs_delta = time_delta
return '{}:{}:{}:{}'.format(days_delta, hour_delta, mins_delta, secs_delta)
################################################################################
# Metric-related ops #
################################################################################
def _fast_hist(label_true, label_pred, n_class):
mask = (label_true >= 0) & (label_true < n_class) # Exclude unlabelled data.
hist = np.bincount(n_class * label_true[mask] + label_pred[mask],\
minlength=n_class ** 2).reshape(n_class, n_class)
return hist
def scores(label_trues, label_preds, n_class):
hist = np.zeros((n_class, n_class))
for lt, lp in zip(label_trues, label_preds):
hist += _fast_hist(lt.flatten(), lp.flatten(), n_class)
return hist
def get_result_metrics(histogram):
tp = np.diag(histogram)
fp = np.sum(histogram, 0) - tp
fn = np.sum(histogram, 1) - tp
iou = tp / (tp + fp + fn)
prc = tp / (tp + fn)
opc = np.sum(tp) / np.sum(histogram)
result = {"iou": iou,
"mean_iou": np.nanmean(iou),
"precision_per_class (per class accuracy)": prc,
"mean_precision (class-avg accuracy)": np.nanmean(prc),
"overall_precision (pixel accuracy)": opc}
result = {k: 100*v for k, v in result.items()}
return result
def compute_negative_euclidean(featmap, centroids, metric_function):
centroids = centroids.unsqueeze(-1).unsqueeze(-1)
return - (1 - 2*metric_function(featmap)\
+ (centroids*centroids).sum(dim=1).unsqueeze(0)) # negative l2 squared
def get_metric_as_conv(centroids):
N, C = centroids.size()
centroids_weight = centroids.unsqueeze(-1).unsqueeze(-1)
metric_function = nn.Conv2d(C, N, 1, padding=0, stride=1, bias=False)
metric_function.weight.data = centroids_weight
metric_function = nn.DataParallel(metric_function)
metric_function = metric_function.cuda()
return metric_function
################################################################################
# General torch ops #
################################################################################
def freeze_all(model):
for param in model.module.parameters():
param.requires_grad = False
def initialize_classifier(args):
classifier = get_linear(args.in_dim, args.K_train)
classifier = nn.DataParallel(classifier)
classifier = classifier.cuda()
return classifier
def get_linear(indim, outdim):
classifier = nn.Conv2d(indim, outdim, kernel_size=1, stride=1, padding=0, bias=True)
classifier.weight.data.normal_(0, 0.01)
classifier.bias.data.zero_()
return classifier
def feature_flatten(feats):
if len(feats.size()) == 2:
# feature already flattened.
return feats
feats = feats.view(feats.size(0), feats.size(1), -1).transpose(2, 1)\
.contiguous().view(-1, feats.size(1))
return feats
################################################################################
# Faiss related #
################################################################################
def get_faiss_module(args):
res = faiss.StandardGpuResources()
cfg = faiss.GpuIndexFlatConfig()
cfg.useFloat16 = False
cfg.device = 0 #NOTE: Single GPU only.
idx = faiss.GpuIndexFlatL2(res, args.in_dim, cfg)
return idx
def get_init_centroids(args, K, featlist, index):
clus = faiss.Clustering(args.in_dim, K)
clus.seed = np.random.randint(args.seed)
clus.niter = args.kmeans_n_iter
clus.max_points_per_centroid = 10000000
clus.train(featlist, index)
return faiss.vector_float_to_array(clus.centroids).reshape(K, args.in_dim)
def module_update_centroids(index, centroids):
index.reset()
index.add(centroids)
return index
def fix_seed_for_reproducability(seed):
"""
Unfortunately, backward() of [interpolate] functional seems to be never deterministic.
Below are related threads:
https://github.com/pytorch/pytorch/issues/7068
https://discuss.pytorch.org/t/non-deterministic-behavior-of-pytorch-upsample-interpolate/42842?u=sbelharbi
"""
# Use random seed.
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
cudnn.deterministic = True
cudnn.benchmark = False
def worker_init_fn(seed):
return lambda x: np.random.seed(seed + x)
################################################################################
# Training Pipelines #
################################################################################
def postprocess_label(args, K, idx, idx_img, scores, n_dual):
out = scores[idx].topk(1, dim=0)[1].flatten().detach().cpu().numpy()
# Save labels.
if not os.path.exists(os.path.join(args.save_model_path, 'label_' + str(n_dual))):
os.makedirs(os.path.join(args.save_model_path, 'label_' + str(n_dual)))
torch.save(out, os.path.join(args.save_model_path, 'label_' + str(n_dual), '{}.pkl'.format(idx_img)))
# Count for re-weighting.
counts = torch.tensor(np.bincount(out, minlength=K)).float()
return counts
def eqv_transform_if_needed(args, dataloader, indice, input):
if args.equiv:
input = dataloader.dataset.transform_eqv(indice, input)
return input
def get_transform_params(args):
inv_list = []
eqv_list = []
if args.augment:
if args.blur:
inv_list.append('blur')
if args.grey:
inv_list.append('grey')
if args.jitter:
inv_list.extend(['brightness', 'contrast', 'saturation', 'hue'])
if args.equiv:
if args.h_flip:
eqv_list.append('h_flip')
if args.v_flip:
eqv_list.append('v_flip')
if args.random_crop:
eqv_list.append('random_crop')
return inv_list, eqv_list
def collate_train(batch):
if batch[0][-1] is not None:
indice = [b[0] for b in batch]
image1 = torch.stack([b[1] for b in batch])
image2 = torch.stack([b[2] for b in batch])
label1 = torch.stack([b[3] for b in batch])
label2 = torch.stack([b[4] for b in batch])
return indice, image1, image2, label1, label2
indice = [b[0] for b in batch]
image1 = torch.stack([b[1] for b in batch])
return indice, image1
def collate_eval(batch):
indice = [b[0] for b in batch]
image = torch.stack([b[1] for b in batch])
label = torch.stack([b[2] for b in batch])
return indice, image, label
def collate_train_baseline(batch):
if batch[0][-1] is not None:
return collate_eval(batch)
indice = [b[0] for b in batch]
image = torch.stack([b[1] for b in batch])
return indice, image
def get_dataset(args, mode, inv_list=[], eqv_list=[]):
if args.cityscapes:
if mode == 'train':
dataset = TrainCityscapes(args.data_root, labeldir=args.save_model_path, res1=args.res1, res2=args.res2,
split='train', mode='compute', inv_list=inv_list, eqv_list=eqv_list, scale=(args.min_scale, 1))
elif mode == 'train_val':
dataset = EvalCityscapes(args.data_root, res=args.res, split='val', mode='test',
label_mode=args.label_mode, long_image=args.long_image)
elif mode == 'eval_val':
dataset = EvalCityscapes(args.data_root, res=args.res, split=args.val_type,
mode='test', label_mode=args.label_mode, long_image=args.long_image, label=False)
elif mode == 'eval_test':
dataset = EvalCityscapes(args.data_root, res=args.res, split='val', mode='test',
label_mode=args.label_mode, long_image=args.long_image)
else:
if mode == 'train':
dataset = TrainCOCO(args.data_root, labeldir=args.save_model_path, split='train', mode='compute', res1=args.res1,
res2=args.res2, inv_list=inv_list, eqv_list=eqv_list, thing=args.thing, stuff=args.stuff,
scale=(args.min_scale, 1))
elif mode == 'train_val':
dataset = EvalCOCO(args.data_root, res=args.res, split='val', mode='test', stuff=args.stuff, thing=args.thing)
elif mode == 'eval_val':
dataset = EvalCOCO(args.data_root, res=args.res, split=args.val_type, mode='test', label=False)
elif mode == 'eval_test':
dataset = EvalCOCO(args.data_root, res=args.res, split='val', mode='test', stuff=args.stuff, thing=args.thing)
return dataset