-
Notifications
You must be signed in to change notification settings - Fork 0
/
A2FPN.py
204 lines (160 loc) · 7.23 KB
/
A2FPN.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models
from torch.nn import Module, Conv2d, Parameter, Softmax
from collections import OrderedDict
def l2_norm(x):
return torch.einsum("bcn, bn->bcn", x, 1 / torch.norm(x, p=2, dim=-2))
class ConvBnRelu(nn.Module):
def __init__(self, in_planes, out_planes, ksize, stride, pad, dilation=1,
groups=1, has_bn=True, norm_layer=nn.BatchNorm2d, bn_eps=1e-5,
has_relu=True, inplace=True, has_bias=False):
super(ConvBnRelu, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=ksize,
stride=stride, padding=pad,
dilation=dilation, groups=groups, bias=has_bias)
self.has_bn = has_bn
if self.has_bn:
self.bn = norm_layer(out_planes, eps=bn_eps)
self.has_relu = has_relu
if self.has_relu:
self.relu = nn.ReLU(inplace=inplace)
def forward(self, x):
x = self.conv(x)
if self.has_bn:
x = self.bn(x)
if self.has_relu:
x = self.relu(x)
return x
class Attention(Module):
def __init__(self, in_places, scale=8, eps=1e-6):
super(Attention, self).__init__()
self.gamma = Parameter(torch.zeros(1))
self.in_places = in_places
self.l2_norm = l2_norm
self.eps = eps
self.query_conv = Conv2d(in_channels=in_places, out_channels=in_places // scale, kernel_size=1)
self.key_conv = Conv2d(in_channels=in_places, out_channels=in_places // scale, kernel_size=1)
self.value_conv = Conv2d(in_channels=in_places, out_channels=in_places, kernel_size=1)
def forward(self, x):
# Apply the feature map to the queries and keys
batch_size, chnnels, width, height = x.shape
Q = self.query_conv(x).view(batch_size, -1, width * height)
K = self.key_conv(x).view(batch_size, -1, width * height)
V = self.value_conv(x).view(batch_size, -1, width * height)
Q = self.l2_norm(Q).permute(-3, -1, -2)
K = self.l2_norm(K)
tailor_sum = 1 / (width * height + torch.einsum("bnc, bc->bn", Q, torch.sum(K, dim=-1) + self.eps))
value_sum = torch.einsum("bcn->bc", V).unsqueeze(-1)
value_sum = value_sum.expand(-1, chnnels, width * height)
matrix = torch.einsum('bmn, bcn->bmc', K, V)
matrix_sum = value_sum + torch.einsum("bnm, bmc->bcn", Q, matrix)
weight_value = torch.einsum("bcn, bn->bcn", matrix_sum, tailor_sum)
weight_value = weight_value.view(batch_size, chnnels, height, width)
return (self.gamma * weight_value).contiguous()
class AttentionAggregationModule(nn.Module):
def __init__(self, in_chan, out_chan):
super(AttentionAggregationModule, self).__init__()
self.convblk = ConvBnRelu(in_chan, out_chan, ksize=1, stride=1, pad=0)
self.conv_atten = Attention(out_chan)
def forward(self, s5, s4, s3, s2):
fcat = torch.cat([s5, s4, s3, s2], dim=1)
feat = self.convblk(fcat)
atten = self.conv_atten(feat)
feat_out = atten + feat
return feat_out
class Conv3x3GNReLU(nn.Module):
def __init__(self, in_channels, out_channels, upsample=False):
super().__init__()
self.upsample = upsample
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, (3, 3),
stride=1, padding=1, bias=False),
nn.GroupNorm(32, out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
x = self.block(x)
if self.upsample:
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
return x
class FPNBlock(nn.Module):
def __init__(self, pyramid_channels, skip_channels):
super().__init__()
self.skip_conv = nn.Conv2d(skip_channels, pyramid_channels, kernel_size=1)
def forward(self, x):
x, skip = x
x = F.interpolate(x, scale_factor=2, mode='nearest')
skip = self.skip_conv(skip)
x = x + skip
return x
class SegmentationBlock(nn.Module):
def __init__(self, in_channels, out_channels, n_upsamples=0):
super().__init__()
blocks = [
Conv3x3GNReLU(in_channels, out_channels, upsample=bool(n_upsamples))
]
if n_upsamples > 1:
for _ in range(1, n_upsamples):
blocks.append(Conv3x3GNReLU(out_channels, out_channels, upsample=True))
self.block = nn.Sequential(*blocks)
def forward(self, x):
return self.block(x)
class A2FPN(nn.Module):
def __init__(
self,
band,
class_num=6,
encoder_channels=[512, 256, 128, 64],
pyramid_channels=64,
segmentation_channels=64,
dropout=0.2,
):
super().__init__()
self.name = 'A2FPN'
self.base_model = models.resnet18(pretrained=True)
# self.base_model = models.resnet34(pretrained=True)
self.base_layers = list(self.base_model.children())
# ==> encoder layers
self.layer_down0 = nn.Sequential(*self.base_layers[:3]) # size=(N, 64, x.H/2, x.W/2)
self.layer_down1 = nn.Sequential(*self.base_layers[3:5]) # size=(N, 64, x.H/4, x.W/4)
self.layer_down2 = self.base_layers[5] # size=(N, 128, x.H/8, x.W/8)
self.layer_down3 = self.base_layers[6] # size=(N, 256, x.H/16, x.W/16)
self.layer_down4 = self.base_layers[7] # size=(N, 512, x.H/32, x.W/32)
self.conv1 = nn.Conv2d(encoder_channels[0], pyramid_channels, kernel_size=(1, 1))
self.p4 = FPNBlock(pyramid_channels, encoder_channels[1])
self.p3 = FPNBlock(pyramid_channels, encoder_channels[2])
self.p2 = FPNBlock(pyramid_channels, encoder_channels[3])
self.s5 = SegmentationBlock(pyramid_channels, segmentation_channels, n_upsamples=3)
self.s4 = SegmentationBlock(pyramid_channels, segmentation_channels, n_upsamples=2)
self.s3 = SegmentationBlock(pyramid_channels, segmentation_channels, n_upsamples=1)
self.s2 = SegmentationBlock(pyramid_channels, segmentation_channels, n_upsamples=0)
self.attention = AttentionAggregationModule(segmentation_channels * 4, segmentation_channels * 4)
self.final_conv = nn.Conv2d(segmentation_channels * 4, class_num, kernel_size=1, padding=0)
self.dropout = nn.Dropout2d(p=dropout, inplace=True)
def forward(self, x):
# ==> get encoder features
c1 = self.layer_down0(x)
c2 = self.layer_down1(c1)
c3 = self.layer_down2(c2)
c4 = self.layer_down3(c3)
c5 = self.layer_down4(c4)
# c5, c4, c3, c2, _ = x
p5 = self.conv1(c5)
p4 = self.p4([p5, c4])
p3 = self.p3([p4, c3])
p2 = self.p2([p3, c2])
s5 = self.s5(p5)
s4 = self.s4(p4)
s3 = self.s3(p3)
s2 = self.s2(p2)
out = self.dropout(self.attention(s5, s4, s3, s2))
out = self.final_conv(out)
out = F.interpolate(out, scale_factor=4, mode='bilinear', align_corners=True)
return out
if __name__ == "__main__":
model = A2FPN(3).cuda()
input = torch.rand(2, 3, 512, 512).cuda()
output = model(input)
print(output.size())