-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtrain_source.py
executable file
·201 lines (160 loc) · 6.91 KB
/
train_source.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
import argparse
import os
import datetime
import logging
import time
import torch
import torch.nn as nn
import torch.utils
import torch.distributed
from torch.utils.data import DataLoader
from core.configs import cfg
from core.datasets import build_dataset
from core.models import build_feature_extractor, build_classifier
from core.solver import adjust_learning_rate
from core.utils.misc import mkdir
from core.utils.logger import setup_logger
from core.utils.metric_logger import MetricLogger
from core.utils.utils import set_random_seed
import setproctitle
import warnings
warnings.filterwarnings('ignore')
def train(cfg):
logger = logging.getLogger("Source_Only.trainer")
# create network
device = torch.device(cfg.MODEL.DEVICE)
feature_extractor = build_feature_extractor(cfg)
feature_extractor.to(device)
classifier = build_classifier(cfg)
classifier.to(device)
print(classifier)
# init optimizer
optimizer_fea = torch.optim.SGD(feature_extractor.parameters(), lr=cfg.SOLVER.BASE_LR, momentum=cfg.SOLVER.MOMENTUM,
weight_decay=cfg.SOLVER.WEIGHT_DECAY)
optimizer_fea.zero_grad()
optimizer_cls = torch.optim.SGD(classifier.parameters(), lr=cfg.SOLVER.BASE_LR * 10, momentum=cfg.SOLVER.MOMENTUM,
weight_decay=cfg.SOLVER.WEIGHT_DECAY)
optimizer_cls.zero_grad()
# load checkpoint
if cfg.resume:
logger.info("Loading checkpoint from {}".format(cfg.resume))
checkpoint = torch.load(cfg.resume, map_location=torch.device('cpu'))
feature_extractor.load_state_dict(checkpoint['feature_extractor'])
classifier.load_state_dict(checkpoint['classifier'])
# init data loader
src_train_data = build_dataset(cfg, mode='train', is_source=True)
src_train_loader = DataLoader(src_train_data, batch_size=cfg.SOLVER.BATCH_SIZE, shuffle=True, num_workers=4,
pin_memory=True, drop_last=True)
# init loss
sup_criterion = nn.CrossEntropyLoss(ignore_index=255)
iteration = 0
start_training_time = time.time()
end = time.time()
max_iters = cfg.SOLVER.MAX_ITER
meters = MetricLogger(delimiter=" ")
logger.info(">>>>>>>>>>>>>>>> Start Training >>>>>>>>>>>>>>>>")
feature_extractor.train()
classifier.train()
for batch_index, src_data in enumerate(src_train_loader):
data_time = time.time() - end
current_lr = adjust_learning_rate(cfg.SOLVER.LR_METHOD, cfg.SOLVER.BASE_LR, iteration, max_iters,
power=cfg.SOLVER.LR_POWER)
for index in range(len(optimizer_fea.param_groups)):
optimizer_fea.param_groups[index]['lr'] = current_lr
for index in range(len(optimizer_cls.param_groups)):
optimizer_cls.param_groups[index]['lr'] = current_lr * 10
optimizer_fea.zero_grad()
optimizer_cls.zero_grad()
src_input, src_label = src_data['img'], src_data['label']
src_input = src_input.cuda(non_blocking=True)
src_label = src_label.cuda(non_blocking=True)
src_size = src_input.shape[-2:]
src_out = classifier(feature_extractor(src_input), size=src_size)
# source supervision loss
loss = torch.Tensor([0]).cuda()
loss_sup = sup_criterion(src_out, src_label)
meters.update(loss_sup=loss_sup.item())
loss += loss_sup
loss.backward()
optimizer_fea.step()
optimizer_cls.step()
batch_time = time.time() - end
end = time.time()
meters.update(time=batch_time, data=data_time)
eta_seconds = meters.time.global_avg * (cfg.SOLVER.STOP_ITER - iteration)
eta_string = str(datetime.timedelta(seconds=int(eta_seconds)))
iteration += 1
if iteration % 20 == 0 or iteration == max_iters:
logger.info(
meters.delimiter.join(
[
"eta: {eta}",
"iter: {iter}",
"{meters}",
"lr: {lr:.6f}",
"max mem: {memory:.02f} GB"
]
).format(
eta=eta_string,
iter=iteration,
meters=str(meters),
lr=optimizer_fea.param_groups[0]["lr"],
memory=torch.cuda.max_memory_allocated() / 1024.0 / 1024.0 / 1024.0
)
)
if iteration == cfg.SOLVER.MAX_ITER or iteration % cfg.SOLVER.CHECKPOINT_PERIOD == 0:
filename = os.path.join(cfg.OUTPUT_DIR, "model_iter{:06d}.pth".format(iteration))
torch.save({'iteration': iteration,
'feature_extractor': feature_extractor.state_dict(),
'classifier': classifier.state_dict(),
'optimizer_fea': optimizer_fea.state_dict(),
'optimizer_cls': optimizer_cls.state_dict(),
}, filename)
if iteration == cfg.SOLVER.MAX_ITER:
break
if iteration == cfg.SOLVER.STOP_ITER:
break
total_training_time = time.time() - start_training_time
total_time_str = str(datetime.timedelta(seconds=total_training_time))
logger.info(
"Total training time: {} ({:.4f} s / it)".format(
total_time_str, total_training_time / cfg.SOLVER.STOP_ITER
)
)
def main():
parser = argparse.ArgumentParser(description="Active Domain Adaptive Semantic Segmentation Training")
parser.add_argument("-cfg",
"--config-file",
default="",
metavar="FILE",
help="path to config file",
type=str)
parser.add_argument("--proctitle",
type=str,
default="AL-RIPU",
help="allow a process to change its title", )
parser.add_argument(
"opts",
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER
)
args = parser.parse_args()
if args.opts is not None:
args.opts[-1] = args.opts[-1].strip('\r\n')
torch.backends.cudnn.benchmark = True
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
output_dir = cfg.OUTPUT_DIR
if output_dir:
mkdir(output_dir)
setproctitle.setproctitle(f'{args.proctitle}')
logger = setup_logger("Source_Only", output_dir, 0)
logger.info(args)
logger.info("Loaded configuration file {}".format(args.config_file))
logger.info("Running with config:\n{}".format(cfg))
set_random_seed(cfg.SEED)
train(cfg)
if __name__ == '__main__':
main()