-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine_clam.py
130 lines (111 loc) · 3.77 KB
/
engine_clam.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
from typing import Optional
import torch
import torch.nn.functional as F
from engine_base import BaseEngine
from model_clam import CLAM
class EngineCLAM(BaseEngine):
def __init__(
self,
in_channels: int,
intermediate_dim: int,
n_classes: int,
stain_info: bool,
dropout: bool,
k_sample: int,
inst_loss: Optional[str] = 'svm',
bag_weight: Optional[float] = None
) -> None:
super().__init__(in_channels, intermediate_dim, n_classes, stain_info, dropout)
# Init model
self.model = CLAM(
in_channels=in_channels,
intermediate_dim=intermediate_dim,
dropout=dropout,
stain_info=stain_info,
n_classes=n_classes,
inst_loss_type=inst_loss,
k_sample=k_sample
)
self.bag_weight = bag_weight
def training_step(self, batch, batch_idx):
x = batch['img']
x_fname = batch['filename']
y = batch['label']
logits, _, _, inst_loss, _, _ = self.model(x,
fname=x_fname,
label=y,
# instance_eval=True
)
total_loss = self.clam_loss(
logits=logits,
y=y,
inst_loss=inst_loss
)
self.log("train_loss",
total_loss,
on_step=False,
on_epoch=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']
logits, _, _, inst_loss, _, _ = self.model(x,
fname=x_fname,
label=y,
# instance_eval=True
)
total_loss = self.clam_loss(
logits=logits,
y=y,
inst_loss=inst_loss
)
self.log("val_loss",
total_loss,
on_step=True,
on_epoch=False,
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']
logits, y_prob, y_pred, inst_loss, A_raw, top_ids = self.model(x,
fname=x_fname,
label=y,
# instance_eval=True
)
total_loss = self.clam_loss(
logits=logits,
y=y,
inst_loss=inst_loss
)
return {
'loss': total_loss,
'y_pred': y_pred,
'y_prob': y_prob,
'target': y,
'top_ids': top_ids,
'A_raw': A_raw,
'filename': x_fname
}
def clam_loss(self,
logits,
y,
inst_loss,
):
bag_loss = self.bag_loss_fn(logits, y)
total_loss = (self.bag_weight * bag_loss) + \
((1 - self.bag_weight) * inst_loss)
total_loss = total_loss.unsqueeze_(0)
return total_loss