forked from echonax07/MMSeaIce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunet.py
766 lines (623 loc) · 33.3 KB
/
unet.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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""U-Net model."""
__author__ = 'Muhammed Patel'
__contributor__ = 'Xinwwei chen, Fernando Pena Cantu,Javier Turnes, Eddie Park'
__copyright__ = ['university of waterloo']
__contact__ = ['[email protected]', '[email protected]']
__version__ = '1.0.0'
__date__ = '2024-04-05'
# -- File info -- #
from torchvision.models.resnet import BasicBlock # type: ignore
from torch import nn # type: ignore
from torchvision import models # type: ignore
# -- Third-party modules -- #
import torch # type: ignore
class UNet(torch.nn.Module):
"""PyTorch U-Net Class. Uses unet_parts."""
def __init__(self, options):
super().__init__()
self.input_block = DoubleConv(options, input_n=len(options['train_variables']),
output_n=options['unet_conv_filters'][0])
self.contract_blocks = torch.nn.ModuleList()
for contract_n in range(1, len(options['unet_conv_filters'])):
self.contract_blocks.append(
ContractingBlock(options=options,
input_n=options['unet_conv_filters'][contract_n - 1],
output_n=options['unet_conv_filters'][contract_n]))
# only used to contract input patch.
self.bridge = ContractingBlock(
options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1])
self.expand_blocks = torch.nn.ModuleList()
self.expand_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
#self.sic_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SIC'])
#self.sod_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SOD'])
#self.floe_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['FLOE'])
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return {'SIC': self.sic_feature_map(x_expand),
'SOD': self.sod_feature_map(x_expand),
'FLOE': self.floe_feature_map(x_expand)}
class FeatureMap(torch.nn.Module):
"""Class to perform final 1D convolution before calculating cross entropy or using softmax."""
def __init__(self, input_n, output_n):
super(FeatureMap, self).__init__()
self.feature_out = torch.nn.Conv2d(input_n, output_n, kernel_size=(1, 1), stride=(1, 1))
def forward(self, x):
"""Pass x through final layer."""
return self.feature_out(x)
class DoubleConv(torch.nn.Module):
"""Class to perform a double conv layer in the U-NET architecture. Used in unet_model.py."""
def __init__(self, options, input_n, output_n):
super(DoubleConv, self).__init__()
self.double_conv = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=input_n,
out_channels=output_n,
kernel_size=options['conv_kernel_size'],
stride=options['conv_stride_rate'],
padding=options['conv_padding'],
padding_mode=options['conv_padding_style'],
bias=False),
torch.nn.BatchNorm2d(output_n),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=output_n,
out_channels=output_n,
kernel_size=options['conv_kernel_size'],
stride=options['conv_stride_rate'],
padding=options['conv_padding'],
padding_mode=options['conv_padding_style'],
bias=False),
torch.nn.BatchNorm2d(output_n),
torch.nn.ReLU()
)
def forward(self, x):
"""Pass x through the double conv layer."""
x = self.double_conv(x)
return x
class ContractingBlock(torch.nn.Module):
"""Class to perform downward pass in the U-Net."""
def __init__(self, options, input_n, output_n):
super(ContractingBlock, self).__init__()
self.contract_block = torch.nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
self.double_conv = DoubleConv(options, input_n, output_n)
def forward(self, x):
"""Pass x through the downward layer."""
x = self.contract_block(x)
x = self.double_conv(x)
return x
class ExpandingBlock(torch.nn.Module):
"""Class to perform upward layer in the U-Net."""
def __init__(self, options, input_n, output_n):
super(ExpandingBlock, self).__init__()
self.padding_style = options['conv_padding_style']
self.upsample = torch.nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.double_conv = DoubleConv(options, input_n=input_n + output_n, output_n=output_n)
def forward(self, x, x_skip):
"""Pass x through the upward layer and concatenate with opposite layer."""
x = self.upsample(x)
# Insure that x and skip H and W dimensions match.
x = expand_padding(x, x_skip, padding_style=self.padding_style)
x = torch.cat([x, x_skip], dim=1)
return self.double_conv(x)
def expand_padding(x, x_contract, padding_style: str = 'constant'):
"""
Insure that x and x_skip H and W dimensions match.
Parameters
----------
x :
Image tensor of shape (batch size, channels, height, width). Expanding path.
x_contract :
Image tensor of shape (batch size, channels, height, width) Contracting path.
or torch.Size. Contracting path.
padding_style : str
Type of padding.
Returns
-------
x : ndtensor
Padded expanding path.
"""
# Check whether x_contract is tensor or shape.
if type(x_contract) == type(x):
x_contract = x_contract.size()
# Calculate necessary padding to retain patch size.
pad_y = x_contract[2] - x.size()[2]
pad_x = x_contract[3] - x.size()[3]
if padding_style == 'zeros':
padding_style = 'constant'
x = torch.nn.functional.pad(x, [pad_x // 2, pad_x - pad_x // 2, pad_y // 2, pad_y - pad_y // 2], mode=padding_style)
return x
class UNet_sep_dec(torch.nn.Module):
"""PyTorch U-Net Class. Uses unet_parts."""
def __init__(self, options):
super().__init__()
self.input_block = DoubleConv(options, input_n=len(
options['train_variables']), output_n=options['unet_conv_filters'][0])
self.contract_blocks = torch.nn.ModuleList()
for contract_n in range(1, len(options['unet_conv_filters'])):
self.contract_blocks.append(
ContractingBlock(options=options,
input_n=options['unet_conv_filters'][contract_n - 1],
output_n=options['unet_conv_filters'][contract_n])) # only used to contract input patch.
self.bridge = ContractingBlock(
options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1])
self.expand_blocks = torch.nn.ModuleList()
self.expand_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
self.sic_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SIC'])
self.sod_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SOD'])
self.floe_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['FLOE'])
def Decoder(self, x_contract):
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return x_expand
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
return {'SIC': self.sic_feature_map(self.Decoder(x_contract)),
'SOD': self.sod_feature_map(self.Decoder(x_contract)),
'FLOE': self.floe_feature_map(self.Decoder(x_contract))}
class Sep_feat_dif_stages(torch.nn.Module):
"""PyTorch U-Net Class. Uses unet_parts."""
def __init__(self, options):
super().__init__()
self.stage = options['common_features_last_layer']
self.drop = nn.Identity()
if 'resnet' in options['backbone']:
a = Resnet_backbone(options, len(options['train_variables']), drop_rate=0.1, output_stride=16)
self.comm_feat = a.comm_feat
self.ind_feat1 = a.ind_feat1
self.ind_feat2 = a.ind_feat2
self.ind_feat3 = a.ind_feat3
self.drop = a.drop
count_stage = a.count_stage
else: # unet
self.comm_feat, self.ind_feat1, self.ind_feat2, self.ind_feat3 = [torch.nn.ModuleList() for i in range(4)]
# input_block
count_stage = 1
if count_stage <= self.stage:
self.comm_feat.append(DoubleConv(options, input_n=len(
options['train_variables']), output_n=options['unet_conv_filters'][0]))
else:
a, b, c = [DoubleConv(options, input_n=len(options['train_variables']),
output_n=options['unet_conv_filters'][0]) for i in range(3)]
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# contract_blocks, only used to contract input patch
for contract_n in range(1, len(options['unet_conv_filters'])):
count_stage += 1
if count_stage <= self.stage:
self.comm_feat.append(ContractingBlock(
options=options, input_n=options['unet_conv_filters'][contract_n - 1], output_n=options['unet_conv_filters'][contract_n]))
else:
a, b, c = [ContractingBlock(options=options, input_n=options['unet_conv_filters']
[contract_n - 1], output_n=options['unet_conv_filters'][contract_n]) for i in range(3)]
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# bridge
count_stage += 1
if count_stage <= self.stage:
self.comm_feat.append(ContractingBlock(
options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1]))
else:
a, b, c = [ContractingBlock(options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]) for i in range(3)]
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# expand_blocks
count_stage += 1
if count_stage <= self.stage:
self.comm_feat.append(ExpandingBlock(
options=options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1]))
else:
a, b, c = [ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]) for i in range(3)]
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
count_stage += 1
if count_stage <= self.stage:
self.comm_feat.append(ExpandingBlock(
options=options, input_n=options['unet_conv_filters'][expand_n - 1], output_n=options['unet_conv_filters'][expand_n - 2]))
else:
a, b, c = [ExpandingBlock(options=options, input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]) for i in range(3)]
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
self.sic_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SIC'])
self.sod_feature_map = FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SOD'])
self.floe_feature_map = FeatureMap(
input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['FLOE'])
def Independent(self, ind_feat, features, up_idx):
for block in ind_feat:
if not isinstance(block, ExpandingBlock):
features.append(self.drop(block(features[-1])))
up_idx += 1
else:
features.append(block(features[-1], features[up_idx - 1]))
up_idx -= 1
return features[-1]
def forward(self, x):
"""Forward model pass."""
features = [x]
up_idx = 0
for block in self.comm_feat:
if not isinstance(block, ExpandingBlock):
features.append(self.drop(block(features[-1])))
up_idx += 1
else:
features.append(block(features[-1], features[up_idx - 1]))
up_idx -= 1
return {'SIC': self.sic_feature_map(self.Independent(self.ind_feat1, features[:], up_idx)),
'SOD': self.sod_feature_map(self.Independent(self.ind_feat2, features[:], up_idx)),
'FLOE': self.floe_feature_map(self.Independent(self.ind_feat3, features[:], up_idx))}
class Resnet_backbone(torch.nn.Module):
def __init__(self, options, in_chans, drop_rate, output_stride):
super(Resnet_backbone, self).__init__()
cnn_func = getattr(models, options['backbone'])
self.enc = cnn_func()
if options['backbone'] == 'resnet18':
layers = [2, 2, 2, 2] # number of layers in residual blocks
elif options['backbone'] == 'resnet34':
layers = [3, 4, 6, 3]
elif options['backbone'] == 'resnet50':
layers = [3, 4, 6, 3]
elif options['backbone'] == 'resnet101':
layers = [3, 4, 23, 3]
# Custom strides
self.strides = [1]
for i in range(4):
if output_stride > 1:
self.strides.append(2)
output_stride //= 2
else:
self.strides.append(1)
self.stage = options['common_features_last_layer']
self.comm_feat, self.ind_feat1, self.ind_feat2, self.ind_feat3 = [torch.nn.ModuleList() for i in range(4)]
self.count_stage = 1
self.enc.inplanes = options['unet_conv_filters'][0]
# self.enc.conv1 = nn.Conv2d(in_chans, self.enc.inplanes, kernel_size=7, stride=self.strides[0], padding=3, bias=False)
# self.enc.bn1 = nn.BatchNorm2d(options['unet_conv_filters'][0])
# if self.strides[1] > 1:
# self.enc.maxpool = nn.MaxPool2d(kernel_size=3, stride=self.strides[1], padding=1)
# else: self.enc.maxpool = nn.Identity()
if self.count_stage <= self.stage:
self.comm_feat.append(nn.Sequential(nn.Conv2d(in_chans, self.enc.inplanes, kernel_size=7, stride=self.strides[0], padding=3, bias=False),
nn.BatchNorm2d(options['unet_conv_filters'][0]),
nn.ReLU(inplace=True)))
else:
a, b, c = [nn.Sequential(nn.Conv2d(in_chans, self.enc.inplanes, kernel_size=7, stride=self.strides[0], padding=3, bias=False),
nn.BatchNorm2d(options['unet_conv_filters'][0]),
nn.ReLU(inplace=True)) for i in range(3)]
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# self.enc.layer1 = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][ 1], layers[0])
self.count_stage += 1
if self.count_stage <= self.stage:
self.comm_feat.append(nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=self.strides[1], padding=1) if self.strides[1] > 1 else nn.Identity(),
self.enc._make_layer(BasicBlock, options['unet_conv_filters'][1], layers[0])))
else:
aux = self.enc.inplanes
a = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=self.strides[1], padding=1) if self.strides[1] > 1 else nn.Identity(),
self.enc._make_layer(BasicBlock, options['unet_conv_filters'][1], layers[0]))
self.enc.inplanes = aux
b = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=self.strides[1], padding=1) if self.strides[1] > 1 else nn.Identity(),
self.enc._make_layer(BasicBlock, options['unet_conv_filters'][1], layers[0]))
self.enc.inplanes = aux
c = nn.Sequential(nn.MaxPool2d(kernel_size=3, stride=self.strides[1], padding=1) if self.strides[1] > 1 else nn.Identity(),
self.enc._make_layer(BasicBlock, options['unet_conv_filters'][1], layers[0]))
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# self.enc.layer2 = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][ 2], layers[1], stride=self.strides[2])
self.count_stage += 1
if self.count_stage <= self.stage:
self.comm_feat.append(self.enc._make_layer(
BasicBlock, options['unet_conv_filters'][2], layers[1], stride=self.strides[2]))
else:
aux = self.enc.inplanes
a = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][2], layers[1], stride=self.strides[2])
self.enc.inplanes = aux
b = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][2], layers[1], stride=self.strides[2])
self.enc.inplanes = aux
c = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][2], layers[1], stride=self.strides[2])
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# self.enc.layer3 = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][ 3] , layers[2], stride=self.strides[3])
self.count_stage += 1
if self.count_stage <= self.stage:
self.comm_feat.append(self.enc._make_layer(
BasicBlock, options['unet_conv_filters'][3], layers[2], stride=self.strides[3]))
else:
aux = self.enc.inplanes
a = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][3], layers[2], stride=self.strides[3])
self.enc.inplanes = aux
b = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][3], layers[2], stride=self.strides[3])
self.enc.inplanes = aux
c = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][3], layers[2], stride=self.strides[3])
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
# self.enc.layer4 = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][-1], layers[3], stride=self.strides[4])
self.count_stage += 1
if self.count_stage <= self.stage:
self.comm_feat.append(self.enc._make_layer(
BasicBlock, options['unet_conv_filters'][-1], layers[3], stride=self.strides[4]))
else:
aux = self.enc.inplanes
a = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][-1], layers[3], stride=self.strides[4])
self.enc.inplanes = aux
b = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][-1], layers[3], stride=self.strides[4])
self.enc.inplanes = aux
c = self.enc._make_layer(BasicBlock, options['unet_conv_filters'][-1], layers[3], stride=self.strides[4])
self.ind_feat1.append(a)
self.ind_feat2.append(b)
self.ind_feat3.append(c)
self.drop = nn.Dropout2d(drop_rate)
class UNet_regression(UNet):
def __init__(self, options):
super().__init__(options)
self.regression_layer = torch.nn.Linear(options['unet_conv_filters'][0], 1)
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return {'SIC': self.regression_layer(x_expand.permute(0, 2, 3, 1)),
'SOD': self.sod_feature_map(x_expand),
'FLOE': self.floe_feature_map(x_expand)}
class UNet_regression_SIR(UNet_regression):
def __init__(self, options):
super().__init__(options)
self.sir_name = options['charts'][0]
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return {self.sir_name: self.sod_feature_map(x_expand)}
class UNet_regression_all(UNet):
def __init__(self, options):
super().__init__(options)
self.regression_layer = torch.nn.Linear(options['unet_conv_filters'][0], 1)
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return {'SIC': self.regression_layer(x_expand.permute(0, 2, 3, 1)),
'SOD': self.regression_layer(x_expand.permute(0, 2, 3, 1)),
'FLOE': self.regression_layer(x_expand.permute(0, 2, 3, 1))}
class UNet_sep_dec_regression(UNet):
def __init__(self, options):
super().__init__(options)
# SIC decoder
self.expand_sic_blocks = torch.nn.ModuleList()
self.expand_sic_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_sic_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
# SOD decoder
self.expand_sod_blocks = torch.nn.ModuleList()
self.expand_sod_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_sod_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
# FLOE decoder
self.expand_floe_blocks = torch.nn.ModuleList()
self.expand_floe_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_floe_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
# regression layer
self.regression_layer = torch.nn.Linear(options['unet_conv_filters'][0], 1)
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
x_expand_sic = x_expand
x_expand_sod = x_expand
x_expand_floe = x_expand
# decoder SIC
for expand_block in self.expand_sic_blocks:
x_expand_sic = expand_block(x_expand_sic, x_contract[up_idx - 1])
up_idx -= 1
up_idx = len(x_contract)
# decoder SOD
for expand_block in self.expand_sod_blocks:
x_expand_sod = expand_block(x_expand_sod, x_contract[up_idx - 1])
up_idx -= 1
up_idx = len(x_contract)
# decoder FLOE
for expand_block in self.expand_floe_blocks:
x_expand_floe = expand_block(x_expand_floe, x_contract[up_idx - 1])
up_idx -= 1
return {'SIC': self.regression_layer(x_expand_sic.permute(0, 2, 3, 1)),
'SOD': self.sod_feature_map(x_expand_sod),
'FLOE': self.floe_feature_map(x_expand_floe)}
class UNet_sep_dec_mse(torch.nn.Module):
"""PyTorch U-Net Class. Uses unet_parts."""
def __init__(self, options):
super().__init__()
self.input_block = DoubleConv(options, input_n=len(options['train_variables']),
output_n=options['unet_conv_filters'][0])
self.contract_blocks = torch.nn.ModuleList()
for contract_n in range(1, len(options['unet_conv_filters'])):
self.contract_blocks.append(
ContractingBlock(options=options,
input_n=options['unet_conv_filters'][contract_n - 1],
output_n=options['unet_conv_filters'][contract_n]))
# only used to contract input patch.
self.bridge = ContractingBlock(
options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1])
self.expand_sic_blocks = torch.nn.ModuleList()
self.expand_sic_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_sic_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
self.expand_sod_blocks = torch.nn.ModuleList()
self.expand_sod_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_sod_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
self.expand_floe_blocks = torch.nn.ModuleList()
self.expand_floe_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_floe_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
self.sod_feature_map = FeatureMap(
input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['SOD'])
self.floe_feature_map = FeatureMap(
input_n=options['unet_conv_filters'][0], output_n=options['n_classes']['FLOE'])
self.regression_layer = torch.nn.Linear(options['unet_conv_filters'][0], 1)
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
x_expand_sic = x_expand
x_expand_sod = x_expand
x_expand_floe = x_expand
for expand_block in self.expand_sic_blocks:
x_expand_sic = expand_block(x_expand_sic, x_contract[up_idx - 1])
up_idx -= 1
up_idx = len(x_contract)
for expand_block in self.expand_sod_blocks:
x_expand_sod = expand_block(x_expand_sod, x_contract[up_idx - 1])
up_idx -= 1
up_idx = len(x_contract)
for expand_block in self.expand_floe_blocks:
x_expand_floe = expand_block(x_expand_floe, x_contract[up_idx - 1])
up_idx -= 1
return {'SIC': self.regression_layer(x_expand_sic.permute(0,2,3,1)),
'SOD': self.sod_feature_map(x_expand_sod),
'FLOE': self.floe_feature_map(x_expand_floe)}
class UNet_SIR_RFS_MSS(torch.nn.Module):
"""PyTorch U-Net Class. Uses unet_parts."""
def __init__(self, options):
super().__init__()
self.charts = options['charts']
self.input_block = DoubleConv(options, input_n=len(options['train_variables']),
output_n=options['unet_conv_filters'][0])
self.contract_blocks = torch.nn.ModuleList()
for contract_n in range(1, len(options['unet_conv_filters'])):
self.contract_blocks.append(
ContractingBlock(options=options,
input_n=options['unet_conv_filters'][contract_n - 1],
output_n=options['unet_conv_filters'][contract_n]))
if 'dropout_rate' in options:
self.dropout = nn.Dropout2d(p=options['dropout_rate'])
# only used to contract input patch.
self.bridge = ContractingBlock(
options, input_n=options['unet_conv_filters'][-1], output_n=options['unet_conv_filters'][-1])
self.expand_blocks = torch.nn.ModuleList()
self.expand_blocks.append(
ExpandingBlock(options=options, input_n=options['unet_conv_filters'][-1],
output_n=options['unet_conv_filters'][-1]))
for expand_n in range(len(options['unet_conv_filters']), 1, -1):
self.expand_blocks.append(ExpandingBlock(options=options,
input_n=options['unet_conv_filters'][expand_n - 1],
output_n=options['unet_conv_filters'][expand_n - 2]))
for chart in self.charts:
self.add_module(f'output_{chart}', FeatureMap(input_n=options['unet_conv_filters'][0], output_n=options['n_classes'][chart]))
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return {
chart: self.get_submodule(f'output_{chart}')(x_expand)
for chart in self.charts
}
class UNet_regression_SIR_IS2(UNet):
def __init__(self, options):
super().__init__(options)
self.charts = options['charts']
for chart in self.charts:
self.add_module(f'output_{chart}', torch.nn.Linear(options['unet_conv_filters'][0], 1))
def forward(self, x):
"""Forward model pass."""
x_contract = [self.input_block(x)]
for contract_block in self.contract_blocks:
x_contract.append(contract_block(x_contract[-1]))
x_expand = self.bridge(x_contract[-1])
up_idx = len(x_contract)
for expand_block in self.expand_blocks:
x_expand = expand_block(x_expand, x_contract[up_idx - 1])
up_idx -= 1
return {
chart: self.get_submodule(f'output_{chart}')(x_expand.permute(0, 2, 3, 1))
for chart in self.charts
}