[69f1e5]: / code / mil_models_pytorch.py

Download this file

228 lines (178 with data), 8.5 kB

  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
import torch
import torchvision
import numpy as np
class MILArchitecture(torch.nn.Module):
def __init__(self, classes, mode='embedding', aggregation='mean', backbone='VGG19', include_background=False):
super(MILArchitecture, self).__init__()
"""Data Generator object for MIL.
CNN based architecture for MIL classification.
Args:
classes:
mode:
aggregation: max, mean, attentionMIL, mcAttentionMIL
backbone:
include_background:
Returns:
MILDataGenerator object
Last Updates: Julio Silva (19/03/21)
"""
'Internal states initialization'
self.classes = classes
self.mode = mode
self.aggregation = aggregation
self.backbone = backbone
self.include_background = include_background
self.C = []
self.prototypical = False
if self.include_background:
self.nClasses = len(classes) + 1
else:
self.nClasses = len(classes)
self.eps = 1e-6
# Backbone
self.bb = Encoder(pretrained=True, backbone=backbone, aggregation=True)
# Classifiers
if self.aggregation == 'mcAttentionMIL':
self.classifiers = torch.nn.ModuleList()
for i in np.arange(0, self.nClasses):
self.classifiers.append(torch.nn.Linear(512, 1))
else:
self.classifier = torch.nn.Linear(512, self.nClasses)
# MIL aggregation
self.milAggregation = MILAggregation(aggregation=aggregation, nClasses=self.nClasses, mode=self.mode)
def forward(self, images):
# Patch-Level feature extraction
features = self.bb(images)
if self.mode == 'instance':
# Classification
patch_classification = torch.softmax(self.classifier(torch.squeeze(features)), 1)
# MIL aggregation
global_classification = self.milAggregation(patch_classification)
if self.mode == 'embedding' or self.mode == 'mixed': # Activation on BCE loss
# Embedding aggregation
if self.aggregation == 'mcAttentionMIL':
embedding, patch_classification = self.milAggregation(torch.squeeze(features))
global_classifications = []
for i in np.arange(0, self.nClasses):
global_classifications.append(self.classifiers[i](embedding[:, i]))
global_classification = torch.cat(global_classifications, dim=0)
elif self.aggregation == 'attentionMIL':
embedding, w = self.milAggregation(torch.squeeze(features))
global_classification = self.classifier(embedding)
patch_classification = w
else:
embedding = self.milAggregation(torch.squeeze(features))
global_classification = self.classifier(embedding)
patch_classification = self.classifier(torch.squeeze(features))
if self.include_background:
global_classification = global_classification[1:]
return global_classification, patch_classification, features
class Encoder(torch.nn.Module):
def __init__(self, pretrained=True, backbone='resnet18', aggregation=False):
super(Encoder, self).__init__()
self.aggregation = aggregation
self.pretrained = pretrained
self.backbone = backbone
if backbone == 'resnet18':
resnet = torchvision.models.resnet18(pretrained=pretrained)
self.F = torch.nn.Sequential(resnet.conv1,
resnet.bn1,
resnet.relu,
resnet.maxpool,
resnet.layer1,
resnet.layer2,
resnet.layer3,
resnet.layer4)
elif backbone == 'vgg19':
vgg19 = torchvision.models.vgg16(pretrained=pretrained)
self.F = vgg19.features
# placeholder for the gradients
self.gradients = None
def forward(self, x):
out = self.F(x)
# register the hook
h = out.register_hook(self.activations_hook)
if self.aggregation:
out = torch.nn.AdaptiveAvgPool2d((1, 1))(out)
return out
# method for the gradient extraction
def get_activations_gradient(self):
return self.gradients
# method for the activation exctraction
def get_activations(self, x):
return self.features_conv(x)
# hook for the gradients of the activations
def activations_hook(self, grad):
self.gradients = grad
class MILAggregation(torch.nn.Module):
def __init__(self, aggregation='mean', nClasses=2, mode='embedding'):
super(MILAggregation, self).__init__()
"""Aggregation module for MIL.
Args:
aggregation:
Returns:
MILAggregation module for CNN MIL Architecture
Last Updates: Julio Silva (19/03/21)
"""
self.mode = mode
self.aggregation = aggregation
self.nClasses = nClasses
if self.aggregation == 'attentionMIL':
self.attentionModule = attentionMIL()
if self.aggregation == 'mcAttentionMIL':
self.attentionModules = torch.nn.ModuleList()
for i in np.arange(0, self.nClasses):
self.attentionModules.append(attentionMIL())
def forward(self, feats):
if self.aggregation == 'max':
embedding = torch.max(feats, dim=0)[0]
return embedding
elif self.aggregation == 'mean':
embedding = torch.mean(feats, dim=0)
return embedding
elif self.aggregation == 'attentionMIL':
# Attention embedding from Ilse et al. (2018) for MIL. It only works at the binary scenario at instance-level
embedding, w_logits = self.attentionModule(feats)
return embedding, torch.softmax(w_logits, dim=0)
elif self.aggregation == 'mcAttentionMIL':
attention_weights = []
embeddings = []
for i in np.arange(0, self.nClasses):
embeddings.append(self.attentionModules[i](feats)[0].unsqueeze(1))
attention_weights.append(self.attentionModules[i](feats)[1])
#patch_classification = torch.softmax(torch.cat(attention_weights, 1), 0)
if self.mode == 'embedding':
embedding = torch.cat(embeddings, 1)
patch_classification = torch.softmax(torch.cat(attention_weights, 1), 1)
#w = patch_classification
elif self.mode == 'mixed':
patch_classification = torch.softmax(torch.cat(attention_weights, 1), 0)
w = patch_classification * (1/torch.sum(patch_classification, 0) + 1e-6)
feats = torch.transpose(feats, 1, 0)
embedding = torch.squeeze(torch.mm(feats, w))
return embedding, patch_classification
class attentionMIL(torch.nn.Module):
def __init__(self, L=512, D=128, K=1):
super(attentionMIL, self).__init__()
# Attention embedding from Ilse et al. (2018) for MIL. It only works at the binary scenario.
self.L = L
self.D = D
self.K = K
self.attention_V = torch.nn.Sequential(
torch.nn.Linear(self.L, self.D),
torch.nn.Tanh()
)
self.attention_U = torch.nn.Sequential(
torch.nn.Linear(self.L, self.D),
torch.nn.Sigmoid()
)
self.attention_weights = torch.nn.Linear(self.D, self.K)
def forward(self, feats):
# Attention weights computation
A_V = self.attention_V(feats) # Attention
A_U = self.attention_U(feats) # Gate
w_logits = self.attention_weights(A_V * A_U) # Probabilities - softmax over instances
# Weighted average computation per class
feats = torch.transpose(feats, 1, 0)
embedding = torch.squeeze(torch.mm(feats, torch.softmax(w_logits, dim=0))) # KxL
return embedding, w_logits