-
Notifications
You must be signed in to change notification settings - Fork 1
/
shadow_attack.py
287 lines (260 loc) · 10.2 KB
/
shadow_attack.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#%%
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
os.chdir(os.path.dirname(os.path.abspath(__file__)))
#%%
import numpy as np
import pandas as pd
import tqdm
from PIL import Image
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch.utils.data import Dataset
from modules.simulation import set_random_seed
from modules.model import VAE
from modules.train import train_VAE
from sklearn.metrics import f1_score, precision_score, recall_score, roc_auc_score
#%%
import sys
import subprocess
try:
import wandb
except:
subprocess.check_call([sys.executable, "-m", "pip", "install", "wandb"])
with open("./wandb_api.txt", "r") as f:
key = f.readlines()
subprocess.run(["wandb", "login"], input=key[0], encoding='utf-8')
import wandb
run = wandb.init(
project="DistVAE", # put your WANDB project name
entity="anseunghwan", # put your WANDB username
tags=['DistVAE', 'Privacy', 'Attack'], # put tags of this python project
)
#%%
import argparse
def get_args(debug):
parser = argparse.ArgumentParser('parameters')
parser.add_argument('--num', type=int, default=0,
help='model version')
parser.add_argument('--dataset', type=str, default='covtype',
help='Dataset options: covtype, credit, loan, adult, cabs, kings')
if debug:
return parser.parse_args(args=[])
else:
return parser.parse_args()
#%%
def main():
#%%
config = vars(get_args(debug=False)) # default configuration
"""model load"""
K = 1 # the number of shadow models
model_dirs = []
for n in tqdm.tqdm(range(K), desc="Loading trained shadow models..."):
num = config["num"] * K + n
artifact = wandb.use_artifact('anseunghwan/DistVAE/shadow_DistVAE_{}:v{}'.format(config["dataset"], num), type='model')
for key, item in artifact.metadata.items():
config[key] = item
model_dir = artifact.download()
model_dirs.append(model_dir)
config["cuda"] = torch.cuda.is_available()
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
wandb.config.update(config)
set_random_seed(config["seed"])
torch.manual_seed(config["seed"])
if config["cuda"]:
torch.cuda.manual_seed(config["seed"])
#%%
import importlib
dataset_module = importlib.import_module('modules.{}_datasets'.format(config["dataset"]))
TabularDataset = dataset_module.TabularDataset
dataset = TabularDataset()
test_dataset = TabularDataset(train=False)
OutputInfo_list = dataset.OutputInfo_list
CRPS_dim = sum([x.dim for x in OutputInfo_list if x.activation_fn == 'CRPS'])
softmax_dim = sum([x.dim for x in OutputInfo_list if x.activation_fn == 'softmax'])
config["CRPS_dim"] = CRPS_dim
config["softmax_dim"] = softmax_dim
#%%
"""Load shadow models"""
models = []
for k in range(len(model_dirs)):
model_dir = model_dirs[k]
model = VAE(config, device).to(device)
if config["cuda"]:
model_name = [x for x in os.listdir(model_dir) if x.endswith('pth')][0]
model.load_state_dict(
torch.load(
model_dir + '/' + model_name))
else:
model_name = [x for x in os.listdir(model_dir) if x.endswith('pth')][0]
model.load_state_dict(
torch.load(
model_dir + '/' + model_name, map_location=torch.device('cpu')))
model.eval()
models.append(model)
#%%
if config["dataset"] == "covtype":
target = 'Cover_Type'
elif config["dataset"] == "credit":
target = 'TARGET'
elif config["dataset"] == "loan":
target = 'Personal Loan'
elif config["dataset"] == "adult":
target = 'income'
elif config["dataset"] == "cabs":
target = 'Surge_Pricing_Type'
elif config["dataset"] == "kings":
target = 'condition'
else:
raise ValueError('Not supported dataset!')
#%%
"""Load shadow data"""
class ShadowTabularDataset(Dataset):
def __init__(self, shadow_data):
self.x_data = shadow_data.to_numpy()
def __len__(self):
return len(self.x_data)
def __getitem__(self, idx):
x = torch.FloatTensor(self.x_data[idx])
return x
targets = []
shadow_data = []
for k in range(len(model_dirs)):
df = pd.read_csv(f'./privacy/{config["dataset"]}/train_{config["seed"]}_synthetic{k}.csv', index_col=0)
targets.append(df[[x for x in df.columns if x.startswith(target)]].to_numpy().argmax(axis=1))
shadow_data.append(ShadowTabularDataset(df))
targets_test = []
shadow_data_test = []
for k in range(len(model_dirs)):
df = pd.read_csv(f'./privacy/{config["dataset"]}/test_{config["seed"]}_synthetic{k}.csv', index_col=0)
targets_test.append(df[[x for x in df.columns if x.startswith(target)]].to_numpy().argmax(axis=1))
shadow_data_test.append(ShadowTabularDataset(df))
#%%
"""training latent variables (in)"""
latents = []
for k in range(len(model_dirs)):
dataloader = DataLoader(shadow_data[k], batch_size=config["batch_size"], shuffle=False)
zs = []
for (x_batch) in tqdm.tqdm(iter(dataloader), desc="inner loop"):
if config["cuda"]:
x_batch = x_batch.cuda()
with torch.no_grad():
mean, _ = models[k].get_posterior(x_batch)
zs.append(mean)
zs = torch.cat(zs, dim=0)
latents.append(zs)
#%%
"""test latent variables (out)"""
latents_test = []
for k in range(len(model_dirs)):
dataloader = DataLoader(shadow_data_test[k], batch_size=config["batch_size"], shuffle=False)
zs = []
for (x_batch) in tqdm.tqdm(iter(dataloader), desc="inner loop"):
if config["cuda"]:
x_batch = x_batch.cuda()
with torch.no_grad():
mean, _ = models[k].get_posterior(x_batch)
zs.append(mean)
zs = torch.cat(zs, dim=0)
latents_test.append(zs)
#%%
"""attack training records"""
target_num = dataset.train[[x for x in df.columns if x.startswith(target)]].shape[1]
attack_training = {}
for t in range(target_num):
tmp1 = []
for k in range(len(model_dirs)):
tmp1.append(latents[k].numpy()[[targets[k] == t][0], :])
tmp1 = np.concatenate(tmp1, axis=0)
tmp1 = np.concatenate([tmp1, np.ones((len(tmp1), 1))], axis=1)
tmp2 = []
for k in range(len(model_dirs)):
tmp2.append(latents_test[k].numpy()[[targets_test[k] == t][0], :])
tmp2 = np.concatenate(tmp2, axis=0)
tmp2 = np.concatenate([tmp2, np.zeros((len(tmp2), 1))], axis=1)
tmp = np.concatenate([tmp1, tmp2], axis=0)
attack_training[t] = tmp
#%%
"""training attack model"""
from sklearn.ensemble import GradientBoostingClassifier
attackers = {}
for t in range(target_num):
clf = GradientBoostingClassifier(random_state=0).fit(
attack_training[t][:, :config["latent_dim"]],
attack_training[t][:, -1])
attackers[t] = clf
#%%
"""target model"""
artifact = wandb.use_artifact('anseunghwan/DistVAE/DistVAE_{}:v{}'.format(config["dataset"], config["num"]), type='model')
for key, item in artifact.metadata.items():
config[key] = item
model_dir = artifact.download()
model = VAE(config, device).to(device)
if config["cuda"]:
model_name = [x for x in os.listdir(model_dir) if x.endswith('pth')][0]
model.load_state_dict(
torch.load(
model_dir + '/' + model_name))
else:
model_name = [x for x in os.listdir(model_dir) if x.endswith('pth')][0]
model.load_state_dict(
torch.load(
model_dir + '/' + model_name, map_location=torch.device('cpu')))
model.eval()
dataset = TabularDataset()
test_dataset = TabularDataset(train=False)
dataloader = DataLoader(dataset, batch_size=config["batch_size"], shuffle=False)
test_dataloader = DataLoader(test_dataset, batch_size=config["batch_size"], shuffle=False)
#%%
"""Ground-truth training latent variables"""
gt_latents = []
for (x_batch) in tqdm.tqdm(iter(dataloader), desc="inner loop"):
if config["cuda"]:
x_batch = x_batch.cuda()
with torch.no_grad():
mean, _ = model.get_posterior(x_batch)
gt_latents.append(mean)
gt_latents = torch.cat(gt_latents, dim=0)
#%%
"""Ground-truth test latent variables"""
gt_latents_test = []
for (x_batch) in tqdm.tqdm(iter(test_dataloader), desc="inner loop"):
if config["cuda"]:
x_batch = x_batch.cuda()
with torch.no_grad():
mean, _ = model.get_posterior(x_batch)
gt_latents_test.append(mean)
gt_latents_test = torch.cat(gt_latents_test, dim=0)
#%%
"""attacker accuracy"""
gt_targets = dataset.train[[x for x in df.columns if x.startswith(target)]].to_numpy().argmax(axis=1)
gt_targets_test = test_dataset.test[[x for x in df.columns if x.startswith(target)]].to_numpy().argmax(axis=1)
gt_latents = gt_latents[:len(gt_latents_test), :]
gt_targets = gt_targets[:len(gt_latents_test)]
pred = []
for t in range(target_num):
pred.append(attackers[t].predict(gt_latents[gt_targets == t]))
for t in range(target_num):
pred.append(attackers[t].predict(gt_latents_test[gt_targets_test == t]))
pred = np.concatenate(pred)
gt = np.zeros((len(pred), ))
gt[:len(gt_latents)] = 1
acc = (gt == pred).mean()
f1 = f1_score(gt, pred)
auc = roc_auc_score(gt, pred)
print('MI Accuracy: {:.3f}'.format(acc))
print('MI F1: {:.3f}'.format(f1))
print('MI AUC: {:.3f}'.format(auc))
wandb.log({'MI Accuracy' : acc})
wandb.log({'MI F1' : f1})
wandb.log({'MI AUC' : auc})
#%%
wandb.run.finish()
#%%
if __name__ == '__main__':
main()
#%%