-
Notifications
You must be signed in to change notification settings - Fork 55
/
train_semantic.py
411 lines (361 loc) · 12.7 KB
/
train_semantic.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""
Main script for semantic experiments
Author: Vivien Sainte Fare Garnot (github/VSainteuf)
License: MIT
"""
import argparse
import json
import os
import pickle as pkl
import pprint
import time
import numpy as np
import torch
import torch.nn as nn
import torch.utils.data as data
import torchnet as tnt
from src import utils, model_utils
from src.dataset import PASTIS_Dataset
from src.learning.metrics import confusion_matrix_analysis
from src.learning.miou import IoU
from src.learning.weight_init import weight_init
parser = argparse.ArgumentParser()
# Model parameters
parser.add_argument(
"--model",
default="utae",
type=str,
help="Type of architecture to use. Can be one of: (utae/unet3d/fpn/convlstm/convgru/uconvlstm/buconvlstm)",
)
## U-TAE Hyperparameters
parser.add_argument("--encoder_widths", default="[64,64,64,128]", type=str)
parser.add_argument("--decoder_widths", default="[32,32,64,128]", type=str)
parser.add_argument("--out_conv", default="[32, 20]")
parser.add_argument("--str_conv_k", default=4, type=int)
parser.add_argument("--str_conv_s", default=2, type=int)
parser.add_argument("--str_conv_p", default=1, type=int)
parser.add_argument("--agg_mode", default="att_group", type=str)
parser.add_argument("--encoder_norm", default="group", type=str)
parser.add_argument("--n_head", default=16, type=int)
parser.add_argument("--d_model", default=256, type=int)
parser.add_argument("--d_k", default=4, type=int)
# Set-up parameters
parser.add_argument(
"--dataset_folder",
default="",
type=str,
help="Path to the folder where the results are saved.",
)
parser.add_argument(
"--res_dir",
default="./results",
help="Path to the folder where the results should be stored",
)
parser.add_argument(
"--num_workers", default=8, type=int, help="Number of data loading workers"
)
parser.add_argument("--rdm_seed", default=1, type=int, help="Random seed")
parser.add_argument(
"--device",
default="cuda",
type=str,
help="Name of device to use for tensor computations (cuda/cpu)",
)
parser.add_argument(
"--display_step",
default=50,
type=int,
help="Interval in batches between display of training metrics",
)
parser.add_argument(
"--cache",
dest="cache",
action="store_true",
help="If specified, the whole dataset is kept in RAM",
)
# Training parameters
parser.add_argument("--epochs", default=100, type=int, help="Number of epochs per fold")
parser.add_argument("--batch_size", default=4, type=int, help="Batch size")
parser.add_argument("--lr", default=0.001, type=float, help="Learning rate")
parser.add_argument("--mono_date", default=None, type=str)
parser.add_argument("--ref_date", default="2018-09-01", type=str)
parser.add_argument(
"--fold",
default=None,
type=int,
help="Do only one of the five fold (between 1 and 5)",
)
parser.add_argument("--num_classes", default=20, type=int)
parser.add_argument("--ignore_index", default=-1, type=int)
parser.add_argument("--pad_value", default=0, type=float)
parser.add_argument("--padding_mode", default="reflect", type=str)
parser.add_argument(
"--val_every",
default=1,
type=int,
help="Interval in epochs between two validation steps.",
)
parser.add_argument(
"--val_after",
default=0,
type=int,
help="Do validation only after that many epochs.",
)
list_args = ["encoder_widths", "decoder_widths", "out_conv"]
parser.set_defaults(cache=False)
def iterate(
model, data_loader, criterion, config, optimizer=None, mode="train", device=None
):
loss_meter = tnt.meter.AverageValueMeter()
iou_meter = IoU(
num_classes=config.num_classes,
ignore_index=config.ignore_index,
cm_device=config.device,
)
t_start = time.time()
for i, batch in enumerate(data_loader):
if device is not None:
batch = recursive_todevice(batch, device)
(x, dates), y = batch
y = y.long()
if mode != "train":
with torch.no_grad():
out = model(x, batch_positions=dates)
else:
optimizer.zero_grad()
out = model(x, batch_positions=dates)
loss = criterion(out, y)
if mode == "train":
loss.backward()
optimizer.step()
with torch.no_grad():
pred = out.argmax(dim=1)
iou_meter.add(pred, y)
loss_meter.add(loss.item())
if (i + 1) % config.display_step == 0:
miou, acc = iou_meter.get_miou_acc()
print(
"Step [{}/{}], Loss: {:.4f}, Acc : {:.2f}, mIoU {:.2f}".format(
i + 1, len(data_loader), loss_meter.value()[0], acc, miou
)
)
t_end = time.time()
total_time = t_end - t_start
print("Epoch time : {:.1f}s".format(total_time))
miou, acc = iou_meter.get_miou_acc()
metrics = {
"{}_accuracy".format(mode): acc,
"{}_loss".format(mode): loss_meter.value()[0],
"{}_IoU".format(mode): miou,
"{}_epoch_time".format(mode): total_time,
}
if mode == "test":
return metrics, iou_meter.conf_metric.value() # confusion matrix
else:
return metrics
def recursive_todevice(x, device):
if isinstance(x, torch.Tensor):
return x.to(device)
elif isinstance(x, dict):
return {k: recursive_todevice(v, device) for k, v in x.items()}
else:
return [recursive_todevice(c, device) for c in x]
def prepare_output(config):
os.makedirs(config.res_dir, exist_ok=True)
for fold in range(1, 6):
os.makedirs(os.path.join(config.res_dir, "Fold_{}".format(fold)), exist_ok=True)
def checkpoint(fold, log, config):
with open(
os.path.join(config.res_dir, "Fold_{}".format(fold), "trainlog.json"), "w"
) as outfile:
json.dump(log, outfile, indent=4)
def save_results(fold, metrics, conf_mat, config):
with open(
os.path.join(config.res_dir, "Fold_{}".format(fold), "test_metrics.json"), "w"
) as outfile:
json.dump(metrics, outfile, indent=4)
pkl.dump(
conf_mat,
open(
os.path.join(config.res_dir, "Fold_{}".format(fold), "conf_mat.pkl"), "wb"
),
)
def overall_performance(config):
cm = np.zeros((config.num_classes, config.num_classes))
for fold in range(1, 6):
cm += pkl.load(
open(
os.path.join(config.res_dir, "Fold_{}".format(fold), "conf_mat.pkl"),
"rb",
)
)
if config.ignore_index is not None:
cm = np.delete(cm, config.ignore_index, axis=0)
cm = np.delete(cm, config.ignore_index, axis=1)
_, perf = confusion_matrix_analysis(cm)
print("Overall performance:")
print("Acc: {}, IoU: {}".format(perf["Accuracy"], perf["MACRO_IoU"]))
with open(os.path.join(config.res_dir, "overall.json"), "w") as file:
file.write(json.dumps(perf, indent=4))
def main(config):
fold_sequence = [
[[1, 2, 3], [4], [5]],
[[2, 3, 4], [5], [1]],
[[3, 4, 5], [1], [2]],
[[4, 5, 1], [2], [3]],
[[5, 1, 2], [3], [4]],
]
np.random.seed(config.rdm_seed)
torch.manual_seed(config.rdm_seed)
prepare_output(config)
device = torch.device(config.device)
fold_sequence = (
fold_sequence if config.fold is None else [fold_sequence[config.fold - 1]]
)
for fold, (train_folds, val_fold, test_fold) in enumerate(fold_sequence):
if config.fold is not None:
fold = config.fold - 1
# Dataset definition
dt_args = dict(
folder=config.dataset_folder,
norm=True,
reference_date=config.ref_date,
mono_date=config.mono_date,
target="semantic",
sats=["S2"],
)
dt_train = PASTIS_Dataset(**dt_args, folds=train_folds, cache=config.cache)
dt_val = PASTIS_Dataset(**dt_args, folds=val_fold, cache=config.cache)
dt_test = PASTIS_Dataset(**dt_args, folds=test_fold)
collate_fn = lambda x: utils.pad_collate(x, pad_value=config.pad_value)
train_loader = data.DataLoader(
dt_train,
batch_size=config.batch_size,
shuffle=True,
drop_last=True,
collate_fn=collate_fn,
)
val_loader = data.DataLoader(
dt_val,
batch_size=config.batch_size,
shuffle=True,
drop_last=True,
collate_fn=collate_fn,
)
test_loader = data.DataLoader(
dt_test,
batch_size=config.batch_size,
shuffle=True,
drop_last=True,
collate_fn=collate_fn,
)
print(
"Train {}, Val {}, Test {}".format(len(dt_train), len(dt_val), len(dt_test))
)
# Model definition
model = model_utils.get_model(config, mode="semantic")
config.N_params = utils.get_ntrainparams(model)
with open(os.path.join(config.res_dir, "conf.json"), "w") as file:
file.write(json.dumps(vars(config), indent=4))
print(model)
print("TOTAL TRAINABLE PARAMETERS :", config.N_params)
print("Trainable layers:")
for name, p in model.named_parameters():
if p.requires_grad:
print(name)
model = model.to(device)
model.apply(weight_init)
# Optimizer and Loss
optimizer = torch.optim.Adam(model.parameters(), lr=config.lr)
weights = torch.ones(config.num_classes, device=device).float()
weights[config.ignore_index] = 0
criterion = nn.CrossEntropyLoss(weight=weights)
# Training loop
trainlog = {}
best_mIoU = 0
for epoch in range(1, config.epochs + 1):
print("EPOCH {}/{}".format(epoch, config.epochs))
model.train()
train_metrics = iterate(
model,
data_loader=train_loader,
criterion=criterion,
config=config,
optimizer=optimizer,
mode="train",
device=device,
)
if epoch % config.val_every == 0 and epoch > config.val_after:
print("Validation . . . ")
model.eval()
val_metrics = iterate(
model,
data_loader=val_loader,
criterion=criterion,
config=config,
optimizer=optimizer,
mode="val",
device=device,
)
print(
"Loss {:.4f}, Acc {:.2f}, IoU {:.4f}".format(
val_metrics["val_loss"],
val_metrics["val_accuracy"],
val_metrics["val_IoU"],
)
)
trainlog[epoch] = {**train_metrics, **val_metrics}
checkpoint(fold + 1, trainlog, config)
if val_metrics["val_IoU"] >= best_mIoU:
best_mIoU = val_metrics["val_IoU"]
torch.save(
{
"epoch": epoch,
"state_dict": model.state_dict(),
"optimizer": optimizer.state_dict(),
},
os.path.join(
config.res_dir, "Fold_{}".format(fold + 1), "model.pth.tar"
),
)
else:
trainlog[epoch] = {**train_metrics}
checkpoint(fold + 1, trainlog, config)
print("Testing best epoch . . .")
model.load_state_dict(
torch.load(
os.path.join(
config.res_dir, "Fold_{}".format(fold + 1), "model.pth.tar"
)
)["state_dict"]
)
model.eval()
test_metrics, conf_mat = iterate(
model,
data_loader=test_loader,
criterion=criterion,
config=config,
optimizer=optimizer,
mode="test",
device=device,
)
print(
"Loss {:.4f}, Acc {:.2f}, IoU {:.4f}".format(
test_metrics["test_loss"],
test_metrics["test_accuracy"],
test_metrics["test_IoU"],
)
)
save_results(fold + 1, test_metrics, conf_mat.cpu().numpy(), config)
if config.fold is None:
overall_performance(config)
if __name__ == "__main__":
config = parser.parse_args()
for k, v in vars(config).items():
if k in list_args and v is not None:
v = v.replace("[", "")
v = v.replace("]", "")
config.__setattr__(k, list(map(int, v.split(","))))
assert config.num_classes == config.out_conv[-1]
pprint.pprint(config)
main(config)