-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseg_modules.py
691 lines (604 loc) · 33.8 KB
/
seg_modules.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
# This is the file where the basic components of the U-Net architecture are defined to make it very modular:
# Since the same modules are used multiple times in the same architecture and sometimes in other architectures as well
# it becomes highly convenient for us to define them as classes and instantiate them while using them later
# These modules are used by the new_models.py to further build upon these elementary modules to make a full network
import torch
import torch.nn as nn
import torch.nn.functional as F
class in_conv(nn.Module):
def __init__(self, input_channels, output_channels, kernel_size = 3,
dropout_p=0.3, leakiness=1e-2, conv_bias=True,
inst_norm_affine=True, res = False, lrelu_inplace=True):
"""[The initial convolution to enter the network, kind of like encode]
[This function will create the input convolution]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of modalities]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
dropout_p {number} -- [dropout probablity] (default: {0.3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
nn.Module.__init__(self)
self.residual = res
self.dropout_p = dropout_p
self.conv_bias = conv_bias
self.leakiness = leakiness
self.inst_norm_affine = inst_norm_affine
self.lrelu_inplace = lrelu_inplace
self.dropout = nn.Dropout3d(dropout_p)
self.in_0 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_1 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.conv0 = nn.Conv3d(input_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv1 = nn.Conv3d(output_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv2 = nn.Conv3d(output_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
def forward(self, x):
"""The forward function for initial convolution
[input --> conv0 --> | --> in --> lrelu --> conv1 --> dropout --> in -|
| |
output <-- + <-------------------------- conv2 <-- lrelu <--|]
Arguments:
x {[Tensor]} -- [Takes in a type of torch Tensor]
Returns:
[Tensor] -- [Returns a torch Tensor]
"""
x = self.conv0(x)
if self.residual == True:
skip = x
x = F.leaky_relu(self.in_0(x), negative_slope=self.leakiness,
inplace=self.lrelu_inplace)
x = self.conv1(x)
if self.dropout_p is not None and self.dropout_p > 0:
x = self.dropout(x)
x = F.leaky_relu(self.in_1(x), negative_slope=self.leakiness,
inplace=self.lrelu_inplace)
x = self.conv2(x)
if self.residual == True:
x = x + skip
#print(x.shape)
return x
class DownsamplingModule(nn.Module):
def __init__(self, input_channels, output_channels, leakiness=1e-2,
dropout_p=0.3, kernel_size=3, conv_bias=True,
inst_norm_affine=True, lrelu_inplace=True):
"""[To Downsample a given input with convolution operation]
[This one will be used to downsample a given comvolution while doubling
the number filters]
Arguments:
input_channels {[int]} -- [The input number of channels are taken
and then are downsampled to double usually]
output_channels {[int]} -- [the output number of channels are
usually the double of what of input]
Keyword Arguments:
leakiness {float} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
#nn.Module.__init__(self)
super(DownsamplingModule, self).__init__()
self.dropout_p=dropout_p
self.conv_bias = conv_bias
self.leakiness = leakiness
self.inst_norm_affine = inst_norm_affine
self.lrelu_inplace = True
self.in_0 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.conv0 = nn.Conv3d(input_channels, output_channels, kernel_size = 3,
stride=2, padding=(kernel_size - 1) // 2,
bias = self.conv_bias)
def forward(self, x):
"""[This is a forward function for ]
[input -- > in --> lrelu --> ConvDS --> output]
Arguments:
x {[Tensor]} -- [Takes in a type of torch Tensor]
Returns:
[Tensor] -- [Returns a torch Tensor]
"""
x = F.leaky_relu(self.in_0(self.conv0(x)),
negative_slope=self.leakiness,
inplace=self.lrelu_inplace)
#print(x.shape)
return x
class EncodingModule(nn.Module):
def __init__(self, input_channels, output_channels, kernel_size = 3,
dropout_p=0.3, leakiness=1e-2, conv_bias=True,
inst_norm_affine=True, res = False, lrelu_inplace=True):
"""[The Encoding convolution module to learn the information and use later]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
dropout_p {number} -- [dropout probablity] (default: {0.3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
nn.Module.__init__(self)
self.res = res
self.dropout_p = dropout_p
self.conv_bias = conv_bias
self.leakiness = leakiness
self.inst_norm_affine = inst_norm_affine
self.lrelu_inplace = lrelu_inplace
self.dropout = nn.Dropout3d(dropout_p)
self.in_0 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_1 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.conv0 = nn.Conv3d(output_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv1 = nn.Conv3d(output_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
def forward(self, x):
"""The forward function for initial convolution
[input --> | --> in --> lrelu --> conv0 --> dropout --> in -|
| |
output <-- + <-------------------------- conv1 <-- lrelu <--|]
Arguments:
x {[Tensor]} -- [Takes in a type of torch Tensor]
Returns:
[Tensor] -- [Returns a torch Tensor]
"""
if self.res == True:
skip = x
x = F.leaky_relu(self.in_0(x), negative_slope=self.leakiness,
inplace=self.lrelu_inplace)
x = self.conv0(x)
if self.dropout_p is not None and self.dropout_p > 0:
x = self.dropout(x)
x = F.leaky_relu(self.in_1(x), negative_slope=self.leakiness, inplace=self.lrelu_inplace)
x = self.conv1(x)
if self.res == True:
x = x + skip
#print(x.shape)
return x
class Interpolate(nn.Module):
def __init__(self, size=None, scale_factor=None, mode='nearest',
align_corners=True):
super(Interpolate, self).__init__()
self.align_corners = align_corners
self.mode = mode
self.scale_factor = scale_factor
self.size = size
def forward(self, x):
return nn.functional.interpolate(x, size=self.size, scale_factor=self.scale_factor,
mode=self.mode, align_corners=self.align_corners)
class UpsamplingModule(nn.Module):
def __init__(self, input_channels, output_channels, leakiness=1e-2,
lrelu_inplace=True, kernel_size=3, scale_factor=2,
conv_bias=True, inst_norm_affine=True):
"""[summary]
[description]
Arguments:
input__channels {[type]} -- [description]
output_channels {[type]} -- [description]
Keyword Arguments:
leakiness {number} -- [description] (default: {1e-2})
lrelu_inplace {bool} -- [description] (default: {True})
kernel_size {number} -- [description] (default: {3})
scale_factor {number} -- [description] (default: {2})
conv_bias {bool} -- [description] (default: {True})
inst_norm_affine {bool} -- [description] (default: {True})
"""
nn.Module.__init__(self)
self.lrelu_inplace = lrelu_inplace
self.inst_norm_affine = inst_norm_affine
self.conv_bias = conv_bias
self.leakiness = leakiness
self.scale_factor = scale_factor
self.interpolate = Interpolate(scale_factor=self.scale_factor, mode='trilinear',
align_corners=True)
self.conv0 = nn.Conv3d(input_channels, output_channels, kernel_size=1,
stride=1, padding=0,
bias = self.conv_bias)
def forward(self, x):
"""[summary]
[description]
Extends:
"""
x = self.conv0(self.interpolate(x))
#print(x.shape)
return x
class FCNUpsamplingModule(nn.Module):
def __init__(self, input_channels, output_channels, leakiness=1e-2,
lrelu_inplace=True, kernel_size=3, scale_factor=2,
conv_bias=True, inst_norm_affine=True):
"""[summary]
[description]
Arguments:
input__channels {[type]} -- [description]
output_channels {[type]} -- [description]
Keyword Arguments:
leakiness {number} -- [description] (default: {1e-2})
lrelu_inplace {bool} -- [description] (default: {True})
kernel_size {number} -- [description] (default: {3})
scale_factor {number} -- [description] (default: {2})
conv_bias {bool} -- [description] (default: {True})
inst_norm_affine {bool} -- [description] (default: {True})
"""
nn.Module.__init__(self)
self.lrelu_inplace = lrelu_inplace
self.inst_norm_affine = inst_norm_affine
self.conv_bias = conv_bias
self.leakiness = leakiness
self.scale_factor = scale_factor
self.interpolate = Interpolate(scale_factor=2**(self.scale_factor-1), mode='trilinear',
align_corners=True)
self.conv0 = nn.Conv3d(input_channels, output_channels, kernel_size=1,
stride=1, padding=0,
bias = self.conv_bias)
def forward(self, x):
"""[summary]
[description]
Extends:
"""
#print("Pre interpolate and conv:", x.shape)
x = self.interpolate(self.conv0(x))
#print("Post interpolate and conv:", x.shape)
return x
class DecodingModule(nn.Module):
def __init__(self, input_channels, output_channels, leakiness=1e-2, conv_bias=True, kernel_size=3,
inst_norm_affine=True, res=True, lrelu_inplace=True):
"""[The Decoding convolution module to learn the information and use later]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
nn.Module.__init__(self)
self.lrelu_inplace = lrelu_inplace
self.inst_norm_affine = inst_norm_affine
self.conv_bias = conv_bias
self.leakiness = leakiness
self.res = res
self.in_0 = nn.InstanceNorm3d(input_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_1 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_2 = nn.InstanceNorm3d(output_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.conv0 = nn.Conv3d(input_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv1 = nn.Conv3d(output_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv2 = nn.Conv3d(output_channels, output_channels, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
def forward(self, x1, x2):
x = torch.cat([x1, x2], dim=1)
#print(x.shape)
x = F.leaky_relu(self.in_0(x))
x = self.conv0(x)
if self.res == True:
skip = x
x = F.leaky_relu(self.in_1(x))
x = F.leaky_relu(self.in_2(self.conv1(x)))
x = self.conv2(x)
if self.res == True:
x = x + skip
return x
class out_conv(nn.Module):
def __init__(self, input_channels, output_channels, leakiness=1e-2, kernel_size=3,
conv_bias=True, inst_norm_affine=True, res=True, lrelu_inplace=True):
"""[The Out convolution module to learn the information and use later]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
nn.Module.__init__(self)
self.lrelu_inplace = lrelu_inplace
self.inst_norm_affine = inst_norm_affine
self.conv_bias = conv_bias
self.leakiness = leakiness
self.res = res
self.in_0 = nn.InstanceNorm3d(input_channels,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_1 = nn.InstanceNorm3d(input_channels//2,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_2 = nn.InstanceNorm3d(input_channels//2,
affine=self.inst_norm_affine,
track_running_stats=True)
self.in_3 = nn.InstanceNorm3d(input_channels//2,
affine=self.inst_norm_affine,
track_running_stats=True)
self.conv0 = nn.Conv3d(input_channels, input_channels//2, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv1 = nn.Conv3d(input_channels//2, input_channels//2, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv2 = nn.Conv3d(input_channels//2, input_channels//2, kernel_size=3,
stride=1, padding=(kernel_size - 1) // 2,
bias=self.conv_bias)
self.conv3 = nn.Conv3d(input_channels//2, output_channels, kernel_size=1,
stride=1, padding=0,
bias=self.conv_bias)
def forward(self, x1, x2):
x = torch.cat([x1, x2], dim=1)
#print(x.shape)
x = F.leaky_relu(self.in_0(x))
x = self.conv0(x)
if self.res == True:
skip = x
x = F.leaky_relu(self.in_1(x))
x = F.leaky_relu(self.in_2(self.conv1(x)))
x = self.conv2(x)
if self.res == True:
x = x + skip
x = F.leaky_relu(self.in_3(x))
x = F.softmax(self.conv3(x),dim=1)
return x
#the operations in the initialization functions just define these functions and then they are used in the forward method
#Here the number of input and output channels are the same as this is the encoding inception module - in the decoding inception module they will differ - input # of features will be twice the output # of features
class InceptionModule(nn.Module):
def __init__(self,input_channels,output_channels,dropout_p=0.3,leakiness=1e-2,conv_bias=True,inst_norm_affine=True,res=False,lrelu_inplace=True):
nn.Module.__init__(self)
"""[The Inception Module learns multi-scale features by parallel conv pathways ]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
self.res = res
self.output_channels = output_channels
self.dropout_p = dropout_p
self.conv_bias = conv_bias
self.leakiness = leakiness
self.inst_norm_affine = inst_norm_affine
self.lrelu_inplace = lrelu_inplace
self.dropout = nn.Dropout3d(dropout_p)
self.inst_norm = nn.InstanceNorm3d(int(output_channels/4),affine = self.inst_norm_affine, track_running_stats = True)
self.inst_norm_final = nn.InstanceNorm3d(output_channels,affine = self.inst_norm_affine, track_running_stats = True)
self.conv_1x1 = nn.Conv3d(output_channels,int(output_channels/4),kernel_size = 1,stride=1,padding=0,bias = self.conv_bias)
self.conv_3x3 = nn.Conv3d(int(output_channels/4),int(output_channels/4),kernel_size=3,stride=1,padding=1,bias=self.conv_bias)
self.conv_1x1_final = nn.Conv3d(output_channels,output_channels,kernel_size = 1, stride = 1, padding=0,bias = self.conv_bias)
def forward(self,x):
output_channels = self.output_channels
if self.res == True:
skip = x
x1 = F.leaky_relu(self.inst_norm(self.conv_1x1(x)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x2 = F.leaky_relu(self.inst_norm(self.conv_1x1(x)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x2 = F.leaky_relu(self.inst_norm(self.conv_3x3(x2)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x3 = F.leaky_relu(self.inst_norm(self.conv_1x1(x)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x3 = F.leaky_relu(self.inst_norm(self.conv_3x3(x3)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x3 = F.leaky_relu(self.inst_norm(self.conv_3x3(x3)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x4 = F.leaky_relu(self.inst_norm(self.conv_1x1(x)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x4 = F.leaky_relu(self.inst_norm(self.conv_3x3(x4)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x4 = F.leaky_relu(self.inst_norm(self.conv_3x3(x4)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x4 = F.leaky_relu(self.inst_norm(self.conv_3x3(x4)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x = torch.cat((x1,x2,x3,x4),dim=1)
x = self.inst_norm_final(self.conv_1x1_final(x))
x = x + skip
x = F.leaky_relu(x,negative_slope = self.leakiness,inplace = self.lrelu_inplace)
return x
class ResNetModule(nn.Module):
def __init__(self,input_channels,output_channels,dropout_p=0.3,leakiness=1e-2,conv_bias=True,inst_norm_affine=True,res=False,lrelu_inplace=True):
nn.Module.__init__(self)
"""[The Resnet module is used at the beginning and end of the network - again for multi-scale feature extraction]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
self.output_channels = output_channels
self.dropout_p = dropout_p
self.leakiness = leakiness
self.conv_bias = conv_bias
self.inst_norm_affine = inst_norm_affine
self.res = res
self.lrelu_inplace = lrelu_inplace
self.dropout = nn.Dropout3d(dropout_p)
self.inst_norm = nn.InstanceNorm3d(output_channels,affine = self.inst_norm_affine, track_running_stats = True)
self.conv = nn.Conv3d(output_channels,output_channels,kernel_size=3,stride=1,padding=1,bias = self.conv_bias)
def forward(self,x):
if self.res == True:
skip = x
x = F.leaky_relu(self.inst_norm(self.conv(x)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
x = self.inst_norm(self.conv(x))
x = x + skip
x = F.leaky_relu(x,negative_slope = self.leakiness, inplace = self.lrelu_inplace)
return x
class IncDownsamplingModule(nn.Module):
def __init__(self, input_channels, output_channels, leakiness=1e-2, kernel_size=1, conv_bias=True,
inst_norm_affine=True, lrelu_inplace=True):
nn.Module.__init__(self)
"""[Downsampling using convolution with stride 2]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
self.output_channels = output_channels
self.input_channels = input_channels
self.leakiness = leakiness
self.conv_bias = conv_bias
self.lrelu_inplace = lrelu_inplace
self.inst_norm_affine = inst_norm_affine
self.inst_norm = nn.InstanceNorm3d(output_channels,affine = self.inst_norm_affine, track_running_stats = True)
self.down = nn.Conv3d(input_channels,output_channels,kernel_size = 1, stride = 2, padding = 0, bias = self.conv_bias)
def forward(self,x):
x = F.leaky_relu(self.inst_norm(self.down(x)),negative_slope = self.leakiness, inplace = self.lrelu_inplace)
return x
class IncConv(nn.Module):
def __init__(self,input_channels,output_channels,dropout_p=0.3,leakiness=1e-2,conv_bias=True,inst_norm_affine=True,res=False,lrelu_inplace=True):
nn.Module.__init__(self)
"""[1 * 1 * 1 convolutions]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
self.output_channels = output_channels
self.leakiness = leakiness
self.conv_bias = conv_bias
self.lrelu_inplace = lrelu_inplace
self.inst_norm_affine = inst_norm_affine
self.inst_norm = nn.InstanceNorm3d(output_channels,affine=self.inst_norm_affine,track_running_stats = True)
self.conv = nn.Conv3d(input_channels,output_channels,kernel_size=1,stride=1,padding=0,bias=self.conv_bias)
def forward(self,x):
x = F.leaky_relu(self.inst_norm(self.conv(x)),negative_slope = self.leakiness,inplace = self.lrelu_inplace)
return x
class IncDropout(nn.Module):
def __init__(self,input_channels,output_channels,dropout_p=0.3,leakiness=1e-2,conv_bias=True,inst_norm_affine=True, res = False, lrelu_inplace = True):
nn.Module.__init__(self)
"""[Dropout regualarizer used at the end of the network]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
self.input_channels = input_channels
self.output_channels = output_channels
self.dropout_p = dropout_p
self.leakiness = leakiness
self.conv_bias = conv_bias
self.inst_norm_affine = inst_norm_affine
self.res = res
self.lrelu_inplace = lrelu_inplace
self.dropout = nn.Dropout3d(dropout_p)
self.conv = nn.Conv3d(input_channels,output_channels,kernel_size=1,stride=1,padding=0,bias=self.conv_bias)
def forward(self,x):
x = self.dropout(x)
x = self.conv(x)
return x
class IncUpsamplingModule(nn.Module):
def __init__(self,input_channels,output_channels,dropout_p=0.3,leakiness=1e-2,conv_bias=True,inst_norm_affine=True, res = False, lrelu_inplace = True):
nn.Module.__init__(self)
"""[Upsampling using transpose convolution]
[This function will create the Learning convolutions]
Arguments:
input_channels {[int]} -- [the input number of channels, in our case
the number of channels from downsample]
output_channels {[int]} -- [the output number of channels, will det-
-ermine the upcoming channels]
Keyword Arguments:
kernel_size {number} -- [size of filter] (default: {3})
leakiness {number} -- [the negative leakiness] (default: {1e-2})
conv_bias {bool} -- [to use the bias in filters] (default: {True})
inst_norm_affine {bool} -- [affine use in norm] (default: {True})
res {bool} -- [to use residual connections] (default: {False})
lrelu_inplace {bool} -- [To update conv outputs with lrelu outputs]
(default: {True})
"""
self.input_channels = input_channels
self.output_channels = output_channels
self.dropout_p = dropout_p
self.leakiness = leakiness
self.conv_bias = conv_bias
self.inst_norm_affine = inst_norm_affine
self.res = res
self.lrelu_inplace = lrelu_inplace
self.inst_norm = nn.InstanceNorm3d(output_channels,affine = self.inst_norm_affine, track_running_stats = True)
self.up = nn.ConvTranspose3d(input_channels,output_channels,kernel_size=1,stride=2,padding=0,output_padding =1,bias = self.conv_bias)
def forward(self,x):
x = F.leaky_relu(self.inst_norm(self.up(x)),negative_slope=self.leakiness,inplace=self.lrelu_inplace)
return x