-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
227 lines (196 loc) · 8.22 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
import argparse
import datetime
import os
import shutil
import sys
import time
import warnings
import random
from functools import partial
import cv2
import torch
import torch.cuda.amp as amp
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data as data
from loguru import logger
from torch.optim.lr_scheduler import MultiStepLR
import utils.config as config
import wandb
from utils.dataset import RefDataset
from engine.engine import train, validate
from model import build_segmenter
from utils.misc import (init_random_seed, set_random_seed, setup_logger,
worker_init_fn)
warnings.filterwarnings("ignore")
cv2.setNumThreads(0)
def get_parser():
parser = argparse.ArgumentParser(
description='Pytorch Referring Expression Segmentation')
parser.add_argument('--config',
default='path to xxx.yaml',
type=str,
help='config file')
parser.add_argument('--opts',
default=None,
nargs=argparse.REMAINDER,
help='override some settings in the config.')
args = parser.parse_args()
assert args.config is not None
cfg = config.load_cfg_from_cfg_file(args.config)
if args.opts is not None:
cfg = config.merge_cfg_from_list(cfg, args.opts)
return cfg
@logger.catch
def main():
args = get_parser()
args.manual_seed = init_random_seed(args.manual_seed)
set_random_seed(args.manual_seed, deterministic=False)
args.ngpus_per_node = torch.cuda.device_count()
args.world_size = args.ngpus_per_node * args.world_size
mp.spawn(main_worker, nprocs=args.ngpus_per_node, args=(args, ))
def main_worker(gpu, args):
args.exp_name = '_'.join([args.exp_name] + [str(name) for name in [args.ladder_dim, args.nhead, args.dim_ffn, args.multi_stage]])
args.output_dir = os.path.join(args.output_folder, args.exp_name)
# local rank & global rank
args.gpu = gpu
args.rank = args.rank * args.ngpus_per_node + gpu
torch.backends.cudnn.enabled = False
torch.cuda.set_device(args.gpu)
# logger
setup_logger(args.output_dir,
distributed_rank=args.gpu,
filename="train.log",
mode="a")
# dist init
# dist.init_process_group(backend=args.dist_backend,
# # init_method=args.dist_url,
# init_method=f'tcp://localhost:{random.randint(6000, 7000)}',
# world_size=args.world_size,
# rank=args.rank,)
dist.init_process_group(backend=args.dist_backend,
init_method=args.dist_url,
world_size=args.world_size,
rank=args.rank)
# wandb
if args.rank == 0:
wandb.init(job_type="training",
mode="offline",
config=args,
project="ETRIS",
name=args.exp_name,
tags=[args.dataset, args.clip_pretrain])
dist.barrier()
# build model
model, param_list = build_segmenter(args)
if args.sync_bn:
model = nn.SyncBatchNorm.convert_sync_batchnorm(model)
logger.info(model)
model = nn.parallel.DistributedDataParallel(model.cuda(),
device_ids=[args.gpu],
find_unused_parameters=False)
# find_unused_parameters=True)
# build optimizer & lr scheduler
optimizer = torch.optim.Adam(param_list, lr=args.base_lr, weight_decay=args.weight_decay)
scheduler = MultiStepLR(optimizer, milestones=args.milestones, gamma=args.lr_decay)
scaler = amp.GradScaler()
# build dataset
args.batch_size = int(args.batch_size / args.ngpus_per_node)
args.batch_size_val = int(args.batch_size_val / args.ngpus_per_node)
args.workers = int((args.workers + args.ngpus_per_node - 1) / args.ngpus_per_node)
train_data = RefDataset(lmdb_dir=args.train_lmdb,
mask_dir=args.mask_root,
dataset=args.dataset,
split=args.train_split,
mode='train',
input_size=args.input_size,
word_length=args.word_len)
val_data = RefDataset(lmdb_dir=args.val_lmdb,
mask_dir=args.mask_root,
dataset=args.dataset,
split=args.val_split,
mode='val',
input_size=args.input_size,
word_length=args.word_len)
# build dataloader
init_fn = partial(worker_init_fn,
num_workers=args.workers,
rank=args.rank,
seed=args.manual_seed)
train_sampler = data.distributed.DistributedSampler(train_data, shuffle=True)
val_sampler = data.distributed.DistributedSampler(val_data, shuffle=False)
train_loader = data.DataLoader(train_data,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True,
worker_init_fn=init_fn,
sampler=train_sampler,
drop_last=True)
val_loader = data.DataLoader(val_data,
batch_size=args.batch_size_val,
shuffle=False,
num_workers=args.workers_val,
pin_memory=True,
sampler=val_sampler,
drop_last=False)
best_IoU = 0.0
# resume
if args.resume:
if os.path.isfile(args.resume):
logger.info("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
best_IoU = checkpoint["best_iou"]
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
logger.info("=> loaded checkpoint '{}' (epoch {})".format(
args.resume, checkpoint['epoch']))
else:
raise ValueError(
"=> resume failed! no checkpoint found at '{}'. Please check args.resume again!"
.format(args.resume))
# start training
start_time = time.time()
for epoch in range(args.start_epoch, args.epochs):
epoch_log = epoch + 1
# shuffle loader
train_sampler.set_epoch(epoch_log)
# train
train(train_loader, model, optimizer, scheduler, scaler, epoch_log, args)
# evaluation
iou, prec_dict = validate(val_loader, model, epoch_log, args)
# save model
if dist.get_rank() == 0:
lastname = os.path.join(args.output_dir, "last_model.pth")
torch.save(
{
'epoch': epoch_log,
'cur_iou': iou,
'best_iou': best_IoU,
'prec': prec_dict,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict()
}, lastname)
if iou >= best_IoU:
best_IoU = iou
bestname = os.path.join(args.output_dir, "best_model.pth")
shutil.copyfile(lastname, bestname)
# update lr
scheduler.step(epoch_log)
torch.cuda.empty_cache()
time.sleep(2)
if dist.get_rank() == 0:
wandb.finish()
logger.info("* Best IoU={} * ".format(best_IoU))
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
logger.info('* Training time {} *'.format(total_time_str))
if __name__ == '__main__':
main()
sys.exit(0)