forked from Haochen-Wang409/U2PL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_sup.py
315 lines (255 loc) · 9.3 KB
/
train_sup.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
import argparse
import logging
import os
import os.path as osp
import pprint
import random
import time
from datetime import datetime
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.nn.functional as F
import yaml
from tensorboardX import SummaryWriter
from u2pl.dataset.builder import get_loader
from u2pl.models.model_helper import ModelBuilder
from u2pl.utils.dist_helper import setup_distributed
from u2pl.utils.loss_helper import get_criterion
from u2pl.utils.lr_helper import get_optimizer, get_scheduler
from u2pl.utils.utils import (
AverageMeter,
get_rank,
get_world_size,
init_log,
intersectionAndUnion,
load_state,
set_random_seed,
)
parser = argparse.ArgumentParser(description="Semi-Supervised Semantic Segmentation")
parser.add_argument("--config", type=str, default="config.yaml")
parser.add_argument("--local_rank", type=int, default=0)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--port", default=None, type=int)
logger = init_log("global", logging.INFO)
logger.propagate = 0
def main():
global args, cfg
args = parser.parse_args()
seed = args.seed
cfg = yaml.load(open(args.config, "r"), Loader=yaml.Loader)
cfg["exp_path"] = os.path.dirname(args.config)
cfg["save_path"] = os.path.join(cfg["exp_path"], cfg["saver"]["snapshot_dir"])
cudnn.enabled = True
cudnn.benchmark = True
rank, word_size = setup_distributed(port=args.port)
if rank == 0:
logger.info("{}".format(pprint.pformat(cfg)))
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
tb_logger = SummaryWriter(
osp.join(cfg["exp_path"], "log/events_seg/" + current_time)
)
else:
tb_logger = None
if args.seed is not None:
print("set random seed to", args.seed)
set_random_seed(args.seed)
if not osp.exists(cfg["saver"]["snapshot_dir"]) and rank == 0:
os.makedirs(cfg["saver"]["snapshot_dir"])
# Create network.
model = ModelBuilder(cfg["net"])
modules_back = [model.encoder]
if cfg["net"].get("aux_loss", False):
modules_head = [model.auxor, model.decoder]
else:
modules_head = [model.decoder]
if cfg["net"].get("sync_bn", True):
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model.cuda()
local_rank = int(os.environ["LOCAL_RANK"])
model = torch.nn.parallel.DistributedDataParallel(
model,
device_ids=[local_rank],
output_device=local_rank,
find_unused_parameters=False,
)
criterion = get_criterion(cfg)
train_loader_sup, val_loader = get_loader(cfg, seed=seed)
# Optimizer and lr decay scheduler
cfg_trainer = cfg["trainer"]
cfg_optim = cfg_trainer["optimizer"]
times = 10 if "pascal" in cfg["dataset"]["type"] else 1
params_list = []
for module in modules_back:
params_list.append(
dict(params=module.parameters(), lr=cfg_optim["kwargs"]["lr"])
)
for module in modules_head:
params_list.append(
dict(params=module.parameters(), lr=cfg_optim["kwargs"]["lr"] * times)
)
optimizer = get_optimizer(params_list, cfg_optim)
best_prec = 0
last_epoch = 0
# auto_resume > pretrain
if cfg["saver"].get("auto_resume", False):
lastest_model = os.path.join(cfg["save_path"], "ckpt.pth")
if not os.path.exists(lastest_model):
"No checkpoint found in '{}'".format(lastest_model)
else:
print(f"Resume model from: '{lastest_model}'")
best_prec, last_epoch = load_state(
lastest_model, model, optimizer=optimizer, key="model_state"
)
elif cfg["saver"].get("pretrain", False):
laod_state(cfg["saver"]["pretrain"], model, keys="model_state")
optimizer_old = get_optimizer(params_list, cfg_optim)
lr_scheduler = get_scheduler(
cfg_trainer, len(train_loader_sup), optimizer_old, start_epoch=last_epoch
)
# Start to train model
for epoch in range(last_epoch, cfg_trainer["epochs"]):
# Training
train(
model,
optimizer,
lr_scheduler,
criterion,
train_loader_sup,
epoch,
tb_logger,
)
# Validation and store checkpoint
prec = validate(model, val_loader, epoch)
if rank == 0:
state = {
"epoch": epoch,
"model_state": model.state_dict(),
"optimizer_state": optimizer.state_dict(),
"best_miou": best_prec,
}
if prec > best_prec:
best_prec = prec
state["best_miou"] = prec
torch.save(
state, osp.join(cfg["saver"]["snapshot_dir"], "ckpt_best.pth")
)
torch.save(state, osp.join(cfg["saver"]["snapshot_dir"], "ckpt.pth"))
logger.info(
"\033[31m * Currently, the best val result is: {:.2f}\033[0m".format(
best_prec * 100
)
)
tb_logger.add_scalar("mIoU val", prec, epoch)
def train(
model,
optimizer,
lr_scheduler,
criterion,
data_loader,
epoch,
tb_logger,
):
model.train()
data_loader.sampler.set_epoch(epoch)
data_loader_iter = iter(data_loader)
rank, world_size = dist.get_rank(), dist.get_world_size()
losses = AverageMeter(10)
data_times = AverageMeter(10)
batch_times = AverageMeter(10)
learning_rates = AverageMeter(10)
batch_end = time.time()
for step in range(len(data_loader)):
batch_start = time.time()
data_times.update(batch_start - batch_end)
i_iter = epoch * len(data_loader) + step
lr = lr_scheduler.get_lr()
learning_rates.update(lr[0])
lr_scheduler.step()
image, label = data_loader_iter.next()
batch_size, h, w = label.size()
image, label = image.cuda(), label.cuda()
outs = model(image)
pred = outs["pred"]
pred = F.interpolate(pred, (h, w), mode="bilinear", align_corners=True)
if "aux_loss" in cfg["net"].keys():
aux = outs["aux"]
aux = F.interpolate(aux, (h, w), mode="bilinear", align_corners=True)
loss = criterion([pred, aux], label)
else:
loss = criterion(pred, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# gather all loss from different gpus
reduced_loss = loss.clone().detach()
dist.all_reduce(reduced_loss)
losses.update(reduced_loss.item())
batch_end = time.time()
batch_times.update(batch_end - batch_start)
if i_iter % 10 == 0 and rank == 0:
logger.info(
"Iter [{}/{}]\t"
"Data {data_time.val:.2f} ({data_time.avg:.2f})\t"
"Time {batch_time.val:.2f} ({batch_time.avg:.2f})\t"
"Loss {loss.val:.4f} ({loss.avg:.4f})\t"
"LR {lr.val:.5f} ({lr.avg:.5f})\t".format(
i_iter,
cfg["trainer"]["epochs"] * len(data_loader),
data_time=data_times,
batch_time=batch_times,
loss=losses,
lr=learning_rates,
)
)
tb_logger.add_scalar("lr", learning_rates.avg, i_iter)
tb_logger.add_scalar("Loss", losses.avg, i_iter)
def validate(
model,
data_loader,
epoch,
):
model.eval()
data_loader.sampler.set_epoch(epoch)
num_classes, ignore_label = (
cfg["net"]["num_classes"],
cfg["dataset"]["ignore_label"],
)
rank, world_size = dist.get_rank(), dist.get_world_size()
intersection_meter = AverageMeter()
union_meter = AverageMeter()
for step, batch in enumerate(data_loader):
images, labels = batch
images = images.cuda()
labels = labels.long().cuda()
batch_size, h, w = labels.shape
with torch.no_grad():
outs = model(images)
# get the output produced by model_teacher
output = outs["pred"]
output = F.interpolate(output, (h, w), mode="bilinear", align_corners=True)
output = output.data.max(1)[1].cpu().numpy()
target_origin = labels.cpu().numpy()
# start to calculate miou
intersection, union, target = intersectionAndUnion(
output, target_origin, num_classes, ignore_label
)
# gather all validation information
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)
if rank == 0:
for i, iou in enumerate(iou_class):
logger.info(" * class [{}] IoU {:.2f}".format(i, iou * 100))
logger.info(" * epoch {} mIoU {:.2f}".format(epoch, mIoU * 100))
return mIoU
if __name__ == "__main__":
main()