-
Notifications
You must be signed in to change notification settings - Fork 1
/
RGA_modules.py
166 lines (143 loc) · 5.46 KB
/
RGA_modules.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import torch
from torch import nn
from torch.nn import functional as F
import pdb
# ===================
# RGA Module
# ===================
class RGA_Module(nn.Module):
def __init__(self, in_channel, in_spatial, use_spatial=True, use_channel=True, cha_ratio=8, spa_ratio=8, down_ratio=8):
super(RGA_Module, self).__init__()
self.in_channel = in_channel
self.in_spatial = in_spatial
self.use_spatial = use_spatial
self.use_channel = use_channel
print ('Use_Spatial_Att: {};\tUse_Channel_Att: {}.'.format(self.use_spatial, self.use_channel))
self.inter_channel = in_channel // cha_ratio
self.inter_spatial = in_spatial // spa_ratio
# Embedding functions for original features
if self.use_spatial:
self.gx_spatial = nn.Sequential(
nn.Conv2d(in_channels=self.in_channel, out_channels=self.inter_channel,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_channel),
nn.ReLU()
)
if self.use_channel:
self.gx_channel = nn.Sequential(
nn.Conv2d(in_channels=self.in_spatial, out_channels=self.inter_spatial,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_spatial),
nn.ReLU()
)
# Embedding functions for relation features
if self.use_spatial:
self.gg_spatial = nn.Sequential(
nn.Conv2d(in_channels=self.in_spatial * 2, out_channels=self.inter_spatial,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_spatial),
nn.ReLU()
)
if self.use_channel:
self.gg_channel = nn.Sequential(
nn.Conv2d(in_channels=self.in_channel*2, out_channels=self.inter_channel,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_channel),
nn.ReLU()
)
# Networks for learning attention weights
if self.use_spatial:
num_channel_s = 1 + self.inter_spatial
self.W_spatial = nn.Sequential(
nn.Conv2d(in_channels=num_channel_s, out_channels=num_channel_s//down_ratio,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_channel_s//down_ratio),
nn.ReLU(),
nn.Conv2d(in_channels=num_channel_s//down_ratio, out_channels=1,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(1)
)
if self.use_channel:
num_channel_c = 1 + self.inter_channel
self.W_channel = nn.Sequential(
nn.Conv2d(in_channels=num_channel_c, out_channels=num_channel_c//down_ratio,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(num_channel_c//down_ratio),
nn.ReLU(),
nn.Conv2d(in_channels=num_channel_c//down_ratio, out_channels=1,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(1)
)
# Embedding functions for modeling relations
if self.use_spatial:
self.theta_spatial = nn.Sequential(
nn.Conv2d(in_channels=self.in_channel, out_channels=self.inter_channel,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_channel),
nn.ReLU()
)
self.phi_spatial = nn.Sequential(
nn.Conv2d(in_channels=self.in_channel, out_channels=self.inter_channel,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_channel),
nn.ReLU()
)
if self.use_channel:
self.theta_channel = nn.Sequential(
nn.Conv2d(in_channels=self.in_spatial, out_channels=self.inter_spatial,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_spatial),
nn.ReLU()
)
self.phi_channel = nn.Sequential(
nn.Conv2d(in_channels=self.in_spatial, out_channels=self.inter_spatial,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(self.inter_spatial),
nn.ReLU()
)
def forward(self, x):
b, c, h, w = x.size()
# 空间注意力
if self.use_spatial:
# spatial attention
theta_xs = self.theta_spatial(x)
phi_xs = self.phi_spatial(x)
theta_xs = theta_xs.view(b, self.inter_channel, -1)
theta_xs = theta_xs.permute(0, 2, 1)
phi_xs = phi_xs.view(b, self.inter_channel, -1)
Gs = torch.matmul(theta_xs, phi_xs)
Gs_in = Gs.permute(0, 2, 1).view(b, h*w, h, w)
Gs_out = Gs.view(b, h*w, h, w)
Gs_joint = torch.cat((Gs_in, Gs_out), 1)
Gs_joint = self.gg_spatial(Gs_joint)
g_xs = self.gx_spatial(x)
g_xs = torch.mean(g_xs, dim=1, keepdim=True)
ys = torch.cat((g_xs, Gs_joint), 1)
W_ys = self.W_spatial(ys)
if not self.use_channel:
out = F.sigmoid(W_ys.expand_as(x)) * x
return out
else:
# 使用空间注意力
x = F.sigmoid(W_ys.expand_as(x)) * x
# 通道注意力
if self.use_channel:
# channel attention
xc = x.view(b, c, -1).permute(0, 2, 1).unsqueeze(-1)
theta_xc = self.theta_channel(xc).squeeze(-1).permute(0, 2, 1)
phi_xc = self.phi_channel(xc).squeeze(-1)
Gc = torch.matmul(theta_xc, phi_xc)
Gc_in = Gc.permute(0, 2, 1).unsqueeze(-1)
Gc_out = Gc.unsqueeze(-1)
Gc_joint = torch.cat((Gc_in, Gc_out), 1)
Gc_joint = self.gg_channel(Gc_joint)
g_xc = self.gx_channel(xc)
g_xc = torch.mean(g_xc, dim=1, keepdim=True)
yc = torch.cat((g_xc, Gc_joint), 1)
W_yc = self.W_channel(yc).transpose(1, 2)
out = F.sigmoid(W_yc) * x
return out
if __name__ == '__main__':
rga = RGA_Module(2048, 14*14, use_spatial=True, use_channel=True, cha_ratio=8, spa_ratio=8, down_ratio=8)