-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
253 lines (218 loc) · 9.81 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
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
import numpy as np
from sklearn.metrics import roc_curve, roc_auc_score, precision_recall_fscore_support, accuracy_score
import torch
import os
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 five_scores(bag_labels, bag_logits, bag_hat):
# auc_value = roc_auc_score(bag_labels, bag_logits)
# precision, recall, f1score, _ = precision_recall_fscore_support(bag_labels, bag_hat, average='binary')
# accuracy = accuracy_score(bag_labels, bag_hat)
# return accuracy, auc_value, precision, recall, f1score
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,threshold_optimal=None):
if threshold_optimal is None:
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
precision, recall, fscore, _ = precision_recall_fscore_support(bag_labels, bag_predictions, average='binary')
accuracy = accuracy_score(bag_labels, bag_predictions)
# accuracy = 1- np.count_nonzero(np.array(bag_labels).astype(int)- bag_predictions.astype(int)) / len(bag_labels)
return accuracy, auc_value, precision, recall, fscore,threshold_optimal
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
############# Survival Prediction ###################
def nll_loss(hazards, S, Y, c, alpha=0.4, eps=1e-7):
batch_size = len(Y)
Y = Y.view(batch_size, 1) # ground truth bin, 1,2,...,k
c = c.view(batch_size, 1).float() # censorship status, 0 or 1
if S is None:
S = torch.cumprod(1 - hazards, dim=1) # surival is cumulative product of 1 - hazards
# without padding, S(0) = S[0], h(0) = h[0]
S_padded = torch.cat([torch.ones_like(c), S], 1) # S(-1) = 0, all patients are alive from (-inf, 0) by definition
# after padding, S(0) = S[1], S(1) = S[2], etc, h(0) = h[0]
# h[y] = h(1)
# S[1] = S(1)
uncensored_loss = -(1 - c) * (
torch.log(torch.gather(S_padded, 1, Y).clamp(min=eps)) + torch.log(torch.gather(hazards, 1, Y).clamp(min=eps))
)
censored_loss = -c * torch.log(torch.gather(S_padded, 1, Y + 1).clamp(min=eps))
neg_l = censored_loss + uncensored_loss
loss = (1 - alpha) * neg_l + alpha * uncensored_loss
loss = loss.mean()
return loss
class NLLSurvLoss(object):
def __init__(self, alpha=0.15):
self.alpha = alpha
def __call__(self, hazards, S, Y, c, alpha=None):
if alpha is None:
return nll_loss(hazards, S, Y, c, alpha=self.alpha)
else:
return nll_loss(hazards, S, Y, c, alpha=alpha)
def calculate_consistency_loss(global_attn, relative_area, con_batch_size):
# if isinstance(global_attn, list):
# global_attn = torch.tensor(global_attn)
# global_attn = global_attn.squeeze(0)
relative_area = relative_area.squeeze(0)
total_loss = torch.zeros(1, device=global_attn.device)
count = 0
unique_areas = torch.unique(relative_area)
for area in unique_areas:
indices = (relative_area == area).nonzero(as_tuple=True)[0]
if indices.numel() < 2:
continue
current_attn = global_attn[indices]
n = indices.size(0)
for i in range(0, n, con_batch_size):
end_i = min(i + con_batch_size, n)
batch_attn = current_attn[i:end_i].unsqueeze(1) # 添加一个维度以便广播
all_attn = current_attn.unsqueeze(0) # 为全局注意力添加维度以便广播
diffs = batch_attn - all_attn
norms = torch.norm(diffs, dim=1) # [end_i - i, 36424]
norms = torch.norm(diffs, dim=1).unsqueeze(1) # 现在norms的形状是[batch_size, 1]
total_loss += norms.sum()
count += norms.numel()
average_loss = total_loss / count if count > 0 else torch.tensor(0.0, device=global_attn.device, dtype=global_attn.dtype)
# return average_loss
return average_loss.squeeze()