-
Notifications
You must be signed in to change notification settings - Fork 2
/
MAResUNet.py
213 lines (163 loc) · 7.25 KB
/
MAResUNet.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
207
208
209
210
211
212
213
import torch
import torch.nn as nn
from torchvision import models
import torch.nn.functional as F
from torch.nn import Module, Conv2d, Parameter, Softmax
from functools import partial
nonlinearity = partial(F.relu, inplace=True)
def conv3otherRelu(in_planes, out_planes, kernel_size=None, stride=None, padding=None):
# 3x3 convolution with padding and relu
if kernel_size is None:
kernel_size = 3
assert isinstance(kernel_size, (int, tuple)), 'kernel_size is not in (int, tuple)!'
if stride is None:
stride = 1
assert isinstance(stride, (int, tuple)), 'stride is not in (int, tuple)!'
if padding is None:
padding = 1
assert isinstance(padding, (int, tuple)), 'padding is not in (int, tuple)!'
return nn.Sequential(
nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding, bias=True),
nn.ReLU(inplace=True) # inplace=True
)
def l2_norm(x):
return torch.einsum("bcn, bn->bcn", x, 1 / torch.norm(x, p=2, dim=-2))
class PAM_Module(Module):
def __init__(self, in_places, scale=8, eps=1e-6):
super(PAM_Module, self).__init__()
self.gamma = Parameter(torch.zeros(1))
self.in_places = in_places
# self.exp_feature = exp_feature_map
# self.tanh_feature = tanh_feature_map
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 (x + self.gamma * weight_value).contiguous()
class CAM_Module(Module):
def __init__(self):
super(CAM_Module, self).__init__()
self.gamma = Parameter(torch.zeros(1))
self.softmax = Softmax(dim=-1)
def forward(self, x):
batch_size, chnnels, width, height = x.shape
proj_query = x.view(batch_size, chnnels, -1)
proj_key = x.view(batch_size, chnnels, -1).permute(0, 2, 1)
energy = torch.bmm(proj_query, proj_key)
energy_new = torch.max(energy, -1, keepdim=True)[0].expand_as(energy) - energy
attention = self.softmax(energy_new)
proj_value = x.view(batch_size, chnnels, -1)
out = torch.bmm(attention, proj_value)
out = out.view(batch_size, chnnels, height, width)
out = self.gamma * out + x
return out
class PAM_CAM_Layer(nn.Module):
def __init__(self, in_ch):
super(PAM_CAM_Layer, self).__init__()
self.conv1 = conv3otherRelu(in_ch, in_ch)
self.PAM = PAM_Module(in_ch)
self.CAM = CAM_Module()
self.conv2P = nn.Sequential(nn.Dropout2d(0.1, False), conv3otherRelu(in_ch, in_ch, 1, 1, 0))
self.conv2C = nn.Sequential(nn.Dropout2d(0.1, False), conv3otherRelu(in_ch, in_ch, 1, 1, 0))
self.conv3 = nn.Sequential(nn.Dropout2d(0.1, False), conv3otherRelu(in_ch, in_ch, 1, 1, 0))
def forward(self, x):
x = self.conv1(x)
x = self.conv2P(self.PAM(x)) + self.conv2C(self.CAM(x))
return self.conv3(x)
class DecoderBlock(nn.Module):
def __init__(self, in_channels, n_filters):
super(DecoderBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels // 4, 1)
self.norm1 = nn.BatchNorm2d(in_channels // 4)
self.relu1 = nonlinearity
self.deconv2 = nn.ConvTranspose2d(in_channels // 4, in_channels // 4, 3, stride=2, padding=1, output_padding=1)
self.norm2 = nn.BatchNorm2d(in_channels // 4)
self.relu2 = nonlinearity
self.conv3 = nn.Conv2d(in_channels // 4, n_filters, 1)
self.norm3 = nn.BatchNorm2d(n_filters)
self.relu3 = nonlinearity
def forward(self, x):
x = self.conv1(x)
x = self.norm1(x)
x = self.relu1(x)
x = self.deconv2(x)
x = self.norm2(x)
x = self.relu2(x)
x = self.conv3(x)
x = self.norm3(x)
x = self.relu3(x)
return x
class MAResUNet(nn.Module):
def __init__(self, num_channels=3, num_classes=5):
super(MAResUNet, self).__init__()
self.name = 'MAResUNet'
filters = [64, 128, 256, 512]
resnet = models.resnet34(pretrained=True)
self.firstconv = resnet.conv1
self.firstbn = resnet.bn1
self.firstrelu = resnet.relu
self.firstmaxpool = resnet.maxpool
self.encoder1 = resnet.layer1
self.encoder2 = resnet.layer2
self.encoder3 = resnet.layer3
self.encoder4 = resnet.layer4
self.attention4 = PAM_CAM_Layer(filters[3])
self.attention3 = PAM_CAM_Layer(filters[2])
self.attention2 = PAM_CAM_Layer(filters[1])
self.attention1 = PAM_CAM_Layer(filters[0])
self.decoder4 = DecoderBlock(filters[3], filters[2])
self.decoder3 = DecoderBlock(filters[2], filters[1])
self.decoder2 = DecoderBlock(filters[1], filters[0])
self.decoder1 = DecoderBlock(filters[0], filters[0])
self.finaldeconv1 = nn.ConvTranspose2d(filters[0], 32, 4, 2, 1)
self.finalrelu1 = nonlinearity
self.finalconv2 = nn.Conv2d(32, 32, 3, padding=1)
self.finalrelu2 = nonlinearity
self.finalconv3 = nn.Conv2d(32, num_classes, 3, padding=1)
def forward(self, x):
# Encoder
x1 = self.firstconv(x)
x1 = self.firstbn(x1)
x1 = self.firstrelu(x1)
x1 = self.firstmaxpool(x1)
e1 = self.encoder1(x1)
e2 = self.encoder2(e1)
e3 = self.encoder3(e2)
e4 = self.encoder4(e3)
e4 = self.attention4(e4)
# Decoder
d4 = self.decoder4(e4) + self.attention3(e3)
d3 = self.decoder3(d4) + self.attention2(e2)
d2 = self.decoder2(d3) + self.attention1(e1)
d1 = self.decoder1(d2)
out = self.finaldeconv1(d1)
out = self.finalrelu1(out)
out = self.finalconv2(out)
out = self.finalrelu2(out)
out = self.finalconv3(out)
# d3 = F.interpolate(d3, size=x.size()[2:], mode='bilinear', align_corners=False)
# return torch.sigmoid(out)
return out
if __name__ == '__main__':
num_classes = 10
in_batch, inchannel, in_h, in_w = 10, 3, 256, 256
x = torch.randn(in_batch, inchannel, in_h, in_w)
net = MAResUNet(3)
out = net(x)
print(out.shape)