-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
303 lines (263 loc) · 10.7 KB
/
train.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
import argparse
import logging
import os
import random
from datetime import datetime
import numpy as np
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from apex import amp
from apex.parallel import DistributedDataParallel
from tensorboardX import SummaryWriter
from torch.utils.data.distributed import DistributedSampler
from tqdm import tqdm
from configs import CFG
from criterions import build_criterion
from datas import build_dataset, build_dataloader
from metric import Metric
from models import build_model
from optimizers import build_optimizer
from schedulers import build_scheduler
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('config',
type=str,
help='config file')
parser.add_argument('--checkpoint',
type=str,
help='checkpoint file')
parser.add_argument('--path',
type=str,
default=os.path.join('runs', datetime.now().strftime('%Y%m%d-%H%M%S')),
help='path for experiment output files')
parser.add_argument('--no-validate',
action='store_true',
help='whether not to validate in the training process')
parser.add_argument('-n',
'--nodes',
type=int,
default=1,
help='number of nodes / machines')
parser.add_argument('-g',
'--gpus',
type=int,
default=1,
help='number of GPUs per node / machine')
parser.add_argument('-r',
'--rank-node',
type=int,
default=0,
help='ranking of the current node / machine')
parser.add_argument('--backend',
type=str,
default='nccl',
help='backend for PyTorch DDP')
parser.add_argument('--master-ip',
type=str,
default='localhost',
help='network IP of the master node / machine')
parser.add_argument('--master-port',
type=str,
default='8888',
help='network port of the master process on the master node / machine')
parser.add_argument('--seed',
type=int,
default=42,
help='random seed')
parser.add_argument('--opt-level',
type=str,
default='O0',
help='optimization level for nvidia/apex')
args = parser.parse_args()
args.world_size = args.nodes * args.gpus
return args
def worker(rank_gpu, args):
rank_process = args.gpus * args.rank_node + rank_gpu
# initialize process group
dist.init_process_group(backend=args.backend,
init_method=f'tcp://{args.master_ip}:{args.master_port}',
world_size=args.world_size,
rank=rank_process)
logging.info('train on {} processes'.format(dist.get_world_size()))
# use device cuda:n in the process #n
torch.cuda.set_device(rank_gpu)
device = torch.device('cuda', rank_gpu)
# set random seed
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
# initialize TensorBoard summary writer on the master process
if dist.get_rank() == 0:
writer = SummaryWriter(logdir=args.path)
# build dataset
train_dataset = build_dataset('train')
val_dataset = build_dataset('val')
assert train_dataset.num_classes == val_dataset.num_classes
NUM_CHANNELS = train_dataset.num_channels
NUM_CLASSES = train_dataset.num_classes
# build data sampler
train_sampler = DistributedSampler(train_dataset, shuffle=True)
# build data loader
train_dataloader = build_dataloader(train_dataset, train_sampler, 'train')
val_dataloader = build_dataloader(val_dataset, None, 'val')
# build model
model = build_model(NUM_CHANNELS, NUM_CLASSES)
model.to(device)
# build criterion
criterion = build_criterion()
criterion.to(device)
# build metric
metric = Metric(NUM_CLASSES)
# build optimizer
optimizer = build_optimizer(model)
# build scheduler
scheduler = build_scheduler(optimizer)
# mixed precision
model, optimizer = amp.initialize(model, optimizer, opt_level=args.opt_level)
# DDP
model = DistributedDataParallel(model)
epoch = 0
iteration = 0
best_miou = 0.
# load checkpoint if specified
if args.checkpoint is not None:
if not os.path.isfile(args.checkpoint):
raise RuntimeError('checkpoint {} not found'.format(args.checkpoint))
checkpoint = torch.load(args.checkpoint)
model.module.load_state_dict(checkpoint['model']['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer']['state_dict'])
epoch = checkpoint['optimizer']['epoch']
iteration = checkpoint['optimizer']['iteration']
best_miou = checkpoint['metric']['mIoU']
logging.info('load checkpoint {} with mIoU={:.4f}'.format(args.checkpoint, best_miou))
# train - validation loop
while True:
epoch += 1
if epoch > CFG.EPOCHS:
if dist.get_rank() == 0:
writer.close()
return
train_dataloader.sampler.set_epoch(epoch)
if dist.get_rank() == 0:
lr = optimizer.param_groups[0]['lr']
writer.add_scalar('lr-epoch', lr, epoch)
# train
model.train() # set model to training mode
metric.reset() # reset metric
train_bar = tqdm(train_dataloader, desc='training', ascii=True)
train_loss = 0.
for x, label in train_bar:
iteration += 1
x, label = x.to(device), label.to(device)
y = model(x)
loss = criterion(y, label)
train_loss += loss.item()
if dist.get_rank() == 0:
writer.add_scalar('train/loss-iteration', loss.item(), iteration)
optimizer.zero_grad()
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
optimizer.step()
pred = y.argmax(axis=1)
metric.add(pred.data.cpu().numpy(), label.data.cpu().numpy())
train_bar.set_postfix({
'epoch': epoch,
'loss': f'{loss.item():.4f}',
'PA': f'{metric.PA():.4f}',
'mPA': f'{metric.mPA():.4f}',
'mIoU': f'{metric.mIoU():.4f}',
'P': ','.join([f'{p:.4f}' for p in metric.Ps()]),
'R': ','.join([f'{r:.4f}' for r in metric.Rs()]),
'IoU': ','.join([f'{iou:.4f}' for iou in metric.IoUs()]),
})
train_loss /= len(train_dataloader)
if dist.get_rank() == 0:
writer.add_scalar('train/loss-epoch', train_loss, epoch)
pa, mpa, miou, ps, rs, ious = metric.PA(), metric.mPA(), metric.mIoU(), metric.Ps(), metric.Rs(), metric.IoUs()
if dist.get_rank() == 0:
writer.add_scalar('train/PA-epoch', pa, epoch)
writer.add_scalar('train/mPA-epoch', mpa, epoch)
writer.add_scalar('train/mIoU-epoch', miou, epoch)
# validate
if args.no_validate:
continue
model.eval() # set model to evaluation mode
metric.reset() # reset metric
val_bar = tqdm(val_dataloader, desc='validating', ascii=True)
val_loss = 0.
with torch.no_grad(): # disable gradient back-propagation
for x, label in val_bar:
x, label = x.to(device), label.to(device)
y = model(x)
loss = criterion(y, label)
val_loss += loss.item()
pred = y.argmax(axis=1)
metric.add(pred.data.cpu().numpy(), label.data.cpu().numpy())
val_bar.set_postfix({
'epoch': epoch,
'loss': f'{loss.item():.4f}',
'PA': f'{metric.PA():.4f}',
'mPA': f'{metric.mPA():.4f}',
'mIoU': f'{metric.mIoU():.4f}',
'P': ','.join([f'{p:.4f}' for p in metric.Ps()]),
'R': ','.join([f'{r:.4f}' for r in metric.Rs()]),
'IoU': ','.join([f'{iou:.4f}' for iou in metric.IoUs()]),
})
val_loss /= len(val_dataloader)
if dist.get_rank() == 0:
writer.add_scalar('val/loss-epoch', val_loss, epoch)
pa, mpa, miou, ps, rs, ious = metric.PA(), metric.mPA(), metric.mIoU(), metric.Ps(), metric.Rs(), metric.IoUs()
if dist.get_rank() == 0:
writer.add_scalar('val/PA-epoch', pa, epoch)
writer.add_scalar('val/mPA-epoch', mpa, epoch)
writer.add_scalar('val/mIoU-epoch', miou, epoch)
# adjust learning rate if specified
if scheduler is not None:
scheduler.step(val_loss) # TODO: remove val_loss
# save checkpoint on the master process
if dist.get_rank() == 0:
checkpoint = {
'model': {
'state_dict': model.state_dict(),
},
'optimizer': {
'state_dict': optimizer.state_dict(),
'epoch': epoch,
'iteration': iteration,
},
'metric': {
'PA': pa,
'mPA': mpa,
'mIoU': miou,
'Ps': ps,
'Rs': rs,
'IoUs': ious,
},
}
torch.save(checkpoint, os.path.join(args.path, 'last.pth'))
if miou > best_miou:
best_miou = miou
torch.save(checkpoint, os.path.join(args.path, 'best.pth'))
def main():
# parse command line arguments
args = parse_args()
# create experiment output path if not exists
if not os.path.exists(args.path):
os.makedirs(args.path, exist_ok=True)
# merge config with config file
CFG.merge_from_file(args.config)
# dump config
with open(os.path.join(args.path, 'config.yaml'), 'w') as f:
f.write(CFG.dump())
# log to stdout only
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s: %(message)s',
handlers=[
logging.StreamHandler(),
])
mp.spawn(worker, args=(args,), nprocs=args.gpus)
if __name__ == '__main__':
main()