-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSConv.py
236 lines (214 loc) · 12.3 KB
/
CSConv.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import torch
import torch.nn as nn
import torch.nn.functional as F
class CFG(nn.Module):
def __init__(self, in_planes, K, T, init_weight=True):
super(CFG, self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d(1)
if in_planes!=3:
hidden_planes = in_planes
else:
hidden_planes = K
self.fc1_1 = nn.Conv2d(in_planes, hidden_planes, 1, bias=False)
self.bn1_1 = nn.BatchNorm2d(hidden_planes)
self.fc1_2_new = nn.Conv2d(hidden_planes, int(K*in_planes), 1, bias=True)
self.bn1_2_new = nn.BatchNorm2d(int(K*in_planes))
self.T = T
if init_weight:
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
if isinstance(m ,nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.avgpool(x)
x1 = self.fc1_1(x)
x1 = self.bn1_1(x1)
x1 = F.relu(x1)
x1 = self.fc1_2_new(x1)
x1 = self.bn1_2_new(x1)
x1 = x1.view(x.size(0), -1)
return F.softmax(x1/self.T, 1)
class CSConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size=(3,3), stride=1, padding=1, dilation=1, groups=1, bias=True, K=3,T=34, init_weight=True):
super(CSConv, self).__init__()
assert in_planes%groups==0
self.in_planes = in_planes
self.out_planes = out_planes
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.K = K
self.bias = bias
self.Theta = CFG(in_planes, K, T)
#self.attention
self.Anchored_weight = nn.Parameter(torch.randn(K, out_planes, in_planes, kernel_size[0], kernel_size[1]), requires_grad=True)
#self.weight
if bias:
self.Anchored_bia = nn.Parameter(torch.zeros(K, out_planes))
#self.bias
else:
self.Anchored_bia = None
if init_weight:
self._initialize_weights()
def _initialize_weights(self):
for i in range(self.K):
nn.init.kaiming_normal_(self.Anchored_weight[i], mode='fan_out', nonlinearity='relu')
def forward(self, x):
batch_size, in_planes, height, width = x.size()
Theta = self.Theta(x)
x = x.view(1, -1, height, width)
#regerate cs_weight
Anchored_weight = self.Anchored_weight.transpose(1,2).contiguous().view((self.K * self.in_planes), -1)
cs_weight = torch.rand(batch_size,self.K * self.in_planes,self.out_planes * self.kernel_size[0]* self.kernel_size[1]).cuda()
for i in range(batch_size):
cs_weight[i] = Theta[i].unsqueeze(1)*Anchored_weight
cs_weight = cs_weight.view(batch_size, self.K, self.in_planes, self.out_planes, self.kernel_size[0], self.kernel_size[1]).sum(dim=1).transpose(1,2).contiguous().view(-1, self.in_planes, self.kernel_size[0], self.kernel_size[1])
if self.bias is not None:
cs_bia = torch.mm(Theta.view(batch_size,self.K,-1).mean(axis=2,keepdim=False), self.Anchored_bia).view(-1)
#conv with cs_weight
output = F.conv2d(x, weight=cs_weight, bias=cs_bia, stride=self.stride, padding=self.padding,dilation=self.dilation, groups=self.groups*batch_size)
output = output.view(batch_size, self.out_planes, output.size(-2), output.size(-1))
return output
class M_CFG(nn.Module):
def __init__(self, in_planes, K, T, init_weight=True):
super(M_CFG, self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d(1)
if in_planes!=3:
hidden_planes = in_planes
else:
hidden_planes = K
self.fc1_1 = nn.Conv2d(in_planes, hidden_planes, 1, bias=False)
self.bn1_1 = nn.BatchNorm2d(hidden_planes)
self.fc1_2 = nn.Conv2d(hidden_planes, int(K*in_planes*0.5), 1, bias=True)
self.bn1_2 = nn.BatchNorm2d(int(K*in_planes*0.5))
self.fc2_1 = nn.Conv2d(in_planes, hidden_planes, 1, bias=False)
self.bn2_1 = nn.BatchNorm2d(hidden_planes)
self.fc2_2 = nn.Conv2d(hidden_planes, int(K*in_planes*0.5), 1, bias=True)
self.bn2_2 = nn.BatchNorm2d(int(K*in_planes*0.5))
self.T = T
if init_weight:
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
if isinstance(m ,nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.avgpool(x)
x1 = self.fc1_1(x)
x1 = self.bn1_1(x1)
x1 = F.relu(x1)
x1 = self.fc1_2(x1)
x1 = self.bn1_2(x1)
x1 = x1.view(x.size(0), -1)
x2 = self.fc2_1(x)
x2 = self.bn2_1(x2)
x2 = F.relu(x2)
x2 = self.fc2_2(x2)
x2 = self.bn2_2(x2)
x2 = x2.view(x.size(0), -1)
return F.softmax(x1/self.T, 1), F.softmax(x2/self.T, 1)
class M_CSConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size=(3,3), stride=1, padding=1, dilation=1, groups=1, bias=True, K=3,T=34, init_weight=True):
super(M_CSConv, self).__init__()
assert in_planes%groups==0
self.in_planes = in_planes
self.out_planes = out_planes
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.K = K
self.bias = bias
self.Theta = M_CFG(in_planes*2, K, T)
self.Anchored_weight = nn.Parameter(torch.randn(K, out_planes, in_planes, kernel_size[0], kernel_size[1]), requires_grad=True)
if bias:
self.Anchored_bia = nn.Parameter(torch.zeros(K, out_planes))
else:
self.Anchored_bia = None
if init_weight:
self._initialize_weights()
def _initialize_weights(self):
for i in range(self.K):
nn.init.kaiming_normal_(self.Anchored_weight[i], mode='fan_out', nonlinearity='relu')
def forward(self, center, boundary):
concat_feature = torch.cat((center,boundary),dim=1)
Theta_center, Theta_boundary = self.Theta(concat_feature)
batch_size, in_planes, height, width = center.size()
center = center.view(1, -1, height, width)
boundary = boundary.view(1, -1, height, width)
Anchored_weight = self.Anchored_weight.transpose(1,2).contiguous().view((self.K * self.in_planes), -1)
cs_weight_center = torch.rand(batch_size,self.K * self.in_planes,self.out_planes * self.kernel_size[0]* self.kernel_size[1]).cuda()
cs_weight_boundary = torch.rand(batch_size,self.K * self.in_planes,self.out_planes * self.kernel_size[0]* self.kernel_size[1]).cuda()
for i in range(batch_size):
cs_weight_center[i] = Theta_center[i].unsqueeze(1)*Anchored_weight
cs_weight_boundary[i] = Theta_boundary[i].unsqueeze(1)*Anchored_weight
cs_weight_center = cs_weight_center.view(batch_size, self.K, self.in_planes, self.out_planes, self.kernel_size[0], self.kernel_size[1]).sum(dim=1).transpose(1,2).contiguous().view(-1, self.in_planes, self.kernel_size[0], self.kernel_size[1])
cs_weight_boundary = cs_weight_boundary.view(batch_size, self.K, self.in_planes, self.out_planes, self.kernel_size[0], self.kernel_size[1]).sum(dim=1).transpose(1,2).contiguous().view(-1, self.in_planes, self.kernel_size[0], self.kernel_size[1])
if self.bias is not None:
cs_bia_center = torch.mm(Theta_center.view(batch_size,self.K,-1).mean(axis=2,keepdim=False), self.Anchored_bia).view(-1)
cs_bia_boundary = torch.mm(Theta_boundary.view(batch_size,self.K,-1).mean(axis=2,keepdim=False), self.Anchored_bia).view(-1)
output_center = F.conv2d(center, weight=cs_weight_center, bias=cs_bia_center, stride=self.stride, padding=self.padding,dilation=self.dilation, groups=self.groups*batch_size)
output_boundary = F.conv2d(boundary, weight=cs_weight_boundary, bias=cs_bia_boundary, stride=self.stride, padding=self.padding,dilation=self.dilation, groups=self.groups*batch_size)
output_center = output_center.view(batch_size, self.out_planes, output_center.size(-2), output_center.size(-1))
output_boundary = output_boundary.view(batch_size, self.out_planes, output_boundary.size(-2), output_boundary.size(-1))
return output_center,output_boundary
class CCM(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size=(3,3), stride=1, padding=1, dilation=1, groups=1, bias=True, K=3,T=34, init_weight=True):
super(CCM, self).__init__()
assert in_planes%groups==0
self.in_planes = in_planes
self.out_planes = out_planes
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.K = K
self.bias = bias
self.Theta = M_CFG(in_planes*2, K, T)
self.Anchored_weight = nn.Parameter(torch.randn(K, out_planes, in_planes, kernel_size[0], kernel_size[1]), requires_grad=True)
if bias:
self.Anchored_bia = nn.Parameter(torch.zeros(K, out_planes))
else:
self.Anchored_bia = None
if init_weight:
self._initialize_weights()
def _initialize_weights(self):
for i in range(self.K):
nn.init.kaiming_normal_(self.Anchored_weight[i], mode='fan_out', nonlinearity='relu')
def forward(self, center, boundary, mask):
concat_feature = torch.cat((center,boundary),dim=1)*mask
Theta_center, Theta_boundary = self.Theta(concat_feature)
batch_size, in_planes, height, width = center.size()
center = center.view(1, -1, height, width)
boundary = boundary.view(1, -1, height, width)
Anchored_weight = self.Anchored_weight.transpose(1,2).contiguous().view((self.K * self.in_planes), -1)
cs_weight_center = torch.rand(batch_size,self.K * self.in_planes,self.out_planes * self.kernel_size[0]* self.kernel_size[1]).cuda()
cs_weight_boundary = torch.rand(batch_size,self.K * self.in_planes,self.out_planes * self.kernel_size[0]* self.kernel_size[1]).cuda()
for i in range(batch_size):
cs_weight_center[i] = Theta_center[i].unsqueeze(1)*Anchored_weight
cs_weight_boundary[i] = Theta_boundary[i].unsqueeze(1)*Anchored_weight
cs_weight_center = cs_weight_center.view(batch_size, self.K, self.in_planes, self.out_planes, self.kernel_size[0], self.kernel_size[1]).sum(dim=1).transpose(1,2).contiguous().view(-1, self.in_planes, self.kernel_size[0], self.kernel_size[1])
cs_weight_boundary = cs_weight_boundary.view(batch_size, self.K, self.in_planes, self.out_planes, self.kernel_size[0], self.kernel_size[1]).sum(dim=1).transpose(1,2).contiguous().view(-1, self.in_planes, self.kernel_size[0], self.kernel_size[1])
if self.bias is not None:
cs_bia_center = torch.mm(Theta_center.view(batch_size,self.K,-1).mean(axis=2,keepdim=False), self.Anchored_bia).view(-1)
cs_bia_boundary = torch.mm(Theta_boundary.view(batch_size,self.K,-1).mean(axis=2,keepdim=False), self.Anchored_bia).view(-1)
output_center = F.conv2d(center, weight=cs_weight_center, bias=cs_bia_center, stride=self.stride, padding=self.padding,dilation=self.dilation, groups=self.groups*batch_size)
output_boundary = F.conv2d(boundary, weight=cs_weight_boundary, bias=cs_bia_boundary, stride=self.stride, padding=self.padding,dilation=self.dilation, groups=self.groups*batch_size)
output_center = output_center.view(batch_size, self.out_planes, output_center.size(-2), output_center.size(-1))
output_boundary = output_boundary.view(batch_size, self.out_planes, output_boundary.size(-2), output_boundary.size(-1))
return output_center,output_boundary