-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
181 lines (142 loc) · 4.97 KB
/
utils.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
import os
import random
import numpy as np
import torch
import cv2
from tqdm import tqdm
from sklearn.metrics import fbeta_score
from torch.optim.lr_scheduler import CosineAnnealingLR
from torch.optim import Optimizer
import gc
from main import Logger
from warmup_scheduler import GradualWarmupScheduler
from config import CFG
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def set_seed(seed=None, cudnn_deterministic=True):
if seed is None:
seed = 42
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = cudnn_deterministic
torch.backends.cudnn.benchmark = False
def make_dirs(cfg):
for dir in [cfg.model_dir, cfg.submission_dir, cfg.log_dir]:
os.makedirs(dir, exist_ok=True)
def cfg_init(cfg, mode="train"):
set_seed(cfg.seed)
# set_env_name()
# set_dataset_path(cfg)
if mode == "train":
make_dirs(cfg)
def rle(img):
"""
img: numpy array, 1 - mask, 0 - background
Returns run length as string formated
"""
pixels = img.flatten()
# pixels = (pixels >= thr).astype(int)
pixels = np.concatenate([[0], pixels, [0]])
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return " ".join(str(x) for x in runs)
def fbeta_numpy(targets, preds, beta=0.5, smooth=1e-5):
"""
https://www.kaggle.com/competitions/vesuvius-challenge-ink-detection/discussion/397288
"""
y_true_count = targets.sum()
ctp = preds[targets == 1].sum()
cfp = preds[targets == 0].sum()
beta_squared = beta * beta
c_precision = ctp / (ctp + cfp + smooth)
c_recall = ctp / (y_true_count + smooth)
dice = (
(1 + beta_squared)
* (c_precision * c_recall)
/ (beta_squared * c_precision + c_recall + smooth)
)
return dice
def calc_fbeta(mask, mask_pred, _range=(30, 100 + 1), _step=5):
mask = mask.astype(int).flatten()
mask_pred = mask_pred.flatten()
best_th = 0
best_dice = 0
for th in np.array(range(*_range, _step)) / 100:
# dice = fbeta_score(mask, (mask_pred >= th).astype(int), beta=0.5)
dice = fbeta_numpy(mask, (mask_pred >= th).astype(int), beta=0.5)
print(f"th: {th}, fbeta: {dice}")
if dice > best_dice:
best_dice = dice
best_th = th
else:
break
Logger.info(f"best_th: {best_th}, fbeta: {best_dice}")
torch.cuda.empty_cache()
gc.collect()
return best_dice, best_th
def calc_cv(mask_gt, mask_pred, _range=(20, 80 + 1), prec=5):
best_dice, best_th = calc_fbeta(mask_gt, mask_pred, _range=_range, _step=prec)
return best_dice, best_th
class GradualWarmupSchedulerV2(GradualWarmupScheduler):
"""
https://www.kaggle.com/code/underwearfitting/single-fold-training-of-resnet200d-lb0-965
"""
def __init__(self, optimizer, multiplier, total_epoch, after_scheduler=None):
super(GradualWarmupSchedulerV2, self).__init__(
optimizer, multiplier, total_epoch, after_scheduler
)
def get_lr(self):
if self.last_epoch > self.total_epoch:
if self.after_scheduler:
if not self.finished:
self.after_scheduler.base_lrs = [
base_lr * self.multiplier for base_lr in self.base_lrs
]
self.finished = True
return self.after_scheduler.get_lr()
return [base_lr * self.multiplier for base_lr in self.base_lrs]
if self.multiplier == 1.0:
return [
base_lr * (float(self.last_epoch) / self.total_epoch)
for base_lr in self.base_lrs
]
else:
return [
base_lr
* ((self.multiplier - 1.0) * self.last_epoch / self.total_epoch + 1.0)
for base_lr in self.base_lrs
]
def get_scheduler(cfg, optimizer):
scheduler_cosine = CosineAnnealingLR(optimizer, cfg.epochs, eta_min=1e-7)
scheduler = GradualWarmupSchedulerV2(
optimizer, multiplier=10, total_epoch=1, after_scheduler=scheduler_cosine
)
return scheduler
def init_logger(log_file):
from logging import getLogger, INFO, FileHandler, Formatter, StreamHandler
logger = getLogger(__name__)
logger.setLevel(INFO)
handler1 = StreamHandler()
handler1.setFormatter(Formatter("%(message)s"))
handler2 = FileHandler(filename=log_file)
handler2.setFormatter(Formatter("%(message)s"))
logger.addHandler(handler1)
logger.addHandler
def gc_collect():
gc.collect()
torch.cuda.empty_cache()