-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels4.py
198 lines (173 loc) · 6.06 KB
/
models4.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
# CBAM
import torch
import torch.nn as nn
import torch.nn.functional as F
class ChannelAttention(nn.Module):
def __init__(self, in_planes, ratio=16):
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc1 = nn.Conv2d(in_planes, in_planes // 16, 1, bias=False)
self.relu1 = nn.ReLU()
self.fc2 = nn.Conv2d(in_planes // 16, in_planes, 1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
out = avg_out + max_out
return self.sigmoid(out)
class SpatialAttention(nn.Module):
def __init__(self, kernel_size=7):
super(SpatialAttention, self).__init__()
assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
padding = 3 if kernel_size == 7 else 1
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = torch.mean(x, dim=1, keepdim=True)
max_out, _ = torch.max(x, dim=1, keepdim=True)
x = torch.cat([avg_out, max_out], dim=1)
x = self.conv1(x)
return self.sigmoid(x)
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
#Conv1
self.layer1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.layer2 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1)
)
self.layer3 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1)
)
self.ca1 = ChannelAttention(32)
self.sa1 = SpatialAttention()
#Conv2
self.layer5 = nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1)
self.layer6 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1)
)
self.layer7 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1)
)
self.ca2 = ChannelAttention(64)
self.sa2 = SpatialAttention()
#Conv3
self.layer9 = nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1)
self.layer10 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1)
)
self.layer11 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1)
)
self.ca3 = ChannelAttention(128)
self.sa3 = SpatialAttention()
def forward(self, x):
#Conv1
x = self.layer1(x)
out = x
x = self.layer2(x) + x
x = self.layer3(x) + x
x = x + out
x = (self.ca1(x) * x)
x = (self.sa1(x) * x) + x
#Conv2
x = self.layer5(x)
out = x
x = self.layer6(x) + x
x = self.layer7(x) + x
x = x + out
x = (self.ca2(x) * x)
x = (self.sa2(x) * x) + x
#Conv3
x = self.layer9(x)
out = x
x = self.layer10(x) + x
x = self.layer11(x) + x
x = x + out
x = (self.ca3(x) * x)
x = (self.sa3(x) * x) + x
return x
class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
# Deconv3
self.layer13 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1)
)
self.layer14 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1)
)
self.ca4 = ChannelAttention(128)
self.sa4 = SpatialAttention()
self.layer16 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1)
#Deconv2
self.layer17 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1)
)
self.layer18 = nn.Sequential(
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1)
)
self.ca5 = ChannelAttention(64)
self.sa5 = SpatialAttention()
self.layer20 = nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1)
#Deconv1
self.layer21 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1)
)
self.layer22 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1)
)
self.ca6 = ChannelAttention(32)
self.sa6 = SpatialAttention()
self.layer24 = nn.Conv2d(32, 3, kernel_size=3, padding=1)
def forward(self,x):
#Deconv3
out = x
x = self.layer13(x) + x
x = self.layer14(x) + x
x = x + out
x = (self.ca4(x) * x)
x = (self.sa4(x) * x) + x
x = self.layer16(x)
#Deconv2
out = x
x = self.layer17(x) + x
x = self.layer18(x) + x
x = self.ca5(x) * x
x = (self.sa5(x) * x) + x
x = x + out
x = self.layer20(x)
#Deconv1
out = x
x = self.layer21(x) + x
x = self.layer22(x) + x
x = x + out
x = self.ca6(x) * x
x = (self.sa6(x) * x) + x
x = self.layer24(x)
return x