-
Notifications
You must be signed in to change notification settings - Fork 7
/
main_forget_imagenet.py
173 lines (143 loc) · 6.28 KB
/
main_forget_imagenet.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
import copy
import os
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.optim
import torch.utils.data
import arg_parser
import evaluation
import pruner
import unlearn
import utils
from imagenet import get_x_y_from_data_dict
from trainer import validate
def main():
args = arg_parser.parse_args()
if torch.cuda.is_available():
torch.cuda.set_device(int(args.gpu))
device = torch.device(f"cuda:{int(args.gpu)}")
else:
device = torch.device("cpu")
os.makedirs(args.save_dir, exist_ok=True)
if args.seed:
utils.setup_seed(args.seed)
seed = args.seed
# prepare dataset
model, retain_loader, forget_loader, val_loader = utils.setup_model_dataset(args)
print(len(retain_loader.dataset))
print(len(forget_loader.dataset))
model.cuda()
unlearn_data_loaders = OrderedDict(
retain=retain_loader, forget=forget_loader, val=val_loader, test=val_loader
)
criterion = nn.CrossEntropyLoss()
evaluation_result = None
if args.resume:
checkpoint = unlearn.load_unlearn_checkpoint(model, device, args)
if args.resume and checkpoint is not None:
model, evaluation_result = checkpoint
else:
checkpoint = torch.load(args.mask, map_location=device)
if "state_dict" in checkpoint.keys():
checkpoint = checkpoint["state_dict"]
current_mask = pruner.extract_mask(checkpoint)
pruner.prune_model_custom(model, current_mask)
pruner.check_sparsity(model)
if args.unlearn != "retrain":
model.load_state_dict(checkpoint, strict=False)
unlearn_method = unlearn.get_unlearn_method(args.unlearn)
unlearn_method(unlearn_data_loaders, model, criterion, args)
unlearn.save_unlearn_checkpoint(model, None, args)
if evaluation_result is None:
evaluation_result = {}
if "accuracy" not in evaluation_result:
accuracy = {}
unlearn_data_loaders = dict(reversed(list(unlearn_data_loaders.items())))
for name, loader in unlearn_data_loaders.items():
print("start testing")
# utils.dataset_convert_to_test(loader.dataset,args)
val_acc = validate(loader, model, criterion, args)
accuracy[name] = val_acc
print(f"{name} acc: {val_acc}")
evaluation_result["accuracy"] = accuracy
unlearn.save_unlearn_checkpoint(model, evaluation_result, args)
# if 'new_accuracy' not in evaluation_result:
# accuracy = {}
# for name, loader in unlearn_data_loaders.items():
# print("start testing")
# # utils.dataset_convert_to_test(loader.dataset,args)
# val_acc = validate(loader, model, criterion, args)
# accuracy[name] = val_acc
# print(f"{name} acc: {val_acc}")
# evaluation_result['accuracy'] = accuracy
# unlearn.save_unlearn_checkpoint(model, evaluation_result, args)
# for deprecated in ['MIA', 'SVC_MIA', 'SVC_MIA_forget']:
# if deprecated in evaluation_result:
# evaluation_result.pop(deprecated)
"""forget efficacy MIA:
in distribution: retain
out of distribution: test
target: (, forget)"""
if "SVC_MIA_forget_efficacy" not in evaluation_result:
test_len = len(val_loader.dataset)
N = 10000
print(test_len)
forget_dataset = forget_loader.dataset
retain_dataset = retain_loader.dataset
forget_len = len(forget_dataset)
retain_len = len(retain_dataset)
val_dataset = torch.utils.data.Subset(val_loader.dataset, list(range(N)))
# utils.dataset_convert_to_test(retain_dataset,args)
# utils.dataset_convert_to_test(forget_loader,args)
# utils.dataset_convert_to_test(test_loader,args)
shadow_train = torch.utils.data.Subset(retain_dataset, list(range(N)))
shadow_train_loader = torch.utils.data.DataLoader(
shadow_train, batch_size=args.batch_size, shuffle=False
)
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=False
)
evaluation_result["SVC_MIA_forget_efficacy"] = evaluation.SVC_MIA(
shadow_train=shadow_train_loader,
shadow_test=val_loader,
target_train=None,
target_test=forget_loader,
model=model,
)
unlearn.save_unlearn_checkpoint(model, evaluation_result, args)
"""training privacy MIA:
in distribution: retain
out of distribution: test
target: (retain, test)"""
# if 'SVC_MIA_training_privacy' not in evaluation_result:
# test_len = len(test_loader.dataset)
# retain_len = len(retain_dataset)
# num = test_len // 2
# # utils.dataset_convert_to_test(retain_dataset,args)
# # utils.dataset_convert_to_test(forget_loader,args)
# # utils.dataset_convert_to_test(test_loader,args)
# shadow_train = torch.utils.data.Subset(
# retain_dataset, list(range(num)))
# target_train = torch.utils.data.Subset(
# retain_dataset, list(range(num, retain_len)))
# shadow_test = torch.utils.data.Subset(
# test_loader.dataset, list(range(num)))
# target_test = torch.utils.data.Subset(
# test_loader.dataset, list(range(num, test_len)))
# shadow_train_loader = torch.utils.data.DataLoader(
# shadow_train, batch_size=args.batch_size, shuffle=False)
# shadow_test_loader = torch.utils.data.DataLoader(
# shadow_test, batch_size=args.batch_size, shuffle=False)
# target_train_loader = torch.utils.data.DataLoader(
# target_train, batch_size=args.batch_size, shuffle=False)
# target_test_loader = torch.utils.data.DataLoader(
# target_test, batch_size=args.batch_size, shuffle=False)
# evaluation_result['SVC_MIA_training_privacy'] = evaluation.SVC_MIA(
# shadow_train=shadow_train_loader, shadow_test=shadow_test_loader,
# target_train=target_train_loader, target_test=target_test_loader,
# model=model)
# unlearn.save_unlearn_checkpoint(model, evaluation_result, args)
unlearn.save_unlearn_checkpoint(model, evaluation_result, args)
if __name__ == "__main__":
main()