-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.py
181 lines (145 loc) · 7.5 KB
/
layers.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
import torch
import torch.nn as nn
import torch.nn.functional as F
def resize(A, size, mode='padding'):
if mode == 'interpolation':
out = F.interpolate(A, size)
if mode == 'padding':
rows_a, cols_a = A.shape[-2:]
rows_b, cols_b = size
p4d = (0, cols_b - cols_a, 0, rows_b - rows_a)
out = F.pad(A, p4d, 'replicate')
return out
class Down(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm='batch', use_drop_out=False, relu=0.0):
super(Down, self).__init__()
self.layer1 = CBDR2d(in_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, relu)
self.layer2 = CBDR2d(out_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, relu)
self.maxpool = nn.MaxPool2d(2,2)
def forward(self, x):
x = self.layer1(x)
residual = self.layer2(x)
x = self.maxpool(residual)
return x, residual
class Up(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm='batch', use_drop_out=False, relu=0.0, use_resize=False):
super(Up, self).__init__()
self.use_resize=use_resize
self.up_conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
self.layer1 = CBDR2d(in_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, relu)
self.layer2 = CBDR2d(out_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, relu)
def resize(self, A, size):
return F.interpolate(A, size)
def forward(self, x, residual):
x = self.up_conv(x)
if self.use_resize:
x = resize(x, residual.shape[-2:])
x = torch.cat([x, residual], dim=1)
x = self.layer1(x)
x = self.layer2(x)
return x
class CBDR2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm='batch', use_drop_out=False, relu=0.0):
super(CBDR2d, self).__init__()
sequence = []
sequence += [nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias)]
if norm == 'batch':
sequence += [nn.BatchNorm2d(num_features=out_channels)]
elif norm == 'instance':
sequence += [nn.InstanceNorm2d(num_features=out_channels)]
if use_drop_out:
sequence += [nn.Dropout2d(0.5)]
if relu is not None:
sequence += [nn.ReLU() if relu == 0.0 else nn.LeakyReLU(relu)]
self.main = nn.Sequential(*sequence)
def forward(self, x):
return self.main(x)
class CBDAR2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm='batch', use_drop_out=False, use_attention=False, attention_mode='CBAM', relu=0.0):
super(CBDAR2d, self).__init__()
# assert attention_mode in ['CBAM', 'RCBAM', 'proposed', 'None']
self.norm = norm
self.use_drop_out = use_drop_out
self.use_attention = use_attention
self.attention_mode = attention_mode
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias)
if attention_mode == 'RCBAM':
self.residual_conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias)
self.bnorm = nn.BatchNorm2d(num_features=out_channels)
self.inorm = nn.InstanceNorm2d(num_features=out_channels)
self.dropout = nn.Dropout2d(0.5)
self.ca = ChannelAttention(in_channels=out_channels, ratio=16)
self.sa = SpatialAttention(kernel_size=7)
self.activation = nn.ReLU() if relu == 0.0 else nn.LeakyReLU(relu)
def forward(self, x):
residual = x
x = self.conv(x)
if self.norm == 'batch':
x = self.bnorm(x)
elif self.norm == 'instance':
x = self.inorm(x)
if self.use_drop_out:
x = self.dropout(x)
if self.use_attention:
if self.attention_mode == 'CBAM':
x = self.ca(x) * x
x = self.sa(x) * x #
elif self.attention_mode == 'RCBAM':
x = self.ca(x) * x
x = self.sa(x) * x #
x = x + self.residual_conv(residual)
x = self.activation(x)
return x
#######################################################################################
class ChannelAttention(nn.Module):
def __init__(self, in_channels, ratio=16):
super(ChannelAttention, self).__init__()
self.ratio = ratio
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc = nn.Sequential(nn.Conv2d(in_channels, in_channels // self.ratio, 1, bias=False),
nn.ReLU(),
nn.Conv2d(in_channels // self.ratio, in_channels, 1, bias=False))
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc(self.avg_pool(x))
max_out = self.fc(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__()
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=kernel_size//2, 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 DownWithAttention(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm='batch', use_drop_out=False, attention_mode='CBAM',relu=0.0):
super(DownWithAttention, self).__init__()
self.layer1 = CBDAR2d(in_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, use_attention=False, relu=relu)
self.layer2 = CBDAR2d(out_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, use_attention=True, attention_mode=attention_mode, relu=relu)
self.maxpool = nn.MaxPool2d(2,2)
def forward(self, x):
x = self.layer1(x)
residual = self.layer2(x)
x = self.maxpool(residual)
return x, residual
class UpWithAttention(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm='batch', use_drop_out=False, attention_mode='CBAM', relu=0.0, use_resize=False):
super(UpWithAttention, self).__init__()
self.use_resize = use_resize
self.up_conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
self.layer1 = CBDAR2d(in_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, use_attention=False, relu=relu)
self.layer2 = CBDAR2d(out_channels, out_channels, kernel_size, stride, padding, bias, norm, use_drop_out, use_attention=True, attention_mode=attention_mode, relu=relu)
def forward(self, x, residual):
x = self.up_conv(x)
if self.use_resize:
x = resize(x, residual.shape[-2:])
x = torch.cat([x, residual], dim=1)
x = self.layer1(x)
x = self.layer2(x)
return x