forked from NaveenPaluru/Segmentation-COVID-19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anamnet.py
executable file
·282 lines (242 loc) · 8.92 KB
/
anamnet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 1 10:04:03 2020
@author: naveen_p
"""
import torch
import torch.nn as nn
class Bottleneck(nn.Module):
def __init__(self,
channels,
internal_ratio=4,
kernel_size=3,
padding =1,
dilation=1,
asymmetric=False,
dropout_prob=0,
bias=False,
relu=True):
super().__init__()
# Check in the internal_scale parameter is within the expected range
# [1, channels]
if internal_ratio <= 1 or internal_ratio > channels:
raise RuntimeError("Value out of range. Expected value in the "
"interval [1, {0}], got internal_scale={1}."
.format(channels, internal_ratio))
internal_channels = channels // internal_ratio
if relu:
activation = nn.ReLU
else:
activation = nn.PReLU
# Main branch - shortcut connection
# 1x1 projection convolution
self.ext_conv1 = nn.Sequential(
nn.Conv2d(
channels,
internal_channels,
kernel_size=1,
stride=1,
bias=bias), nn.BatchNorm2d(internal_channels), activation())
self.ext_conv2 = nn.Sequential(
nn.Conv2d(
internal_channels,
internal_channels,
kernel_size=kernel_size,
stride=1,
padding=padding,
dilation=dilation,
bias=bias), nn.BatchNorm2d(internal_channels), activation())
# 1x1 expansion convolution
self.ext_conv3 = nn.Sequential(
nn.Conv2d(
internal_channels,
channels,
kernel_size=1,
stride=1,
bias=bias), nn.BatchNorm2d(channels), activation())
self.ext_regul = nn.Dropout2d(p=dropout_prob)
# PReLU layer to apply after adding the branches
self.out_activation = activation()
def forward(self, x):
# Main branch shortcut
main = x
# Extension branch
ext = self.ext_conv1(x)
ext = self.ext_conv2(ext)
ext = self.ext_conv3(ext)
ext = self.ext_regul(ext)
# Add main and extension branches
out = main + ext
return self.out_activation(out)
class AnamNet(nn.Module):
def __init__(self):
super(AnamNet, self).__init__()
# Conv block 1 - Down 1
self.conv1_block = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=64,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
self.max1 = nn.MaxPool2d(kernel_size=2, stride=2)
# BottleNeck 1
self.bottleneck1 = Bottleneck(64)
# Conv block 2 - Down 2
self.conv2_block = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
)
self.max2 = nn.MaxPool2d(kernel_size=2, stride=2)
# BottleNeck 2
self.bottleneck2 = Bottleneck(128)
# Conv block 3 - Down 3
self.conv3_block = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
self.max3 = nn.MaxPool2d(kernel_size=2, stride=2)
# BottleNeck 3
self.bottleneck3 = Bottleneck(256)
# Conv block 4 - Down 4
self.conv4_block = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=256,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
self.max4 = nn.MaxPool2d(kernel_size=2, stride=2)
# Up 1
self.up_1 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=2, stride=2)
self.bottleneck4 = Bottleneck(256)
# Up Conv block 1
self.conv_up_1 = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=256,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
# Up 2
self.up_2 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=2, stride=2)
self.bottleneck5 =Bottleneck(256)
# Up Conv block 2
self.conv_up_2 = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=256,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
)
# Up 3
self.up_3 = nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size=2, stride=2)
self.bottleneck6 = Bottleneck(128)
# Up Conv block 3
self.conv_up_3 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=128,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
)
# Up 4
self.up_4 = nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=2, stride=2)
# Up Conv block 4
self.conv_up_4 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=64,
kernel_size=3, padding=1, stride=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
# Final output
self.conv_final = nn.Conv2d(in_channels=64, out_channels=3,
kernel_size=1, padding=0, stride=1)
def forward(self, x):
#print('inputTensor', x.shape)
#---------------------------------------------------------------------
# Down 1
x = self.conv1_block(x)
#print('after conv1', x.shape)
conv1_out = x
conv1_dim = x.shape[2]
x = self.max1(x)
#print('after pool1', x.shape)
#--------------------------------------------------------------------
x = self.bottleneck1(x)
#print('after bnck1', x.shape)
# Down 2
x = self.conv2_block(x)
#print('after conv2', x.shape)
conv2_out = x
conv2_dim = x.shape[2]
x = self.max2(x)
#print('after pool2', x.shape)
#-------------------------------------------------------------------
x = self.bottleneck2(x)
#print('after bnck2', x.shape)
# Down 3
x = self.conv3_block(x)
#print('after conv3', x.shape)
conv3_out = x
conv3_dim = x.shape[2]
x = self.max3(x)
#print('after pool3', x.shape)
#------------------------------------------------------------------
x = self.bottleneck3(x)
#print('after bnck3', x.shape)
# Down 4
x = self.conv4_block(x)
#print('after conv4', x.shape)
conv4_out = x
conv4_dim = x.shape[2]
x = self.max4(x)
#print('after pool4', x.shape)
#----------------------------------------------------------------
# Up 1
x = self.up_1(x)
#print('after up_1', x.shape)
x = self.bottleneck4(x)
#print('after bnck4', x.shape)
x = torch.cat([x, conv4_out], dim=1)
#print('after cat_1',x.shape)
x = self.conv_up_1(x)
#print('after conv1', x.shape)
#-----------------------------------------------------------------
# Up 2
x = self.up_2(x)
#print('after up_2', x.shape)
x = self.bottleneck5(x)
#print('after bnck5', x.shape)
x = torch.cat([x, conv3_out], dim=1)
#print('after cat_2', x.shape)
x = self.conv_up_2(x)
#print('after conv2', x.shape)
#----------------------------------------------------------------
# Up 3
x = self.up_3(x)
#print('after up_3', x.shape)
x = self.bottleneck6(x)
#print('after bnck6', x.shape)
x = torch.cat([x, conv2_out], dim=1)
#print('after cat_3', x.shape)
x = self.conv_up_3(x)
#print('after conv3', x.shape)
#----------------------------------------------------------------
# Up 4
x = self.up_4(x)
#print('after up_3', x.shape)
x = torch.cat([x, conv1_out], dim=1)
#print('after cat_4', x.shape)
x = self.conv_up_4(x)
#print('after conv4', x.shape)
# Final output
x = self.conv_final(x)
#print('Finaloutshape',x.shape)
#-----------------------------------------------------------------
return x
if __name__ == "__main__":
x= torch.rand(1,1,512,512)
net=AnamNet()
yy=net(x)
print('Out Shape :', yy.shape)