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
|
import torch
import torch.nn as nn
from nets.vgg import VGG16
class conv_block(nn.Module):
"""
Convolution Block
"""
def __init__(self, in_ch, out_ch):
super(conv_block, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=1, padding=1, bias=True),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, kernel_size=3, stride=1, padding=1, bias=True),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
out = self.conv(x)
return out
class up_conv(nn.Module):
"""
Up Convolution Block
"""
def __init__(self, in_ch, out_ch):
super(up_conv, self).__init__()
self.up = nn.Sequential(
nn.Upsample(scale_factor=2),
nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=1, padding=1, bias=True),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
def forward(self, x):
out = self.up(x)
return out
class Attention_block(nn.Module):
"""
Attention Block
"""
def __init__(self, F_g, F_l, F_int):
super(Attention_block, self).__init__()
self.W_g = nn.Sequential(
nn.Conv2d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True),
nn.BatchNorm2d(F_int)
)
self.W_x = nn.Sequential(
nn.Conv2d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True),
nn.BatchNorm2d(F_int)
)
self.psi = nn.Sequential(
nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
nn.BatchNorm2d(1),
nn.Sigmoid()
)
self.relu = nn.ReLU(inplace=True)
def forward(self, g, x):
g1 = self.W_g(g)
x1 = self.W_x(x)
psi = self.relu(g1 + x1)
psi = self.psi(psi)
out = x * psi
return out
class Att_Unet(nn.Module):
def __init__(self, num_classes=21, pretrained=True, backbone="vgg", base_size=64):
super(Att_Unet, self).__init__()
filters = [base_size, base_size * 2, base_size * 4, base_size * 8, base_size * 16]
if backbone == "vgg":
self.vgg = VGG16(pretrained=pretrained)
else:
raise ValueError('Unsupported backbone - `{}`, Use vgg.'.format(backbone))
self.Connection_Conv = nn.Conv2d(512, 1024, kernel_size=3, stride=1, padding=1, bias=True)
# 32,32,1024->64,64,512
self.Up5 = up_conv(filters[4], filters[3])
self.Att5 = Attention_block(F_g=filters[3], F_l=filters[3], F_int=filters[2])
self.Conv_block5 = conv_block(filters[4], filters[3])
# 64,64,512->128,128,256
self.Up4 = up_conv(filters[3], filters[2])
self.Att4 = Attention_block(F_g=filters[2], F_l=filters[2], F_int=filters[1])
self.Conv_block4 = conv_block(filters[3], filters[2])
# 128,128,256->256,256,128
self.Up3 = up_conv(filters[2], filters[1])
self.Att3 = Attention_block(F_g=filters[1], F_l=filters[1], F_int=filters[0])
self.Conv_block3 = conv_block(filters[2], filters[1])
# 256,256,128->512,512,64
self.Up2 = up_conv(filters[1], filters[0])
self.Att2 = Attention_block(F_g=filters[0], F_l=filters[0], F_int=filters[0] // 2)
self.Conv_block2 = conv_block(filters[1], filters[0])
self.final = nn.Conv2d(filters[0], num_classes, kernel_size=1, stride=1, padding=0)
self.backbone = backbone
def forward(self, inputs):
if self.backbone == "vgg":
[feat1, feat2, feat3, feat4, feat5] = self.vgg.forward(inputs)
# print(feat1.shape)
# print(feat2.shape)
# print(feat3.shape)
# print(feat4.shape)
# print(feat5.shape)
connection_temp = self.Connection_Conv(feat5)
d5 = self.Up5(connection_temp)
e4 = self.Att5(g=d5, x=feat4)
d5 = torch.cat((e4, d5), dim=1)
a4 = self.Conv_block5(d5)
# print(a4.shape)
d4 = self.Up4(a4)
e3 = self.Att4(g=d4, x=feat3)
d4 = torch.cat((e3, d4), dim=1)
a3 = self.Conv_block4(d4)
# print(a3.shape)
d3 = self.Up3(a3)
e2 = self.Att3(g=d3, x=feat2)
d3 = torch.cat((e2, d3), dim=1)
a2 = self.Conv_block3(d3)
# print(a2.shape)
d2 = self.Up2(a2)
e1 = self.Att2(g=d2, x=feat1)
d2 = torch.cat((e1, d2), dim=1)
a1 = self.Conv_block2(d2)
# print(a1.shape)
out = self.final(a1)
return out
def freeze_backbone(self):
if self.backbone == "vgg":
for param in self.vgg.parameters():
param.requires_grad = False
def unfreeze_backbone(self):
if self.backbone == "vgg":
for param in self.vgg.parameters():
param.requires_grad = True
if __name__ == '__main__':
x = torch.randn((3, 3, 512, 512))
model = Att_Unet(num_classes=4, pretrained=False, backbone="vgg", base_size=64)
pred = model(x)
print(x.shape)
print(pred.shape)
|