-
Notifications
You must be signed in to change notification settings - Fork 2
/
unet_relu.py
222 lines (191 loc) · 7.64 KB
/
unet_relu.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
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.functional import sigmoid
class UNet(nn.Module):
def __init__(self, f = 32):
super(UNet, self).__init__()
# print('Old')
# print('\n')
self.f = f
# Conv block 1 - Down 1
self.conv1_block = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=self.f,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f, out_channels=self.f,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
self.max1 = nn.MaxPool2d(kernel_size=2, stride=2)
# Conv block 2 - Down 2
self.conv2_block = nn.Sequential(
nn.Conv2d(in_channels=f, out_channels=self.f*2,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*2, out_channels=self.f*2,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
self.max2 = nn.MaxPool2d(kernel_size=2, stride=2)
# Conv block 3 - Down 3
self.conv3_block = nn.Sequential(
nn.Conv2d(in_channels=self.f*2, out_channels=self.f*4,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*4, out_channels=self.f*4,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
self.max3 = nn.MaxPool2d(kernel_size=2, stride=2)
# Conv block 4 - Down 4
self.conv4_block = nn.Sequential(
nn.Conv2d(in_channels=self.f*4, out_channels=self.f*8,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*8, out_channels=self.f*8,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
self.max4 = nn.MaxPool2d(kernel_size=2, stride=2)
# Conv block 5 - Down 5
self.conv5_block = nn.Sequential(
nn.Conv2d(in_channels=self.f*8, out_channels=self.f*16,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*16, out_channels=self.f*16,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
# Up 1
self.up_1 = nn.ConvTranspose2d(in_channels=self.f*16, out_channels=self.f*8, kernel_size=2, stride=2)
# Up Conv block 1
self.conv_up_1 = nn.Sequential(
nn.Conv2d(in_channels=self.f*16, out_channels=self.f*8,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*8, out_channels=self.f*8,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
# Up 2
self.up_2 = nn.ConvTranspose2d(in_channels=self.f*8, out_channels=self.f*4, kernel_size=2, stride=2)
# Up Conv block 2
self.conv_up_2 = nn.Sequential(
nn.Conv2d(in_channels=self.f*8, out_channels=self.f*4,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*4, out_channels=self.f*4,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
# Up 3
self.up_3 = nn.ConvTranspose2d(in_channels=self.f*4, out_channels=self.f*2, kernel_size=2, stride=2)
# Up Conv block 3
self.conv_up_3 = nn.Sequential(
nn.Conv2d(in_channels=self.f*4, out_channels=self.f*2,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f*2, out_channels=self.f*2,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
# Up 4
self.up_4 = nn.ConvTranspose2d(in_channels=self.f*2, out_channels=self.f, kernel_size=2, stride=2)
# Up Conv block 4
self.conv_up_4 = nn.Sequential(
nn.Conv2d(in_channels=self.f*2, out_channels=self.f,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=self.f, out_channels=self.f,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
# Final output
# self.conv_final = nn.Conv2d(in_channels=32, out_channels=2,
# kernel_size=1, padding=0, stride=1)
self.conv_final = nn.Sequential(
nn.Conv2d(in_channels=self.f, out_channels=1,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=3, padding=1, stride=1),
nn.ReLU(inplace=True),
)
def forward(self, x):
# print('input', x.shape)
# Down 1
x = self.conv1_block(x)
# print('after conv1', x.shape)
conv1_out = x # Save out1
conv1_dim = x.shape[2]
x = self.max1(x)
# print('before conv2', 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('before conv3', 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('before conv4', x.shape)
# Down 4
x = self.conv4_block(x)
# print('after conv5', x.shape)
conv4_out = x
conv4_dim = x.shape[2]
x = self.max4(x)
# print('after conv4', x.shape)
# Midpoint
x = self.conv5_block(x)
# print('mid', x.shape)
# Up 1
x = self.up_1(x)
# print('up_1', x.shape)
lower = int((conv4_dim - x.shape[2]) / 2)
upper = int(conv4_dim - lower)
conv4_out_modified = conv4_out[:, :, lower:upper, lower:upper]
x = torch.cat([x, conv4_out_modified], dim=1)
# print('after cat_1', x.shape)
x = self.conv_up_1(x)
# print('after conv_1', x.shape)
# Up 2
x = self.up_2(x)
# print('up_2', x.shape)
lower = int((conv3_dim - x.shape[2]) / 2)
upper = int(conv3_dim - lower)
conv3_out_modified = conv3_out[:, :, lower:upper, lower:upper]
x = torch.cat([x, conv3_out_modified], dim=1)
# print('after cat_2', x.shape)
x = self.conv_up_2(x)
# print('after conv_2', x.shape)
# Up 3
x = self.up_3(x)
# print('up_3', x.shape)
lower = int((conv2_dim - x.shape[2]) / 2)
upper = int(conv2_dim - lower)
conv2_out_modified = conv2_out[:, :, lower:upper, lower:upper]
x = torch.cat([x, conv2_out_modified], dim=1)
# print('after cat_3', x.shape)
x = self.conv_up_3(x)
# print('after conv_3', x.shape)
# Up 4
x = self.up_4(x)
# print('up_4', x.shape)
lower = int((conv1_dim - x.shape[2]) / 2)
upper = int(conv1_dim - lower)
conv1_out_modified = conv1_out[:, :, lower:upper, lower:upper]
x = torch.cat([x, conv1_out_modified], dim=1)
# print('after cat_4', x.shape)
x = self.conv_up_4(x)
# print('after conv_4', x.shape)
# Final output
x = self.conv_final(x)
# print('final', x.shape)
return x