-
Notifications
You must be signed in to change notification settings - Fork 19
/
trainUDA.py
655 lines (522 loc) · 25.8 KB
/
trainUDA.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import argparse
import os
import sys
import random
import timeit
import datetime
import numpy as np
import pickle
import scipy.misc
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.utils import data, model_zoo
from torch.autograd import Variable
import torchvision.transforms as transform
from model.deeplabv2 import Res_Deeplab
from utils.loss import CrossEntropy2d
from utils.loss import CrossEntropyLoss2dPixelWiseWeighted
from utils.loss import MSELoss2d
from utils import transformmasks
from utils import transformsgpu
from utils.helpers import colorize_mask
import utils.palette as palette
from utils.sync_batchnorm import convert_model
from utils.sync_batchnorm import DataParallelWithCallback
from data import get_loader, get_data_path
from data.augmentations import *
from tqdm import tqdm
import PIL
from torchvision import transforms
import json
from torch.utils import tensorboard
from evaluateUDA import evaluate
import time
start = timeit.default_timer()
start_writeable = datetime.datetime.now().strftime('%m-%d_%H-%M')
def get_arguments():
"""Parse all the arguments provided from the CLI.
Returns:
A list of parsed arguments.
"""
parser = argparse.ArgumentParser(description="DeepLab-ResNet Network")
parser.add_argument("--gpus", type=int, default=1,
help="choose number of gpu devices to use (default: 1)")
parser.add_argument("-c", "--config", type=str, default='config.json',
help='Path to the config file (default: config.json)')
parser.add_argument("-r", "--resume", type=str, default=None,
help='Path to the .pth file to resume from (default: None)')
parser.add_argument("-n", "--name", type=str, default=None, required=True,
help='Name of the run (default: None)')
parser.add_argument("--save-images", type=str, default=None,
help='Include to save images (default: None)')
return parser.parse_args()
def loss_calc(pred, label):
label = Variable(label.long()).cuda()
if len(gpus) > 1:
criterion = torch.nn.DataParallel(CrossEntropy2d(ignore_label=ignore_label), device_ids=gpus).cuda() # Ignore label ??
else:
criterion = CrossEntropy2d(ignore_label=ignore_label).cuda() # Ignore label ??
return criterion(pred, label)
def lr_poly(base_lr, iter, max_iter, power):
return base_lr * ((1 - float(iter) / max_iter) ** (power))
def adjust_learning_rate(optimizer, i_iter):
lr = lr_poly(learning_rate, i_iter, num_iterations, lr_power)
optimizer.param_groups[0]['lr'] = lr
if len(optimizer.param_groups) > 1 :
optimizer.param_groups[1]['lr'] = lr * 10
def create_ema_model(model):
#ema_model = getattr(models, config['arch']['type'])(self.train_loader.dataset.num_classes, **config['arch']['args']).to(self.device)
ema_model = Res_Deeplab(num_classes=num_classes)
for param in ema_model.parameters():
param.detach_()
mp = list(model.parameters())
mcp = list(ema_model.parameters())
n = len(mp)
for i in range(0, n):
mcp[i].data[:] = mp[i].data[:].clone()
#_, availble_gpus = self._get_available_devices(self.config['n_gpu'])
#ema_model = torch.nn.DataParallel(ema_model, device_ids=availble_gpus)
if len(gpus)>1:
#return torch.nn.DataParallel(ema_model, device_ids=gpus)
if use_sync_batchnorm:
ema_model = convert_model(ema_model)
ema_model = DataParallelWithCallback(ema_model, device_ids=gpus)
else:
ema_model = torch.nn.DataParallel(ema_model, device_ids=gpus)
return ema_model
def update_ema_variables(ema_model, model, alpha_teacher, iteration):
# Use the "true" average until the exponential average is more correct
alpha_teacher = min(1 - 1 / (iteration + 1), alpha_teacher)
if len(gpus)>1:
for ema_param, param in zip(ema_model.module.parameters(), model.module.parameters()):
#ema_param.data.mul_(alpha).add_(1 - alpha, param.data)
ema_param.data[:] = alpha_teacher * ema_param[:].data[:] + (1 - alpha_teacher) * param[:].data[:]
else:
for ema_param, param in zip(ema_model.parameters(), model.parameters()):
#ema_param.data.mul_(alpha).add_(1 - alpha, param.data)
ema_param.data[:] = alpha_teacher * ema_param[:].data[:] + (1 - alpha_teacher) * param[:].data[:]
return ema_model
def strongTransform(parameters, data=None, target=None):
assert ((data is not None) or (target is not None))
data, target = transformsgpu.oneMix(mask = parameters["Mix"], data = data, target = target)
data, target = transformsgpu.colorJitter(colorJitter = parameters["ColorJitter"], img_mean = torch.from_numpy(IMG_MEAN.copy()).cuda(), data = data, target = target)
data, target = transformsgpu.gaussian_blur(blur = parameters["GaussianBlur"], data = data, target = target)
data, target = transformsgpu.flip(flip = parameters["flip"], data = data, target = target)
return data, target
def weakTransform(parameters, data=None, target=None):
data, target = transformsgpu.flip(flip = parameters["flip"], data = data, target = target)
return data, target
def getWeakInverseTransformParameters(parameters):
return parameters
def getStrongInverseTransformParameters(parameters):
return parameters
class DeNormalize(object):
def __init__(self, mean):
self.mean = mean
def __call__(self, tensor):
IMG_MEAN = torch.from_numpy(self.mean.copy())
IMG_MEAN, _ = torch.broadcast_tensors(IMG_MEAN.unsqueeze(1).unsqueeze(2), tensor)
tensor = tensor+IMG_MEAN
tensor = (tensor/255).float()
tensor = torch.flip(tensor,(0,))
return tensor
class Learning_Rate_Object(object):
def __init__(self,learning_rate):
self.learning_rate = learning_rate
def save_image(image, epoch, id, palette):
with torch.no_grad():
if image.shape[0] == 3:
restore_transform = transforms.Compose([
DeNormalize(IMG_MEAN),
transforms.ToPILImage()])
image = restore_transform(image)
#image = PIL.Image.fromarray(np.array(image)[:, :, ::-1]) # BGR->RGB
image.save(os.path.join('../visualiseImages/', str(epoch)+ id + '.png'))
else:
mask = image.numpy()
colorized_mask = colorize_mask(mask, palette)
colorized_mask.save(os.path.join('../visualiseImages', str(epoch)+ id + '.png'))
def _save_checkpoint(iteration, model, optimizer, config, ema_model, save_best=False, overwrite=True):
checkpoint = {
'iteration': iteration,
'optimizer': optimizer.state_dict(),
'config': config,
}
if len(gpus) > 1:
checkpoint['model'] = model.module.state_dict()
if train_unlabeled:
checkpoint['ema_model'] = ema_model.module.state_dict()
else:
checkpoint['model'] = model.state_dict()
if train_unlabeled:
checkpoint['ema_model'] = ema_model.state_dict()
if save_best:
filename = os.path.join(checkpoint_dir, f'best_model.pth')
torch.save(checkpoint, filename)
print("Saving current best model: best_model.pth")
else:
filename = os.path.join(checkpoint_dir, f'checkpoint-iter{iteration}.pth')
print(f'\nSaving a checkpoint: {filename} ...')
torch.save(checkpoint, filename)
if overwrite:
try:
os.remove(os.path.join(checkpoint_dir, f'checkpoint-iter{iteration - save_checkpoint_every}.pth'))
except:
pass
def _resume_checkpoint(resume_path, model, optimizer, ema_model):
print(f'Loading checkpoint : {resume_path}')
checkpoint = torch.load(resume_path)
# Load last run info, the model params, the optimizer and the loggers
iteration = checkpoint['iteration'] + 1
print('Starting at iteration: ' + str(iteration))
if len(gpus) > 1:
model.module.load_state_dict(checkpoint['model'])
else:
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer'])
if train_unlabeled:
if len(gpus) > 1:
ema_model.module.load_state_dict(checkpoint['ema_model'])
else:
ema_model.load_state_dict(checkpoint['ema_model'])
return iteration, model, optimizer, ema_model
def main():
print(config)
best_mIoU = 0
if consistency_loss == 'MSE':
if len(gpus) > 1:
unlabeled_loss = torch.nn.DataParallel(MSELoss2d(), device_ids=gpus).cuda()
else:
unlabeled_loss = MSELoss2d().cuda()
elif consistency_loss == 'CE':
if len(gpus) > 1:
unlabeled_loss = torch.nn.DataParallel(CrossEntropyLoss2dPixelWiseWeighted(ignore_index=ignore_label), device_ids=gpus).cuda()
else:
unlabeled_loss = CrossEntropyLoss2dPixelWiseWeighted(ignore_index=ignore_label).cuda()
cudnn.enabled = True
# create network
model = Res_Deeplab(num_classes=num_classes)
# load pretrained parameters
#saved_state_dict = torch.load(args.restore_from)
# load pretrained parameters
if restore_from[:4] == 'http' :
saved_state_dict = model_zoo.load_url(restore_from)
else:
saved_state_dict = torch.load(restore_from)
# Copy loaded parameters to model
new_params = model.state_dict().copy()
for name, param in new_params.items():
if name in saved_state_dict and param.size() == saved_state_dict[name].size():
new_params[name].copy_(saved_state_dict[name])
model.load_state_dict(new_params)
# init ema-model
if train_unlabeled:
ema_model = create_ema_model(model)
ema_model.train()
ema_model = ema_model.cuda()
else:
ema_model = None
if len(gpus)>1:
if use_sync_batchnorm:
model = convert_model(model)
model = DataParallelWithCallback(model, device_ids=gpus)
else:
model = torch.nn.DataParallel(model, device_ids=gpus)
model.train()
model.cuda()
cudnn.benchmark = True
if dataset == 'cityscapes':
data_loader = get_loader('cityscapes')
data_path = get_data_path('cityscapes')
if random_crop:
data_aug = Compose([RandomCrop_city(input_size)])
else:
data_aug = None
#data_aug = Compose([RandomHorizontallyFlip()])
train_dataset = data_loader(data_path, is_transform=True, augmentations=data_aug, img_size=input_size, img_mean = IMG_MEAN)
train_dataset_size = len(train_dataset)
print ('dataset size: ', train_dataset_size)
if labeled_samples is None:
trainloader = data.DataLoader(train_dataset,
batch_size=batch_size, shuffle=True, num_workers=num_workers, pin_memory=True)
trainloader_remain = data.DataLoader(train_dataset,
batch_size=batch_size, shuffle=True, num_workers=num_workers, pin_memory=True)
trainloader_remain_iter = iter(trainloader_remain)
else:
partial_size = labeled_samples
print('Training on number of samples:', partial_size)
np.random.seed(random_seed)
trainloader_remain = data.DataLoader(train_dataset,
batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True)
trainloader_remain_iter = iter(trainloader_remain)
#New loader for Domain transfer
if True:
data_loader = get_loader('gta')
data_path = get_data_path('gta')
if random_crop:
data_aug = Compose([RandomCrop_gta(input_size)])
else:
data_aug = None
#data_aug = Compose([RandomHorizontallyFlip()])
train_dataset = data_loader(data_path, list_path = './data/gta5_list/train.txt', augmentations=data_aug, img_size=(1280,720), mean=IMG_MEAN)
trainloader = data.DataLoader(train_dataset,
batch_size=batch_size, shuffle=True, num_workers=num_workers, pin_memory=True)
trainloader_iter = iter(trainloader)
print('gta size:',len(trainloader))
#Load new data for domain_transfer
# optimizer for segmentation network
learning_rate_object = Learning_Rate_Object(config['training']['learning_rate'])
if optimizer_type == 'SGD':
if len(gpus) > 1:
optimizer = optim.SGD(model.module.optim_parameters(learning_rate_object),
lr=learning_rate, momentum=momentum,weight_decay=weight_decay)
else:
optimizer = optim.SGD(model.optim_parameters(learning_rate_object),
lr=learning_rate, momentum=momentum,weight_decay=weight_decay)
elif optimizer_type == 'Adam':
if len(gpus) > 1:
optimizer = optim.Adam(model.module.optim_parameters(learning_rate_object),
lr=learning_rate, momentum=momentum,weight_decay=weight_decay)
else:
optimizer = optim.Adam(model.optim_parameters(learning_rate_object),
lr=learning_rate, weight_decay=weight_decay)
optimizer.zero_grad()
interp = nn.Upsample(size=(input_size[0], input_size[1]), mode='bilinear', align_corners=True)
start_iteration = 0
if args.resume:
start_iteration, model, optimizer, ema_model = _resume_checkpoint(args.resume, model, optimizer, ema_model)
accumulated_loss_l = []
accumulated_loss_u = []
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
with open(checkpoint_dir + '/config.json', 'w') as handle:
json.dump(config, handle, indent=4, sort_keys=True)
epochs_since_start = 0
for i_iter in range(start_iteration, num_iterations):
model.train()
loss_u_value = 0
loss_l_value = 0
optimizer.zero_grad()
if lr_schedule:
adjust_learning_rate(optimizer, i_iter)
# training loss for labeled data only
try:
batch = next(trainloader_iter)
if batch[0].shape[0] != batch_size:
batch = next(trainloader_iter)
except:
epochs_since_start = epochs_since_start + 1
print('Epochs since start: ',epochs_since_start)
trainloader_iter = iter(trainloader)
batch = next(trainloader_iter)
#if random_flip:
# weak_parameters={"flip":random.randint(0,1)}
#else:
weak_parameters={"flip": 0}
images, labels, _, _ = batch
images = images.cuda()
labels = labels.cuda().long()
#images, labels = weakTransform(weak_parameters, data = images, target = labels)
pred = interp(model(images))
L_l = loss_calc(pred, labels) # Cross entropy loss for labeled data
#L_l = torch.Tensor([0.0]).cuda()
if train_unlabeled:
try:
batch_remain = next(trainloader_remain_iter)
if batch_remain[0].shape[0] != batch_size:
batch_remain = next(trainloader_remain_iter)
except:
trainloader_remain_iter = iter(trainloader_remain)
batch_remain = next(trainloader_remain_iter)
images_remain, _, _, _, _ = batch_remain
images_remain = images_remain.cuda()
inputs_u_w, _ = weakTransform(weak_parameters, data = images_remain)
#inputs_u_w = inputs_u_w.clone()
logits_u_w = interp(ema_model(inputs_u_w))
logits_u_w, _ = weakTransform(getWeakInverseTransformParameters(weak_parameters), data = logits_u_w.detach())
pseudo_label = torch.softmax(logits_u_w.detach(), dim=1)
max_probs, targets_u_w = torch.max(pseudo_label, dim=1)
if mix_mask == "class":
for image_i in range(batch_size):
classes = torch.unique(labels[image_i])
#classes=classes[classes!=ignore_label]
nclasses = classes.shape[0]
#if nclasses > 0:
classes = (classes[torch.Tensor(np.random.choice(nclasses, int((nclasses+nclasses%2)/2),replace=False)).long()]).cuda()
if image_i == 0:
MixMask0 = transformmasks.generate_class_mask(labels[image_i], classes).unsqueeze(0).cuda()
else:
MixMask1 = transformmasks.generate_class_mask(labels[image_i], classes).unsqueeze(0).cuda()
elif mix_mask == None:
MixMask = torch.ones((inputs_u_w.shape))
strong_parameters = {"Mix": MixMask0}
if random_flip:
strong_parameters["flip"] = random.randint(0, 1)
else:
strong_parameters["flip"] = 0
if color_jitter:
strong_parameters["ColorJitter"] = random.uniform(0, 1)
else:
strong_parameters["ColorJitter"] = 0
if gaussian_blur:
strong_parameters["GaussianBlur"] = random.uniform(0, 1)
else:
strong_parameters["GaussianBlur"] = 0
inputs_u_s0, _ = strongTransform(strong_parameters, data = torch.cat((images[0].unsqueeze(0),images_remain[0].unsqueeze(0))))
strong_parameters["Mix"] = MixMask1
inputs_u_s1, _ = strongTransform(strong_parameters, data = torch.cat((images[1].unsqueeze(0),images_remain[1].unsqueeze(0))))
inputs_u_s = torch.cat((inputs_u_s0,inputs_u_s1))
logits_u_s = interp(model(inputs_u_s))
strong_parameters["Mix"] = MixMask0
_, targets_u0 = strongTransform(strong_parameters, target = torch.cat((labels[0].unsqueeze(0),targets_u_w[0].unsqueeze(0))))
strong_parameters["Mix"] = MixMask1
_, targets_u1 = strongTransform(strong_parameters, target = torch.cat((labels[1].unsqueeze(0),targets_u_w[1].unsqueeze(0))))
targets_u = torch.cat((targets_u0,targets_u1)).long()
if pixel_weight == "threshold_uniform":
unlabeled_weight = torch.sum(max_probs.ge(0.968).long() == 1).item() / np.size(np.array(targets_u.cpu()))
pixelWiseWeight = unlabeled_weight * torch.ones(max_probs.shape).cuda()
elif pixel_weight == "threshold":
pixelWiseWeight = max_probs.ge(0.968).float().cuda()
elif pixel_weight == False:
pixelWiseWeight = torch.ones(max_probs.shape).cuda()
onesWeights = torch.ones((pixelWiseWeight.shape)).cuda()
strong_parameters["Mix"] = MixMask0
_, pixelWiseWeight0 = strongTransform(strong_parameters, target = torch.cat((onesWeights[0].unsqueeze(0),pixelWiseWeight[0].unsqueeze(0))))
strong_parameters["Mix"] = MixMask1
_, pixelWiseWeight1 = strongTransform(strong_parameters, target = torch.cat((onesWeights[1].unsqueeze(0),pixelWiseWeight[1].unsqueeze(0))))
pixelWiseWeight = torch.cat((pixelWiseWeight0,pixelWiseWeight1)).cuda()
if consistency_loss == 'MSE':
unlabeled_weight = torch.sum(max_probs.ge(0.968).long() == 1).item() / np.size(np.array(targets_u.cpu()))
#pseudo_label = torch.cat((pseudo_label[1].unsqueeze(0),pseudo_label[0].unsqueeze(0)))
L_u = consistency_weight * unlabeled_weight * unlabeled_loss(logits_u_s, pseudo_label)
elif consistency_loss == 'CE':
L_u = consistency_weight * unlabeled_loss(logits_u_s, targets_u, pixelWiseWeight)
loss = L_l + L_u
else:
loss = L_l
if len(gpus) > 1:
#print('before mean = ',loss)
loss = loss.mean()
#print('after mean = ',loss)
loss_l_value += L_l.mean().item()
if train_unlabeled:
loss_u_value += L_u.mean().item()
else:
loss_l_value += L_l.item()
if train_unlabeled:
loss_u_value += L_u.item()
loss.backward()
optimizer.step()
# update Mean teacher network
if ema_model is not None:
alpha_teacher = 0.99
ema_model = update_ema_variables(ema_model = ema_model, model = model, alpha_teacher=alpha_teacher, iteration=i_iter)
print('iter = {0:6d}/{1:6d}, loss_l = {2:.3f}, loss_u = {3:.3f}'.format(i_iter, num_iterations, loss_l_value, loss_u_value))
if i_iter % save_checkpoint_every == 0 and i_iter!=0:
if epochs_since_start * len(trainloader) < save_checkpoint_every:
_save_checkpoint(i_iter, model, optimizer, config, ema_model, overwrite=False)
else:
_save_checkpoint(i_iter, model, optimizer, config, ema_model)
if config['utils']['tensorboard']:
if 'tensorboard_writer' not in locals():
tensorboard_writer = tensorboard.SummaryWriter(log_dir, flush_secs=30)
accumulated_loss_l.append(loss_l_value)
if train_unlabeled:
accumulated_loss_u.append(loss_u_value)
if i_iter % log_per_iter == 0 and i_iter != 0:
tensorboard_writer.add_scalar('Training/Supervised loss', np.mean(accumulated_loss_l), i_iter)
accumulated_loss_l = []
if train_unlabeled:
tensorboard_writer.add_scalar('Training/Unsupervised loss', np.mean(accumulated_loss_u), i_iter)
accumulated_loss_u = []
if i_iter % val_per_iter == 0 and i_iter != 0:
model.eval()
if dataset == 'cityscapes':
mIoU, eval_loss = evaluate(model, dataset, ignore_label=250, input_size=(512,1024), save_dir=checkpoint_dir)
model.train()
if mIoU > best_mIoU and save_best_model:
best_mIoU = mIoU
_save_checkpoint(i_iter, model, optimizer, config, ema_model, save_best=True)
if config['utils']['tensorboard']:
tensorboard_writer.add_scalar('Validation/mIoU', mIoU, i_iter)
tensorboard_writer.add_scalar('Validation/Loss', eval_loss, i_iter)
if save_unlabeled_images and train_unlabeled and i_iter % save_checkpoint_every == 0:
# Saves two mixed images and the corresponding prediction
save_image(inputs_u_s[0].cpu(),i_iter,'input1',palette.CityScpates_palette)
save_image(inputs_u_s[1].cpu(),i_iter,'input2',palette.CityScpates_palette)
_, pred_u_s = torch.max(logits_u_s, dim=1)
save_image(pred_u_s[0].cpu(),i_iter,'pred1',palette.CityScpates_palette)
save_image(pred_u_s[1].cpu(),i_iter,'pred2',palette.CityScpates_palette)
_save_checkpoint(num_iterations, model, optimizer, config, ema_model)
model.eval()
if dataset == 'cityscapes':
mIoU, val_loss = evaluate(model, dataset, ignore_label=250, input_size=(512,1024), save_dir=checkpoint_dir)
model.train()
if mIoU > best_mIoU and save_best_model:
best_mIoU = mIoU
_save_checkpoint(i_iter, model, optimizer, config, ema_model, save_best=True)
if config['utils']['tensorboard']:
tensorboard_writer.add_scalar('Validation/mIoU', mIoU, i_iter)
tensorboard_writer.add_scalar('Validation/Loss', val_loss, i_iter)
end = timeit.default_timer()
print('Total time: ' + str(end-start) + 'seconds')
if __name__ == '__main__':
print('---------------------------------Starting---------------------------------')
args = get_arguments()
if False:#args.resume:
config = torch.load(args.resume)['config']
else:
config = json.load(open(args.config))
model = config['model']
dataset = config['dataset']
if config['pretrained'] == 'coco':
restore_from = 'http://vllab1.ucmerced.edu/~whung/adv-semi-seg/resnet101COCO-41f33a49.pth'
num_classes=19
IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype=np.float32)
batch_size = config['training']['batch_size']
num_iterations = config['training']['num_iterations']
input_size_string = config['training']['data']['input_size']
h, w = map(int, input_size_string.split(','))
input_size = (h, w)
ignore_label = config['ignore_label']
learning_rate = config['training']['learning_rate']
optimizer_type = config['training']['optimizer']
lr_schedule = config['training']['lr_schedule']
lr_power = config['training']['lr_schedule_power']
weight_decay = config['training']['weight_decay']
momentum = config['training']['momentum']
num_workers = config['training']['num_workers']
use_sync_batchnorm = config['training']['use_sync_batchnorm']
random_seed = config['seed']
labeled_samples = config['training']['data']['labeled_samples']
#unlabeled CONFIGURATIONS
train_unlabeled = config['training']['unlabeled']['train_unlabeled']
mix_mask = config['training']['unlabeled']['mix_mask']
pixel_weight = config['training']['unlabeled']['pixel_weight']
consistency_loss = config['training']['unlabeled']['consistency_loss']
consistency_weight = config['training']['unlabeled']['consistency_weight']
random_flip = config['training']['unlabeled']['flip']
color_jitter = config['training']['unlabeled']['color_jitter']
gaussian_blur = config['training']['unlabeled']['blur']
random_scale = config['training']['data']['scale']
random_crop = config['training']['data']['crop']
save_checkpoint_every = config['utils']['save_checkpoint_every']
if args.resume:
checkpoint_dir = os.path.join(*args.resume.split('/')[:-1]) + '_resume-' + start_writeable
else:
checkpoint_dir = os.path.join(config['utils']['checkpoint_dir'], start_writeable + '-' + args.name)
log_dir = checkpoint_dir
val_per_iter = config['utils']['val_per_iter']
use_tensorboard = config['utils']['tensorboard']
log_per_iter = config['utils']['log_per_iter']
save_best_model = config['utils']['save_best_model']
if args.save_images:
print('Saving unlabeled images')
save_unlabeled_images = True
else:
save_unlabeled_images = False
gpus = (0,1,2,3)[:args.gpus]
main()