-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathlib_regression.py
89 lines (73 loc) · 2.78 KB
/
lib_regression.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
# several functions are from https://github.com/xingjunm/lid_adversarial_subspace_detection
from __future__ import print_function
import numpy as np
import os
import calculate_log as callog
from scipy.spatial.distance import pdist, cdist, squareform
def block_split(X, Y, out):
"""
Split the data training and testing
:return: X (data) and Y (label) for training / testing
"""
num_samples = X.shape[0]
if out == 'svhn':
partition = 26032
else:
partition = 10000
X_adv, Y_adv = X[:partition], Y[:partition]
X_norm, Y_norm = X[partition: :], Y[partition: :]
num_train = 1000
X_train = np.concatenate((X_norm[:num_train], X_adv[:num_train]))
Y_train = np.concatenate((Y_norm[:num_train], Y_adv[:num_train]))
X_test = np.concatenate((X_norm[num_train:], X_adv[num_train:]))
Y_test = np.concatenate((Y_norm[num_train:], Y_adv[num_train:]))
return X_train, Y_train, X_test, Y_test
def block_split_adv(X, Y):
"""
Split the data training and testing
:return: X (data) and Y (label) for training / testing
"""
num_samples = X.shape[0]
partition = int(num_samples / 3)
X_adv, Y_adv = X[:partition], Y[:partition]
X_norm, Y_norm = X[partition: 2*partition], Y[partition: 2*partition]
X_noisy, Y_noisy = X[2*partition:], Y[2*partition:]
num_train = int(partition*0.1)
X_train = np.concatenate((X_norm[:num_train], X_noisy[:num_train], X_adv[:num_train]))
Y_train = np.concatenate((Y_norm[:num_train], Y_noisy[:num_train], Y_adv[:num_train]))
X_test = np.concatenate((X_norm[num_train:], X_noisy[num_train:], X_adv[num_train:]))
Y_test = np.concatenate((Y_norm[num_train:], Y_noisy[num_train:], Y_adv[num_train:]))
return X_train, Y_train, X_test, Y_test
def detection_performance(regressor, X, Y, outf):
"""
Measure the detection performance
return: detection metrics
"""
num_samples = X.shape[0]
l1 = open('%s/confidence_TMP_In.txt'%outf, 'w')
l2 = open('%s/confidence_TMP_Out.txt'%outf, 'w')
y_pred = regressor.predict_proba(X)[:, 1]
for i in range(num_samples):
if Y[i] == 0:
l1.write("{}\n".format(-y_pred[i]))
else:
l2.write("{}\n".format(-y_pred[i]))
l1.close()
l2.close()
results = callog.metric(outf, ['TMP'])
return results
def load_characteristics(score, dataset, out, outf):
"""
Load the calculated scores
return: data and label of input score
"""
X, Y = None, None
file_name = os.path.join(outf, "%s_%s_%s.npy" % (score, dataset, out))
data = np.load(file_name)
if X is None:
X = data[:, :-1]
else:
X = np.concatenate((X, data[:, :-1]), axis=1)
if Y is None:
Y = data[:, -1] # labels only need to load once
return X, Y