Repository navigation
Expand file tree
/
Copy pathFSL_model.py
More file actions
624 lines (500 loc) · 20.4 KB
/
Copy pathFSL_model.py
File metadata and controls
624 lines (500 loc) · 20.4 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import math
# pytorch modules
import torch
import torch.nn as nn
from torchmetrics import Accuracy
import pytorch_lightning as pl
# debugging module
#from icecream import ic
###################################
### embedding network which is kept the same for every FSL network
### we use a standard conv4-net
###################################
def init_layer(L):
"""
Initialize the weights of a layer using fan-in initialization.
Args:
L (nn.Module): The layer to initialize.
Returns:
None
"""
if isinstance(L, nn.Conv2d):
n = L.kernel_size[0]*L.kernel_size[1]*L.out_channels
L.weight.data.normal_(0,math.sqrt(2.0/float(n)))
elif isinstance(L, nn.BatchNorm2d):
L.weight.data.fill_(1)
L.bias.data.fill_(0)
class ConvBlock(nn.Module):
"""
Convolutional block module used in the embedding network.
Args:
indim (int): Number of input channels.
outdim (int): Number of output channels.
pool (bool): Whether to apply max pooling after convolution. Default is True.
padding (int): Amount of padding. Default is 1.
Attributes:
indim (int): Number of input channels.
outdim (int): Number of output channels.
C (nn.Conv2d): Convolutional layer.
BN (nn.BatchNorm2d): Batch normalization layer.
relu (nn.ReLU): ReLU activation layer.
parametrized_layers (list): List of layers with learnable parameters.
pool (nn.MaxPool2d): Max pooling layer.
trunk (nn.Sequential): Sequential container for the layers.
Methods:
forward(x): Forward pass through the block.
"""
def __init__(self, indim, outdim, pool=True, padding=1):
super(ConvBlock, self).__init__()
self.indim = indim
self.outdim = outdim
self.C = nn.Conv2d(indim, outdim, 3, padding=padding)
self.BN = nn.BatchNorm2d(outdim)
self.relu = nn.ReLU(inplace=True)
self.parametrized_layers = [self.C, self.BN, self.relu]
if pool:
self.pool = nn.MaxPool2d(2)
self.parametrized_layers.append(self.pool)
for layer in self.parametrized_layers:
init_layer(layer)
self.trunk = nn.Sequential(*self.parametrized_layers)
def forward(self, x):
"""
Forward pass through the ConvBlock.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor.
"""
out = self.trunk(x)
return out
class ConvNet(nn.Module):
"""
Convolutional network module used in the embedding network.
Attributes:
trunk (nn.Sequential): Sequential container for the ConvBlocks.
Methods:
forward(x): Forward pass through the ConvNet.
"""
def __init__(self):
super(ConvNet, self).__init__()
trunk = []
for i in range(4):
indim = 2 if i == 0 else 64
outdim = 64
B = ConvBlock(indim, outdim, pool=True)
trunk.append(B)
trunk.append(nn.Flatten())
self.trunk = nn.Sequential(*trunk)
def forward(self, x):
"""
Forward pass through the ConvNet.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor.
"""
out = self.trunk(x)
return out
###################################
### embedding network which is kept the same for every FSL network
###################################
class EmbeddingNetwork(nn.Module):
"""
Embedding network module.
Args:
channels (int): Number of input channels.
crop_size (int): Size of the input image after cropping.
Attributes:
convolutional_relu_stack (nn.Sequential): Sequential container for the convolutional layers and ReLU activations.
linear_relu_stack (nn.Sequential): Sequential container for the linear layers and ReLU activations.
Methods:
forward(x): Forward pass through the EmbeddingNetwork.
"""
def __init__(self, channels, crop_size):
super(EmbeddingNetwork, self).__init__()
fc_nodes = 100
dropout = 0.2
self.convolutional_relu_stack = nn.Sequential(
nn.Conv2d(channels, 32, 3, stride=1, padding='valid'),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 64, 3, stride=1, padding='valid'),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 128, 3, stride=1, padding='valid'),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Flatten(),
nn.Linear(in_features=(crop_size-6)**2 * 128, out_features=fc_nodes),
nn.Dropout(dropout),
nn.ReLU(),
nn.BatchNorm1d(fc_nodes),
)
self.linear_relu_stack = nn.Sequential(
nn.Linear(fc_nodes, fc_nodes),
nn.Dropout(dropout),
nn.ReLU(),
nn.BatchNorm1d(fc_nodes)
)
def forward(self, x):
"""
Forward pass through the EmbeddingNetwork.
Args:
x (torch.Tensor): Input tensor.
Returns:
torch.Tensor: Output tensor.
"""
x = self.convolutional_relu_stack(x)
for i in range(2):
x = self.linear_relu_stack(x)
return x
###################################
### Matching network implementation
###################################
class MatchingNetwork(nn.Module):
"""
Matching network module.
Args:
device (torch.device): Device to run the module on.
Attributes:
embedding_layer (ConvNet): Embedding network.
cos_dist (nn.CosineSimilarity): Cosine similarity module.
softmax (nn.Softmax): Softmax activation module.
device (torch.device): Device to run the module on.
Methods:
forward(query, support): Forward pass through the MatchingNetwork.
"""
def __init__(self, device):
super(MatchingNetwork, self).__init__()
self.embedding_layer = ConvNet()
self.cos_dist = nn.CosineSimilarity(dim=1)
self.softmax = nn.Softmax(dim=0)
self.device = device
def forward(self, query, support):
"""
Forward pass through the MatchingNetwork.
Args:
query (dict): Dictionary containing the query data.
support (dict): Dictionary containing the support data.
Returns:
torch.Tensor: Output tensor.
"""
support["embeddings"] = self.embedding_layer(support["image"])
query["embeddings"] = self.embedding_layer(query["image"])
cos_distances = []
for embedding in support["embeddings"]:
cos_distances.append(torch.exp(self.cos_dist(query["embeddings"], embedding)))
cos_distances = torch.stack(cos_distances).squeeze(-1)
attentions = self.softmax(cos_distances)
support["attentions"] = attentions
y = torch.matmul(support["attentions"].T, torch.nn.functional.one_hot(support["target"]).float().to(self.device))
return y
###################################
### Prototypical network implementation
###################################
class PrototypicalNetwork(nn.Module):
"""
Prototypical network module.
Attributes:
embedding_layer (ConvNet): Embedding network.
Methods:
forward(query, support): Forward pass through the PrototypicalNetwork.
"""
def __init__(self):
super(PrototypicalNetwork, self).__init__()
self.embedding_layer = ConvNet()
def forward(self, query, support):
"""
Forward pass through the PrototypicalNetwork.
Args:
query (dict): Dictionary containing the query data.
support (dict): Dictionary containing the support data.
Returns:
torch.Tensor: Output tensor.
"""
support["embeddings"] = self.embedding_layer(support["image"])
query["embeddings"] = self.embedding_layer(query["image"])
support_embeds = []
for idx in range(len(support["classlist"])):
embeds = support["embeddings"][support["target"] == idx]
support_embeds.append(embeds)
support_embeds = torch.stack(support_embeds)
prototypes = support_embeds.mean(dim=1)
support["prototypes"] = prototypes
distances = torch.cdist(query["embeddings"].unsqueeze(0), prototypes.unsqueeze(0), p=2).squeeze(0)
distances = distances ** 2
logits = -distances
return logits
###################################
### Relation network implementation
###################################
class RelationNetwork(nn.Module):
"""
Relation network module.
Attributes:
embedding_layer (ConvNet): Embedding network.
fc_nodes (int): Number of nodes in the fully connected layers.
relation_module (nn.Sequential): Sequential container for the relation module layers.
Methods:
forward(query, support): Forward pass through the RelationNetwork.
"""
def __init__(self):
super(RelationNetwork, self).__init__()
self.embedding_layer = ConvNet()
self.fc_nodes = 100
self.relation_module = nn.Sequential(
nn.Linear(512, self.fc_nodes),
nn.Dropout(0.2),
nn.ReLU(),
nn.BatchNorm1d(self.fc_nodes),
nn.Linear(self.fc_nodes, self.fc_nodes),
nn.Dropout(0.2),
nn.ReLU(),
nn.BatchNorm1d(self.fc_nodes),
nn.Linear(self.fc_nodes, 1),
nn.Dropout(0.2),
nn.ReLU(),
)
def forward(self, query, support):
"""
Forward pass through the RelationNetwork.
Args:
query (dict): Dictionary containing the query data.
support (dict): Dictionary containing the support data.
Returns:
torch.Tensor: Output tensor.
"""
support["embeddings"] = self.embedding_layer(support["image"])
query["embeddings"] = self.embedding_layer(query["image"])
support_embeds = []
for idx in range(len(support["classlist"])):
embeds = support["embeddings"][support["target"] == idx]
support_embeds.append(embeds)
support_embeds = torch.stack(support_embeds)
sums = support_embeds.sum(dim=1)
support["sums"] = sums / torch.sum(sums)
relation_scores = {}
for qvector in query['embeddings']:
relation_scores[qvector] = []
concats = []
for svector in sums:
concat = torch.cat((qvector, svector))
concats.append(concat)
relation_scores[qvector] = self.relation_module(torch.stack(concats)).squeeze(1)
fin_rel_scores = torch.stack([rel_score for rel_score in relation_scores.values()])
return fin_rel_scores
###################################
### pytorch lightning network which trains using episodic training
### it can take any of the three networks above (matching, prototypical, or relation)
### as the FS network
###################################
class FewShotLearner(pl.LightningModule):
def __init__(self,
FSLnet: nn.Module,
learning_rate: float = 0.001,
n_way: int = 5
):
super().__init__()
self.save_hyperparameters(ignore=['matchnet'])
self.FSLnet = FSLnet
self.learning_rate = learning_rate
self.loss = nn.CrossEntropyLoss()
self.metrics = nn.ModuleDict({
'accuracy': Accuracy(task="multiclass", num_classes=n_way)
})
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate)
return optimizer
def step(self, batch, batch_idx, tag: str):
support, query = batch
logits = self.FSLnet(query, support)
loss = self.loss(logits, query["target"])
output = {"loss": loss}
for k, metric in self.metrics.items():
output[k] = metric(logits, query["target"])
for k, v in output.items():
self.log(f"{k}/{tag}", v)
return output
def training_step(self, batch, batch_idx):
return self.step(batch, batch_idx, "train")
def validation_step(self, batch, batch_idx):
return self.step(batch, batch_idx, "val")
def test_step(self, batch, batch_idx):
return self.step(batch, batch_idx, "test")
###################################
### simple shot classifier
###################################
class SimpleShotEmbed(nn.Module):
def __init__(self, classes, crop_size=20):
super().__init__()
self.embedding_layer = ConvNet()
if crop_size == 20:
self.dense = nn.Linear(256, classes)
if crop_size == 40:
self.dense = nn.Linear(256, classes)
def forward(self, x, training = True):
x = self.embedding_layer(x)
if training == True:
x = self.dense(x)
logits = torch.nn.functional.softmax(x,dim=1)
return logits
else:
x = torch.nn.functional.normalize(x)
return x
###################################
### simple shot fsl network
###################################
class SimpleShot(nn.Module):
'''
This class implements a simpleshot FSL network. It takes a model as input and uses it to compute
embeddings for the query and support sets. It then computes the euclidean distance between the
query embeddings and the average support embeddings for each class. It then returns the class
with the smallest distance.
'''
def __init__(self, model):#, channels, crop_size, n_outputs, fc_layers, fc_nodes, dropout):
super().__init__()
self.classifier = model
def forward(self, query, support, n_way):
# compute embeddings for query and support sets
support["embeddings"] = self.classifier(support["image"], training=False)
query["embeddings"] = self.classifier(query["image"], training=False)
# make an average feature vector for each class
average_support_vecs = []
for i in range(n_way):
average_support_vecs.append( support["embeddings"][support["target"]==i,:].mean(dim=0) )
average_support_vecs = torch.stack(average_support_vecs)
# L2 normalise output
support["embeddings"] = torch.nn.functional.normalize(support["embeddings"])
average_support_vecs = torch.nn.functional.normalize(average_support_vecs)
# support["embeddings"] i of shape (n_query, dimension of embedding vector)
# average_support_vecs is of shape (n_way, dimension of embedding vector)
# find euclidean distances between them
distances = torch.cdist(query["embeddings"], average_support_vecs, p=2).squeeze(0)
# distances is a tensor of dimensions (n_query, n_ways)
distances = distances ** 2
logits = torch.argmin(distances, dim=1)
support["embeddings_norm"] = torch.nn.functional.normalize(support["embeddings"])
query["embeddings_norm"] = torch.nn.functional.normalize(query["embeddings"])
return logits #, distances
class knnNet(nn.Module):
'''
'''
def __init__(self, device):
super().__init__()
self.classifier = simpleShotembed
self.device = device
def forward(self, query, support, neighbors=1, embeddings=True):
KNN = KNeighborsClassifier(n_neighbors=neighbors)
if embeddings:
# compute embeddings for query and support sets
support["embeddings"] = self.classifier(support["image"], training=False)
query["embeddings"] = self.classifier(query["image"], training=False)
KNN.fit(support["embeddings"].detach().numpy(), support["target"].detach().numpy())
y = KNN.predict(query["embeddings"].detach().numpy())
else:
KNN.fit(torch.flatten(support["image"], start_dim=1, end_dim=-1).detach().numpy(), support["target"].detach().numpy() )
y = KNN.predict(torch.flatten(query["image"], start_dim=1, end_dim=-1).detach().numpy())
return torch.tensor(y).float().to(self.device)
###################################
### UNet implementation
###################################
class DoubleConv(nn.Module):
"""
Double convolution that does not change the resolution.
Args:
n_channels1: number of channels in the input
n_channels2: number of channels after first conv layer
n_channels3: number of channels after second conv layer
Methods:
forward: applies the double conv.
"""
def __init__(self, n_channels1, n_channels2, n_channels3):
super().__init__()
self.double_conv = nn.Sequential(
nn.Conv2d(n_channels1, n_channels2, kernel_size=3, padding='same'),
nn.ReLU(inplace=True),
nn.BatchNorm2d(n_channels2, eps=0.001, momentum = 0.99),
nn.Conv2d(n_channels2, n_channels3, kernel_size=3, padding='same'),
nn.ReLU(inplace=True),
nn.BatchNorm2d(n_channels3, eps=0.001, momentum = 0.99))
def forward(self, x):
return self.double_conv(x)
class downsample(nn.Module):
"""
A double convolution followed by a maxpooling layer to downsample.
Args:
n_channels1: number of channels in the input
n_channels2: number of channels after first conv layer
n_channels3: number of channels after second conv layer
"""
def __init__(self, n_channels1, n_channels2, n_channels3):
super().__init__()
self.doubleconvDown = DoubleConv(n_channels1, n_channels2, n_channels3)
def forward(self, x):
skip = self.doubleconvDown(x)
y = nn.MaxPool2d(2)(skip)
y = nn.Dropout(0.3)(y)
return skip, y
class upsample(nn.Module):
"""
A transpose convolution (to upsample) followed by a double convolution.
Args:
n_channels1: number of channels in the input
n_channels2: number of channels after first conv layer
n_channels3: number of channels after second conv layer
"""
def __init__(self, n_channels1, n_channels2, n_channels3):
super().__init__()
self.convTranspose = nn.Sequential(nn.ConvTranspose2d(n_channels1, n_channels2, 2 , stride=2))
self.doubleConv = DoubleConv(n_channels1, n_channels2, n_channels3) #n_channels2 is first because it's concatenated with skip
def forward(self, skip, x):
# upsample
x = self.convTranspose(x)
# concatenate with the skip
x = torch.concatenate([x, skip], axis=1)
# dropout
x = nn.Dropout(0.3)(x)
# double convolution
x = self.doubleConv(x)
return x
class UNet(nn.Module):
"""
A fully convolutional network that outputs a binary map.
"""
def __init__(self):
super().__init__()
# downsample 1
self.downsample1 = downsample(1, 32, 32)
# downsample 2
self.downsample2 = downsample(32, 64, 64)
# downsample 3
self.downsample3 = downsample(64, 128, 128)
# bottleneck
self.bottleneck = DoubleConv(128,256,256)
# upsample 1
self.upsample1 = upsample(256, 128, 128)
# upsample 2
self.upsample2 = upsample(128, 64, 64)
# upsample 3
self.upsample3 = upsample(64, 32, 32)
# final layer
self.output = nn.Sequential(nn.Conv2d(32,2, kernel_size=1, padding="same"))
def forward(self, x):
# downsample 1
skip1, x1 = self.downsample1(x)
# downsample 2
skip2, x2 = self.downsample2(x1)
# downsample 3
skip3, x3 = self.downsample3(x2)
# bottleneck
bottleneck = self.bottleneck(x3)
# upsample 1
x4 = self.upsample1(skip3, bottleneck)
# upsample 2
x5 = self.upsample2(skip2, x4)
# upsample 3
x6 = self.upsample3(skip1, x5)
outputs = self.output(x6)
return outputs
###################################