-
Notifications
You must be signed in to change notification settings - Fork 22
/
unet.py
executable file
·196 lines (135 loc) · 5.72 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
from torch import nn
import torch
# encoding block
class encoding_block(nn.Module):
"""
Convolutional batch norm block with relu activation (main block used in the encoding steps)
"""
def __init__(self, in_size, out_size, kernel_size=3, padding=0, stride=1, dilation=1, batch_norm=True, dropout=False):
super().__init__()
if batch_norm:
# reflection padding for same size output as input (reflection padding has shown better results than zero padding)
layers = [nn.ReflectionPad2d(padding=(kernel_size -1)//2),
nn.Conv2d(in_size, out_size, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation),
nn.PReLU(),
nn.BatchNorm2d(out_size),
nn.ReflectionPad2d(padding=(kernel_size - 1)//2),
nn.Conv2d(out_size, out_size, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation),
nn.PReLU(),
nn.BatchNorm2d(out_size),
]
else:
layers = [nn.ReflectionPad2d(padding=(kernel_size - 1)//2),
nn.Conv2d(in_size, out_size, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation),
nn.PReLU(),
nn.ReflectionPad2d(padding=(kernel_size - 1)//2),
nn.Conv2d(out_size, out_size, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation),
nn.PReLU(),]
if dropout:
layers.append(nn.Dropout())
self.encoding_block = nn.Sequential(*layers)
def forward(self, input):
output = self.encoding_block(input)
return output
# decoding block
class decoding_block(nn.Module):
def __init__(self, in_size, out_size, batch_norm=False, upsampling=True):
super().__init__()
if upsampling:
self.up = nn.Sequential(nn.Upsample(mode='bilinear', scale_factor=2),
nn.Conv2d(in_size, out_size, kernel_size=1))
else:
self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2)
self.conv = encoding_block(in_size, out_size, batch_norm=batch_norm)
def forward(self, input1, input2):
output2 = self.up(input2)
output1 = nn.functional.upsample(input1, output2.size()[2:], mode='bilinear')
return self.conv(torch.cat([output1, output2], 1))
class UNet(nn.Module):
"""
Main UNet architecture
"""
def __init__(self, num_classes=1):
super().__init__()
# encoding
self.conv1 = encoding_block(3, 64)
self.maxpool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = encoding_block(64, 128)
self.maxpool2 = nn.MaxPool2d(kernel_size=2)
self.conv3 = encoding_block(128, 256)
self.maxpool3 = nn.MaxPool2d(kernel_size=2)
self.conv4 = encoding_block(256, 512)
self.maxpool4 = nn.MaxPool2d(kernel_size=2)
# center
self.center = encoding_block(512, 1024)
# decoding
self.decode4 = decoding_block(1024, 512)
self.decode3 = decoding_block(512, 256)
self.decode2 = decoding_block(256, 128)
self.decode1 = decoding_block(128, 64)
# final
self.final = nn.Conv2d(64, num_classes, kernel_size=1)
def forward(self, input):
# encoding
conv1 = self.conv1(input)
maxpool1 = self.maxpool1(conv1)
conv2 = self.conv2(maxpool1)
maxpool2 = self.maxpool2(conv2)
conv3 = self.conv3(maxpool2)
maxpool3 = self.maxpool3(conv3)
conv4 = self.conv4(maxpool3)
maxpool4 = self.maxpool4(conv4)
# center
center = self.center(maxpool4)
# decoding
decode4 = self.decode4(conv4, center)
decode3 = self.decode3(conv3, decode4)
decode2 = self.decode2(conv2, decode3)
decode1 = self.decode1(conv1, decode2)
# final
final = nn.functional.upsample(self.final(decode1), input.size()[2:], mode='bilinear')
return final
class UNetSmall(nn.Module):
"""
Main UNet architecture
"""
def __init__(self, num_classes=1):
super().__init__()
# encoding
self.conv1 = encoding_block(3, 32)
self.maxpool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = encoding_block(32, 64)
self.maxpool2 = nn.MaxPool2d(kernel_size=2)
self.conv3 = encoding_block(64, 128)
self.maxpool3 = nn.MaxPool2d(kernel_size=2)
self.conv4 = encoding_block(128, 256)
self.maxpool4 = nn.MaxPool2d(kernel_size=2)
# center
self.center = encoding_block(256, 512)
# decoding
self.decode4 = decoding_block(512, 256)
self.decode3 = decoding_block(256, 128)
self.decode2 = decoding_block(128, 64)
self.decode1 = decoding_block(64, 32)
# final
self.final = nn.Conv2d(32, num_classes, kernel_size=1)
def forward(self, input):
# encoding
conv1 = self.conv1(input)
maxpool1 = self.maxpool1(conv1)
conv2 = self.conv2(maxpool1)
maxpool2 = self.maxpool2(conv2)
conv3 = self.conv3(maxpool2)
maxpool3 = self.maxpool3(conv3)
conv4 = self.conv4(maxpool3)
maxpool4 = self.maxpool4(conv4)
# center
center = self.center(maxpool4)
# decoding
decode4 = self.decode4(conv4, center)
decode3 = self.decode3(conv3, decode4)
decode2 = self.decode2(conv2, decode3)
decode1 = self.decode1(conv1, decode2)
# final
final = nn.functional.upsample(self.final(decode1), input.size()[2:], mode='bilinear')
return final