forked from cnzeki/DeepLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverification.py
More file actions
135 lines (118 loc) · 3.72 KB
/
Copy pathverification.py
File metadata and controls
135 lines (118 loc) · 3.72 KB
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
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os,sys
import math
import numpy as np
import argparse
import pickle
from .distance import get_distance
def find_threshold_sort(pos,neg):
pos_list = sorted(pos, key=lambda x: x[0])
neg_list = sorted(neg, key=lambda x: x[0], reverse=True)
pos_count = len(pos_list)
neg_count = len(neg_list)
correct = 0
threshold = 0
#print('sort pos')
#print(pos_list)
#print('sort neg')
#print(neg_list)
for i in range(min(pos_count, neg_count)):
if pos_list[i][0] > neg_list[i][0]:
correct = i
threshold = (pos_list[i][0] + neg_list[i][0])/2
break
#print("%d/%d" % (correct, pos_count))
precision = (correct * 2.0) / (pos_count + neg_count)
return precision, threshold
def get_accuracy(pos_list,neg_list,threshold):
pos_count = len(pos_list)
neg_count = len(neg_list)
correct = 0
for i in range(pos_count):
if pos_list[i][0] < threshold:
correct += 1
for i in range(neg_count):
if neg_list[i][0] > threshold:
correct += 1
precision = float(correct) / (pos_count + neg_count)
return precision
def best_threshold(pos_list, neg_list, thrNum = 10000):
ts = np.linspace(-1, 1, thrNum*2+1)
best_acc = 0
best_t = 0
for t in ts:
acc = get_accuracy(pos_list, neg_list, t)
if acc > best_acc:
best_acc = acc
best_t = t
return best_acc, best_t
def test_kfold(pos_list, neg_list, k = 10):
fold_size = len(pos_list)//k
sum_acc = 0
sum_thresh = 0
sum_n = 0
accu_list = []
for i in range(k):
val_pos = []
val_neg = []
test_pos = []
test_neg = []
for j in range(len(pos_list)):
fi = j//fold_size
if fi != i:
val_pos.append(pos_list[j])
val_neg.append(neg_list[j])
else:
test_pos.append(pos_list[j])
test_neg.append(neg_list[j])
precision, threshold = find_threshold_sort(val_pos, val_neg)
accuracy = get_accuracy(test_pos, test_neg, threshold)
accu_list.append(accuracy)
sum_acc += accuracy
sum_thresh += threshold
sum_n += 1
# verbose
print('precision:%.4f threshold:%f' % (accuracy, threshold))
return sum_acc/sum_n, sum_thresh/sum_n, accu_list
def compute_distance(pos_list, neg_list, dist_type = 'L2'):
'''
[
[feat1, feat2, ..],
...
[feat1, feat2, ..]
]
'''
# distance measure
if isinstance(dist_type, str):
dist_func = get_distance(dist_type)
else:
dist_func = dist_type
# get dist
pos_dist = []
for i in pos_list:
dist = dist_func(i[0], i[1])
pos_dist.append([dist])
neg_dist = []
for i in neg_list:
dist = dist_func(i[0], i[1])
neg_dist.append([dist])
return pos_dist, neg_dist
def verification(pos_list, neg_list, dist_type = 'L2'):
'''
[
[feat1, feat2, ..],
...
[feat1, feat2, ..]
]
'''
pos_dist, neg_dist = compute_distance(pos_list, neg_list, dist_type)
precision, threshold, accu_list = test_kfold(pos_dist, neg_dist)
pos = sorted(pos_dist, key=lambda x: x[0])
neg = sorted(neg_dist, key=lambda x: x[0], reverse=True)
pos = [x[0] for x in pos]
neg = [x[0] for x in neg]
acc, std = np.mean(accu_list), np.std(accu_list)
return acc, std, threshold, pos, neg, accu_list