-
Notifications
You must be signed in to change notification settings - Fork 92
/
train_isda.py
executable file
·391 lines (295 loc) · 15 KB
/
train_isda.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
import argparse
import time
import torch
import torch.nn as nn
from torch.utils import data
import numpy as np
import pickle
import cv2
import torch.optim as optim
import scipy.misc
import torch.distributed as dist
import torch.backends.cudnn as cudnn
import sys
import os
from tqdm import tqdm
import os.path as osp
import networks
from dataset.datasets import CSDataSet
import math
import random
import time
import logging
# from tensorboardX import SummaryWriter
from utils.pyt_utils import decode_labels, inv_preprocess, decode_predictions
from loss.criterion import CriterionDSN, CriterionOhemDSN
from engine import Engine
from utils.pyt_utils import load_model
# from utils.encoding import DataParallelModel, DataParallelCriterion
from torch.nn import functional as F
IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32)
BATCH_SIZE = 8
DATA_DIRECTORY = 'cityscapes'
DATA_LIST_PATH = './dataset/list/cityscapes/train.lst'
IGNORE_LABEL = 255
INPUT_SIZE = '769,769'
LEARNING_RATE = 1e-2
MOMENTUM = 0.9
NUM_CLASSES = 19
NUM_STEPS = 40000
POWER = 0.9
RANDOM_SEED = 12345
RESTORE_FROM = './dataset/MS_DeepLab_resnet_pretrained_init.pth'
SAVE_NUM_IMAGES = 2
SAVE_PRED_EVERY = 10000
SNAPSHOT_DIR = './isda_results/'
WEIGHT_DECAY = 0.0005
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_parser():
"""Parse all the arguments provided from the CLI.
Returns:
A list of parsed arguments.
"""
parser = argparse.ArgumentParser(description="DeepLab-ResNet Network")
parser.add_argument("--batch-size", type=int, default=BATCH_SIZE,
help="Number of images sent to the network in one step.")
parser.add_argument("--data-dir", type=str, default=DATA_DIRECTORY,
help="Path to the directory containing the PASCAL VOC dataset.")
parser.add_argument("--data-list", type=str, default=DATA_LIST_PATH,
help="Path to the file listing the images in the dataset.")
parser.add_argument("--ignore-label", type=int, default=IGNORE_LABEL,
help="The index of the label to ignore during the training.")
parser.add_argument("--input-size", type=str, default=INPUT_SIZE,
help="Comma-separated string with height and width of images.")
parser.add_argument("--is-training", action="store_true",
help="Whether to updates the running means and variances during the training.")
parser.add_argument("--learning-rate", type=float, default=LEARNING_RATE,
help="Base learning rate for training with polynomial decay.")
parser.add_argument("--momentum", type=float, default=MOMENTUM,
help="Momentum component of the optimiser.")
parser.add_argument("--not-restore-last", action="store_true",
help="Whether to not restore last (FC) layers.")
parser.add_argument("--num-classes", type=int, default=NUM_CLASSES,
help="Number of classes to predict (including background).")
parser.add_argument("--start-iters", type=int, default=0,
help="Number of classes to predict (including background).")
parser.add_argument("--num-steps", type=int, default=NUM_STEPS,
help="Number of training steps.")
parser.add_argument("--print-frequency", type=int, default=50,
help="Number of training steps.")
parser.add_argument("--power", type=float, default=POWER,
help="Decay parameter to compute the learning rate.")
parser.add_argument("--random-mirror", action="store_true",
help="Whether to randomly mirror the inputs during the training.")
parser.add_argument("--random-scale", action="store_true",
help="Whether to randomly scale the inputs during the training.")
parser.add_argument("--random-seed", type=int, default=RANDOM_SEED,
help="Random seed to have reproducible results.")
parser.add_argument("--restore-from", type=str, default=RESTORE_FROM,
help="Where restore model parameters from.")
parser.add_argument("--save-num-images", type=int, default=SAVE_NUM_IMAGES,
help="How many images to save.")
parser.add_argument("--save-pred-every", type=int, default=SAVE_PRED_EVERY,
help="Save summaries and checkpoint every often.")
parser.add_argument("--snapshot-dir", type=str, default=SNAPSHOT_DIR,
help="Where to save snapshots of the model.")
parser.add_argument("--weight-decay", type=float, default=WEIGHT_DECAY,
help="Regularisation parameter for L2-loss.")
parser.add_argument("--gpu", type=str, default='None',
help="choose gpu device.")
parser.add_argument("--model", type=str, default='None',
help="choose model.")
parser.add_argument("--num-workers", type=int, default=8,
help="choose the number of workers.")
parser.add_argument("--ft", type=bool, default=False,
help="fine-tune the model with large input size.")
parser.add_argument("--ohem", type=str2bool, default='False',
help="use hard negative mining")
parser.add_argument("--ohem-thres", type=float, default=0.6,
help="choose the samples with correct probability underthe threshold.")
parser.add_argument("--ohem-keep", type=int, default=200000,
help="choose the samples with correct probability underthe threshold.")
parser.add_argument('--lambda_0', default=7.5, type=float,
help='The hyper-parameter \lambda_0 for ISDA, select from {1, 2.5, 5, 7.5, 10}. ')
return parser
def lr_poly(base_lr, iter, max_iter, power):
return base_lr * ((1 - float(iter) / max_iter) ** (power))
def adjust_learning_rate(optimizer, learning_rate, i_iter, max_iter, power):
"""Sets the learning rate to the initial LR divided by 5 at 60th, 120th and 160th epochs"""
lr = lr_poly(learning_rate, i_iter, max_iter, power)
optimizer.param_groups[0]['lr'] = lr
return lr
def set_bn_eval(m):
classname = m.__class__.__name__
if classname.find('BatchNorm') != -1:
m.eval()
def set_bn_momentum(m):
classname = m.__class__.__name__
if classname.find('BatchNorm') != -1 or classname.find('InPlaceABN') != -1:
m.momentum = 0.0003
class EstimatorCV():
def __init__(self, feature_num, class_num, device):
super(EstimatorCV, self).__init__()
self.class_num = class_num
self.device = device
self.CoVariance = torch.zeros(class_num, feature_num).to(device)
self.Ave = torch.zeros(class_num, feature_num).to(device)
self.Amount = torch.zeros(class_num).to(device)
def update_CV(self, features, labels):
N = features.size(0)
C = self.class_num
A = features.size(1)
NxCxFeatures = features.view(
N, 1, A
).expand(
N, C, A
)
label_mask = (labels == 255).long()
labels = ((1 - label_mask).mul(labels) + label_mask * 19).long()
onehot = torch.zeros(N, C).cuda()
onehot.scatter_(1, labels.view(-1, 1), 1)
NxCxA_onehot = onehot.view(N, C, 1).expand(N, C, A)
features_by_sort = NxCxFeatures.mul(NxCxA_onehot)
Amount_CxA = NxCxA_onehot.sum(0)
Amount_CxA[Amount_CxA == 0] = 1
ave_CxA = features_by_sort.sum(0) / Amount_CxA
var_temp = features_by_sort - \
ave_CxA.expand(N, C, A).mul(NxCxA_onehot)
var_temp = var_temp.pow(2).sum(0).div(Amount_CxA)
sum_weight_CV = onehot.sum(0).view(C, 1).expand(C, A)
weight_CV = sum_weight_CV.div(
sum_weight_CV + self.Amount.view(C, 1).expand(C, A)
)
weight_CV[weight_CV != weight_CV] = 0
additional_CV = weight_CV.mul(1 - weight_CV).mul((self.Ave - ave_CxA).pow(2))
self.CoVariance = (self.CoVariance.mul(1 - weight_CV) + var_temp
.mul(weight_CV)).detach() + additional_CV.detach()
self.Ave = (self.Ave.mul(1 - weight_CV) + ave_CxA.mul(weight_CV)).detach()
self.Amount += onehot.sum(0)
class ISDALoss(nn.Module):
def __init__(self, feature_num, class_num, device):
super(ISDALoss, self).__init__()
self.estimator = EstimatorCV(feature_num, class_num + 1, device)
self.class_num = class_num
self.cross_entropy = nn.CrossEntropyLoss()
def isda_aug(self, fc, features, y, labels, cv_matrix, ratio):
label_mask = (labels == 255).long()
labels = (1 - label_mask).mul(labels).long()
N = features.size(0)
C = self.class_num
A = features.size(1)
weight_m = list(fc.parameters())[0].squeeze()
NxW_ij = weight_m.expand(N, C, A)
NxW_kj = torch.gather(NxW_ij,
1,
labels.view(N, 1, 1)
.expand(N, C, A))
CV_temp = cv_matrix[labels]
sigma2 = ratio * (weight_m - NxW_kj).pow(2).mul(
CV_temp.view(N, 1, A).expand(N, C, A)
).sum(2)
aug_result = y + 0.5 * sigma2.mul((1 - label_mask).view(N, 1).expand(N, C))
return aug_result
def forward(self, features, final_conv, y, target_x, ratio):
# features = model(x)
N, A, H, W = features.size()
target_x = target_x.view(N, 1, target_x.size(1), target_x.size(2)).float()
target_x = F.interpolate(target_x, size=(H, W), mode='nearest', align_corners=None)
target_x = target_x.long().squeeze()
C = self.class_num
features_NHWxA = features.permute(0, 2, 3, 1).contiguous().view(N * H * W, A)
target_x_NHW = target_x.contiguous().view(N * H * W)
y_NHWxC = y.permute(0, 2, 3, 1).contiguous().view(N * H * W, C)
self.estimator.update_CV(features_NHWxA.detach(), target_x_NHW)
isda_aug_y_NHWxC = self.isda_aug(final_conv, features_NHWxA, y_NHWxC, target_x_NHW,
self.estimator.CoVariance.detach(), ratio)
isda_aug_y = isda_aug_y_NHWxC.view(N, H, W, C).permute(0, 3, 1, 2)
return isda_aug_y
def main():
"""Create the model and start the training."""
parser = get_parser()
with Engine(custom_parser=parser) as engine:
args = parser.parse_args()
cudnn.benchmark = True
seed = args.random_seed
if engine.distributed:
seed = engine.local_rank
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
# data loader
h, w = map(int, args.input_size.split(','))
input_size = (h, w)
dataset = CSDataSet(args.data_dir, args.data_list, max_iters=None, crop_size=input_size,
scale=args.random_scale, mirror=args.random_mirror, mean=IMG_MEAN)
train_loader, train_sampler = engine.get_train_loader(dataset)
# config network and criterion
if args.ohem:
criterion = CriterionOhemDSN(thresh=args.ohem_thres, min_kept=args.ohem_keep)
else:
criterion = CriterionDSN() # CriterionCrossEntropy()
seg_model = eval('networks.' + args.model + '.Seg_Model')(
num_classes=args.num_classes, criterion=criterion,
pretrained_model=args.restore_from
)
# group weight and config optimizer
optimizer = optim.SGD(
[{'params': filter(lambda p: p.requires_grad, seg_model.parameters()), 'lr': args.learning_rate}],
lr=args.learning_rate, momentum=args.momentum, weight_decay=args.weight_decay)
optimizer.zero_grad()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
seg_model.to(device)
isda_augmentor_1 = ISDALoss(512, args.num_classes, device)
isda_augmentor_2 = ISDALoss(512, args.num_classes, device)
model = engine.data_parallel(seg_model)
model.train()
if not os.path.exists(args.snapshot_dir):
os.makedirs(args.snapshot_dir)
run = True
global_iteration = args.start_iters
avgloss = 0
while run:
epoch = global_iteration // len(train_loader)
if engine.distributed:
train_sampler.set_epoch(epoch)
bar_format = '{desc}[{elapsed}<{remaining},{rate_fmt}]'
pbar = tqdm(range(len(train_loader)), file=sys.stdout,
bar_format=bar_format)
dataloader = iter(train_loader)
for idx in pbar:
global_iteration += 1
images, labels, _, _ = dataloader.next()
images = images.cuda(non_blocking=True)
labels = labels.long().cuda(non_blocking=True)
optimizer.zero_grad()
lr = adjust_learning_rate(optimizer, args.learning_rate, global_iteration - 1, args.num_steps,
args.power)
[x, x_dsn], [feature_x, feature_x_dsn] = model(images, isda=True)
ratio = args.lambda_0 * global_iteration / args.num_steps
x_isda = isda_augmentor_1(feature_x, model.module.final_conv_1, x, labels, ratio)
x_dsn_isda = isda_augmentor_2(feature_x_dsn, model.module.final_conv_2, x_dsn, labels, ratio)
loss = criterion([x_isda, x_dsn_isda], labels)
reduce_loss = engine.all_reduce_tensor(loss)
loss.backward()
optimizer.step()
print_str = 'Epoch{}/Iters{}'.format(epoch, global_iteration) \
+ ' Iter{}/{}:'.format(idx + 1, len(train_loader)) \
+ ' lr=%.2e' % lr \
+ ' loss=%.2f' % reduce_loss.item()
pbar.set_description(print_str, refresh=False)
if (not engine.distributed) or (engine.distributed and engine.local_rank == 0):
if global_iteration % args.save_pred_every == 0 or global_iteration >= args.num_steps:
print('taking snapshot ...')
torch.save(seg_model.state_dict(),
osp.join(args.snapshot_dir, 'CS_scenes_' + str(global_iteration) + '.pth'))
if global_iteration >= args.num_steps:
run = False
break
if __name__ == '__main__':
main()