-
Notifications
You must be signed in to change notification settings - Fork 22
/
Blanced_attention.py
208 lines (184 loc) · 8.42 KB
/
Blanced_attention.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
import torch.nn as nn
from collections import OrderedDict
import torch
import torch.nn.functional as F
def mean_channels(F):
assert(F.dim() == 4)
spatial_sum = F.sum(3, keepdim=True).sum(2, keepdim=True)
return spatial_sum / (F.size(2) * F.size(3))
def stdv_channels(F):
assert(F.dim() == 4)
F_mean = mean_channels(F)
F_variance = (F - F_mean).sum(3, keepdim=True).sum(2, keepdim=True) / (F.size(2) * F.size(3))
return F_variance
class ChannelAttention(nn.Module):
def __init__(self, in_planes, ratio=16):
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
# self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc1 = nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False)
self.relu1 = nn.PReLU(in_planes // ratio)
self.fc2 = nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
# max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
out = avg_out
return self.sigmoid(out)
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(1, 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(max_out)
return self.sigmoid(x)
#调用BlancedAttention 模块即可
class BlancedAttention(nn.Module):
def __init__(self, in_planes, reduction=16):
super(BlancedAttention, self).__init__()
self.ca = ChannelAttention(in_planes, reduction)
self.sa = SpatialAttention()
# self.conv=nn.Conv2d(in_planes*2,in_planes,1)
# self.norm=nn.BatchNorm2d(in_planes)
def forward(self, x):
ca_ch = self.ca(x)
sa_ch = self.sa(x)
out=ca_ch.mul(sa_ch)*x
# out_fused = self.conv(torch.cat([ca_ch, sa_ch], dim=1))
return out
class ChannelAttention_maxpool(nn.Module):
def __init__(self, in_planes, ratio=16):
super(ChannelAttention_maxpool, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc1 = nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False)
self.relu1 = nn.PReLU(in_planes // ratio)
self.fc2 = nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
out = avg_out+max_out
return self.sigmoid(out)
class SpatialAttention_averagepool(nn.Module):
def __init__(self, kernel_size=7):
super(SpatialAttention_averagepool, 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, 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)
return self.sigmoid(x)
class BlancedAttention_CAM_SAM_ADD(nn.Module):
def __init__(self, in_planes, reduction=16):
super(BlancedAttention_CAM_SAM_ADD, self).__init__()
self.ca = ChannelAttention_maxpool(in_planes, reduction)
self.sa = SpatialAttention_averagepool()
# self.conv=nn.Conv2d(in_planes*2,in_planes,1)
# self.norm=nn.BatchNorm2d(in_planes)
def forward(self, x):
ca_ch = self.ca(x)
sa_ch = self.sa(x)
out=ca_ch.mul(sa_ch)*x
# out_fused = self.conv(torch.cat([ca_ch, sa_ch], dim=1))
return out
# class ChannelAttention2(nn.Module):
# def __init__(self, in_planes, ratio=16):
# super(ChannelAttention2, self).__init__()
# self.avg_pool = nn.AdaptiveAvgPool2d(1)
# self.max_pool = nn.AdaptiveMaxPool2d(1)
#
# self.fc1 = nn.Conv2d(2*in_planes, 2*in_planes // ratio, 1, bias=False)
# self.relu1 = nn.PReLU(2*in_planes // ratio)
# self.fc2 = nn.Conv2d(2*in_planes // ratio, in_planes, 1, bias=False)
#
# self.sigmoid = nn.Sigmoid()
#
# def forward(self, x):
# avg=self.avg_pool(x)
# max=self.max_pool(x)
# out = self.fc2(self.relu1(self.fc1(torch.cat([avg,max],1))))
# return self.sigmoid(out)
#
# class SpatialAttention2(nn.Module):
# def __init__(self, kernel_size=7):
# super(SpatialAttention2, 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, 1, kernel_size, padding=padding, bias=False)
# self.sigmoid = nn.Sigmoid()
#
# def forward(self, x):
# avg_out = torch.mean(x, dim=1, keepdim=True)
# # std_out=torch.std(x, dim=1, keepdim=True)
# # real_out=avg_out+std_out
# 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 BlancedAttention3(nn.Module):
# def __init__(self, in_planes, reduction=16):
# super(BlancedAttention3, self).__init__()
#
# self.ca = ChannelAttention2(in_planes, reduction)
# self.sa = SpatialAttention2()
# self.conv=nn.Conv2d(in_planes*2,in_planes,1)
#
# def forward(self, x):
# ca_ch = self.ca(x)* x
# sa_ch = self.sa(x) * x
# out_fused = self.conv(torch.cat([ca_ch, sa_ch], dim=1))
# return out_fused
#
# ####demo
# def conv_layer(in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1):
# padding = int((kernel_size - 1) / 2) * dilation
# return nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding=padding, bias=True, dilation=dilation,
# groups=groups)
# def activation(act_type, inplace=True, neg_slope=0.05, n_prelu=1):
# act_type = act_type.lower()
# if act_type == 'relu':
# layer = nn.ReLU(inplace)
# elif act_type == 'lrelu':
# layer = nn.LeakyReLU(neg_slope, inplace)
# elif act_type == 'prelu':
# layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
# else:
# raise NotImplementedError('activation layer [{:s}] is not found'.format(act_type))
# return layer
# class IMDModule_blancedattention(nn.Module):
# def __init__(self, in_channels, distillation_rate=0.25):
# super(IMDModule_blancedattention, self).__init__()
# self.distilled_channels = int(in_channels * distillation_rate)
# self.remaining_channels = int(in_channels - self.distilled_channels)
# self.c1 = conv_layer(in_channels, in_channels, 3)
# self.c2 = conv_layer(self.remaining_channels, in_channels, 3)
# self.c3 = conv_layer(self.remaining_channels, in_channels, 3)
# self.c4 = conv_layer(self.remaining_channels, self.distilled_channels, 3)
# self.act = activation('lrelu', neg_slope=0.05)
# self.attention = BlancedAttention(self.distilled_channels*4,reduction=16)#此处调用
#
# def forward(self, input):
# out_c1 = self.act(self.c1(input))
# distilled_c1, remaining_c1 = torch.split(out_c1, (self.distilled_channels, self.remaining_channels), dim=1)
# out_c2 = self.act(self.c2(remaining_c1))
# distilled_c2, remaining_c2 = torch.split(out_c2, (self.distilled_channels, self.remaining_channels), dim=1)
# out_c3 = self.act(self.c3(remaining_c2))
# distilled_c3, remaining_c3 = torch.split(out_c3, (self.distilled_channels, self.remaining_channels), dim=1)
# out_c4 = self.c4(remaining_c3)
# out = torch.cat([distilled_c1, distilled_c2, distilled_c3, out_c4], dim=1)
# out_fused = self.attention(out) + input #此处调用
# return out_fused