-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
executable file
·188 lines (145 loc) · 5.61 KB
/
main.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
import argparse
import os
from pathlib import Path
import numpy as np
import torch
import torch.nn as nn
import torch.distributed as dist
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.callbacks.progress.tqdm_progress import TQDMProgressBar
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.utilities.distributed import rank_zero_only
import albumentations as A
from src.backbones.txt_model import TimeTexture_flair
from src.datamodule import DataModule
from src.task_module import SegmentationTask
from src.utils_prints import print_config, print_metrics, print_inference_time
from src.utils_dataset import read_config
from src.load_data import load_data
from src.prediction_writer import PredictionWriter
from src.metrics import generate_miou
argParser = argparse.ArgumentParser()
argParser.add_argument("--config_file", help="Path to the .yml config file")
def main(config):
seed_everything(2022, workers=True)
out_dir = Path(config["out_folder"], config["out_model_name"])
out_dir.mkdir(parents=True, exist_ok=True)
d_train, d_val, d_test = load_data(config)
# Augmentation
if config["use_augmentation"] == True:
transform_set = A.Compose([A.VerticalFlip(p=0.5),
A.HorizontalFlip(p=0.5),
A.RandomRotate90(p=0.5)])
else:
transform_set = None
# Dataset definition
data_module = DataModule(
dict_train=d_train,
dict_val=d_val,
dict_test=d_test,
config=config,
drop_last=True,
augmentation_set = transform_set
)
model = TimeTexture_flair(config)
#@rank_zero_only
#def track_model():
# print(model)
#track_model()
# Optimizer and Loss
optimizer = torch.optim.SGD(model.parameters(), lr=config["lr"])
with torch.no_grad():
weights_aer = torch.FloatTensor(np.array(list(config['weights_aerial_satellite'].values()))[:,0])
weights_sat = torch.FloatTensor(np.array(list(config['weights_aerial_satellite'].values()))[:,1])
criterion_vhr = nn.CrossEntropyLoss(weight=weights_aer)
criterion_hr = nn.CrossEntropyLoss(weight=weights_sat)
seg_module = SegmentationTask(
model=model,
num_classes=config["num_classes"],
criterion=nn.ModuleList([criterion_vhr, criterion_hr]),
optimizer=optimizer,
config=config
)
# Callbacks
ckpt_callback = ModelCheckpoint(
monitor="val_loss",
dirpath=os.path.join(out_dir,"checkpoints"),
filename="ckpt-{epoch:02d}-{val_loss:.2f}"+'_'+config["out_model_name"],
save_top_k=1,
mode="min",
save_weights_only=True, # can be changed accordingly
)
early_stop_callback = EarlyStopping(
monitor="val_loss",
min_delta=0.00,
patience=30, # if no improvement after 30 epoch, stop learning.
mode="min",
)
prog_rate = TQDMProgressBar(refresh_rate=config["progress_rate"])
callbacks = [
ckpt_callback,
early_stop_callback,
prog_rate,
]
#Logger
logger = TensorBoardLogger(
save_dir=out_dir,
name=Path("tensorboard_logs"+'_'+config["out_model_name"]).as_posix()
)
loggers = [
logger
]
# Train
trainer = Trainer(
accelerator=config["accelerator"],
devices=config["gpus_per_node"],
strategy=config["strategy"],
num_nodes=config["num_nodes"],
max_epochs=config["num_epochs"],
num_sanity_val_steps=0,
callbacks = callbacks,
logger=loggers,
enable_progress_bar = config["enable_progress_bar"],
)
trainer.fit(seg_module, datamodule=data_module)
trainer.validate(seg_module, datamodule=data_module)
# Predict
writer_callback = PredictionWriter(
output_dir = os.path.join(out_dir, "predictions"+"_"+config["out_model_name"]),
write_interval = "batch",
)
# Predict Trainer
trainer = Trainer(
accelerator = config["accelerator"],
devices = config["gpus_per_node"],
strategy = config["strategy"],
num_nodes = config["num_nodes"],
callbacks = [writer_callback],
enable_progress_bar = config["enable_progress_bar"],
)
## Enable time measurement
starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
starter.record()
trainer.predict(seg_module, datamodule=data_module, return_predictions=False)
if config['strategy'] != None:
dist.barrier()
torch.cuda.synchronize()
ender.record()
torch.cuda.empty_cache()
inference_time_seconds = starter.elapsed_time(ender) / 1000.0
print_inference_time(inference_time_seconds, config)
@rank_zero_only
def print_finish():
print('-- [FINISHED.] --', f'output dir : {out_dir}', sep='\n')
print_finish()
truth_msk = config['data']['path_labels_test']
pred_msk = os.path.join(out_dir, "predictions"+"_"+config["out_model_name"])
mIou, ious = generate_miou(truth_msk, pred_msk)
print_metrics(mIou, ious)
if __name__ == "__main__":
args = argParser.parse_args()
config = read_config(args.config_file)
assert config["num_classes"] == config["out_conv"][-1]
print_config(config)
main(config)