-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdfc_model.py
542 lines (426 loc) · 17.9 KB
/
dfc_model.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
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
# https://github.com/rwindsor1/biobank-self-supervised-alignment/blob/main/src/models/VGGEncoders.py
import sys, os, glob
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision.models import resnet18, resnet50
class DualBaseline(nn.Module):
def __init__(
self, model, s1_channels, s2_channels, num_classes=8, feature_dim=1024
):
super(DualBaseline, self).__init__()
self.model_s1 = eval(model)(pretrained=False, num_classes=num_classes)
self.model_s1.conv1 = torch.nn.Conv2d(
s1_channels,
64,
kernel_size=(7, 7),
stride=(2, 2),
padding=(3, 3),
bias=False,
)
self.model_s1.fc = torch.nn.Identity()
self.model_s2 = eval(model)(pretrained=False, num_classes=num_classes)
self.model_s2.conv1 = torch.nn.Conv2d(
s2_channels,
64,
kernel_size=(7, 7),
stride=(2, 2),
padding=(3, 3),
bias=False,
)
self.model_s2.fc = torch.nn.Identity()
self.fc = torch.nn.Linear(feature_dim, num_classes)
def forward(self, x):
s1 = x["s1"]
s2 = x["s2"]
s1_emb = self.model_s1(s1)
s2_emb = self.model_s2(s2)
z = torch.cat([s1_emb, s2_emb], dim=1)
z = self.fc(z)
return z
class ConvSequence(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding=0, stride=1):
super(ConvSequence, self).__init__()
self.layers = nn.Sequential(
nn.Conv2d(
in_channels,
out_channels,
kernel_size,
padding=padding,
stride=stride,
bias=False,
),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
nn.Conv2d(
out_channels,
out_channels,
kernel_size,
padding=padding,
stride=1,
bias=False,
),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
def forward(self, x):
return self.layers(x)
class DoubleAlignmentDownstream(nn.Module):
"""concatenate outputs from two backbones and add one linear layer"""
def __init__(self, base_model, device, config):
super(DoubleAlignmentDownstream, self).__init__()
self.base_model = base_model
self.backbone1 = eval(base_model)(
embedding_size=config.embedding_size, input_modes=config.s1_input_channels
).to(device)
self.backbone2 = eval(base_model)(
embedding_size=config.embedding_size, input_modes=config.s2_input_channels
).to(device)
# dim_mlp1 = self.backbone1.fc.in_features
# dim_mlp2 = self.backbone2.fc.in_features
# add final linear layer
# note: this will produce differently sized vectors z depending on kernel_size, config.embedding_size and config.image_size
# roughly: z.shape[1] = 2*config.embedding_size * (config.image_px_size/8/kernel_size)**2
# where 2*config.embedding_size is due to the two inputs
# and config.image_px_size/8 due to pooling of the VGG encoders
kernel_size = 8
# dim_fc = 2*config.embedding_size * (config.image_px_size/8/kernel_size)**2
dim_fc = 2 * config.embedding_size
print("FC dim: ", dim_fc)
# self.avg_pool = torch.nn.AvgPool2d(kernel_size=kernel_size).to(device)
self.avg_pool = torch.nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc = nn.Linear(int(dim_fc), config.num_classes, bias=True).to(device)
def _get_basemodel(self, model_name):
try:
model = eval(self.base_model)
except KeyError:
raise InvalidBackboneError("Invalid backbone architecture")
else:
return model
def forward(self, x):
x1 = self.backbone1(x["s1"])
x2 = self.backbone2(x["s2"])
# in_map = torch.cat([x1, x2], dim=1)
# out_map = self.avg_pool(in_map)
# z = out_map.flatten(start_dim=1, end_dim=-1)
x1 = self.avg_pool(x1).flatten(start_dim=1, end_dim=-1)
x2 = self.avg_pool(x2).flatten(start_dim=1, end_dim=-1)
z = torch.cat([x1, x2], dim=1)
z = self.fc(z)
return z
def load_trained_state_dict(self, checkpoint):
"""load the pre-trained backbone weights"""
# log = self.load_state_dict(weights, strict=True)
log_s1 = self.backbone1.load_state_dict(
checkpoint["s1_model_weights"], strict=True
)
log_s2 = self.backbone2.load_state_dict(
checkpoint["s2_model_weights"], strict=True
)
# freeze all layers but the last fc
for name, param in self.named_parameters():
if name not in ["fc.weight", "fc.bias"]:
param.requires_grad = False
class VGGEncoder(nn.Module):
"""Scan encoding model"""
def __init__(self, embedding_size=512, input_modes=1):
super(VGGEncoder, self).__init__()
self.embedding_size = embedding_size
self.convs1 = ConvSequence(input_modes, 64, kernel_size=(3, 3), padding=1)
self.convs2 = ConvSequence(64, 128, kernel_size=(3, 3), padding=1)
self.convs3 = ConvSequence(128, 256, kernel_size=(3, 3), padding=1)
self.convs4 = ConvSequence(256, embedding_size, kernel_size=(3, 3), padding=1)
def forward(self, x):
x1 = self.convs1(x)
x2 = F.max_pool2d(x1, kernel_size=2)
x2 = self.convs2(x2)
x3 = F.max_pool2d(x2, kernel_size=2)
x3 = self.convs3(x3)
x4 = F.max_pool2d(x3, kernel_size=2)
x4 = self.convs4(x4)
return x4
def forward_with_skips(self, x):
x1 = self.convs1(x)
x2 = F.max_pool2d(x1, kernel_size=2)
x2 = self.convs2(x2)
x3 = F.max_pool2d(x2, kernel_size=2)
x3 = self.convs3(x3)
x4 = F.max_pool2d(x3, kernel_size=2)
x4 = self.convs4(x4)
return x4, x3, x2, x1
class VGGEncoderShallow(nn.Module):
"""Scan encoding model"""
def __init__(self, embedding_size=128, input_modes=1):
super(VGGEncoderShallow, self).__init__()
self.embedding_size = embedding_size
self.convs1 = ConvSequence(input_modes, 64, kernel_size=(3, 3), padding=1)
self.convs2 = ConvSequence(64, embedding_size, kernel_size=(3, 3), padding=1)
def forward(self, x):
x1 = self.convs1(x)
x2 = F.max_pool2d(x1, kernel_size=2)
x2 = self.convs2(x2)
return x2
class Classifier(nn.Module):
"""Put a linear head on top of two VGGEncoders"""
def __init__(self, s1_model, s2_model, embedding_size=128, num_classes=10):
super(Classifier, self).__init__()
self.s1_model = s1_model
self.s2_model = s2_model
self.fc = torch.nn.Linear(embedding_size * 2, num_classes)
self.avgpool = torch.nn.AdaptiveAvgPool2d((1, 1))
def forward(self, s1, s2):
s1_embedding = self.s1_model(s1)
s2_embedding = self.s2_model(s2)
embedding = torch.cat((s1_embedding, s2_embedding), dim=1)
embedding = self.avgpool(embedding)
embedding = torch.flatten(embedding, 1)
output = self.fc(embedding)
return output
class SegmentorVGG(nn.Module):
"""Small segmentation network on top of the VGGEncoders"""
def __init__(self, s1_model, s2_model, embedding_size=128, num_classes=10):
super(Segmentor, self).__init__()
self.s1_model = s1_model
self.s2_model = s2_model
self.up = torch.nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.conv = torch.nn.Conv2d(
embedding_size * 2, embedding_size, kernel_size=3, padding=1
)
self.bn = torch.nn.BatchNorm2d(embedding_size)
self.relu = torch.nn.ReLU(inplace=True)
self.out_conv = torch.nn.Conv2d(embedding_size, num_classes, kernel_size=1)
def forward(self, s1, s2):
s1_embedding = self.s1_model(s1)
s2_embedding = self.s2_model(s2)
embedding = torch.cat((s1_embedding, s2_embedding), dim=1)
embedding = self.up(embedding)
embedding = self.conv(embedding)
embedding = self.bn(embedding)
embedding = self.relu(embedding)
output = self.out_conv(embedding)
return output
# https://github.com/milesial/Pytorch-UNet/blob/master/unet/unet_parts.py
""" Parts of the U-Net model """
class DoubleConv(nn.Module):
"""(convolution => [BN] => ReLU) * 2"""
def __init__(self, in_channels, out_channels, mid_channels=None):
super().__init__()
if not mid_channels:
mid_channels = out_channels
self.double_conv = nn.Sequential(
nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(mid_channels),
nn.ReLU(inplace=True),
nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.double_conv(x)
class Down(nn.Module):
"""Downscaling with maxpool then double conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2), DoubleConv(in_channels, out_channels)
)
def forward(self, x):
return self.maxpool_conv(x)
class Up(nn.Module):
"""Upscaling then double conv"""
def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
# if bilinear, use the normal convolutions to reduce the number of channels
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
else:
self.up = nn.ConvTranspose2d(
in_channels, in_channels // 2, kernel_size=2, stride=2
)
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.up(x1)
# input is CHW
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2])
# if you have padding issues, see
# https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
# https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
x = torch.cat([x2, x1], dim=1)
return self.conv(x)
class FeedbackUp(nn.Module):
"""Upscaling then double conv, return the upsampled to same size inputs too"""
def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
# if bilinear, use the normal convolutions to reduce the number of channels
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
else:
self.up = nn.ConvTranspose2d(
in_channels, in_channels // 2, kernel_size=2, stride=2
)
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.up(x1)
# input is CHW
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2])
# if you have padding issues, see
# https://github.com/HaiyongJiang/U-Net-Pytorch-Unstructured-Buggy/commit/0e854509c2cea854e247a9c615f175f76fbb2e3a
# https://github.com/xiaopeng-liao/Pytorch-UNet/commit/8ebac70e633bac59fc22bb5195e513d5832fb3bd
x = torch.cat([x2, x1], dim=1)
return self.conv(x), x1, x2
class OutConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(OutConv, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)
def forward(self, x):
return self.conv(x)
""" Full assembly of the parts to form the complete network """
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(UNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
factor = 2 if bilinear else 1
self.down4 = Down(512, 1024 // factor)
self.up1 = Up(1024, 512 // factor, bilinear)
self.up2 = Up(512, 256 // factor, bilinear)
self.up3 = Up(256, 128 // factor, bilinear)
self.up4 = Up(128, 64, bilinear)
self.outc = OutConv(64, n_classes)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits
class FeedbackUNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(FeedbackUNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
factor = 2 if bilinear else 1
self.down4 = Down(512, 1024 // factor)
self.up1 = FeedbackUp(1024, 512 // factor, bilinear)
self.up2 = FeedbackUp(512, 256 // factor, bilinear)
self.up3 = FeedbackUp(256, 128 // factor, bilinear)
self.up4 = FeedbackUp(128, 64, bilinear)
self.outc = OutConv(64, n_classes)
def forward(self, x_in):
x1 = self.inc(x_in)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x, dec1, enc1 = self.up1(x5, x4)
x, dec2, enc2 = self.up2(x, x3)
x, dec3, enc3 = self.up3(x, x2)
x, dec4, enc4 = self.up4(x, x1)
logits = self.outc(x)
return {
"logits": logits,
# "dec0" : torch.max(logits, dim=1)[1], "enc0" : x_in,
"dec1": dec1,
"enc1": enc1,
"dec2": dec2,
"enc2": enc2,
"dec3": dec3,
"enc3": enc3,
"dec4": dec4,
"enc4": enc4,
}
class UNetEncoder(nn.Module):
"""Encoder part of a UNet"""
def __init__(self, n_channels, bilinear=True):
super(UNetEncoder, self).__init__()
self.n_channels = n_channels
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
factor = 2 if bilinear else 1
self.down4 = Down(512, 1024 // factor)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
return x1, x2, x3, x4, x5
class DoubleUp(nn.Module):
"""Upscaling then double conv based on two UNet encoders"""
def __init__(self, in_channels, out_channels, bilinear=True):
super().__init__()
# if bilinear, use the normal convolutions to reduce the number of channels
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
else:
self.up = nn.ConvTranspose2d(
in_channels, in_channels // 2, kernel_size=2, stride=2
)
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, z, x2, y2):
z = self.up(z)
assert x2.shape == y2.shape
# input is CHW
diffY = x2.size()[2] - z.size()[2]
diffX = x2.size()[3] - z.size()[3]
z = F.pad(z, [diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2])
z = torch.cat([z, x2, y2], dim=1)
return self.conv(z)
class DoubleUNet(nn.Module):
"""UNet with two separate encoders for different modalities
maps are concatenated at every level"""
def __init__(self, n_channels1, n_channels2, n_classes, bilinear=True):
super(DoubleUNet, self).__init__()
self.n_channels1 = n_channels1
self.n_channels2 = n_channels2
self.n_classes = n_classes
self.bilinear = bilinear
# unet1 = UNet(n_channels1, n_classes)
# unet2 = UNet(n_channels2, n_classes)
# self.encoder1 = torch.nn.Sequential(*[*list(unet1.inc.children()), *list(unet1.down1.children()), *list(unet1.down2.children()), *list(unet1.down3.children()), *list(unet1.down4.children())])
# self.encoder2 = torch.nn.Sequential(*[*list(unet2.inc.children()), *list(unet2.down1.children()), *list(unet2.down2.children()), *list(unet2.down3.children()), *list(unet2.down4.children())])
self.encoder1 = UNetEncoder(n_channels1, bilinear)
self.encoder2 = UNetEncoder(n_channels2, bilinear)
factor = 2 if bilinear else 1
self.up1 = DoubleUp(2048, 1024 // factor, bilinear)
self.up2 = DoubleUp(1024, 512 // factor, bilinear)
self.up3 = DoubleUp(512, 256 // factor, bilinear)
self.up4 = DoubleUp(256, 128, bilinear)
self.outc = OutConv(128, n_classes)
def forward(self, x, y):
x1, x2, x3, x4, x5 = self.encoder1(x)
y1, y2, y3, y4, y5 = self.encoder2(y)
# merge representations at lowest layer
z = torch.cat([x5, y5], dim=1)
z = self.up1(z, x4, y4)
z = self.up2(z, x3, y3)
z = self.up3(z, x2, y2)
z = self.up4(z, x1, y1)
logits = self.outc(z)
return logits