-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.py
255 lines (213 loc) · 8.61 KB
/
util.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class ConvReg(nn.Module):
"""Convolutional regression for FitNet (feature map layer)"""
def __init__(self, s_shape, t_shape, use_relu=True):
super(ConvReg, self).__init__()
self.use_relu = use_relu
s_N, s_C, s_H, s_W = s_shape
t_N, t_C, t_H, t_W = t_shape
if s_H == 2 * t_H:
self.conv = nn.Conv2d(s_C, t_C, kernel_size=3, stride=2, padding=1)
elif s_H * 2 == t_H:
self.conv = nn.ConvTranspose2d(s_C, t_C, kernel_size=4, stride=2, padding=1)
elif s_H >= t_H:
self.conv = nn.Conv2d(s_C, t_C, kernel_size=(1+s_H-t_H, 1+s_W-t_W))
else:
raise NotImplemented('student size {}, teacher size {}'.format(s_H, t_H))
self.bn = nn.BatchNorm2d(t_C)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
if self.use_relu:
return self.relu(self.bn(x))
else:
return self.bn(x)
class Regress(nn.Module):
"""Simple Linear Regression for FitNet (feature vector layer)"""
def __init__(self, dim_in=1024, dim_out=1024):
super(Regress, self).__init__()
self.linear = nn.Linear(dim_in, dim_out)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = x.view(x.shape[0], -1)
x = self.linear(x)
x = self.relu(x)
return x
class SelfA(nn.Module):
"""Cross layer Self Attention"""
def __init__(self, s_len, t_len, input_channel, s_n, s_t, factor=4):
super(SelfA, self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d((1,1))
for i in range(t_len):
setattr(self, 'key_weight'+str(i), MLPEmbed(input_channel, input_channel//factor))
for i in range(s_len):
setattr(self, 'query_weight'+str(i), MLPEmbed(input_channel, input_channel//factor))
for i in range(s_len):
for j in range(t_len):
setattr(self, 'regressor'+str(i)+str(j), AAEmbed(s_n[i], s_t[j]))
def forward(self, feat_s, feat_t):
sim_t = list(range(len(feat_t)))
sim_s = list(range(len(feat_s)))
bsz = feat_s[0].shape[0]
# similarity matrix
for i in range(len(feat_t)):
sim_temp = feat_t[i].reshape(bsz, -1)
sim_t[i] = torch.matmul(sim_temp, sim_temp.t())
for i in range(len(feat_s)):
sim_temp = feat_s[i].reshape(bsz, -1)
sim_s[i] = torch.matmul(sim_temp, sim_temp.t())
# key of target layers
proj_key = self.key_weight0(sim_t[0])
proj_key = proj_key[:, :, None]
for i in range(1, len(sim_t)):
temp_proj_key = getattr(self, 'key_weight'+str(i))(sim_t[i])
proj_key = torch.cat([proj_key, temp_proj_key[:, :, None]], 2)
# query of source layers
proj_query = self.query_weight0(sim_s[0])
proj_query = proj_query[:, None, :]
for i in range(1, len(sim_s)):
temp_proj_query = getattr(self, 'query_weight'+str(i))(sim_s[i])
proj_query = torch.cat([proj_query, temp_proj_query[:, None, :]], 1)
# attention weight
energy = torch.bmm(proj_query, proj_key) # batch_size X No.stu feature X No.tea feature
attention = F.softmax(energy, dim = -1)
# feature space alignment
proj_value_stu = []
value_tea = []
for i in range(len(sim_s)):
proj_value_stu.append([])
value_tea.append([])
for j in range(len(sim_t)):
s_H, t_H = feat_s[i].shape[2], feat_t[j].shape[2]
if s_H > t_H:
input = F.adaptive_avg_pool2d(feat_s[i], (t_H, t_H))
proj_value_stu[i].append(getattr(self, 'regressor'+str(i)+str(j))(input))
value_tea[i].append(feat_t[j])
elif s_H < t_H or s_H == t_H:
target = F.adaptive_avg_pool2d(feat_t[j], (s_H, s_H))
proj_value_stu[i].append(getattr(self, 'regressor'+str(i)+str(j))(feat_s[i]))
value_tea[i].append(target)
return proj_value_stu, value_tea, attention
class AAEmbed(nn.Module):
"""non-linear embed by MLP"""
def __init__(self, num_input_channels=1024, num_target_channels=128):
super(AAEmbed, self).__init__()
self.num_mid_channel = 2 * num_target_channels
def conv1x1(in_channels, out_channels, stride=1):
return nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0, stride=stride, bias=False)
def conv3x3(in_channels, out_channels, stride=1):
return nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride, bias=False)
self.regressor = nn.Sequential(
conv1x1(num_input_channels, self.num_mid_channel),
nn.BatchNorm2d(self.num_mid_channel),
nn.ReLU(inplace=True),
conv3x3(self.num_mid_channel, self.num_mid_channel),
nn.BatchNorm2d(self.num_mid_channel),
nn.ReLU(inplace=True),
conv1x1(self.num_mid_channel, num_target_channels),
)
def forward(self, x):
x = self.regressor(x)
return x
class Embed(nn.Module):
"""Embedding module"""
def __init__(self, dim_in=1024, dim_out=128):
super(Embed, self).__init__()
self.linear = nn.Linear(dim_in, dim_out)
self.l2norm = Normalize(2)
def forward(self, x):
x = x.view(x.shape[0], -1)
x = self.linear(x)
x = self.l2norm(x)
return x
class LinearEmbed(nn.Module):
"""Linear Embedding"""
def __init__(self, dim_in=1024, dim_out=128):
super(LinearEmbed, self).__init__()
self.linear = nn.Linear(dim_in, dim_out)
def forward(self, x):
x = x.view(x.shape[0], -1)
x = self.linear(x)
return x
class MLPEmbed(nn.Module):
"""non-linear embed by MLP"""
def __init__(self, dim_in=1024, dim_out=128):
super(MLPEmbed, self).__init__()
self.linear1 = nn.Linear(dim_in, 2 * dim_out)
self.relu = nn.ReLU(inplace=True)
self.linear2 = nn.Linear(2 * dim_out, dim_out)
self.l2norm = Normalize(2)
def forward(self, x):
x = x.view(x.shape[0], -1)
x = self.relu(self.linear1(x))
x = self.l2norm(self.linear2(x))
return x
class Normalize(nn.Module):
"""normalization layer"""
def __init__(self, power=2):
super(Normalize, self).__init__()
self.power = power
def forward(self, x):
norm = x.pow(self.power).sum(1, keepdim=True).pow(1. / self.power)
out = x.div(norm)
return out
class Flatten(nn.Module):
"""flatten module"""
def __init__(self):
super(Flatten, self).__init__()
def forward(self, feat):
return feat.view(feat.size(0), -1)
class PoolEmbed(nn.Module):
"""pool and embed"""
def __init__(self, layer=0, dim_out=128, pool_type='avg'):
super().__init__()
if layer == 0:
pool_size = 8
nChannels = 16
elif layer == 1:
pool_size = 8
nChannels = 16
elif layer == 2:
pool_size = 6
nChannels = 32
elif layer == 3:
pool_size = 4
nChannels = 64
elif layer == 4:
pool_size = 1
nChannels = 64
else:
raise NotImplementedError('layer not supported: {}'.format(layer))
self.embed = nn.Sequential()
if layer <= 3:
if pool_type == 'max':
self.embed.add_module('MaxPool', nn.AdaptiveMaxPool2d((pool_size, pool_size)))
elif pool_type == 'avg':
self.embed.add_module('AvgPool', nn.AdaptiveAvgPool2d((pool_size, pool_size)))
self.embed.add_module('Flatten', Flatten())
self.embed.add_module('Linear', nn.Linear(nChannels*pool_size*pool_size, dim_out))
self.embed.add_module('Normalize', Normalize(2))
def forward(self, x):
return self.embed(x)
if __name__ == '__main__':
import torch
g_s = [
torch.randn(2, 16, 16, 16),
torch.randn(2, 32, 8, 8),
torch.randn(2, 64, 4, 4),
]
g_t = [
torch.randn(2, 32, 16, 16),
torch.randn(2, 64, 8, 8),
torch.randn(2, 128, 4, 4),
]
s_shapes = [s.shape for s in g_s]
t_shapes = [t.shape for t in g_t]
net = ConnectorV2(s_shapes, t_shapes)
out = net(g_s)
for f in out:
print(f.shape)