-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
169 lines (134 loc) · 6.09 KB
/
model.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
import torch.nn as M
from torchsummary import summary
def DepthwiseConv(in_channels, kernel_size, stride, padding):
return M.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, stride=stride,
padding=padding, groups=in_channels, bias=False)
def PointwiseConv(in_channels, out_channels):
return M.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, padding=0, bias=True)
class CovSepBlock(M.Module):
def __init__(self, in_channels, out_channels, kernel_size=5, stride=1, padding=0):
super().__init__()
self.dc = DepthwiseConv(in_channels, kernel_size, stride=stride, padding=padding)
self.pc = PointwiseConv(in_channels, out_channels)
def forward(self, x):
x = self.dc(x)
x = self.pc(x)
return x
class Encoder(M.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.sepconv = CovSepBlock(in_channels, out_channels // 4, padding=2)
self.activate = M.ReLU(inplace=True)
self.sepconv2 = CovSepBlock(out_channels // 4, out_channels, padding=2)
self.proj = M.Identity()
if in_channels != out_channels:
self.proj = CovSepBlock(in_channels, out_channels, kernel_size=3, padding=1)
self.activate2 = M.ReLU(inplace=True)
def forward(self, x):
branch = self.proj(x)
x = self.sepconv(x)
x = self.activate(x)
x = self.sepconv2(x)
x += branch
return self.activate2(x)
class Upsampling(M.Module):
def __init__(self, in_channels, out_channels, kernel_size=2):
super().__init__()
self.upsample = M.ConvTranspose2d(in_channels, out_channels, kernel_size=kernel_size, stride=2)
def forward(self, x):
return self.upsample(x)
class Downsampling(M.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.sepconv = CovSepBlock(in_channels=in_channels, out_channels=out_channels // 4, stride=2, padding=2)
# self.activate = M.ReLU()
self.activate = M.ReLU(inplace=True)
self.sepconv2 = CovSepBlock(in_channels=out_channels // 4, out_channels=out_channels, padding=2)
self.branchconv = CovSepBlock(in_channels, out_channels, kernel_size=3, stride=2, padding=1)
self.activate2 = M.ReLU(inplace=True)
def forward(self, x):
branch = x
x = self.sepconv(x)
x = self.activate(x)
x = self.sepconv2(x)
branch = self.branchconv(branch)
x += branch
return self.activate2(x)
class Decoder(M.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.sepconv = CovSepBlock(in_channels, out_channels, kernel_size=3, padding=1)
# self.activate = M.ReLU()
self.activate = M.ReLU(inplace=True)
self.sepconv2 = CovSepBlock(out_channels, out_channels, kernel_size=3, padding=1)
def forward(self, x):
branch = x
x = self.sepconv(x)
x = self.activate(x)
x = self.sepconv2(x)
return x + branch
def EncoderStage(in_channels, out_channels, num_encoder):
seq = [
Downsampling(in_channels, out_channels),
]
for _ in range(num_encoder):
seq.append(
Encoder(out_channels, out_channels)
)
return M.Sequential(*seq)
class DecoderStage(M.Module):
def __init__(self, in_channels, out_channels, skip_in_channels):
super().__init__()
self.decoder = Decoder(in_channels, in_channels)
self.upsampling = Upsampling(in_channels, out_channels)
self.skipconnect = CovSepBlock(skip_in_channels, out_channels, kernel_size=3, padding=1)
self.activate = M.ReLU(inplace=True)
def forward(self, x):
input, skip = x
input = self.decoder(input)
input = self.upsampling(input)
skip = self.skipconnect(skip)
skip = self.activate(skip)
# print(input.shape, skip.shape)
return input + skip
class SimpleNet(M.Module):
def __init__(self):
super().__init__()
self.conv = M.Conv2d(in_channels=4, out_channels=16, kernel_size=3, padding=1)
self.relu = M.ReLU(inplace=True)
self.encoder_stage1 = EncoderStage(in_channels=16, out_channels=64, num_encoder=1)
self.encoder_stage2 = EncoderStage(in_channels=64, out_channels=128, num_encoder=1)
self.encoder_stage3 = EncoderStage(in_channels=128, out_channels=256, num_encoder=3)
self.encoder_stage4 = EncoderStage(in_channels=256, out_channels=512, num_encoder=3)
# Strange ???
self.enc2dec = CovSepBlock(in_channels=512, out_channels=64, kernel_size=3, padding=1)
self.med_activate = M.ReLU(inplace=True)
self.decoder_stage1 = DecoderStage(in_channels=64, skip_in_channels=256, out_channels=64)
self.decoder_stage2 = DecoderStage(in_channels=64, skip_in_channels=128, out_channels=32)
self.decoder_stage3 = DecoderStage(in_channels=32, skip_in_channels=64, out_channels=32)
self.decoder_stage4 = DecoderStage(in_channels=32, skip_in_channels=16, out_channels=16)
self.output_layer = M.Sequential(*(Decoder(in_channels=16, out_channels=16),
M.Conv2d(in_channels=16, out_channels=4, kernel_size=3, padding=1)))
def forward(self, img):
assert img.shape[1] == 4
pre = self.conv(img)
pre = self.relu(pre)
first = self.encoder_stage1(pre)
second = self.encoder_stage2(first)
third = self.encoder_stage3(second)
fourth = self.encoder_stage4(third)
med = self.enc2dec(fourth)
med = self.med_activate(med)
de_first = self.decoder_stage1((med, third))
de_second = self.decoder_stage2((de_first, second))
de_thrid = self.decoder_stage3((de_second, first))
de_fourth = self.decoder_stage4((de_thrid, pre))
output = self.output_layer(de_fourth)
return output + img
def check():
model = SimpleNet()
# for p in model.named_parameters():
# print(p)
print(summary(model))
if __name__ == '__main__':
check()