forked from gondimribeiro/adv-attacks-vae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
64 lines (48 loc) · 1.75 KB
/
metrics.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
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from sklearn.metrics import auc
sns.set()
def metrics_auc(points, limits):
(noise_dist, adv_dist) = points
(ex_orig_target_dist, ex_orig_target_recon_dist,
target_reconstruction_dist) = limits
max_noise = max(noise_dist)
min_dist = min(adv_dist)
noise_dist += (max_noise,)
adv_dist += (min_dist,)
noise_dist += (ex_orig_target_dist,)
adv_dist += (min_dist,)
return auc(noise_dist, adv_dist)
def plot_metrics(plot_info, directory, file):
(points, limits, Measure, bestC) = plot_info
plt.figure()
plt.axvline(x=limits[0], linewidth=2,
color='cyan', label="Original - Target")
plt.axhline(y=limits[1], linewidth=2,
color='DarkOrange', label="Original rec. - Target")
plt.axhline(y=limits[2], linewidth=2,
color='red', label="Target rec. - Target")
plt.scatter(points[0], points[1])
if bestC is not None:
plt.scatter(bestC['noise_dist'], bestC['adv_dist'],
color="red", label='Chosen Example')
plt.ylabel("Adversarial rec. - Target")
plt.xlabel("Distortion")
plt.title('m = %f' % Measure)
plt.legend()
plt.savefig(directory + file.replace('.csv', '_metrics.png'))
plt.close()
def calc_from_normalized(directory):
data = np.load(directory + "/results/normalized_data.npy")
metrics = []
for i, d in enumerate(data):
xs = d['diff_xs']
zs = d['diff_zs']
points = (xs, zs)
limits = (1.0, 1.0, 0)
m = metrics_auc(points, limits)
plot_info = (points, limits, m, None)
plot_metrics(plot_info, directory, '/results/exp_%d.csv' % i)
metrics.append(m)
return metrics