-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_block.py
129 lines (100 loc) · 4.68 KB
/
match_block.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
class match_block(nn.Module):
def __init__(self, inplanes):
super(match_block, self).__init__()
self.sub_sample = False
self.in_channels = inplanes
self.inter_channels = None
if self.inter_channels is None:
self.inter_channels = self.in_channels // 2
if self.inter_channels == 0:
self.inter_channels = 1
max_pool_layer = nn.MaxPool2d(kernel_size=(2,2))
self.g = nn.Conv2d(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0)
self.W = nn.Sequential(
nn.Conv2d(in_channels=self.inter_channels, out_channels=self.in_channels,
kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(self.in_channels)
)
nn.init.constant_(self.W[1].weight, 0)
nn.init.constant_(self.W[1].bias, 0)
self.Q = nn.Sequential(
nn.Conv2d(in_channels=self.inter_channels, out_channels=self.in_channels,
kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(self.in_channels)
)
nn.init.constant_(self.Q[1].weight, 0)
nn.init.constant_(self.Q[1].bias, 0)
self.theta = nn.Conv2d(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0)
self.phi = nn.Conv2d(in_channels=self.in_channels, out_channels=self.inter_channels,
kernel_size=1, stride=1, padding=0)
self.concat_project = nn.Sequential(
nn.Conv2d(self.inter_channels * 2, 1, 1, 1,0, bias=False),
nn.ReLU()
)
self.ChannelGate = ChannelGate(self.in_channels)
self.globalAvgPool = nn.AdaptiveAvgPool2d(1)
def forward(self, aim, detect):
batch_size, channels, height_a, width_a = aim.shape
batch_size, channels, height_d, width_d = detect.shape
d_x = self.g(detect).view(batch_size, self.inter_channels, -1)
d_x = d_x.permute(0, 2, 1).contiguous()
a_x = self.g(aim).view(batch_size, self.inter_channels, -1)
a_x = a_x.permute(0, 2, 1).contiguous()
theta_x = self.theta(aim).view(batch_size, self.inter_channels, -1)
theta_x = theta_x.permute(0, 2, 1)
phi_x = self.phi(detect).view(batch_size, self.inter_channels, -1)
f = torch.matmul(theta_x, phi_x)
N = f.size(-1)
f_div_C = f / N
f = f.permute(0, 2, 1).contiguous()
N = f.size(-1)
fi_div_C = f / N
non_aim = torch.matmul(f_div_C, d_x)
non_aim = non_aim.permute(0, 2, 1).contiguous()
non_aim = non_aim.view(batch_size, self.inter_channels, height_a, width_a)
non_aim = self.W(non_aim)
non_aim = non_aim + aim
non_det = torch.matmul(fi_div_C, a_x)
non_det = non_det.permute(0, 2, 1).contiguous()
non_det = non_det.view(batch_size, self.inter_channels, height_d, width_d)
non_det = self.Q(non_det)
non_det = non_det + detect
c_weight = self.ChannelGate(non_aim)
act_aim = non_aim * c_weight
return act_aim, non_det
class ChannelGate(nn.Module):
def __init__(self, gate_channels, reduction_ratio=16, pool_types=['avg', 'max']):
super(ChannelGate, self).__init__()
self.gate_channels = gate_channels
self.mlp = nn.Sequential(
Flatten(),
nn.Linear(gate_channels, gate_channels // reduction_ratio),
nn.ReLU(),
nn.Linear(gate_channels // reduction_ratio, gate_channels)
)
self.pool_types = pool_types
def forward(self, x):
channel_att_sum = None
for pool_type in self.pool_types:
if pool_type == 'avg':
avg_pool = F.avg_pool2d(x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp(avg_pool)
elif pool_type == 'max':
max_pool = F.max_pool2d(x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
channel_att_raw = self.mlp(max_pool)
if channel_att_sum is None:
channel_att_sum = channel_att_raw
else:
channel_att_sum = channel_att_raw + channel_att_sum
scale = torch.sigmoid(channel_att_sum).unsqueeze(2).unsqueeze(3)
return scale
class Flatten(nn.Module):
def forward(self,x):
return x.view(x.size(0), -1)