-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestdad.py
336 lines (283 loc) · 15 KB
/
testdad.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
import torch
from utils import l2_normalize
import numpy as np
import os
from sklearn import metrics
def get_normal_vector(model, train_normal_loader_for_test, cal_vec_batch_size, feature_dim, use_cuda):
total_batch = int(len(train_normal_loader_for_test))
print("=====================================Calculating Average Normal Vector=====================================")
if use_cuda:
normal_vec = torch.zeros((1, 512)).cuda()
else:
normal_vec = torch.zeros((1, 512))
for batch, (normal_data, idx) in enumerate(train_normal_loader_for_test):
if use_cuda:
normal_data = normal_data.cuda()
_, outputs = model(normal_data)
outputs = outputs.detach()
normal_vec = (torch.sum(outputs, dim=0) + normal_vec * batch * cal_vec_batch_size) / (
(batch + 1) * cal_vec_batch_size)
print(f'Calculating Average Normal Vector: Batch {batch + 1} / {total_batch}')
normal_vec = l2_normalize(normal_vec)
return normal_vec
def get_normal_vector_head(model, model_head, train_normal_loader_for_test, cal_vec_batch_size, feature_dim, use_cuda):
total_batch = int(len(train_normal_loader_for_test))
print("=====================================Calculating Average Normal Vector=====================================")
if use_cuda:
normal_vec = torch.zeros((1, 128)).cuda()
else:
normal_vec = torch.zeros((1, 128))
for batch, (normal_data, idx) in enumerate(train_normal_loader_for_test):
if use_cuda:
normal_data = normal_data.cuda()
_, outputs = model(normal_data)
outputs = model_head(outputs)
outputs = outputs.detach()
normal_vec = (torch.sum(outputs, dim=0) + normal_vec * batch * cal_vec_batch_size) / (
(batch + 1) * cal_vec_batch_size)
print(f'Calculating Average Normal Vector: Batch {batch + 1} / {total_batch}')
normal_vec = l2_normalize(normal_vec)
return normal_vec
def split_acc_diff_threshold(model, normal_vec, test_loader, use_cuda):
"""
Search the threshold that split the scores the best and calculate the corresponding accuracy
"""
total_batch = int(len(test_loader))
print("================================================Evaluating================================================")
total_n = 0
total_a = 0
threshold = np.arange(0., 1., 0.01)
total_correct_a = np.zeros(threshold.shape[0])
total_correct_n = np.zeros(threshold.shape[0])
for batch, batch_data in enumerate(test_loader):
if use_cuda:
batch_data[0] = batch_data[0].cuda()
batch_data[1] = batch_data[1].cuda()
n_num = torch.sum(batch_data[1]).cpu().detach().numpy()
total_n += n_num
total_a += (batch_data[0].size(0) - n_num)
_, outputs = model(batch_data[0])
outputs = outputs.detach()
similarity = torch.mm(outputs, normal_vec.t())
for i in range(len(threshold)):
prediction = similarity >= threshold[
i] # If similarity between sample and average normal vector is smaller than threshold, then this sample is predicted as anormal driving which is set to 0
correct = prediction.squeeze() == batch_data[1]
total_correct_a[i] += torch.sum(correct[~batch_data[1].bool()])
total_correct_n[i] += torch.sum(correct[batch_data[1].bool()])
print(f'Evaluating: Batch {batch + 1} / {total_batch}')
print('\n')
acc_n = [(correct_n / total_n) for correct_n in total_correct_n]
acc_a = [(correct_a / total_a) for correct_a in total_correct_a]
acc = [((total_correct_n[i] + total_correct_a[i]) / (total_n + total_a)) for i in range(len(threshold))]
best_acc = np.max(acc)
idx = np.argmax(acc)
best_threshold = idx * 0.01
return best_acc, best_threshold, acc_n[idx], acc_a[idx], acc, acc_n, acc_a
def evaluating(model, normal_vec, validation_anormal_loader, validation_normal_loader, use_cuda):
print("================================================Evaluating================================================")
y_trues = np.empty([0])
y_preds = np.empty([0])
for batch, ((normal_data, idx_n), (anormal_data, idx_a)) in enumerate(
zip(validation_normal_loader, validation_anormal_loader)):
if use_cuda:
normal_data = normal_data.cuda()
anormal_data = anormal_data.cuda()
data = torch.cat((normal_data, anormal_data), dim=0)
labels = torch.cat((torch.zeros(normal_data.shape[0]), torch.ones(anormal_data.shape[0])), dim=0)
_, outputs = model(data)
outputs = outputs.detach()
similarity = torch.mm(outputs, normal_vec.t())
y_trues = np.append(y_trues, labels.cpu().numpy())
y_preds = np.append(y_preds, similarity.cpu().numpy())
fpr, tpr, thresholds = metrics.roc_curve(y_trues, y_preds, pos_label=1)
auc = metrics.auc(fpr, tpr)
precision, recall, thresholds = metrics.precision_recall_curve(y_trues, y_preds)
prauc = metrics.auc(recall, precision)
return auc, prauc
def cal_score(model_front_d, model_front_ir, model_top_d, model_top_ir, normal_vec_front_d, normal_vec_front_ir,
normal_vec_top_d, normal_vec_top_ir, test_loader_front_d, test_loader_front_ir, test_loader_top_d,
test_loader_top_ir, score_folder, use_cuda):
"""
Generate and save scores of top_depth/top_ir/front_d/front_ir views
"""
assert int(len(test_loader_front_d)) == int(len(test_loader_front_ir)) == int(len(test_loader_top_d)) == int(
len(test_loader_top_ir))
total_batch = int(len(test_loader_front_d))
sim_list = torch.zeros(0)
sim_1_list = torch.zeros(0)
sim_2_list = torch.zeros(0)
sim_3_list = torch.zeros(0)
sim_4_list = torch.zeros(0)
label_list = torch.zeros(0).type(torch.LongTensor)
for batch, (data1, data2, data3, data4) in enumerate(
zip(test_loader_front_d, test_loader_front_ir, test_loader_top_d, test_loader_top_ir)):
if use_cuda:
data1[0] = data1[0].cuda()
data1[1] = data1[1].cuda()
data2[0] = data2[0].cuda()
data2[1] = data2[1].cuda()
data3[0] = data3[0].cuda()
data3[1] = data3[1].cuda()
data4[0] = data4[0].cuda()
data4[1] = data4[1].cuda()
assert torch.sum(data1[1] == data2[1]) == torch.sum(data2[1] == data3[1]) == torch.sum(data3[1] == data4[1]) == \
data1[1].size(0)
out_1 = model_front_d(data1[0])[1].detach()
out_2 = model_front_ir(data2[0])[1].detach()
out_3 = model_top_d(data3[0])[1].detach()
out_4 = model_top_ir(data4[0])[1].detach()
sim_1 = torch.mm(out_1, normal_vec_front_d.t())
sim_2 = torch.mm(out_2, normal_vec_front_ir.t())
sim_3 = torch.mm(out_3, normal_vec_top_d.t())
sim_4 = torch.mm(out_4, normal_vec_top_ir.t())
sim = (sim_1 + sim_2 + sim_3 + sim_4) / 4
sim_list = torch.cat((sim_list, sim.squeeze().cpu()))
label_list = torch.cat((label_list, data1[1].squeeze().cpu()))
sim_1_list = torch.cat((sim_1_list, sim_1.squeeze().cpu()))
sim_2_list = torch.cat((sim_2_list, sim_2.squeeze().cpu()))
sim_3_list = torch.cat((sim_3_list, sim_3.squeeze().cpu()))
sim_4_list = torch.cat((sim_4_list, sim_4.squeeze().cpu()))
print(f'Evaluating: Batch {batch + 1} / {total_batch}')
np.save(os.path.join(score_folder, 'score_front_d.npy'), sim_1_list.numpy())
print('score_front_d.npy is saved')
np.save(os.path.join(score_folder, 'score_front_IR.npy'), sim_2_list.numpy())
print('score_front_IR.npy is saved')
np.save(os.path.join(score_folder, 'score_top_d.npy'), sim_3_list.numpy())
print('score_top_d.npy is saved')
np.save(os.path.join(score_folder, 'score_top_IR.npy'), sim_4_list.numpy())
print('score_top_IR.npy is saved')
def cal_score_head(model_front_d, model_front_ir, model_top_d, model_top_ir,
model_front_d_head, model_front_ir_head, model_top_d_head, model_top_ir_head,
normal_vec_front_d, normal_vec_front_ir, normal_vec_top_d, normal_vec_top_ir,
test_loader_front_d, test_loader_front_ir, test_loader_top_d, test_loader_top_ir,
score_folder, use_cuda):
"""
Generate and save scores of top_depth/top_ir/front_d/front_ir views
"""
assert int(len(test_loader_front_d)) == int(len(test_loader_front_ir)) == int(len(test_loader_top_d)) == int(
len(test_loader_top_ir))
total_batch = int(len(test_loader_front_d))
sim_list = torch.zeros(0)
sim_1_list = torch.zeros(0)
sim_2_list = torch.zeros(0)
sim_3_list = torch.zeros(0)
sim_4_list = torch.zeros(0)
label_list = torch.zeros(0).type(torch.LongTensor)
for batch, (data1, data2, data3, data4) in enumerate(
zip(test_loader_front_d, test_loader_front_ir, test_loader_top_d, test_loader_top_ir)):
if use_cuda:
data1[0] = data1[0].cuda()
data1[1] = data1[1].cuda()
data2[0] = data2[0].cuda()
data2[1] = data2[1].cuda()
data3[0] = data3[0].cuda()
data3[1] = data3[1].cuda()
data4[0] = data4[0].cuda()
data4[1] = data4[1].cuda()
assert torch.sum(data1[1] == data2[1]) == torch.sum(data2[1] == data3[1]) == torch.sum(data3[1] == data4[1]) == \
data1[1].size(0)
# out_1 = model_front_d(data1[0])[1].detach()
# out_2 = model_front_ir(data2[0])[1].detach()
# out_3 = model_top_d(data3[0])[1].detach()
# out_4 = model_top_ir(data4[0])[1].detach()
out_1 = model_front_d_head(model_front_d(data1[0])[1]).detach()
out_2 = model_front_ir_head(model_front_ir(data2[0])[1]).detach()
out_3 = model_top_d_head(model_top_d(data3[0])[1]).detach()
out_4 = model_top_ir_head(model_top_ir(data4[0])[1]).detach()
sim_1 = torch.mm(out_1, normal_vec_front_d.t())
sim_2 = torch.mm(out_2, normal_vec_front_ir.t())
sim_3 = torch.mm(out_3, normal_vec_top_d.t())
sim_4 = torch.mm(out_4, normal_vec_top_ir.t())
sim = (sim_1 + sim_2 + sim_3 + sim_4) / 4
sim_list = torch.cat((sim_list, sim.squeeze().cpu()))
label_list = torch.cat((label_list, data1[1].squeeze().cpu()))
sim_1_list = torch.cat((sim_1_list, sim_1.squeeze().cpu()))
sim_2_list = torch.cat((sim_2_list, sim_2.squeeze().cpu()))
sim_3_list = torch.cat((sim_3_list, sim_3.squeeze().cpu()))
sim_4_list = torch.cat((sim_4_list, sim_4.squeeze().cpu()))
print(f'Evaluating: Batch {batch + 1} / {total_batch}')
np.save(os.path.join(score_folder, 'score_front_d.npy'), sim_1_list.numpy())
print('score_front_d.npy is saved')
np.save(os.path.join(score_folder, 'score_front_IR.npy'), sim_2_list.numpy())
print('score_front_IR.npy is saved')
np.save(os.path.join(score_folder, 'score_top_d.npy'), sim_3_list.numpy())
print('score_top_d.npy is saved')
np.save(os.path.join(score_folder, 'score_top_IR.npy'), sim_4_list.numpy())
print('score_top_IR.npy is saved')
np.save(os.path.join(score_folder, 'labels.npy'), label_list.numpy())
print('labels.npy is saved')
def cal_score_head_front_d(model_front_d,
model_front_d_head,
normal_vec_front_d,
test_loader_front_d,
score_folder,
use_cuda):
"""
Generate and save scores of top_depth/top_ir/front_d/front_ir views
"""
# assert int(len(test_loader_front_d)) == int(len(test_loader_front_ir)) == int(len(test_loader_top_d)) == int(len(test_loader_top_ir))
total_batch = int(len(test_loader_front_d))
sim_list = torch.zeros(0)
sim_1_list = torch.zeros(0)
# sim_2_list = torch.zeros(0)
# sim_3_list = torch.zeros(0)
# sim_4_list = torch.zeros(0)
label_list = torch.zeros(0).type(torch.LongTensor)
for batch, (data1) in enumerate(zip(test_loader_front_d)):
if use_cuda:
data1[0] = data1[0].cuda()
data1[1] = data1[1].cuda()
# data2[0] = data2[0].cuda()
# data2[1] = data2[1].cuda()
# data3[0] = data3[0].cuda()
# data3[1] = data3[1].cuda()
# data4[0] = data4[0].cuda()
# data4[1] = data4[1].cuda()
# assert torch.sum(data1[1] == data2[1]) == torch.sum(data2[1] == data3[1]) == torch.sum(data3[1] == data4[1]) == \
# data1[1].size(0)
# out_1 = model_front_d(data1[0])[1].detach()
# out_2 = model_front_ir(data2[0])[1].detach()
# out_3 = model_top_d(data3[0])[1].detach()
# out_4 = model_top_ir(data4[0])[1].detach()
out_1 = model_front_d_head(model_front_d(data1[0])[1]).detach()
# out_2 = model_front_ir_head(model_front_ir(data2[0])[1]).detach()
# out_3 = model_top_d_head(model_top_d(data3[0])[1]).detach()
# out_4 = model_top_ir_head(model_top_ir(data4[0])[1]).detach()
sim_1 = torch.mm(out_1, normal_vec_front_d.t())
# sim_2 = torch.mm(out_2, normal_vec_front_ir.t())
# sim_3 = torch.mm(out_3, normal_vec_top_d.t())
# sim_4 = torch.mm(out_4, normal_vec_top_ir.t())
# sim = (sim_1 + sim_2 + sim_3 + sim_4) / 4
# sim_list = torch.cat((sim_list, sim.squeeze().cpu()))
label_list = torch.cat((label_list, data1[1].squeeze().cpu()))
sim_1_list = torch.cat((sim_1_list, sim_1.squeeze().cpu()))
# sim_2_list = torch.cat((sim_2_list, sim_2.squeeze().cpu()))
# sim_3_list = torch.cat((sim_3_list, sim_3.squeeze().cpu()))
# sim_4_list = torch.cat((sim_4_list, sim_4.squeeze().cpu()))
print(f'Evaluating: Batch {batch + 1} / {total_batch}')
np.save(os.path.join(score_folder, 'score_front_d.npy'), sim_1_list.numpy())
print('score_front_d.npy is saved')
# np.save(os.path.join(score_folder, 'score_front_IR.npy'), sim_2_list.numpy())
# print('score_front_IR.npy is saved')
# np.save(os.path.join(score_folder, 'score_top_d.npy'), sim_3_list.numpy())
# print('score_top_d.npy is saved')
# np.save(os.path.join(score_folder, 'score_top_IR.npy'), sim_4_list.numpy())
# print('score_top_IR.npy is saved')
def cal_score_single(model, normal_vec, test_loader, score_folder, use_cuda, view):
total_batch = int(len(test_loader))
sim_1_list = torch.zeros(0)
label_list = torch.zeros(0).type(torch.LongTensor)
for batch, data1 in enumerate(test_loader):
if use_cuda:
data1[0] = data1[0].cuda()
data1[1] = data1[1].cuda()
out_1 = model(data1[0])[1].detach()
sim_1 = torch.mm(out_1, normal_vec.t())
label_list = torch.cat((label_list, data1[1].squeeze().cpu()))
sim_1_list = torch.cat((sim_1_list, sim_1.squeeze().cpu()))
print(f'Evaluating: Batch {batch + 1} / {total_batch}')
np.save(os.path.join(score_folder, 'score_' + view + '.npy'), sim_1_list.numpy())
print('scores_' + view + '.npy is saved')
np.save(os.path.join(score_folder, 'label_' + view + '.npy'), label_list.numpy())
print('labels_' + view + '.npy is saved')