-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
179 lines (157 loc) · 6.71 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
import numpy as np
from sklearn.metrics import roc_curve, roc_auc_score, precision_recall_fscore_support, accuracy_score
import torch
def seed_torch(seed=2021):
import random
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
@torch.no_grad()
def ema_update(model,targ_model,mm=0.9999):
r"""Performs a momentum update of the target network's weights.
Args:
mm (float): Momentum used in moving average update.
"""
assert 0.0 <= mm <= 1.0, "Momentum needs to be between 0.0 and 1.0, got %.5f" % mm
for param_q, param_k in zip(model.parameters(), targ_model.parameters()):
param_k.data.mul_(mm).add_(param_q.data, alpha=1. - mm) # mm*k +(1-mm)*q
def patch_shuffle(x,group=0,g_idx=None,return_g_idx=False):
b,p,n = x.size()
ps = torch.tensor(list(range(p)))
# padding
H, W = int(np.ceil(np.sqrt(p))), int(np.ceil(np.sqrt(p)))
if group > H or group<= 0:
return group_shuffle(x,group)
_n = -H % group
H, W = H+_n, W+_n
add_length = H * W - p
# print(add_length)
ps = torch.cat([ps,torch.tensor([-1 for i in range(add_length)])])
# patchify
ps = ps.reshape(shape=(group,H//group,group,W//group))
ps = torch.einsum('hpwq->hwpq',ps)
ps = ps.reshape(shape=(group**2,H//group,W//group))
# shuffle
if g_idx is None:
g_idx = torch.randperm(ps.size(0))
ps = ps[g_idx]
# unpatchify
ps = ps.reshape(shape=(group,group,H//group,W//group))
ps = torch.einsum('hwpq->hpwq',ps)
ps = ps.reshape(shape=(H,W))
idx = ps[ps>=0].view(p)
if return_g_idx:
return x[:,idx.long()],g_idx
else:
return x[:,idx.long()]
def group_shuffle(x,group=0):
b,p,n = x.size()
ps = torch.tensor(list(range(p)))
if group > 0 and group < p:
_pad = -p % group
ps = torch.cat([ps,torch.tensor([-1 for i in range(_pad)])])
ps = ps.view(group,-1)
g_idx = torch.randperm(ps.size(0))
ps = ps[g_idx]
idx = ps[ps>=0].view(p)
else:
idx = torch.randperm(p)
return x[:,idx.long()]
def optimal_thresh(fpr, tpr, thresholds, p=0):
loss = (fpr - tpr) - p * tpr / (fpr + tpr + 1)
idx = np.argmin(loss, axis=0)
return fpr[idx], tpr[idx], thresholds[idx]
def make_weights_for_balanced_classes_split(dataset):
N = float(len(dataset))
labels = np.array(dataset.slide_label)
label_uni = set(dataset.slide_label)
weight_per_class = [N/len(labels[labels==c]) for c in label_uni]
weight = [0] * int(N)
for idx in range(len(dataset)):
y = dataset.slide_label[idx]
weight[idx] = weight_per_class[y]
return torch.DoubleTensor(weight)
def five_scores(bag_labels, bag_predictions,sub_typing=False):
fpr, tpr, threshold = roc_curve(bag_labels, bag_predictions, pos_label=1)
fpr_optimal, tpr_optimal, threshold_optimal = optimal_thresh(fpr, tpr, threshold)
# threshold_optimal=0.5
auc_value = roc_auc_score(bag_labels, bag_predictions)
this_class_label = np.array(bag_predictions)
this_class_label[this_class_label>=threshold_optimal] = 1
this_class_label[this_class_label<threshold_optimal] = 0
bag_predictions = this_class_label
avg = 'macro' if sub_typing else 'binary'
precision, recall, fscore, _ = precision_recall_fscore_support(bag_labels, bag_predictions, average=avg)
accuracy = accuracy_score(bag_labels, bag_predictions)
return accuracy, auc_value, precision, recall, fscore
def cosine_scheduler(base_value, final_value, epochs, niter_per_ep, warmup_epochs=0, start_warmup_value=0):
warmup_schedule = np.array([])
warmup_iters = warmup_epochs * niter_per_ep
if warmup_epochs > 0:
warmup_schedule = np.linspace(start_warmup_value, base_value, warmup_iters)
iters = np.arange(epochs * niter_per_ep - warmup_iters)
schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters)))
schedule = np.concatenate((warmup_schedule, schedule))
assert len(schedule) == epochs * niter_per_ep
return schedule
class EarlyStopping:
"""Early stops the training if validation loss doesn't improve after a given patience."""
def __init__(self, patience=20, stop_epoch=50, verbose=False,save_best_model_stage=0.):
"""
Args:
patience (int): How long to wait after last time validation loss improved.
Default: 20
stop_epoch (int): Earliest epoch possible for stopping
verbose (bool): If True, prints a message for each validation loss improvement.
Default: False
"""
self.patience = patience
self.stop_epoch = stop_epoch
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.val_loss_min = np.Inf
self.save_best_model_stage = save_best_model_stage
def __call__(self, epoch, val_loss, model, ckpt_name = 'checkpoint.pt'):
score = -val_loss if epoch >= self.save_best_model_stage else 0.
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_loss, model, ckpt_name)
elif score < self.best_score:
self.counter += 1
print(f'EarlyStopping counter: {self.counter} out of {self.patience}')
if self.counter >= self.patience and epoch > self.stop_epoch:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_loss, model, ckpt_name)
self.counter = 0
def state_dict(self):
return {
'patience': self.patience,
'stop_epoch': self.stop_epoch,
'verbose': self.verbose,
'counter': self.counter,
'best_score': self.best_score,
'early_stop': self.early_stop,
'val_loss_min': self.val_loss_min
}
def load_state_dict(self,dict):
self.patience = dict['patience']
self.stop_epoch = dict['stop_epoch']
self.verbose = dict['verbose']
self.counter = dict['counter']
self.best_score = dict['best_score']
self.early_stop = dict['early_stop']
self.val_loss_min = dict['val_loss_min']
def save_checkpoint(self, val_loss, model, ckpt_name):
'''Saves model when validation loss decrease.'''
if self.verbose:
print(f'Validation loss decreased ({self.val_loss_min:.6f} --> {val_loss:.6f}). Saving model ...')
#torch.save(model.state_dict(), ckpt_name)
self.val_loss_min = val_loss