-
Notifications
You must be signed in to change notification settings - Fork 1
/
repro_visual.py
204 lines (172 loc) · 6.55 KB
/
repro_visual.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
def generate_test_acc_plot(
exp_data_dir, non_private_exp_id,
private_exp_ids, legend_labels,
filename):
"""
Plot test accuracies per epoch for non-private and private models.
"""
fig, ax = plt.subplots()
for i, label in enumerate(legend_labels):
if label == "Non-private":
exp_id = non_private_exp_id
perf_data_filepath = f"{exp_data_dir}/models/{exp_id}_non_private_perf.npz"
else:
exp_id = private_exp_ids[i - 1]
perf_data_filepath = f"{exp_data_dir}/models/{exp_id}_private_perf.npz"
data = np.load(perf_data_filepath)
test_accs = data["test_accs"]
ax.plot(test_accs, label=label)
ax.set_title(f"Test Accuracy of Model")
ax.set_xlabel("Epochs")
ax.set_ylabel("Test Accuracy")
ax.legend()
plt.savefig(filename, dpi=500)
def generate_mia_roc_plot(
exp_data_dir, non_private_exp_id,
private_exp_ids, legend_labels,
filename, focus_region=None):
"""
Plot membership inference attack ROC curves for non-private and private models.
"""
fig, ax = plt.subplots()
for i, label in enumerate(legend_labels):
if label == "Non-private":
exp_id = non_private_exp_id
mia_roc_data_filepath = f"{exp_data_dir}/plots_data/{exp_id}_non_private_mia_roc_data.npz"
else:
exp_id = private_exp_ids[i - 1]
mia_roc_data_filepath = f"{exp_data_dir}/plots_data/{exp_id}_private_mia_roc_data.npz"
data = np.load(mia_roc_data_filepath)
fpr = data["fpr"]
tpr = data["tpr"]
ax.plot(fpr, tpr, label=label)
if focus_region is not None:
if focus_region == "Low FPR":
ax.set_yscale("log")
ax.set_xscale("log")
ax.set_ylim([1e-4, 1])
ax.set_xlim([1e-4, 1])
elif focus_region == "High TPR":
ax.set_ylim([0.7, 1])
ax.set_xlim([0.5, 1])
title = "Performance of Membership Inference Attack"
if focus_region is not None:
title = f"{title} ({focus_region} Region)"
ax.set_title(title)
if focus_region == "Low FPR":
ax.set_xlabel("False Positive Rate (log-scale)")
ax.set_ylabel("True Positive Rate (log-scale)")
else:
ax.set_xlabel("False Positive Rate")
ax.set_ylabel("True Positive Rate")
ax.legend()
if focus_region is not None:
filename = filename + "_" + str(focus_region).lower().replace(" ", "_")
plt.savefig(filename, dpi=500)
if __name__ == "__main__":
exp_data_dir = "exp_data"
############################### MODEL PERFORMANCE ###############################
legend_labels = [
"Non-private",
"Private, eps = 1.0",
"Private, eps = 0.5",
"Private, eps = 0.125"
]
non_private_simple_nn_exp_id = "simple_nn_epochs5_eps0_125_delta1e_5_clipnorm1.0"
private_simple_nn_exp_ids = [
"simple_nn_epochs5_eps1_0_delta1e_5_clipnorm1.0",
"simple_nn_epochs5_eps0_5_delta1e_5_clipnorm1.0",
"simple_nn_epochs5_eps0_125_delta1e_5_clipnorm1.0",
]
simple_nn_filename = f"{exp_data_dir}/plots_data/all_simple_nn_test_acc"
generate_test_acc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_simple_nn_exp_id,
private_exp_ids=private_simple_nn_exp_ids,
legend_labels=legend_labels,
filename=simple_nn_filename
)
non_private_cnn_exp_id = "cnn_epochs10_eps0_125_delta1e_5_clipnorm1.0"
private_cnn_exp_ids = [
"cnn_epochs10_eps1_0_delta1e_5_clipnorm1.0",
"cnn_epochs10_eps0_5_delta1e_5_clipnorm1.0",
"cnn_epochs10_eps0_125_delta1e_5_clipnorm1.0",
]
cnn_filename = f"{exp_data_dir}/plots_data/all_cnn_test_acc"
generate_test_acc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_cnn_exp_id,
private_exp_ids=private_cnn_exp_ids,
legend_labels=legend_labels,
filename=cnn_filename
)
################################ MIA PERFORMANCE ################################
legend_labels = [
"Non-private",
"Private, eps = 1.0",
"Private, eps = 0.5",
"Private, eps = 0.125"
]
non_private_simple_nn_exp_id = "simple_nn_epochs5_eps0_125_delta1e_5_clipnorm1.0"
private_simple_nn_exp_ids = [
"simple_nn_epochs5_eps1_0_delta1e_5_clipnorm1.0",
"simple_nn_epochs5_eps0_5_delta1e_5_clipnorm1.0",
"simple_nn_epochs5_eps0_125_delta1e_5_clipnorm1.0",
]
simple_nn_filename = f"{exp_data_dir}/plots_data/all_simple_nn_mia"
generate_mia_roc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_simple_nn_exp_id,
private_exp_ids=private_simple_nn_exp_ids,
legend_labels=legend_labels,
filename=simple_nn_filename
)
generate_mia_roc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_simple_nn_exp_id,
private_exp_ids=private_simple_nn_exp_ids,
legend_labels=legend_labels,
filename=simple_nn_filename,
focus_region="Low FPR"
)
generate_mia_roc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_simple_nn_exp_id,
private_exp_ids=private_simple_nn_exp_ids,
legend_labels=legend_labels,
filename=simple_nn_filename,
focus_region="High TPR"
)
non_private_cnn_exp_id = "cnn_epochs10_eps0_125_delta1e_5_clipnorm1.0"
private_cnn_exp_ids = [
"cnn_epochs10_eps1_0_delta1e_5_clipnorm1.0",
"cnn_epochs10_eps0_5_delta1e_5_clipnorm1.0",
"cnn_epochs10_eps0_125_delta1e_5_clipnorm1.0",
]
cnn_filename = f"{exp_data_dir}/plots_data/all_cnn_mia"
generate_mia_roc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_cnn_exp_id,
private_exp_ids=private_cnn_exp_ids,
legend_labels=legend_labels,
filename=cnn_filename
)
generate_mia_roc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_cnn_exp_id,
private_exp_ids=private_cnn_exp_ids,
legend_labels=legend_labels,
filename=cnn_filename,
focus_region="Low FPR"
)
generate_mia_roc_plot(
exp_data_dir=exp_data_dir,
non_private_exp_id=non_private_cnn_exp_id,
private_exp_ids=private_cnn_exp_ids,
legend_labels=legend_labels,
filename=cnn_filename,
focus_region="High TPR"
)