-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_s1s2_segformer.py
350 lines (267 loc) · 12.5 KB
/
main_s1s2_segformer.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
import os, json
import random
from easydict import EasyDict as edict
from pathlib import Path
from prettyprinter import pprint
from imageio import imread, imsave
import hydra
import wandb
from omegaconf import DictConfig, OmegaConf
###################################################################################
import os, sys
import numpy as np
from pathlib import Path
import hydra
from omegaconf import DictConfig, OmegaConf
import copy
import time
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from easydict import EasyDict as edict
from tqdm import tqdm as tqdm
import logging
logger = logging.getLogger(__name__)
import smp
from smp.base.modules import Activation
from models.model_selection import get_model
import wandb
f_score = smp.utils.functional.f_score
# Dice/F1 score - https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
# IoU/Jaccard score - https://en.wikipedia.org/wiki/Jaccard_index
# diceLoss = smp.utils.losses.DiceLoss(eps=1)
from models.loss_ref import soft_dice_loss, soft_dice_loss_balanced, jaccard_like_loss, jaccard_like_balanced_loss
AverageValueMeter = smp.utils.train.AverageValueMeter
# Augmentations
from dataset.augument import get_training_augmentation, \
get_validation_augmentation, get_preprocessing
from torch.utils.data import DataLoader
from dataset.wildfire import S1S2 as Dataset # ------------------------------------------------------- Dataset
from models.lr_schedule import get_cosine_schedule_with_warmup
def format_logs(logs):
str_logs = ['{}: {:.4}'.format(k, v) for k, v in logs.items()]
s = ', '.join(str_logs)
return s
# Loss Functions
def loss_fun(cfg, DEVICE='cuda'):
if cfg.model.LOSS_TYPE == 'BCEWithLogitsLoss':
criterion = nn.BCEWithLogitsLoss()
elif cfg.model.LOSS_TYPE == 'smpDiceLoss':
criterion = smp.utils.losses.DiceLoss(eps=1)
elif cfg.model.LOSS_TYPE == 'CrossEntropyLoss':
balance_weight = [cfg.MODEL.NEGATIVE_WEIGHT, cfg.MODEL.POSITIVE_WEIGHT]
balance_weight = torch.tensor(balance_weight).float().to(DEVICE)
criterion = nn.CrossEntropyLoss(weight = balance_weight)
elif cfg.model.LOSS_TYPE == 'SoftDiceLoss':
criterion = soft_dice_loss
elif cfg.model.LOSS_TYPE == 'SoftDiceBalancedLoss':
criterion = soft_dice_loss_balanced
elif cfg.model.LOSS_TYPE == 'JaccardLikeLoss':
criterion = jaccard_like_loss
elif cfg.model.LOSS_TYPE == 'ComboLoss':
criterion = lambda pred, gts: F.binary_cross_entropy_with_logits(pred, gts) + soft_dice_loss(pred, gts)
elif cfg.model.LOSS_TYPE == 'WeightedComboLoss':
criterion = lambda pred, gts: F.binary_cross_entropy_with_logits(pred, gts) + 10 * soft_dice_loss(pred, gts)
elif cfg.model.LOSS_TYPE == 'FrankensteinLoss':
criterion = lambda pred, gts: F.binary_cross_entropy_with_logits(pred, gts) + jaccard_like_balanced_loss(pred, gts)
return criterion
class SegModel(object):
def __init__(self, cfg) -> None:
super().__init__()
self.project_dir = Path(hydra.utils.get_original_cwd())
self.cfg = cfg
self.DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
self.model = get_model(cfg)
self.activation = Activation(cfg.model.ACTIVATION)
# self.model_url = str(self.project_dir / "outputs" / "best_model.pth")
self.rundir = self.project_dir / self.cfg.experiment.output
self.model_url = str( self.rundir / "model.pth")
# if cfg.model.ENCODER is not None:
# self.preprocessing_fn = \
# smp.encoders.get_preprocessing_fn(cfg.model.ENCODER, cfg.model.ENCODER_WEIGHTS)
self.metrics = [smp.utils.metrics.IoU(threshold=0.5),
smp.utils.metrics.Fscore()
]
'''--------------> need to improve <-----------------'''
# specify data folder
self.train_dir = Path(self.cfg.data.dir) / 'train'
self.valid_dir = Path(self.cfg.data.dir) / 'test'
'''--------------------------------------------------'''
def get_dataloaders(self) -> dict:
if self.cfg.model.NUM_CLASS == 1:
classes = ['burned']
elif self.cfg.model.NUM_CLASS == 2:
classes = ['unburn', 'burned']
elif self.cfg.model.NUM_CLASS > 2:
print(" ONLY ALLOW ONE or TWO CLASSES SO FAR !!!")
pass
""" Data Preparation """
train_dataset = Dataset(
self.train_dir,
self.cfg,
# augmentation=get_training_augmentation(),
# preprocessing=get_preprocessing(self.preprocessing_fn),
classes=classes,
)
valid_dataset = Dataset(
self.valid_dir,
self.cfg,
# augmentation=get_validation_augmentation(),
# preprocessing=get_preprocessing(self.preprocessing_fn),
classes=classes,
)
train_size = int(len(train_dataset) * self.cfg.data.train_ratio)
valid_size = len(train_dataset) - train_size
train_set, val_set = torch.utils.data.random_split(train_dataset, [train_size, valid_size])
train_loader = DataLoader(train_set, batch_size=self.cfg.model.batch_size, shuffle=True, num_workers=4)
valid_loader = DataLoader(val_set, batch_size=self.cfg.model.batch_size, shuffle=True, num_workers=4)
test_loader = DataLoader(valid_dataset, batch_size=self.cfg.model.batch_size, shuffle=False, num_workers=4)
dataloaders = {
'train': train_loader, \
'valid': valid_loader, \
'test': test_loader, \
'train_size': train_size, \
'valid_size': valid_size, \
'test_size': len(valid_dataset)
}
return dataloaders
def run(self) -> None:
self.dataloaders = self.get_dataloaders()
self.optimizer = torch.optim.Adam([dict(
params=self.model.parameters(),
lr=self.cfg.model.learning_rate,
weight_decay=self.cfg.model.weight_decay)])
# lr scheduler
per_epoch_steps = self.dataloaders['train_size'] // self.cfg.model.batch_size
total_training_steps = self.cfg.model.max_epoch * per_epoch_steps
warmup_steps = self.cfg.model.warmup_coef * per_epoch_steps
if self.cfg.model.use_lr_scheduler:
self.lr_scheduler = get_cosine_schedule_with_warmup(self.optimizer, warmup_steps, total_training_steps)
self.history_logs = edict()
self.history_logs['train'] = []
self.history_logs['valid'] = []
self.history_logs['test'] = []
# --------------------------------- Train -------------------------------------------
max_score = self.cfg.model.max_score
for epoch in range(0, self.cfg.model.max_epoch):
epoch = epoch + 1
print(f"\n==> train epoch: {epoch}/{self.cfg.model.max_epoch}")
self.train_one_epoch(epoch)
valid_logs = self.valid_logs
# do something (save model, change lr, etc.)
if valid_logs['iou_score'] > max_score:
max_score = valid_logs['iou_score']
if (1 == epoch) or (0 == (epoch % self.cfg.model.save_interval)):
torch.save(self.model, self.model_url)
# torch.save(self.model.state_dict(), self.model_url)
print('Model saved!')
# if epoch % 50 == 0:
# self.optimizer.param_groups[0]['lr'] = 0.1 * self.optimizer.param_groups[0]['lr']
def train_one_epoch(self, epoch):
self.model.to(self.DEVICE)
# wandb.
for phase in ['train', 'valid', 'test']:
if phase == 'train':
self.model.train()
else:
self.model.eval()
logs = self.step(phase)
# print(phase, logs)
currlr = self.lr_scheduler.get_last_lr()[0] if self.cfg.model.use_lr_scheduler else self.optimizer.param_groups[0]['lr']
wandb.log({phase: logs, 'epoch': epoch, 'lr': currlr})
temp = [logs["total_loss"]] + [logs[self.metrics[i].__name__] for i in range(0, len(self.metrics))]
self.history_logs[phase].append(temp)
if phase == 'valid': self.valid_logs = logs
def step(self, phase) -> dict:
logs = {}
loss_meter = AverageValueMeter()
metrics_meters = {metric.__name__: AverageValueMeter() for metric in self.metrics}
# if ('Train' in phase) and (self.cfg.useDataWoCAug):
# dataLoader_woCAug = iter(self.dataloaders['Train_woCAug'])
with tqdm(iter(self.dataloaders[phase]), desc=phase, file=sys.stdout, disable=not self.cfg.model.verbose) as iterator:
for (x, y) in iterator:
self.optimizer.zero_grad()
''' move data to GPU '''
input = []
for x_i in x: input.append(x_i.to(self.DEVICE))
y = y.to(self.DEVICE)
# print(len(input))
''' do prediction '''
out = self.model.forward(input)[-1]
# y_pred = self.activation(out)
''' compute loss '''
# loss_ = diceLoss(y_pred, y)
criterion = loss_fun(self.cfg)
loss_ = criterion(out, y)
if phase == 'train':
loss_.backward()
self.optimizer.step()
if self.cfg.model.use_lr_scheduler:
self.lr_scheduler.step()
# update loss logs
loss_value = loss_.cpu().detach().numpy()
loss_meter.add(loss_value)
# loss_logs = {criterion.__name__: loss_meter.mean}
loss_logs = {'total_loss': loss_meter.mean}
logs.update(loss_logs)
# added for NUM_CLASS >= 2
if self.cfg.model.NUM_CLASS >= 2:
y_pred = self.activation(out)
y = self.activation(y)
# update metrics logs
for metric_fn in self.metrics:
metric_value = metric_fn(y_pred, y).cpu().detach().numpy()
metrics_meters[metric_fn.__name__].add(metric_value)
metrics_logs = {k: v.mean for k, v in metrics_meters.items()}
logs.update(metrics_logs)
# print(logs)
if self.cfg.model.verbose:
s = format_logs(logs)
iterator.set_postfix_str(s)
# if phase == 'train':
# loss_.backward()
# self.optimizer.step()
# if self.cfg.model.use_lr_scheduler:
# self.lr_scheduler.step()
return logs
##############################################################
def set_random_seed(seed, deterministic=False):
"""Set random seed.
Args:
seed (int): Seed to be used.
deterministic (bool): Whether to set the deterministic option for
CUDNN backend, i.e., set `torch.backends.cudnn.deterministic`
to True and `torch.backends.cudnn.benchmark` to False.
Default: False.
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if deterministic:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
@hydra.main(config_path="./config", config_name="segformer")
def run_app(cfg : DictConfig) -> None:
# wandb.init(config=cfg, project=cfg.project.name, name=cfg.experiment.name)
import pandas as pd
from prettyprinter import pprint
from easydict import EasyDict as edict
cfg_dict = OmegaConf.to_container(cfg, resolve=True)
cfg_flat = pd.json_normalize(cfg_dict, sep='.').to_dict(orient='records')[0]
if not cfg.model.DEBUG:
wandb.init(config=cfg_flat, project=cfg.project.name, entity=cfg.project.entity, name=cfg.experiment.name)
pprint(cfg_flat)
# set randome seed
set_random_seed(cfg.data.SEED)
# from experiments.seg_model import SegModel
mySegModel = SegModel(cfg)
mySegModel.run()
# evaluation
from s1s2_evaluator import evaluate_model
evaluate_model(cfg, mySegModel.model_url, mySegModel.rundir / "errMap")
if not cfg.model.DEBUG:
wandb.finish()
if __name__ == "__main__":
run_app()