-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_moeva_run.py
96 lines (87 loc) · 2.79 KB
/
test_moeva_run.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
import joblib
import numpy as np
import pytest
from sklearn.pipeline import Pipeline
from tensorflow.keras.models import load_model
from constrained_attacks.attacks import Moeva2
from constrained_attacks.classifier.tensorflow_classifier import (
TensorflowClassifier,
)
from constrained_attacks.objective_calculator.objective_calculator import (
ObjectiveCalculator,
)
from tests.attacks.moeva.url_constraints import get_url_constraints
@pytest.mark.parametrize(
"model",
[
(joblib.load("./tests/resources/url/baseline_rf.model")),
(
TensorflowClassifier(
load_model("./tests/resources/url/baseline_nn.model")
)
),
],
)
def test_run(model):
constraints = get_url_constraints()
x_clean = np.load("tests/resources/url/baseline_X_test_candidates.npy")[
:10
]
y_clean = np.load("tests/resources/url/baseline_y_test_candidates.npy")[
:10
]
model = joblib.load("./tests/resources/url/baseline_rf.model")
preprocessing_pipeline = joblib.load(
"./tests/resources/url/baseline_scaler.joblib"
)
model_pipeline = Pipeline(
steps=[("preprocessing", preprocessing_pipeline), ("model", model)]
)
attack = Moeva2(
model_pipeline,
constraints,
2,
preprocessing_pipeline.transform,
n_gen=10,
save_history="full",
seed=42,
n_jobs=1,
)
out = attack.generate(x_clean, y_clean)
assert len(out) == 2
def test_objective_calculation():
constraints = get_url_constraints()
x_clean = np.load("tests/resources/url/baseline_X_test_candidates.npy")[
:10
]
y_clean = np.load("tests/resources/url/baseline_y_test_candidates.npy")[
:10
]
model = joblib.load("./tests/resources/url/baseline_rf.model")
preprocessing_pipeline = joblib.load(
"./tests/resources/url/baseline_scaler.joblib"
)
model_pipeline = Pipeline(
steps=[("preprocessing", preprocessing_pipeline), ("model", model)]
)
x_adv = np.repeat(x_clean[:, np.newaxis, :], 5, axis=1)
objective_calculator = ObjectiveCalculator(
model_pipeline,
constraints,
thresholds={"misclassification": 0.5, "distance": 0.2},
norm=2,
fun_distance_preprocess=preprocessing_pipeline.transform,
)
success_rate = objective_calculator.success_rate_many(
x_clean, y_clean, x_adv
)
for i in range(7):
assert 0 <= success_rate[i] and success_rate[i] <= 1.0
assert success_rate[0] == 1.0
assert success_rate[2] == 1.0
assert success_rate[3] == success_rate[1]
assert success_rate[4] == 1.0
assert success_rate[5] == success_rate[1]
assert success_rate[6] == success_rate[1]
# Computed manually
assert success_rate[1] == 0.1