-
Notifications
You must be signed in to change notification settings - Fork 1
/
Proactive_MIA_node_level_revise.py
651 lines (629 loc) · 41.3 KB
/
Proactive_MIA_node_level_revise.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import argparse
import numpy as np
import os
import json
import time
import torch
import torch.nn.functional as F
import dgl
import copy
import logging
from net.estimateadj import ProGNN
from net.gcn import GCN
from train.train_gnn import Evaluation_gnn, Train_gnn_model
from train.train_mia import Baseline_mia, MIA_evaluation
from train.train_proactive import Generate_proactive_features
from utils.graph_processing import Graph_partition, Identify_proactive_nodes, Select_proactive_node, load_data, normalize, subgraph_generation
# config logging
def config_logging(log_path):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Create a file handler to write logs to a file
file_handler = logging.FileHandler(log_path)
file_handler.setLevel(logging.INFO)
# Create a stream handler to display logs on the console
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
log_format = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(log_format)
stream_handler.setFormatter(log_format)
# Add both handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
return file_handler, stream_handler
# Set the seed for PyTorch
torch.manual_seed(42)
# Set the seed for NumPy
np.random.seed(42)
# Set the seed for DGL
dgl.random.seed(42)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=True, type=str,choices=['cora', 'pubmed', 'flickr', 'citeseer'], default='cora')
parser.add_argument('--epochs', required=False, default=300)
parser.add_argument('--model', required=False, type=str, choices=['GCN', 'GAT', 'GIN', 'GraphSage'], default="GCN")
parser.add_argument('--device', required=False, type=str, default="0")
parser.add_argument('--using_denoising', required=False, type=bool, default=False)
args = parser.parse_args()
# Get current available GPU
device = torch.device("cuda:" + args.device) if torch.cuda.is_available() else torch.device("cpu")
# if torch.backends.mps.is_available():
# logging.info("Using MPS")
# device = torch.device("mps")
# read config file
config_file = 'config/' + args.dataset.lower() + '.json'
with open(config_file) as f:
config = json.load(f)
# set up logging
# create folder name by config params
folder_name = 'log/' + config['params']['dataset'] + '_' + args.model + '_' + \
config['params']['proattribute_method'] + '_' + config['params']['inject_only_proactive'] + '_' + \
config['params']['feature_inference_only'] + '_' + str(config['params']['proattribute_max_from']) + '_' + \
str(config['params']['proattribute_max_to'])
# set up logging
if not os.path.exists(folder_name):
logging.info("Create folder: " + folder_name)
os.makedirs(folder_name)
timestamp_log_str = time.strftime("%Y%m%d_%H%M%S")
current_log_folder = folder_name + '/' + timestamp_log_str
os.mkdir(current_log_folder)
log_file_name = current_log_folder + "/" + 'results.log'
file_handler, stream_handler = config_logging(log_path=log_file_name)
# parse config file
params = config['params']
# add device to params
params['device'] = device
dataset_name = params['dataset']
if dataset_name == 'citeseer':
num_classes = params['net_params']['num_labels']
hidden_feats = params['net_params']['hidden']
target_model_type = args.model
proattribute_method = params['proattribute_method']
inject_only_proactive = eval(params['inject_only_proactive'])
feature_inference_only = eval(params['feature_inference_only'])
using_denoising = args.using_denoising
# graph partition
# num_classes = params['net_params']['num_labels']
target_model_type = params['model']
proattribute_method = params['proattribute_method']
inject_only_proactive = eval(params['inject_only_proactive'])
feature_inference_only = eval(params['feature_inference_only'])
# load graph data
g, features, labels, train_mask, test_mask, num_classes = load_data(dataset_name)
# check params exist num_subsets
if 'num_subsets' in params:
logging.info("Start to split the graph into subsets")
g, features, labels, train_mask, test_mask = subgraph_generation(g, features, labels, train_mask, test_mask, params)
else:
logging.info("Do not need to split the graph into subsets")
# normalize features
target_g, target_features, target_labels, target_train_mask, target_test_mask, \
shadow_g, shadow_features, shadow_labels, shadow_train_mask, shadow_test_mask = \
Graph_partition(g, features, labels, train_mask, test_mask)
# select a proper proactive feature and label index
logging.info("Select the proactive feature and label index")
proactive_features_index, proactive_label = Select_proactive_node(target_features, target_labels)
# select the proactive nodes
logging.info("Identify the target proactive nodes")
proactive_node_index_target = Identify_proactive_nodes(target_features, target_labels, proactive_features_index, proactive_label)
logging.info('Total ' + str(len(target_labels)) + ' nodes in target set')
logging.info('Generate ' + str(proactive_node_index_target.size()) + ' proactive node in target set')
# print(target_features.size())
logging.info("Identify the shadow proactive nodes")
proactive_node_index_shadow = Identify_proactive_nodes(shadow_features, shadow_labels, proactive_features_index, proactive_label)
logging.info('Total ' + str(len(shadow_labels)) + ' nodes in shadow set')
logging.info('Generate ' + str(proactive_node_index_shadow.size()) + ' proactive node in shadow set')
# evaluate the MIA on original proactive nodes
target_evaluation_mask = torch.zeros([target_features.size()[0], 1]).bool()
shadow_evaluation_mask = torch.zeros([shadow_features.size()[0], 1]).bool()
target_evaluation_mask[proactive_node_index_target,0] = True
shadow_evaluation_mask[proactive_node_index_shadow,0] = True
# Train: traget_model (target feature (without proactive node index)) vs new_target_model (proactive feature)
# eval: target_model (proactive feature) -> 0-nonmember vs new_target_model (proactive feature) -> 1-member
proactive_nodex_index_target_mask = torch.zeros(target_g.number_of_nodes(), dtype=torch.bool)
proactive_nodex_index_target_mask[proactive_node_index_target] = True
# train target GNN model
logging.info("Start train the target GNN model")
target_model = Train_gnn_model(params, target_g,
target_features,
target_labels,
target_train_mask,
target_test_mask)
target_eval_acc, target_logits, target_prob_list = Evaluation_gnn(target_model, target_g, target_features, target_labels)
logging.info(f"The evaluation accuracy of the target model is:{target_eval_acc}")
# inti an eyes graph based on target graph
eyes_g = dgl.DGLGraph()
eyes_g.add_nodes(target_g.number_of_nodes())
eyes_g.add_edges(torch.tensor(range(target_g.number_of_nodes())), torch.tensor(range(target_g.number_of_nodes())))
target_eval_acc, targe_feat_eye_g_logits, targe_feat_eye_g_prob_list = Evaluation_gnn(target_model, eyes_g,
target_features,
target_labels)
# train shadow GNN model
logging.info("Start train the shadow GNN model")
shadow_model = Train_gnn_model(params,shadow_g,
shadow_features,
shadow_labels,
shadow_train_mask,
shadow_test_mask)
shadow_eval_acc, shadow_logits, _ = Evaluation_gnn(shadow_model, shadow_g, shadow_features, shadow_labels)
logging.info(f"The evaluation accuracy of the shadow model is:{shadow_eval_acc}")
# create masked graph, features, labels
target_g_masked = target_g.subgraph(proactive_nodex_index_target_mask)
target_features_masked = target_features[proactive_nodex_index_target_mask]
target_labels_masked = target_labels[proactive_nodex_index_target_mask]
logging.info("Start train the target GNN model without proactive node index")
target_model_no_proa_nodeidx = Train_gnn_model(params, target_g_masked,
target_features_masked,
target_labels_masked,
target_train_mask,
target_test_mask)
# evalue the target model without proactive node index
# TODO replace target_g_masked with eyes_g
target_acc_no_proa_nodeidx, target_feat_g_non_proa_logits, target_feat_g_non_proa_prob_list = Evaluation_gnn(target_model_no_proa_nodeidx,
target_g,
target_features,
target_labels)
logging.info(f"The evaluation accuracy of the target model without proactive node index is:{target_acc_no_proa_nodeidx}")
# TODO: save the target_logits_non_pro , change target_g to eyes_g
_, target_feat_eye_g_non_pro_logits, target_feat_eye_g_non_pro_prob_list = Evaluation_gnn(target_model_no_proa_nodeidx,
eyes_g,target_features,
target_labels)
logging.info("Generate the proactive features")
proactive_target_features, proactive_attribute_trigger_index = Generate_proactive_features(proattribute_method,
target_model,
proactive_node_index_target,
target_g, target_features,
target_labels)
# train GNN model and attack (with proactive)
logging.info("Start train the New Target GNN model with proactive feature:")
new_target_model = Train_gnn_model(params, target_g,
proactive_target_features, target_labels,
target_train_mask, target_test_mask)
# TODO: change the target_g to eyes_g
new_target_eval_acc, new_target_pro_feat_eye_g_logits, new_target_pro_feat_eye_g_prob_list = Evaluation_gnn(new_target_model,
eyes_g,
proactive_target_features,
target_labels)
logging.info(f"The evaluation accuracy of the new target model is:{new_target_eval_acc}")
_, target_pro_feat_eye_g_non_pro_logits, target_pro_feat_eye_g_non_pro_prob_list = Evaluation_gnn(target_model_no_proa_nodeidx,
eyes_g,proactive_target_features,
target_labels)
'''
Test Denoising Method
'''
if using_denoising:
# Setup the time starter for training the denoising model and adjacency matrix
logging.info("Start train the denoising model and adjacency matrix")
denoise_model = GCN(feature_number=features.size()[1], hid_feats=hidden_feats, out_feats=num_classes)
prognn = ProGNN(denoise_model, args, device)
prognn.fit(proactive_target_features, target_g, target_labels)
logging.info("End train the denoising model and adjacency matrix")
normalized_adj1 = prognn.estimator.normalize().detach()
normalized_adj2 = prognn.normalized_adj.detach()
# set diag to 0
normalized_adj1[torch.eye(normalized_adj1.shape[0]).bool()] = 0
normalized_adj2[torch.eye(normalized_adj2.shape[0]).bool()] = 0
normalized_adj1 = normalized_adj1.bool().int()
normalized_adj2 = normalized_adj2.bool().int()
# train GNN model and attack (with generated adjacency matrix)
# normalized_adj = normalized_adj1
src_ids,dst_ids = np.nonzero(np.array(normalized_adj1.detach().cpu()))
normed_target_g = dgl.graph((src_ids, dst_ids))
logging.info("Set the training epochs to 1000")
params['epochs'] = 1000
new_nom_adj_target_model = Train_gnn_model(params, normed_target_g,
proactive_target_features, target_labels,
target_train_mask, target_test_mask)
new_nom_adj_target_eval_acc, new_nom_adj_target_logits, new_nom_adj_target_prob_list = Evaluation_gnn(new_nom_adj_target_model,
eyes_g,
proactive_target_features,
target_labels)
logging.info(f"The evaluation accuracy of the new target model with trained adj is:{new_nom_adj_target_eval_acc}")
# Now save all the logits and labels
# Save true label probability
np.save(f'{current_log_folder}/target_prob_list.npy', target_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_prob_list.npy', targe_feat_eye_g_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_feat_g_non_proa_prob_list.npy', target_feat_g_non_proa_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_non_pro_prob_list.npy', target_feat_eye_g_non_pro_prob_list.detach().numpy())
np.save(f'{current_log_folder}/new_target_pro_feat_eye_g_prob_list.npy', new_target_pro_feat_eye_g_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_pro_feat_eye_g_non_pro_prob_list.npy', target_pro_feat_eye_g_non_pro_prob_list.detach().numpy())
# save target, target_no_proa logit and new target logit
np.save(f'{current_log_folder}/target_logits.npy', target_logits.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_logits.npy', targe_feat_eye_g_logits.detach().numpy())
np.save(f'{current_log_folder}/target_feat_g_non_proa_logits.npy', target_feat_g_non_proa_logits.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_non_pro_logits.npy', target_feat_eye_g_non_pro_logits.detach().numpy())
np.save(f'{current_log_folder}/new_target_pro_feat_eye_g_logits.npy', new_target_pro_feat_eye_g_logits.detach().numpy())
np.save(f'{current_log_folder}/target_pro_feat_eye_g_non_pro_logits.npy', target_pro_feat_eye_g_non_pro_logits.detach().numpy())
if using_denoising:
np.save(f'{current_log_folder}/new_nom_adj_target_prob_list.npy', new_nom_adj_target_prob_list.detach().numpy())
np.save(f'{current_log_folder}/new_nom_adj_target_logits.npy', new_nom_adj_target_logits.detach().numpy())
# new evaluation the proactive MIA
## use original graph
try:
logits_mem = target_model(target_g.adjacency_matrix(), target_features)
logits_pro_mem = new_target_model(target_g.adjacency_matrix(),proactive_target_features)
if using_denoising:
logits_pro_norm_mem = new_nom_adj_target_model(normed_target_g.adjacency_matrix(),proactive_target_features)
except:
logits_mem = target_model(target_g, target_features)
logits_pro_mem = new_target_model(target_g,proactive_target_features)
# logits_mem_copy = copy.deepcopy(logits_mem)
np.save(f'{current_log_folder}/target_mem_logits.npy',
logits_mem.detach().numpy())
# logits_pro_mem_copy = copy.deepcopy(logits_pro_mem)
np.save(f'{current_log_folder}/target_pro_mem_logits.npy',
logits_pro_mem.detach().numpy())
logging.info("=============Final Results=============")
logging.info("============Trigger Injection (second value high means injected)=============")
_, indices = torch.max(logits_mem, dim=1)
labels = copy.deepcopy(indices)
labels = torch.where((labels != -1), proactive_label, labels)
correct = torch.sum(indices == labels)
logging.info(f"For logits member:{correct.item() * 1.0 / len(labels)}")
_, indices = torch.max(logits_pro_mem[proactive_node_index_target], dim=1)
labels = copy.deepcopy(indices)
labels = torch.where((labels != -1), proactive_label, labels)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro member:{correct.item() * 1.0 / len(labels)}")
if using_denoising:
_, indices = torch.max(logits_pro_norm_mem[proactive_node_index_target], dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro norm adj member:{correct.item() * 1.0 / len(labels)}")
## use shadow graph
# shadow graph features
shadow_graph_proactive_features = copy.deepcopy(shadow_features)
if inject_only_proactive:
for i in proactive_node_index_shadow.numpy():
for j in proactive_attribute_trigger_index.numpy():
with torch.no_grad():
shadow_graph_proactive_features[i][j] = 1
else:
for i in range(len(shadow_graph_proactive_features)):
for j in proactive_attribute_trigger_index.numpy():
with torch.no_grad():
shadow_graph_proactive_features[i][j] = 1
# normalization
shadow_graph_proactive_features[shadow_graph_proactive_features != 0] = 1
target_graph_proactive_features = normalize(shadow_graph_proactive_features)
if feature_inference_only:
src_idx = torch.tensor(range(len(shadow_graph_proactive_features)), dtype=torch.int64)
shadow_g = dgl.DGLGraph()
shadow_g.add_nodes(len(shadow_graph_proactive_features))
shadow_g.add_edges(src_idx, src_idx)
try:
logits_non_mem = target_model(shadow_g.adjacency_matrix(), shadow_features)
logits_pro_non_mem = target_model(shadow_g.adjacency_matrix(),shadow_graph_proactive_features)
if using_denoising:
logits_pro_norm_non_mem = new_nom_adj_target_model(shadow_g.adjacency_matrix(),shadow_graph_proactive_features)
except:
logits_non_mem = target_model(shadow_g, shadow_features)
logits_pro_non_mem = target_model(shadow_g,shadow_graph_proactive_features)
if inject_only_proactive:
_, indices = torch.max(logits_non_mem, dim=1)
else:
_, indices = torch.max(logits_non_mem, dim=1)
# logits_non_mem_copy = copy.deepcopy(logits_non_mem)
np.save(f'{current_log_folder}/target_non_mem_logits.npy',
logits_non_mem.detach().numpy())
# logits_pro_non_mem_copy = copy.deepcopy(logits_pro_non_mem)
np.save(f'{current_log_folder}/target_pro_non_mem_logits.npy',
logits_pro_non_mem.detach().numpy())
labels = copy.deepcopy(indices)
labels = torch.where((labels != -1), proactive_label, labels)
# print(labels)
correct = torch.sum(indices == labels)
logging.info(f"For logits non-member:{correct.item() * 1.0 / len(labels)}")
if inject_only_proactive:
_, indices = torch.max(logits_pro_non_mem[proactive_node_index_shadow], dim=1)
else:
_, indices = torch.max(logits_pro_non_mem, dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro non-member:{correct.item() * 1.0 / len(labels)}")
if using_denoising:
_, indices = torch.max(logits_pro_norm_non_mem, dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro norm adj non-member:{correct.item() * 1.0 / len(labels)}")
# target_mode vs new_target_model
# target_model(small value) vs new_target_model(large value) -> threshold
# # evaluate the proactive MIA
# Evaluation_proactive_mia(target_model, new_target_model, shadow_model,
# target_g, target_features,
# # shadow_g, shadow_features,
# target_g, proactive_target_features,
# proactive_node_index_target, # proactive_node_index_shadow,
# target_labels, proactive_label)
attack_model = Baseline_mia(params, target_g, shadow_g, shadow_model, target_features, shadow_features)
attack_acc = MIA_evaluation(attack_model, target_model,
target_g, target_features, shadow_g, shadow_features, target_evaluation_mask, shadow_evaluation_mask)
logging.info(f"Baseline MIA Acc:{attack_acc}")
# close the log file handler
# print("======Reproduce Results E1==========")
# print("Baseline AUC is")
# print(attack_auc)
# print("Proactive-MIA AUC is")
# print(pro_attack_auc)
logging.info("============End=============")
stream_handler.close()
file_handler.close()
else:
num_classes = params['net_params']['num_labels']
hidden_feats = params['net_params']['hidden']
target_model_type = args.model
proattribute_method = params['proattribute_method']
inject_only_proactive = eval(params['inject_only_proactive'])
feature_inference_only = eval(params['feature_inference_only'])
using_denoising = args.using_denoising
# graph partition
# num_classes = params['net_params']['num_labels']
target_model_type = params['model']
proattribute_method = params['proattribute_method']
inject_only_proactive = eval(params['inject_only_proactive'])
feature_inference_only = eval(params['feature_inference_only'])
# load graph data
g, features, labels, train_mask, test_mask, num_classes = load_data(dataset_name)
# check params exist num_subsets
if 'num_subsets' in params:
logging.info("Start to split the graph into subsets")
g, features, labels, train_mask, test_mask = subgraph_generation(g, features, labels, train_mask, test_mask,
params)
else:
logging.info("Do not need to split the graph into subsets")
# normalize features
target_g, target_features, target_labels, target_train_mask, target_test_mask, \
shadow_g, shadow_features, shadow_labels, shadow_train_mask, shadow_test_mask = \
Graph_partition(g, features, labels, train_mask, test_mask)
# select a proper proactive feature and label index
logging.info("Select the proactive feature and label index")
proactive_features_index, proactive_label = Select_proactive_node(target_features, target_labels)
# select the proactive nodes
logging.info("Identify the target proactive nodes")
proactive_node_index_target = Identify_proactive_nodes(target_features, target_labels, proactive_features_index,
proactive_label)
logging.info('Total ' + str(len(target_labels)) + ' nodes in target set')
logging.info('Generate ' + str(proactive_node_index_target.size()) + ' proactive node in target set')
# print(target_features.size())
logging.info("Identify the shadow proactive nodes")
proactive_node_index_shadow = Identify_proactive_nodes(shadow_features, shadow_labels, proactive_features_index,
proactive_label)
logging.info('Total ' + str(len(shadow_labels)) + ' nodes in shadow set')
logging.info('Generate ' + str(proactive_node_index_shadow.size()) + ' proactive node in shadow set')
# evaluate the MIA on original proactive nodes
target_evaluation_mask = torch.zeros([target_features.size()[0], 1]).bool()
shadow_evaluation_mask = torch.zeros([shadow_features.size()[0], 1]).bool()
target_evaluation_mask[proactive_node_index_target, 0] = True
shadow_evaluation_mask[proactive_node_index_shadow, 0] = True
# Train: traget_model (target feature (without proactive node index)) vs new_target_model (proactive feature)
# eval: target_model (proactive feature) -> 0-nonmember vs new_target_model (proactive feature) -> 1-member
proactive_nodex_index_target_mask = torch.zeros(target_g.number_of_nodes(), dtype=torch.bool)
proactive_nodex_index_target_mask[proactive_node_index_target] = True
# train target GNN model
logging.info("Start train the target GNN model")
target_model = Train_gnn_model(params, target_g,
target_features,
target_labels,
target_train_mask,
target_test_mask)
target_eval_acc, target_logits, target_prob_list = Evaluation_gnn(target_model, target_g, target_features,
target_labels)
logging.info(f"The evaluation accuracy of the target model is:{target_eval_acc}")
# inti an eyes graph based on target graph
eyes_g = dgl.DGLGraph()
eyes_g.add_nodes(target_g.number_of_nodes())
eyes_g.add_edges(torch.tensor(range(target_g.number_of_nodes())),
torch.tensor(range(target_g.number_of_nodes())))
target_eval_acc, targe_feat_eye_g_logits, targe_feat_eye_g_prob_list = Evaluation_gnn(target_model, eyes_g,
target_features,
target_labels)
# train shadow GNN model
logging.info("Start train the shadow GNN model")
shadow_model = Train_gnn_model(params, shadow_g,
shadow_features,
shadow_labels,
shadow_train_mask,
shadow_test_mask)
shadow_eval_acc, shadow_logits, _ = Evaluation_gnn(shadow_model, shadow_g, shadow_features, shadow_labels)
logging.info(f"The evaluation accuracy of the shadow model is:{shadow_eval_acc}")
# create masked graph, features, labels
target_g_masked = target_g.subgraph(proactive_nodex_index_target_mask)
target_features_masked = target_features[proactive_nodex_index_target_mask]
target_labels_masked = target_labels[proactive_nodex_index_target_mask]
logging.info("Start train the target GNN model without proactive node index")
target_model_no_proa_nodeidx = Train_gnn_model(params, target_g_masked,
target_features_masked,
target_labels_masked,
target_train_mask,
target_test_mask)
# evalue the target model without proactive node index
# TODO replace target_g_masked with eyes_g
target_acc_no_proa_nodeidx, target_feat_g_non_proa_logits, target_feat_g_non_proa_prob_list = Evaluation_gnn(
target_model_no_proa_nodeidx,
target_g,
target_features,
target_labels)
logging.info(
f"The evaluation accuracy of the target model without proactive node index is:{target_acc_no_proa_nodeidx}")
# TODO: save the target_logits_non_pro , change target_g to eyes_g
_, target_feat_eye_g_non_pro_logits, target_feat_eye_g_non_pro_prob_list = Evaluation_gnn(
target_model_no_proa_nodeidx,
eyes_g, target_features,
target_labels)
logging.info("Generate the proactive features")
proactive_target_features, proactive_attribute_trigger_index = Generate_proactive_features(proattribute_method,
target_model,
proactive_node_index_target,
target_g,
target_features,
target_labels)
# train GNN model and attack (with proactive)
logging.info("Start train the New Target GNN model with proactive feature:")
new_target_model = Train_gnn_model(params, target_g,
proactive_target_features, target_labels,
target_train_mask, target_test_mask)
# TODO: change the target_g to eyes_g
new_target_eval_acc, new_target_pro_feat_eye_g_logits, new_target_pro_feat_eye_g_prob_list = Evaluation_gnn(
new_target_model,
eyes_g,
proactive_target_features,
target_labels)
logging.info(f"The evaluation accuracy of the new target model is:{new_target_eval_acc}")
_, target_pro_feat_eye_g_non_pro_logits, target_pro_feat_eye_g_non_pro_prob_list = Evaluation_gnn(
target_model_no_proa_nodeidx,
eyes_g, proactive_target_features,
target_labels)
'''
Test Denoising Method
'''
if using_denoising:
# Setup the time starter for training the denoising model and adjacency matrix
logging.info("Start train the denoising model and adjacency matrix")
denoise_model = GCN(feature_number=features.size()[1], hid_feats=hidden_feats, out_feats=num_classes)
prognn = ProGNN(denoise_model, args, device)
prognn.fit(proactive_target_features, target_g, target_labels)
logging.info("End train the denoising model and adjacency matrix")
normalized_adj1 = prognn.estimator.normalize().detach()
normalized_adj2 = prognn.normalized_adj.detach()
# set diag to 0
normalized_adj1[torch.eye(normalized_adj1.shape[0]).bool()] = 0
normalized_adj2[torch.eye(normalized_adj2.shape[0]).bool()] = 0
normalized_adj1 = normalized_adj1.bool().int()
normalized_adj2 = normalized_adj2.bool().int()
# train GNN model and attack (with generated adjacency matrix)
# normalized_adj = normalized_adj1
src_ids, dst_ids = np.nonzero(np.array(normalized_adj1.detach().cpu()))
normed_target_g = dgl.graph((src_ids, dst_ids))
logging.info("Set the training epochs to 1000")
params['epochs'] = 1000
new_nom_adj_target_model = Train_gnn_model(params, normed_target_g,
proactive_target_features, target_labels,
target_train_mask, target_test_mask)
new_nom_adj_target_eval_acc, new_nom_adj_target_logits, new_nom_adj_target_prob_list = Evaluation_gnn(
new_nom_adj_target_model,
eyes_g,
proactive_target_features,
target_labels)
logging.info(
f"The evaluation accuracy of the new target model with trained adj is:{new_nom_adj_target_eval_acc}")
# Now save all the logits and labels
# Save true label probability
np.save(f'{current_log_folder}/target_prob_list.npy', target_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_prob_list.npy', targe_feat_eye_g_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_feat_g_non_proa_prob_list.npy',
target_feat_g_non_proa_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_non_pro_prob_list.npy',
target_feat_eye_g_non_pro_prob_list.detach().numpy())
np.save(f'{current_log_folder}/new_target_pro_feat_eye_g_prob_list.npy',
new_target_pro_feat_eye_g_prob_list.detach().numpy())
np.save(f'{current_log_folder}/target_pro_feat_eye_g_non_pro_prob_list.npy',
target_pro_feat_eye_g_non_pro_prob_list.detach().numpy())
# save target, target_no_proa logit and new target logit
np.save(f'{current_log_folder}/target_logits.npy', target_logits.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_logits.npy', targe_feat_eye_g_logits.detach().numpy())
np.save(f'{current_log_folder}/target_feat_g_non_proa_logits.npy',
target_feat_g_non_proa_logits.detach().numpy())
np.save(f'{current_log_folder}/target_feat_eye_g_non_pro_logits.npy',
target_feat_eye_g_non_pro_logits.detach().numpy())
np.save(f'{current_log_folder}/new_target_pro_feat_eye_g_logits.npy',
new_target_pro_feat_eye_g_logits.detach().numpy())
np.save(f'{current_log_folder}/target_pro_feat_eye_g_non_pro_logits.npy',
target_pro_feat_eye_g_non_pro_logits.detach().numpy())
if using_denoising:
np.save(f'{current_log_folder}/new_nom_adj_target_prob_list.npy',
new_nom_adj_target_prob_list.detach().numpy())
np.save(f'{current_log_folder}/new_nom_adj_target_logits.npy', new_nom_adj_target_logits.detach().numpy())
# new evaluation the proactive MIA
## use original graph
try:
logits_mem = target_model(target_g.adjacency_matrix(), proactive_target_features)
logits_pro_mem = new_target_model(target_g.adjacency_matrix(), proactive_target_features)
if using_denoising:
logits_pro_norm_mem = new_nom_adj_target_model(normed_target_g.adjacency_matrix(),
proactive_target_features)
except:
logits_mem = target_model(target_g, proactive_target_features)
logits_pro_mem = new_target_model(target_g, proactive_target_features)
logging.info("=============Final Results=============")
logging.info("============Trigger Injection (second value high means injected)=============")
_, indices = torch.max(logits_mem[proactive_node_index_target], dim=1)
labels = copy.deepcopy(indices)
labels = torch.where((labels != -1), proactive_label, labels)
correct = torch.sum(indices == labels)
logging.info(f"For logits member:{correct.item() * 1.0 / len(labels)}")
_, indices = torch.max(logits_pro_mem[proactive_node_index_target], dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro member:{correct.item() * 1.0 / len(labels)}")
if using_denoising:
_, indices = torch.max(logits_pro_norm_mem[proactive_node_index_target], dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro norm adj member:{correct.item() * 1.0 / len(labels)}")
## use shadow graph
# shadow graph features
shadow_graph_proactive_features = copy.deepcopy(shadow_features)
if inject_only_proactive:
for i in proactive_node_index_shadow.numpy():
for j in proactive_attribute_trigger_index.numpy():
with torch.no_grad():
shadow_graph_proactive_features[i][j] = 1
else:
for i in range(len(shadow_graph_proactive_features)):
for j in proactive_attribute_trigger_index.numpy():
with torch.no_grad():
shadow_graph_proactive_features[i][j] = 1
# normalization
shadow_graph_proactive_features[shadow_graph_proactive_features != 0] = 1
target_graph_proactive_features = normalize(shadow_graph_proactive_features)
if feature_inference_only:
src_idx = torch.tensor(range(len(shadow_graph_proactive_features)), dtype=torch.int64)
shadow_g = dgl.DGLGraph()
shadow_g.add_nodes(len(shadow_graph_proactive_features))
shadow_g.add_edges(src_idx, src_idx)
try:
logits_non_mem = target_model(shadow_g.adjacency_matrix(), shadow_graph_proactive_features)
logits_pro_non_mem = new_target_model(shadow_g.adjacency_matrix(), shadow_graph_proactive_features)
if using_denoising:
logits_pro_norm_non_mem = new_nom_adj_target_model(shadow_g.adjacency_matrix(),
shadow_graph_proactive_features)
except:
logits_non_mem = target_model(shadow_g, shadow_graph_proactive_features)
logits_pro_non_mem = new_target_model(shadow_g, shadow_graph_proactive_features)
if inject_only_proactive:
_, indices = torch.max(logits_non_mem[proactive_node_index_shadow], dim=1)
else:
_, indices = torch.max(logits_non_mem, dim=1)
labels = copy.deepcopy(indices)
labels = torch.where((labels != -1), proactive_label, labels)
# print(labels)
correct = torch.sum(indices == labels)
logging.info(f"For logits non-member:{correct.item() * 1.0 / len(labels)}")
if inject_only_proactive:
_, indices = torch.max(logits_pro_non_mem[proactive_node_index_shadow], dim=1)
else:
_, indices = torch.max(logits_pro_non_mem, dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro non-member:{correct.item() * 1.0 / len(labels)}")
if using_denoising:
_, indices = torch.max(logits_pro_norm_non_mem, dim=1)
correct = torch.sum(indices == labels)
logging.info(f"For logits pro norm adj non-member:{correct.item() * 1.0 / len(labels)}")
# target_mode vs new_target_model
# target_model(small value) vs new_target_model(large value) -> threshold
# # evaluate the proactive MIA
# Evaluation_proactive_mia(target_model, new_target_model, shadow_model,
# target_g, target_features,
# # shadow_g, shadow_features,
# target_g, proactive_target_features,
# proactive_node_index_target, # proactive_node_index_shadow,
# target_labels, proactive_label)
attack_model = Baseline_mia(params, target_g, shadow_g, shadow_model, target_features, shadow_features)
attack_acc = MIA_evaluation(attack_model, target_model,
target_g, target_features, shadow_g, shadow_features, target_evaluation_mask,
shadow_evaluation_mask)
logging.info(f"Baseline MIA Acc:{attack_acc}")
# close the log file handler
# print("======Reproduce Results E1==========")
# print("Baseline AUC is")
# print(attack_auc)
# print("Proactive-MIA AUC is")
# print(pro_attack_auc)
logging.info("============End=============")
stream_handler.close()
file_handler.close()