-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMy_Unet.py
206 lines (170 loc) · 5.46 KB
/
My_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
197
198
199
200
201
202
203
204
205
206
# Code for different variants of U-Net
# Some parts taken from https://github.com/milesial/Pytorch-UNet
# Implements light (half feature channels) and lighter (quarter number of feature maps) U-Net
import torch
import torch.nn as nn
import torch.nn.functional as F
def count_trainable_parameters(model): # to count trainable parameters
return sum(p.numel() for p in model.parameters() if p.requires_grad)
class double_conv(nn.Module):
def __init__(self, in_ch, out_ch):
super(double_conv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv(x)
return x
class inconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(inconv, self).__init__()
self.conv = double_conv(in_ch, out_ch)
def forward(self, x):
x = self.conv(x)
return x
class down(nn.Module):
def __init__(self, in_ch, out_ch):
super(down, self).__init__()
self.mpconv = nn.Sequential(
nn.MaxPool2d(2),
double_conv(in_ch, out_ch)
)
def forward(self, x):
x = self.mpconv(x)
return x
class up(nn.Module):
def __init__(self, in_ch, out_ch):
super(up, self).__init__()
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.conv = double_conv(in_ch, out_ch)
def forward(self, x1, x2):
x1 = self.up(x1)
# input is CHW
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, (diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2))
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return x
class outconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(outconv, self).__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 1)
def forward(self, x):
x = self.conv(x)
return x
number_classes = 3 # building, road, background
########
# U-Net
########
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.inc = inconv(3, 64)
self.down1 = down(64, 128)
self.down2 = down(128, 256)
self.down3 = down(256, 512)
self.down4 = down(512, 512)
self.up1 = up(1024, 256)
self.up2 = up(512, 128)
self.up3 = up(256, 64)
self.up4 = up(128, 64)
self.outc = outconv(64, number_classes)
def forward(self, x):
x1 = self.inc(x.permute(0, 3, 1, 2))
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
x = self.outc(x)
return F.softmax(x, dim = 1)
########
# light U-Net, half the features maps than the original
########
class Net_light(nn.Module):
def __init__(self):
super(Net_light, self).__init__()
self.inc = inconv(3, 32)
self.down1 = down(32, 64)
self.down2 = down(64, 128)
self.down3 = down(128, 256)
self.down4 = down(256, 256)
self.up1 = up(512, 128)
self.up2 = up(256, 64)
self.up3 = up(128, 32)
self.up4 = up(64, 32)
self.outc = outconv(32, number_classes)
def forward(self, x):
x1 = self.inc(x.permute(0, 3, 1, 2))
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
x = self.outc(x)
return F.softmax(x, dim = 1)
########
# lighter U-Net, quarter the features maps than the original
########
class Net_lighter(nn.Module):
def __init__(self):
super(Net_lighter, self).__init__()
self.inc = inconv(3, 16)
self.down1 = down(16, 32)
self.down2 = down(32, 64)
self.down3 = down(64, 128)
self.down4 = down(128, 128)
self.up1 = up(256, 64)
self.up2 = up(128, 32)
self.up3 = up(64, 16)
self.up4 = up(32, 16)
self.outc = outconv(16, number_classes)
def forward(self, x):
x1 = self.inc(x.permute(0, 3, 1, 2))
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
x = self.outc(x)
return F.softmax(x, dim = 1)
class Q_Net_lighter(nn.Module):
def __init__(self):
super(Q_Net_lighter, self).__init__()
self.inc = inconv(3, 16)
self.down1 = down(16, 32)
self.down2 = down(32, 64)
self.down3 = down(64, 128)
self.down4 = down(128, 128)
self.up1 = up(256, 64)
self.up2 = up(128, 32)
self.up3 = up(64, 16)
self.up4 = up(32, 16)
self.outc = outconv(16, 1)
def forward(self, x):
x1 = self.inc(x.permute(0, 3, 1, 2))
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
x = self.outc(x)
return x