-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.py
executable file
·350 lines (305 loc) · 15.3 KB
/
net.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import random
from matplotlib import pyplot as plt
from torch.nn.modules.pooling import AdaptiveAvgPool1d, AvgPool1d, MaxPool1d
class RIConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size):
super(RIConv, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.conv = nn.Sequential(nn.Conv1d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1), nn.BatchNorm1d(out_channels), nn.LeakyReLU(negative_slope=0.1))
def forward(self, x):
# x = F.pad(x, [0, self.kernel_size-1], mode='circular')
x = F.pad(x, [0, self.kernel_size-1], mode='constant', value=0)
out = self.conv(x)
return out
class RIDowsampling(nn.Module):
def __init__(self, ratio=2):
super(RIDowsampling, self).__init__()
self.ratio = ratio
def forward(self, x):
y = x[:, :, list(range(0, x.shape[2], self.ratio))].unsqueeze(1)
for i in range(1, self.ratio):
index = list(range(i, x.shape[2], self.ratio))
y = torch.cat([y, x[:, :, index].unsqueeze(1)], 1)
norm = torch.norm(torch.norm(y, 1, 2), 1, 2)
idx = torch.argmax(norm, 1)
idx = idx.unsqueeze(1).expand(x.shape[0], self.ratio)
id_matrix = torch.tensor([list(range(self.ratio))]).expand(
x.shape[0], self.ratio).to(device=x.device)
out = y[id_matrix == idx]
return out
class RIAttention(nn.Module):
def __init__(self, channels):
super(RIAttention, self).__init__()
self.channels = channels
self.fc = nn.Sequential(
nn.Linear(in_features=self.channels, out_features=self.channels), nn.Sigmoid())
def forward(self, x):
x1 = torch.mean(x, 2)
w = self.fc(x1)
w = w.unsqueeze(2)
out = w*x
return out
class RINet_attention_cons_pad(nn.Module):
def __init__(self):
super(RINet_attention_cons_pad, self).__init__()
self.conv1 = nn.Sequential(RIAttention(12), RIConv(in_channels=12, out_channels=12, kernel_size=3), RIAttention(
12), RIConv(in_channels=12, out_channels=16, kernel_size=3), RIAttention(16))
self.conv2 = nn.Sequential(RIDowsampling(3), RIConv(
in_channels=16, out_channels=16, kernel_size=3), RIAttention(16))
self.conv3 = nn.Sequential(RIDowsampling(3), RIConv(
in_channels=16, out_channels=32, kernel_size=3), RIAttention(32))
self.conv4 = nn.Sequential(RIDowsampling(2), RIConv(
in_channels=32, out_channels=32, kernel_size=3), RIAttention(32))
self.conv5 = nn.Sequential(RIDowsampling(2), RIConv(
in_channels=32, out_channels=64, kernel_size=3), RIAttention(64))
self.conv6 = nn.Sequential(RIDowsampling(2), RIConv(
in_channels=64, out_channels=128, kernel_size=3), RIAttention(128))
#seq的卷积
self.conv1_seq = nn.Sequential(RIAttention(12), RIConv(in_channels=12, out_channels=12, kernel_size=3), RIAttention(
12), RIConv(in_channels=12, out_channels=16, kernel_size=3), RIAttention(16))
self.conv2_seq = nn.Sequential(RIDowsampling(3), RIConv(
in_channels=16, out_channels=16, kernel_size=3), RIAttention(16))
self.conv3_seq = nn.Sequential(RIDowsampling(3), RIConv(
in_channels=16, out_channels=32, kernel_size=3), RIAttention(32))
self.conv4_seq = nn.Sequential(RIDowsampling(2), RIConv(
in_channels=32, out_channels=32, kernel_size=3), RIAttention(32))
self.conv5_seq = nn.Sequential(RIDowsampling(2), RIConv(
in_channels=32, out_channels=64, kernel_size=3), RIAttention(64))
self.conv6_seq = nn.Sequential(RIDowsampling(2), RIConv(
in_channels=64, out_channels=128, kernel_size=3), RIAttention(128))
self.pool = AdaptiveAvgPool1d(1)
self.linear = nn.Sequential(nn.Linear(in_features=288, out_features=128), nn.LeakyReLU(
negative_slope=0.1), nn.Linear(in_features=128, out_features=1))
def forward(self, seq,x,y_0,y_1,y_2,y_3,y_4,y_5,y_6,y_7):
featurex = self.gen_feature_fuse(seq,x)
featurey_0 = self.gen_feature(y_0)
featurey_1 = self.gen_feature(y_1)
featurey_2 = self.gen_feature(y_2)
featurey_3 = self.gen_feature(y_3)
featurey_4 = self.gen_feature(y_4)
featurey_5 = self.gen_feature(y_5)
featurey_6 = self.gen_feature(y_6)
featurey_7 = self.gen_feature(y_7)
out_0, diff_0 = self.gen_score(featurex, featurey_0)
out_1, diff_1 = self.gen_score(featurex, featurey_1)
out_2, diff_2 = self.gen_score(featurex, featurey_2)
out_3, diff_3 = self.gen_score(featurex, featurey_3)
out_4, diff_4 = self.gen_score(featurex, featurey_4)
out_5, diff_5 = self.gen_score(featurex, featurey_5)
out_6, diff_6 = self.gen_score(featurex, featurey_6)
out_7, diff_7 = self.gen_score(featurex, featurey_7)
out_cat=torch.cat((out_0.reshape(1,-1),out_1.reshape(1,-1),out_2.reshape(1,-1),out_3.reshape(1,-1),out_4.reshape(1,-1),out_5.reshape(1,-1),out_6.reshape(1,-1),out_7.reshape(1,-1)),0)
diff_cat=torch.cat((diff_0.reshape(1,-1),diff_1.reshape(1,-1),diff_2.reshape(1,-1),diff_3.reshape(1,-1),diff_4.reshape(1,-1),diff_5.reshape(1,-1),diff_6.reshape(1,-1),diff_7.reshape(1,-1)),0)
out=torch.max(out_cat,0)[0]
out_cat_idx=torch.max(out_cat,0)[1]
w_indices=torch.arange(0,out.shape[0])
diff=diff_cat[out_cat_idx,w_indices]
return out, diff, out_cat
def gen_feature_fuse(self, seq,x):
fxy = []
xy1_seq=self.conv1_seq(seq)
xy1 = self.conv1(x)+xy1_seq
fxy.append(self.pool(xy1).view(x.shape[0], -1))
xy2_seq = self.conv2_seq(xy1_seq)
xy2 = self.conv2(xy1)+xy2_seq
fxy.append(self.pool(xy2).view(x.shape[0], -1))
xy3_seq = self.conv3_seq(xy2_seq)
xy3 = self.conv3(xy2)+xy3_seq
fxy.append(self.pool(xy3).view(x.shape[0], -1))
xy4_seq = self.conv4_seq(xy3_seq)
xy4 = self.conv4(xy3)+xy4_seq
fxy.append(self.pool(xy4).view(x.shape[0], -1))
xy5_seq = self.conv5_seq(xy4_seq)
xy5 = self.conv5(xy4)+xy5_seq
fxy.append(self.pool(xy5).view(x.shape[0], -1))
xy6_seq = self.conv6_seq(xy5_seq)
xy6 = self.conv6(xy5)+xy6_seq
fxy.append(self.pool(xy6).view(x.shape[0], -1))
featurexy = torch.cat(fxy, 1)
return featurexy
def gen_feature(self, xy):
fxy = []
xy1 = self.conv1(xy)
fxy.append(self.pool(xy1).view(xy.shape[0], -1))
xy2 = self.conv2(xy1)
fxy.append(self.pool(xy2).view(xy.shape[0], -1))
xy3 = self.conv3(xy2)
fxy.append(self.pool(xy3).view(xy.shape[0], -1))
xy4 = self.conv4(xy3)
fxy.append(self.pool(xy4).view(xy.shape[0], -1))
xy5 = self.conv5(xy4)
fxy.append(self.pool(xy5).view(xy.shape[0], -1))
xy6 = self.conv6(xy5)
fxy.append(self.pool(xy6).view(xy.shape[0], -1))
featurexy = torch.cat(fxy, 1)
return featurexy
def gen_score(self, fx, fy):
diff = torch.abs(fx-fy)
# print('diff',diff)
# print('diff.shape',diff.shape)
# print('torch.norm(diff, dim=1)',torch.norm(diff, dim=1))
# print('torch.norm(diff, dim=1).shape',torch.norm(diff, dim=1).shape)
# os._exit()
out = self.linear(diff).view(-1)
if not self.training:
out = torch.sigmoid(out)
# print('out.shape',out.shape)
# print('out',out)
# print('torch.norm(diff, dim=1).shape',torch.norm(diff, dim=1).shape)
# print('torch.norm(diff, dim=1)',torch.norm(diff, dim=1))
# os._exit()
return out, torch.norm(diff, dim=1)
def load(self, model_file):
checkpoint = torch.load(model_file)
self.load_state_dict(checkpoint['state_dict'])
class RIConv_cir_pad(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size):
super(RIConv_cir_pad, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.conv = nn.Sequential(nn.Conv1d(in_channels=in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1), nn.BatchNorm1d(out_channels), nn.LeakyReLU(negative_slope=0.1))
def forward(self, x):
x = F.pad(x, [0, self.kernel_size-1], mode='circular')
# x = F.pad(x, [0, self.kernel_size-1], mode='constant', value=0)
out = self.conv(x)
return out
class RINet_attention_cir_pad(nn.Module):
def __init__(self):
super(RINet_attention_cir_pad, self).__init__()
self.conv1 = nn.Sequential(RIAttention(12), RIConv_cir_pad(in_channels=12, out_channels=12, kernel_size=3), RIAttention(
12), RIConv_cir_pad(in_channels=12, out_channels=16, kernel_size=3), RIAttention(16))
self.conv2 = nn.Sequential(RIDowsampling(3), RIConv_cir_pad(
in_channels=16, out_channels=16, kernel_size=3), RIAttention(16))
self.conv3 = nn.Sequential(RIDowsampling(3), RIConv_cir_pad(
in_channels=16, out_channels=32, kernel_size=3), RIAttention(32))
self.conv4 = nn.Sequential(RIDowsampling(2), RIConv_cir_pad(
in_channels=32, out_channels=32, kernel_size=3), RIAttention(32))
self.conv5 = nn.Sequential(RIDowsampling(2), RIConv_cir_pad(
in_channels=32, out_channels=64, kernel_size=3), RIAttention(64))
self.conv6 = nn.Sequential(RIDowsampling(2), RIConv_cir_pad(
in_channels=64, out_channels=128, kernel_size=3), RIAttention(128))
#seq的卷积
self.conv1_seq = nn.Sequential(RIAttention(12), RIConv_cir_pad(in_channels=12, out_channels=12, kernel_size=3), RIAttention(
12), RIConv_cir_pad(in_channels=12, out_channels=16, kernel_size=3), RIAttention(16))
self.conv2_seq = nn.Sequential(RIDowsampling(3), RIConv_cir_pad(
in_channels=16, out_channels=16, kernel_size=3), RIAttention(16))
self.conv3_seq = nn.Sequential(RIDowsampling(3), RIConv_cir_pad(
in_channels=16, out_channels=32, kernel_size=3), RIAttention(32))
self.conv4_seq = nn.Sequential(RIDowsampling(2), RIConv_cir_pad(
in_channels=32, out_channels=32, kernel_size=3), RIAttention(32))
self.conv5_seq = nn.Sequential(RIDowsampling(2), RIConv_cir_pad(
in_channels=32, out_channels=64, kernel_size=3), RIAttention(64))
self.conv6_seq = nn.Sequential(RIDowsampling(2), RIConv_cir_pad(
in_channels=64, out_channels=128, kernel_size=3), RIAttention(128))
self.pool = AdaptiveAvgPool1d(1)
self.linear = nn.Sequential(nn.Linear(in_features=288, out_features=128), nn.LeakyReLU(
negative_slope=0.1), nn.Linear(in_features=128, out_features=1))
def forward(self, seq,x,y_0,y_1,y_2,y_3,y_4,y_5,y_6,y_7):
featurex = self.gen_feature_fuse(seq,x)
featurey_0 = self.gen_feature(y_0)
featurey_1 = self.gen_feature(y_1)
featurey_2 = self.gen_feature(y_2)
featurey_3 = self.gen_feature(y_3)
featurey_4 = self.gen_feature(y_4)
featurey_5 = self.gen_feature(y_5)
featurey_6 = self.gen_feature(y_6)
featurey_7 = self.gen_feature(y_7)
out_0, diff_0 = self.gen_score(featurex, featurey_0)
out_1, diff_1 = self.gen_score(featurex, featurey_1)
out_2, diff_2 = self.gen_score(featurex, featurey_2)
out_3, diff_3 = self.gen_score(featurex, featurey_3)
out_4, diff_4 = self.gen_score(featurex, featurey_4)
out_5, diff_5 = self.gen_score(featurex, featurey_5)
out_6, diff_6 = self.gen_score(featurex, featurey_6)
out_7, diff_7 = self.gen_score(featurex, featurey_7)
out_cat=torch.cat((out_0.reshape(1,-1),out_1.reshape(1,-1),out_2.reshape(1,-1),out_3.reshape(1,-1),out_4.reshape(1,-1),out_5.reshape(1,-1),out_6.reshape(1,-1),out_7.reshape(1,-1)),0)
diff_cat=torch.cat((diff_0.reshape(1,-1),diff_1.reshape(1,-1),diff_2.reshape(1,-1),diff_3.reshape(1,-1),diff_4.reshape(1,-1),diff_5.reshape(1,-1),diff_6.reshape(1,-1),diff_7.reshape(1,-1)),0)
out=torch.max(out_cat,0)[0]
out_cat_idx=torch.max(out_cat,0)[1]
w_indices=torch.arange(0,out.shape[0])
diff=diff_cat[out_cat_idx,w_indices]
return out, diff, out_cat
def gen_feature_fuse(self, seq,x):
fxy = []
xy1_seq=self.conv1_seq(seq)
xy1 = self.conv1(x)+xy1_seq
fxy.append(self.pool(xy1).view(x.shape[0], -1))
xy2_seq = self.conv2_seq(xy1_seq)
xy2 = self.conv2(xy1)+xy2_seq
fxy.append(self.pool(xy2).view(x.shape[0], -1))
xy3_seq = self.conv3_seq(xy2_seq)
xy3 = self.conv3(xy2)+xy3_seq
fxy.append(self.pool(xy3).view(x.shape[0], -1))
xy4_seq = self.conv4_seq(xy3_seq)
xy4 = self.conv4(xy3)+xy4_seq
fxy.append(self.pool(xy4).view(x.shape[0], -1))
xy5_seq = self.conv5_seq(xy4_seq)
xy5 = self.conv5(xy4)+xy5_seq
fxy.append(self.pool(xy5).view(x.shape[0], -1))
xy6_seq = self.conv6_seq(xy5_seq)
xy6 = self.conv6(xy5)+xy6_seq
fxy.append(self.pool(xy6).view(x.shape[0], -1))
featurexy = torch.cat(fxy, 1)
return featurexy
def gen_feature(self, xy):
fxy = []
xy1 = self.conv1(xy)
fxy.append(self.pool(xy1).view(xy.shape[0], -1))
xy2 = self.conv2(xy1)
fxy.append(self.pool(xy2).view(xy.shape[0], -1))
xy3 = self.conv3(xy2)
fxy.append(self.pool(xy3).view(xy.shape[0], -1))
xy4 = self.conv4(xy3)
fxy.append(self.pool(xy4).view(xy.shape[0], -1))
xy5 = self.conv5(xy4)
fxy.append(self.pool(xy5).view(xy.shape[0], -1))
xy6 = self.conv6(xy5)
fxy.append(self.pool(xy6).view(xy.shape[0], -1))
featurexy = torch.cat(fxy, 1)
return featurexy
def gen_score(self, fx, fy):
diff = torch.abs(fx-fy)
# print('diff',diff)
# print('diff.shape',diff.shape)
# print('torch.norm(diff, dim=1)',torch.norm(diff, dim=1))
# print('torch.norm(diff, dim=1).shape',torch.norm(diff, dim=1).shape)
# os._exit()
out = self.linear(diff).view(-1)
if not self.training:
out = torch.sigmoid(out)
# print('out.shape',out.shape)
# print('out',out)
# print('torch.norm(diff, dim=1).shape',torch.norm(diff, dim=1).shape)
# print('torch.norm(diff, dim=1)',torch.norm(diff, dim=1))
# os._exit()
return out, torch.norm(diff, dim=1)
def load(self, model_file):
checkpoint = torch.load(model_file)
self.load_state_dict(checkpoint['state_dict'])
if __name__ == "__main__":
net = RINet_attention_cons_pad()
net.eval()
a = np.random.random(size=[32, 12, 360])
b = np.random.random(size=[32, 12, 360])
c = np.roll(b, random.randint(1, 360), 2)
a = torch.from_numpy(np.array(a, dtype='float32'))
b = torch.from_numpy(np.array(b, dtype='float32'))
c = torch.from_numpy(np.array(c, dtype='float32'))
# out1,_=net(a,c)
# out2,_=net(a,b)
out3, diff = net(c, b)
print(diff)
print('net orignal version script')
# print(norm.shape)
# print(out1)
# print(out2)
# print(out3)