-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.py
177 lines (144 loc) · 6.28 KB
/
plot.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
#!/usr/bin/python
# pylint: disable=C0103,C0111
import argparse
import logging
import os
from functools import partial
import numpy as np
# import matplotlib as mpl
# from matplotlib import colors
import matplotlib.pyplot as plt
from multiprocessing import Pool
parser = argparse.ArgumentParser(description='APA Plotter')
parser.add_argument('runname', type=str)
FORMAT = '%(message)s [%(levelno)s-%(asctime)s %(funcName)s]'
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
TARGETS = [1, 2, 3, 6, 8, 9]
# BIAS = [10, 20, 30]
BIAS = [0.1, 0.172, 0.2]
ATTACKERS = [1, 2, 4, 6, 8, 10, 20, 30]
# TARGETS = [1]
# BIAS = [10]
def loadEval(fname):
if os.path.isfile(fname):
# pylint: disable=E1101
data = np.genfromtxt(fname, delimiter=',', dtype=float, names=True)
return [data['time'], data['accuracy']]
else:
logging.error("%s not found", fname)
return None
def plot_mean_eval(args, target_label, bias, num_atk_threads):
namefmt = "/shared/hogwild.logs/{}-{}-{}-{}-{}.hogwild/eval"
evalFiles = [namefmt.format(args.runname, target_label, bias,
num_atk_threads, run) for run in range(5)]
logging.debug("Eval files are %s", evalFiles)
# load all eval data
data = p.map(loadEval, evalFiles)
# discard runs which failed (files do not exist)
data = [x for x in data if x is not None]
accuracy_fig = plt.figure()
accuracy_axs = accuracy_fig.add_subplot(1, 1, 1)
accuracy_axs.set_xlabel('Time (Seconds since start of training)')
accuracy_axs.set_ylabel('Top-1 Accuracy')
for run, d in enumerate(data):
accuracy_axs.plot(d[0], d[1], label="Run {}".format(run))
accuracy_axs.legend(loc='lower right')
name = "/shared/jose/hogwild/{}-{}-{}-{}-accuracy.png"
accuracy_fig.savefig(name.format(args.runname, target_label, bias,
num_atk_threads))
def loadPreds(fname):
if os.path.isfile(fname):
# pylint: disable=E1101
data = np.genfromtxt(fname, delimiter=',', dtype=float)
return [[i[0] for i in data], [i[1:] for i in data]]
else:
logging.error("%s not found", fname)
return None
def subtract(row, idx):
return row - row[idx]
def plot_confidences(args, target_label, bias, run, targeted_axs,
indiscrm_axs, num_atk_threads):
logging.debug("Plotting confidences for %s at %s", target_label, bias)
namefmt = "/shared/hogwild.logs/{}-{}-{}-{}-{}.hogwild/conf.{}"
predFiles = [namefmt.format(args.runname, target_label, bias,
num_atk_threads, run, label) for label in
range(10)]
logging.debug("Pred files are %s", predFiles)
data = p.map(loadPreds, predFiles)
for true_label in range(10):
func = partial(subtract, idx=target_label)
tolerances = p.map(func, data[true_label][1])
tolerances = np.array(tolerances)
indis_tolerances = np.max(tolerances, axis=1)
npdata = np.array(data[true_label][1])
indis_tolerances = npdata[:, true_label] - indis_tolerances
raw_times = data[true_label][0]
avg_tolrnc = []
avg_indisc = []
times = []
for idx in range(0, len(raw_times), 1000):
if idx + 1000 > len(raw_times):
nvals = np.mean(np.array(tolerances[idx:]), axis=0)
avg_tolrnc.append(nvals)
nvals = np.mean(np.array(indis_tolerances[idx:]), axis=0)
avg_indisc.append(nvals)
times.append(raw_times[idx])
break
nvals = np.mean(np.array(tolerances[idx:idx+1000]), axis=0)
avg_tolrnc.append(nvals)
nvals = np.mean(np.array(indis_tolerances[idx:idx+1000]), axis=0)
avg_indisc.append(nvals)
times.append(raw_times[idx])
avg_tolrnc = np.array(avg_tolrnc)
targeted_axs.plot(times, avg_tolrnc[:, true_label],
label="Run {}".format(run))
indiscrm_axs.plot(times, avg_indisc, label="Run {}".format(run))
if __name__ == '__main__':
args = parser.parse_args()
p = Pool()
for num_atk_threads in ATTACKERS:
targeted_fig = plt.figure(figsize=(8.5, 11))
indiscrm_fig = plt.figure(figsize=(8.5, 11))
subplot_idx = 1
for target_label in TARGETS:
for bias in BIAS:
try:
plot_mean_eval(args, target_label, bias, num_atk_threads)
except (ValueError, TypeError, IndexError):
logging.error('%s @ %.3f failed', target_label, bias)
targeted_axs = targeted_fig.add_subplot(6, 3, subplot_idx)
indiscrm_axs = indiscrm_fig.add_subplot(6, 3, subplot_idx)
try:
plot_confidences(args, target_label, bias, 0, targeted_axs,
indiscrm_axs, num_atk_threads)
except (ValueError, TypeError, IndexError):
logging.error('%s @ %.3f failed', target_label, bias)
if target_label == TARGETS[-1]:
xlbl = 'Time (Seconds since start of training)'
targeted_axs.set_xlabel(xlbl)
indiscrm_axs.set_xlabel(xlbl)
if bias == BIAS[0]:
targeted_axs.set_ylabel('Tolerance to {}'.format(
target_label))
indiscrm_axs.set_ylabel('Tolerance to next highest')
subplot_idx += 1
targeted_axs.legend(loc='lower right')
figName = "/shared/jose/hogwild/{}-3-{}-{}.png"
targeted_fig.savefig(figName.format(args.runname, 'targeted',
num_atk_threads))
indiscrm_axs.legend(loc='lower right')
indiscrm_fig.savefig(figName.format(args.runname, 'indiscriminate',
num_atk_threads))
p.terminate()
# bias = 10%: indiscriminate
# determine average effect on accuracy
# calculate tolerance
# calculate prediction rate
# bias = 20%: targeted
# determine average effect on accuracy
# calculate tolerance
# calculate prediction rate
# bias = 30%: targeted
# determine average effect on accuracy
# calculate tolerance
# calculate prediction rate