-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNDCModule.py
202 lines (166 loc) · 6.35 KB
/
BNDCModule.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
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
import torchvision
import ptflops
from torch.nn import BatchNorm2d
class ConvBNReLU(nn.Module):
def __init__(self, channel, ks=(3, 3), stride=(1, 1), padding=(1, 1), *args, **kwargs):
super(ConvBNReLU, self).__init__()
self.conv = nn.Conv2d(channel,
channel,
kernel_size=ks,
stride=stride,
padding=padding,
bias=False,
groups=channel)
self.bn = BatchNorm2d(channel)
self.relu = nn.ReLU(inplace=True)
self.init_weight()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
def init_weight(self):
for ly in self.children():
if isinstance(ly, nn.Conv2d):
nn.init.kaiming_normal_(ly.weight, a=1)
if not ly.bias is None: nn.init.constant_(ly.bias, 0)
class Inter_channel(nn.Module):
def __init__(self, channel):
super(Inter_channel, self).__init__()
self.channel = channel
self.conv1 = None
self.conv2 = None
self.linear1 = nn.Linear(channel, 2 * channel)
self.linear2 = nn.Linear(2 * channel, channel)
self.sm = nn.Softmax(dim=1)
def forward(self, x):
b, c, h, w = x.shape
stride = (1,1)
if int(h/w) == 2:
stride1 = stride
stride2 = (2,1)
elif int(h/w) == 1:
stride1 = stride
stride2 = stride
elif int(w/h) == 2:
stride1 = (1,2)
stride2 = stride
if self.conv1 is None:
self.conv1 = ConvBNReLU(self.channel, ks=(h, 1), stride=stride1, padding=(0, 0))
if self.conv2 is None:
self.conv2 = ConvBNReLU(self.channel, ks=(1, w), stride=stride2, padding=(0, 0))
xl1 = self.conv1(x)
xl2 = self.conv2(x)
# len of res1 and res2 is channel
res1 = []
res2 = []
num = []
for i in range(c):
res1.append(xl1[:, i, :, :])
res2.append(xl2[:, i, :, :])
for j in range(c):
dot1 = res1[j]
dot2 = res2[j]
ans = []
for k in range(b):
cul1 = dot1[k, :, :]
cul2 = dot2[k, :, :]
ans.append(torch.mm(cul1, cul2))
# ans pinjie
if b == 1:
ans = ans[0]
else:
for l in range(1, len(ans)):
ans = torch.cat((ans[0], ans[l]))
num.append(ans)
# num to tensor
num = torch.stack(num, dim=0)
num = num.view(b, c)
num = self.linear1(num)
num = self.linear2(num)
num = self.sm(num)
num = num.view(b, c, 1, 1)
return x * num.expand_as(x)
class Intra_channel(nn.Module):
def __init__(self, channels, dimension=2, sub_sample=False, bn_layer=True):
super(Intra_channel, self).__init__()
assert dimension in [1, 2, 3]
self.dimension = dimension
self.sub_sample = sub_sample
self.in_channels = channels
self.inter_channels = channels
if dimension == 3:
conv_nd = nn.Conv3d
max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2))
bn = nn.BatchNorm3d
elif dimension == 2:
conv_nd = nn.Conv2d
max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2))
bn = nn.BatchNorm2d
else:
conv_nd = nn.Conv1d
max_pool_layer = nn.MaxPool1d(kernel_size=(2))
bn = nn.BatchNorm1d
self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0, groups=self.in_channels)
if bn_layer:
self.W = nn.Sequential(
conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels,
kernel_size=1, stride=1, padding=0, groups=self.in_channels),
bn(self.in_channels)
)
nn.init.constant_(self.W[1].weight, 0)
nn.init.constant_(self.W[1].bias, 0)
else:
self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels,
kernel_size=1, stride=1, padding=0, groups=self.in_channels)
nn.init.constant_(self.W.weight, 0)
nn.init.constant_(self.W.bias, 0)
self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0, groups=self.in_channels)
self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0, groups=self.in_channels)
if sub_sample:
self.g = nn.Sequential(self.g, max_pool_layer)
self.phi = nn.Sequential(self.phi, max_pool_layer)
def forward(self, x, return_nl_map=False):
"""
:param x: (b, c, t, h, w)
:param return_nl_map: if True return z, nl_map, else only return z.
:return:
"""
batch_size = x.size(0)
g_x = self.g(x).view(batch_size, self.inter_channels, -1)
g_x = g_x.permute(0, 2, 1)
theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)
theta_x = theta_x.permute(0, 2, 1)
phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)
f = torch.matmul(theta_x, phi_x)
f_div_C = F.softmax(f, dim=-1)
y = torch.matmul(f_div_C, g_x)
y = y.permute(0, 2, 1).contiguous()
y = y.view(batch_size, self.inter_channels, *x.size()[2:])
W_y = self.W(y)
z = W_y + x
if return_nl_map:
return z, f_div_C
return z
class BNDC(nn.Module):
def __init__(self,channel):
super(BNDC, self).__init__()
self.inter_channel = Inter_channel(channel)
self.intra_channel = Intra_channel(channel)
def forward(self,x):
x1 = self.inter_channel(x)
x2 = self.intra_channel(x)
x = x1 + x2
return x
if __name__ == "__main__":
input1 = torch.randn(size=(2, 64, 32, 16))
bndc = BNDC(64)
output1 = bndc(input1)
print(output1.shape)