-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_mmcr.py
212 lines (167 loc) · 8.24 KB
/
train_mmcr.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
import sys
import os
import timeit
import torch
from torch import optim
from torch.utils import data as torch_data
import wandb
import numpy as np
from utils import networks, datasets, loss_functions, evaluation, experiment_manager, parsers
def run_training(cfg):
net = networks.create_network(cfg)
net.to(device)
optimizer = optim.AdamW(net.parameters(), lr=cfg.TRAINER.LR, weight_decay=0.01)
sup_criterion = loss_functions.get_criterion(cfg.MODEL.LOSS_TYPE)
cons_criterion = loss_functions.get_criterion(cfg.CONSISTENCY_TRAINER.LOSS_TYPE)
# reset the generators
dataset = datasets.MultimodalCDDataset(cfg=cfg, run_type='train')
print(dataset)
dataloader_kwargs = {
'batch_size': cfg.TRAINER.BATCH_SIZE,
'num_workers': 0 if cfg.DEBUG else cfg.DATALOADER.NUM_WORKER,
'shuffle': cfg.DATALOADER.SHUFFLE,
'drop_last': True,
'pin_memory': True,
}
dataloader = torch_data.DataLoader(dataset, **dataloader_kwargs)
# unpacking cfg
epochs = cfg.TRAINER.EPOCHS
steps_per_epoch = len(dataloader)
# tracking variables
global_step = epoch_float = 0
# early stopping
best_f1_val, trigger_times = 0, 0
stop_training = False
for epoch in range(1, epochs + 1):
print(f'Starting epoch {epoch}/{epochs}.')
start = timeit.default_timer()
change_loss_set, sem_loss_set, sup_loss_set, cons_loss_set, loss_set = [], [], [], [], []
n_labeled, n_notlabeled = 0, 0
for i, batch in enumerate(dataloader):
net.train()
optimizer.zero_grad()
x_t1 = batch['x_t1'].to(device)
x_t2 = batch['x_t2'].to(device)
logits = net(x_t1, x_t2)
logits_change = logits[0]
logits_stream1_sem_t1, logits_stream1_sem_t2 = logits[1:3]
logits_stream2_sem_t1, logits_stream2_sem_t2 = logits[3:5]
logits_fusion_sem_t1, logits_fusion_sem_t2 = logits[5:]
sup_loss, cons_loss = None, None
is_labeled = batch['is_labeled']
n_labeled += torch.sum(is_labeled).item()
if is_labeled.any():
# change detection
y_change = batch['y_change'].to(device)
change_loss = sup_criterion(logits_change[is_labeled], y_change[is_labeled])
# semantics
y_sem_t1 = batch['y_sem_t1'].to(device)
sem_stream1_t1_loss = sup_criterion(logits_stream1_sem_t1[is_labeled], y_sem_t1[is_labeled])
sem_stream2_t1_loss = sup_criterion(logits_stream2_sem_t1[is_labeled], y_sem_t1[is_labeled])
sem_fusion_t1_loss = sup_criterion(logits_fusion_sem_t1[is_labeled], y_sem_t1[is_labeled])
y_sem_t2 = batch['y_sem_t2'].to(device)
sem_stream1_t2_loss = sup_criterion(logits_stream1_sem_t2[is_labeled], y_sem_t2[is_labeled])
sem_stream2_t2_loss = sup_criterion(logits_stream2_sem_t2[is_labeled], y_sem_t2[is_labeled])
sem_fusion_t2_loss = sup_criterion(logits_fusion_sem_t2[is_labeled], y_sem_t2[is_labeled])
sem_loss = (sem_stream1_t1_loss + sem_stream1_t2_loss + sem_stream2_t1_loss + sem_stream2_t2_loss +
sem_fusion_t1_loss + sem_fusion_t2_loss) / 6
sup_loss = (change_loss + sem_loss) / 2
change_loss_set.append(change_loss.item())
sem_loss_set.append(sem_loss.item())
sup_loss_set.append(sup_loss.item())
if not is_labeled.all():
is_not_labeled = torch.logical_not(is_labeled)
n_notlabeled += torch.sum(is_not_labeled).item()
y_hat_stream1_sem_t1 = torch.sigmoid(logits_stream1_sem_t1)
y_hat_stream1_sem_t2 = torch.sigmoid(logits_stream1_sem_t2)
y_hat_stream2_sem_t1 = torch.sigmoid(logits_stream2_sem_t1)
y_hat_stream2_sem_t2 = torch.sigmoid(logits_stream2_sem_t2)
if cfg.CONSISTENCY_TRAINER.LOSS_TYPE == 'L2':
cons_loss_t1 = cons_criterion(y_hat_stream1_sem_t1[is_not_labeled],
y_hat_stream2_sem_t1[is_not_labeled])
cons_loss_t2 = cons_criterion(y_hat_stream1_sem_t2[is_not_labeled],
y_hat_stream2_sem_t2[is_not_labeled])
else:
cons_loss_t1 = cons_criterion(logits_stream1_sem_t1[is_not_labeled],
y_hat_stream2_sem_t1[is_not_labeled])
cons_loss_t2 = cons_criterion(logits_stream1_sem_t2[is_not_labeled],
y_hat_stream2_sem_t2[is_not_labeled])
cons_loss = (cons_loss_t1 + cons_loss_t2) / 2
cons_loss = cfg.CONSISTENCY_TRAINER.LOSS_FACTOR * cons_loss
cons_loss_set.append(cons_loss.item())
if sup_loss is None and cons_loss is not None:
loss = cons_loss
elif sup_loss is not None and cons_loss is not None:
loss = sup_loss + cons_loss
else:
loss = sup_loss
loss_set.append(loss.item())
loss.backward()
optimizer.step()
global_step += 1
epoch_float = global_step / steps_per_epoch
if global_step % cfg.LOGGING.FREQUENCY == 0:
print(f'Logging step {global_step} (epoch {epoch_float:.2f}).')
time = timeit.default_timer() - start
wandb.log({
'change_loss': np.mean(change_loss_set) if len(change_loss_set) > 0 else 0,
'sem_loss': np.mean(sem_loss_set) if len(sem_loss_set) > 0 else 0,
'sup_loss': np.mean(sup_loss_set) if len(sup_loss_set) > 0 else 0,
'cons_loss': np.mean(cons_loss_set) if len(cons_loss_set) > 0 else 0,
'loss': np.mean(loss_set),
'labeled_percentage': n_labeled / (n_labeled + n_notlabeled) * 100,
'time': time,
'step': global_step,
'epoch': epoch_float,
})
start = timeit.default_timer()
n_labeled, n_notlabeled = 0, 0
change_loss_set, sem_loss_set, sup_loss_set, cons_loss_set, loss_set = [], [], [], [], []
# end of batch
assert (epoch == epoch_float)
print(f'epoch float {epoch_float} (step {global_step}) - epoch {epoch}')
# evaluation at the end of an epoch
_ = evaluation.model_evaluation_mm_dt(net, cfg, 'train', epoch_float, global_step)
f1_val = evaluation.model_evaluation_mm_dt(net, cfg, 'val', epoch_float, global_step)
if f1_val <= best_f1_val:
trigger_times += 1
if trigger_times > cfg.TRAINER.PATIENCE:
stop_training = True
else:
best_f1_val = f1_val
wandb.log({
'best val change F1': best_f1_val,
'step': global_step,
'epoch': epoch_float,
})
print(f'saving network (F1 {f1_val:.3f})', flush=True)
networks.save_checkpoint(net, optimizer, epoch, cfg)
trigger_times = 0
if stop_training:
break # end of training by early stopping
net, *_ = networks.load_checkpoint(cfg, device)
_ = evaluation.model_evaluation_mm_dt(net, cfg, 'test', epoch_float, global_step)
if __name__ == '__main__':
args = parsers.training_argument_parser().parse_known_args()[0]
cfg = experiment_manager.setup_cfg(args)
# make training deterministic
torch.manual_seed(cfg.SEED)
np.random.seed(cfg.SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('=== Runnning on device: p', device)
wandb.init(
name=cfg.NAME,
config=cfg,
project=args.project,
tags=['ssl', 'cd', 'siamese', 'spacenet7', ],
mode='online' if not cfg.DEBUG else 'disabled',
)
try:
run_training(cfg)
except KeyboardInterrupt:
try:
sys.exit(0)
except SystemExit:
os._exit(0)