-
Notifications
You must be signed in to change notification settings - Fork 0
/
supervised_dct.py
196 lines (150 loc) · 7.54 KB
/
supervised_dct.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
import argparse
import logging
import os
import pprint
import torch
import numpy as np
from torch import nn
import torch.distributed as dist
import torch.backends.cudnn as cudnn
from torch.optim import SGD, AdamW
from torch.utils.data import DataLoader
import yaml
import tqdm
from dataset.semi_dct import SemiDatasetDCT
from model.semseg.segmentor import Segmentor
from util.ohem import ProbOhemCrossEntropy2d
from util.utils import count_params, AverageMeter, intersectionAndUnion, init_log
from util.dist_helper import setup_distributed
parser = argparse.ArgumentParser(description='Semi-Supervised Semantic Segmentation')
parser.add_argument('--config', type=str, required=True)
parser.add_argument('--labeled-id-path', type=str, required=True)
parser.add_argument('--unlabeled-id-path', type=str, default=None)
parser.add_argument('--save-path', type=str, required=True)
parser.add_argument('--local_rank', default=0, type=int)
parser.add_argument('--port', default=None, type=int)
def evaluate(model, loader, mode, cfg, local_rank=-1):
model.eval()
assert mode in ['original', 'sliding_window']
intersection_meter = AverageMeter()
union_meter = AverageMeter()
with torch.no_grad():
for img, mask, id, img_shape in tqdm.tqdm(loader, disable=local_rank != 0):
img = img.cuda()
if mode == 'sliding_window':
grid_img = cfg['crop_size_dct']
grid_mask = cfg['crop_size']
b, _, h_img, w_img = img.shape
h_mask, w_mask = img_shape[0]
final = torch.zeros(b, cfg["nclass"], h_mask, w_mask).cuda()
row_img = 0
row_mask = 0
while row_img < h_img:
col_img = 0
col_mask = 0
while col_img < w_img:
pred = model(
img[:, :, row_img: min(h_img, row_img + grid_img), col_img: min(w_img, col_img + grid_img)],
torch.tensor([min(h_mask, row_mask + grid_mask) - row_mask, min(w_mask, col_mask + grid_mask) - col_mask]).cuda()
)
final[:, :, row_mask: min(h_mask, row_mask + grid_mask), col_mask: min(w_mask, col_mask + grid_mask)] += pred.softmax(dim=1)
col_img += int(grid_img * 2 / 3)
col_mask += int(grid_mask * 2 / 3)
row_img += int(grid_img * 2 / 3)
row_mask += int(grid_mask * 2 / 3)
pred = final.argmax(dim=1)
else:
pred = model(img, img_shape[0]).argmax(dim=1)
intersection, union, target = \
intersectionAndUnion(pred.cpu().numpy(), mask.numpy(), cfg['nclass'], 255)
reduced_intersection = torch.from_numpy(intersection).cuda()
reduced_union = torch.from_numpy(union).cuda()
reduced_target = torch.from_numpy(target).cuda()
dist.all_reduce(reduced_intersection)
dist.all_reduce(reduced_union)
dist.all_reduce(reduced_target)
intersection_meter.update(reduced_intersection.cpu().numpy())
union_meter.update(reduced_union.cpu().numpy())
iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
mIOU = np.mean(iou_class) * 100.0
return mIOU, iou_class
def main():
args = parser.parse_args()
cfg = yaml.load(open(args.config, "r"), Loader=yaml.Loader)
logger = init_log('global', logging.INFO)
logger.propagate = 0
rank, word_size = setup_distributed(port=args.port)
if rank == 0:
logger.info('{}\n'.format(pprint.pformat(cfg)))
if rank == 0:
os.makedirs(args.save_path, exist_ok=True)
cudnn.enabled = True
cudnn.benchmark = True
model = Segmentor(cfg)
if rank == 0:
logger.info('Total params: {:.1f}M\n'.format(count_params(model)))
param_groups = [{'params': model.backbone.parameters(), 'lr': cfg['lr']},
{'params': [param for name, param in model.named_parameters() if 'backbone' not in name],
'lr': cfg['lr'] * cfg['lr_multi']}]
if cfg["optim"] == "SGD":
optimizer = SGD(param_groups, lr=cfg['lr'], momentum=0.9, weight_decay=1e-4)
elif cfg["optim"] == "AdamW":
optimizer = AdamW(param_groups, lr=cfg['lr'], weight_decay=0.01, betas=(0.9, 0.999))
local_rank = int(os.environ["LOCAL_RANK"])
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model.cuda(local_rank)
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[local_rank],
output_device=local_rank, find_unused_parameters=False)
if cfg['criterion']['name'] == 'CELoss':
criterion = nn.CrossEntropyLoss(**cfg['criterion']['kwargs']).cuda(local_rank)
elif cfg['criterion']['name'] == 'OHEM':
criterion = ProbOhemCrossEntropy2d(**cfg['criterion']['kwargs']).cuda(local_rank)
else:
raise NotImplementedError('%s criterion is not implemented' % cfg['criterion']['name'])
trainset = SemiDatasetDCT(cfg['dataset'], cfg['data_root'], 'train_l', cfg['crop_size'], args.labeled_id_path)
valset = SemiDatasetDCT(cfg['dataset'], cfg['data_root'], 'val')
trainsampler = torch.utils.data.distributed.DistributedSampler(trainset)
trainloader = DataLoader(trainset, batch_size=cfg['batch_size'],
pin_memory=True, num_workers=2, drop_last=True, sampler=trainsampler)
valsampler = torch.utils.data.distributed.DistributedSampler(valset)
valloader = DataLoader(valset, batch_size=1, pin_memory=True, num_workers=2,
drop_last=False, sampler=valsampler)
iters = 0
total_iters = len(trainloader) * cfg['epochs']
previous_best = 0.0
for epoch in range(cfg['epochs']):
if rank == 0:
logger.info('===========> Epoch: {:}, LR: {:.4f}, Previous best: {:.2f}'.format(
epoch, optimizer.param_groups[0]['lr'], previous_best))
model.train()
total_loss = 0.0
trainsampler.set_epoch(epoch)
for i, (img, _, mask, img_shape) in enumerate(trainloader):
img, mask = img.cuda(), mask.cuda()
pred = model(img, img_shape[0])
loss = criterion(pred, mask)
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
iters += 1
lr = cfg['lr'] * (1 - iters / total_iters) ** 0.9
optimizer.param_groups[0]["lr"] = lr
optimizer.param_groups[1]["lr"] = lr * cfg['lr_multi']
if (i % (max(2, len(trainloader) // 8)) == 0) and (rank == 0):
logger.info('Iters: {:}, Total loss: {:.3f}'.format(i, total_loss / (i+1)))
if cfg['dataset'] == 'cityscapes':
eval_mode = 'center_crop' if epoch < cfg['epochs'] - 20 else 'sliding_window'
else:
eval_mode = 'original'
mIOU, iou_class = evaluate(model, valloader, eval_mode, cfg)
if rank == 0:
logger.info('***** Evaluation {} ***** >>>> meanIOU: {:.2f}\n'.format(eval_mode, mIOU))
if mIOU > previous_best and rank == 0:
if previous_best != 0:
os.remove(os.path.join(args.save_path, '%s_%.2f.pth' % (cfg['backbone'], previous_best)))
previous_best = mIOU
torch.save(model.module.state_dict(),
os.path.join(args.save_path, '%s_%.2f.pth' % (cfg['backbone'], mIOU)))
if __name__ == '__main__':
main()