-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_mipcl.py
166 lines (145 loc) · 4.62 KB
/
engine_mipcl.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
from typing import Optional
import torch
import torch.nn.functional as F
from engine_base import BaseEngine
from losses import InfoNCE, SimMaxLoss, SimMinLoss
from model_mipcl import MIPCL
class EngineMIPCL(BaseEngine):
def __init__(
self,
in_channels: int,
intermediate_dim: int,
n_classes: int,
stain_info: bool,
dropout: bool,
alpha: Optional[float] = None,
thresh: Optional[float] = 0.85,
temperature: Optional[float] = 0.07,
bag_weight: Optional[float] = None
) -> None:
super().__init__(in_channels, intermediate_dim, n_classes, stain_info, dropout)
# Init model
self.model = MIPCL(
in_channels=in_channels,
intermediate_dim=intermediate_dim,
n_classes=n_classes,
stain_info=stain_info,
dropout=dropout,
thresh=thresh
)
# Loss functions
self.alpha = alpha
self.bag_weight = bag_weight
if alpha is not None:
self.criterion = [
SimMaxLoss(alpha=alpha),
SimMinLoss(),
SimMaxLoss(alpha=alpha)
]
else:
# Use InfoNCE
self.criterion = [
InfoNCE(temperature=temperature,
negative_mode='unpaired'),
]
def training_step(self, batch, batch_idx):
x = batch['img']
x_fname = batch['filename']
y = batch['label']
wsi_logits, fg, bg, topk_idx, _, _ = self.model(x, x_fname)
total_loss = self.ccam_loss(
wsi_logits=wsi_logits,
foreground=fg,
background=bg,
label=y
)
if self.global_step % 10000 == 0:
top_pos_k = topk_idx['pos_idx']
top_neg_k = topk_idx['neg_idx']
print(
f"""
total N, top (+), top (-): {x.size(0)}, {len(top_pos_k)}, {len(top_neg_k)}
"""
)
self.log("train_loss",
total_loss,
on_step=False,
on_epoch=True,
# prog_bar=True,
logger=True,
batch_size=1
)
return {
"loss": total_loss,
}
@torch.no_grad()
def validation_step(self, batch, batch_idx):
x = batch['img']
x_fname = batch['filename']
y = batch['label']
wsi_logits, fg, bg, _, _, _ = self.model(x, x_fname)
total_loss = self.ccam_loss(
wsi_logits=wsi_logits,
foreground=fg,
background=bg,
label=y
)
self.log("val_loss",
total_loss,
on_step=False,
on_epoch=True,
# prog_bar=True,
logger=True,
batch_size=1)
return {
'loss': total_loss,
}
@torch.no_grad()
def test_step(self, batch, batch_idx):
x = batch['img']
x_fname = batch['filename']
y = batch['label']
wsi_logits, fg, bg, ids, A_raw, _ = self.model(
x, x_fname)
total_loss = self.ccam_loss(
wsi_logits=wsi_logits,
foreground=fg,
background=bg,
label=y
)
y_pred = torch.topk(wsi_logits, 1, dim=1)[1]
y_pred = y_pred.squeeze(0)
y_prob = F.softmax(wsi_logits, dim=1)
return {
'loss': total_loss,
'y_pred': y_pred,
'y_prob': y_prob,
'target': y,
'top_ids': ids,
'A_raw': A_raw,
'filename': x_fname
}
def ccam_loss(self,
wsi_logits,
foreground,
background,
label):
if self.alpha is not None:
bg_bg = self.criterion[0](background)
bg_fg = self.criterion[1](background, foreground)
fg_fg = self.criterion[2](foreground)
instance_loss = bg_bg + bg_fg + fg_fg
else:
instance_loss = self.criterion[0](
query=foreground,
positive_key=foreground,
negative_keys=background
)
wsi_pred_loss = self.bag_loss_fn(wsi_logits, label)
if self.bag_weight is not None:
total_loss = (self.bag_weight * wsi_pred_loss) + \
((1 - self.bag_weight) * instance_loss)
else:
total_loss = instance_loss + wsi_pred_loss
total_loss = total_loss.unsqueeze_(0)
return total_loss