-
Notifications
You must be signed in to change notification settings - Fork 0
/
models3.py
178 lines (153 loc) · 5.32 KB
/
models3.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
# new Spatial attention model skip connection
import torch
import torch.nn as nn
import torch.nn.functional as F
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, 16, kernel_size, padding=padding, bias=False)
self.conv2 = nn.Conv2d(16, 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)
x = self.conv2(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.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.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.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.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.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.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.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.sa6(x) * x) + x
x = self.layer24(x)
return x