-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZhouModel.py
174 lines (150 loc) · 7.6 KB
/
ZhouModel.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
import torch
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as vtransforms
from typing import Type, Any, Callable, Union, List, Optional
import blocks
####################
# Utility Functions
####################
def Identity(x):
return x
####################
# Model from the paper
# Zhou, Z., Zhou, L., & Shen, K. (2020). Dilated conditional GAN for bone suppression in chest radiographs with enforced semantic features. Medical Physics, 47(12), 6207–6215. https://doi.org/https://doi.org/10.1002/mp.14371
####################
class StandardConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, use_bias=True, normType="BatchNorm"):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = (4,4)
self.stride = (2,2)
self.use_bias = use_bias
self.normType = normType
# components
self.Conv = nn.Conv2d(self.in_channels, self.out_channels, kernel_size=self.kernel_size, stride=self.stride,
padding=1, dilation=1, groups=1, bias=self.use_bias, padding_mode='zeros')
if self.normType == "BatchNorm":
self.norm = nn.BatchNorm2d(self.out_channels, affine=False)
if self.normType == "InstanceNorm":
self.norm = nn.InstanceNorm(self.out_channels, affine=False)
self.lrelu = nn.LeakyReLU(0.2)
def forward(self, x):
out = self.Conv(x)
out = self.norm(out)
out = self.lrelu(out)
return out
class DilatedConvBlock(nn.Module):
def __init__(self, in_channels, out_channels, dilation, use_bias=True, normType="BatchNorm"):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = (4,4)
self.stride = (1,1)
self.dilation = dilation
self.use_bias = use_bias
self.normType = normType
# components
self.dilatedConv = nn.Conv2d(self.in_channels, self.out_channels, kernel_size=self.kernel_size, stride=self.stride,
padding=1, dilation=self.dilation, groups=1, bias=self.use_bias, padding_mode='zeros')
if self.normType == "BatchNorm":
self.norm = nn.BatchNorm2d(self.out_channels, affine=False)
if self.normType == "InstanceNorm":
self.norm = nn.InstanceNorm(self.out_channels, affine=False)
self.lrelu = nn.LeakyReLU(0.2)
def forward(self, x):
out = self.dilatedConv(x)
out = self.norm(out)
out = self.lrelu(out)
return out
class StandardDeconvBlock(nn.Module):
def __init__(self, in_channels, out_channels, use_bias=True, dropoutType="normal", normType = "BatchNorm", reluType="normal"):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size=(4,4)
self.stride=(2,2)
self.use_bias = use_bias
self.normType = normType
self.dropoutType = dropoutType
self.reluType=reluType
# Components
self.dilatedDeconv = nn.ConvTranspose2d(self.in_channels, self.out_channels, self.kernel_size, self.stride,
padding=1, output_padding=0, groups=1, bias=self.use_bias, dilation=1, padding_mode='zeros')
if self.normType == "BatchNorm":
self.norm = nn.BatchNorm2d(self.out_channels, affine=False)
if self.normType == "InstanceNorm":
self.norm = nn.InstanceNorm(self.out_channels, affine=False)
if self.dropoutType == "normal":
self.dropout = nn.Dropout(p=0.5)
if self.dropoutType == "ADL":
self.dropout = blocks.ADL(drop_rate=0.5, gamma=0.9)
if self.reluType=="leaky":
self.lrelu = nn.LeakyReLU(0.2)
if self.reluType=="normal":
self.lrelu = nn.ReLU()
def forward(self, x):
out = self.dilatedDeconv(x)
out = self.norm(out)
out = self.dropout(out)
out = self.lrelu(out)
return out
class DilatedDeconvBlock(nn.Module):
def __init__(self, in_channels, out_channels, dilation , use_bias=True, dropoutType="normal", normType = "BatchNorm", reluType="normal"):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.dilation = dilation
self.kernel_size=(4,4)
self.stride=(1,1)
self.use_bias = use_bias
self.normType = normType
self.dropoutType = dropoutType
self.reluType = reluType
# Components
self.dilatedDeconv = nn.ConvTranspose2d(self.in_channels, self.out_channels, self.kernel_size, self.stride,
padding=1, output_padding=0, groups=1, bias=self.use_bias, dilation=self.dilation, padding_mode='zeros')
if self.normType == "BatchNorm":
self.norm = nn.BatchNorm2d(self.out_channels, affine=False)
if self.normType == "InstanceNorm":
self.norm = nn.InstanceNorm(self.out_channels, affine=False)
if self.dropoutType == "normal":
self.dropout = nn.Dropout(p=0.5)
if self.dropoutType == "ADL":
self.dropout = blocks.ADL(drop_rate=0.5, gamma=0.9)
if self.reluType=="leaky":
self.lrelu = nn.LeakyReLU(0.2)
if self.reluType=="normal":
self.lrelu = nn.ReLU()
def forward(self, x):
out = self.dilatedDeconv(x)
out = self.norm(out)
out = self.dropout(out)
out = self.lrelu(out)
return out
class Generator_unfinished(nn.Module):
def __init__(self, input_array_shape, initial_channels_out=64, use_bias=True, normType="BatchNorm", dropoutType="normal", reluType="normal"):
super().__init__()
self.input_array_shape = input_array_shape
self.initial_channels_out=initial_channels_out
self.use_bias = use_bias
self.normType = normType
self.dropoutType = dropoutType
self.reluType = reluType
print(self.input_array_shape[1])
self.conv1 = StandardConvBlock( in_channels=self.input_array_shape[1], out_channels=self.initial_channels_out*(2**0), use_bias=self.use_bias, normType=self.normType)
self.dconv2 = DilatedConvBlock( in_channels=self.initial_channels_out*(2**0), out_channels=self.initial_channels_out*(2**1), dilation=2, use_bias=self.use_bias, normType=self.normType)
self.dconv3 = DilatedConvBlock( in_channels=self.initial_channels_out*(2**1), out_channels=self.initial_channels_out*(2**2), dilation=4, use_bias=self.use_bias, normType=self.normType)
self.dconv4 = DilatedConvBlock( in_channels=self.initial_channels_out*(2**2), out_channels=self.initial_channels_out*(2**3), dilation=8, use_bias=self.use_bias, normType=self.normType)
self.dconv5 = DilatedConvBlock( in_channels=self.initial_channels_out*(2**3), out_channels=self.initial_channels_out*(2**3), dilation=16, use_bias=self.use_bias, normType=self.normType)
self.dconv6 = DilatedConvBlock( in_channels=self.initial_channels_out*(2**3), out_channels=self.initial_channels_out*(2**3), dilation=32, use_bias=self.use_bias, normType=self.normType)
def forward(self, x):
out1 = self.conv1(x)
out2 = self.dconv2(out1)
out3 = self.dconv3(out2)
out4 = self.dconv4(out3)
out5 = self.dconv5(out4)
out6 = self.dconv6(out5)
return out6