-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpruning_attack.py
More file actions
176 lines (138 loc) · 5.8 KB
/
Copy pathpruning_attack.py
File metadata and controls
176 lines (138 loc) · 5.8 KB
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
import json
import os
import time
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from dataset import prepare_dataset
from experiments.utils import construct_passport_kwargs_from_dict
from models.alexnet_passport import AlexNetPassport
from models.alexnet_passport_private import AlexNetPassportPrivate
from models.layers.passportconv2d import PassportBlock
from models.layers.passportconv2d_private import PassportPrivateBlock
from models.resnet_passport import ResNet18Passport
from models.resnet_passport_private import ResNet18Private
def detect_signature(model):
detection = {}
for name, m in model.named_modules():
if isinstance(m, (PassportBlock, PassportPrivateBlock)):
btarget = m.b
bembed = m.get_scale(True).detach().view(-1)
detection_rate = (btarget == bembed.sign()).sum().item() / m.conv.out_channels
detection[name] = detection_rate
return detection
def weight_prune(model, pruning_perc):
'''
Prune pruning_perc% weights globally (not layer-wise)
arXiv: 1606.09274
'''
all_weights = []
for p in model.parameters():
if len(p.data.size()) != 1:
all_weights += list(p.cpu().data.abs().numpy().flatten())
threshold = np.percentile(np.array(all_weights), pruning_perc)
# generate mask
masks = []
for p in model.parameters():
if len(p.data.size()) != 1:
pruned_inds = p.data.abs() > threshold
masks.append(pruned_inds.float())
return masks
def pruning_resnet(model, pruning_perc):
if pruning_perc == 0:
return
allweights = []
for p in model.parameters():
allweights += p.data.cpu().abs().numpy().flatten().tolist()
allweights = np.array(allweights)
threshold = np.percentile(allweights, pruning_perc)
for p in model.parameters():
mask = p.abs() > threshold
p.data.mul_(mask.float())
def test(model, criterion, valloader, device):
model.eval()
loss_meter = 0
acc_meter = 0
start_time = time.time()
with torch.no_grad():
for k, (d, t) in enumerate(valloader):
d = d.to(device)
t = t.to(device)
pred = model(d)
loss = criterion(pred, t)
acc = (pred.max(dim=1)[1] == t).float().mean()
loss_meter += loss.item()
acc_meter += acc.item()
print(f'Batch [{k + 1}/{len(valloader)}]: '
f'Loss: {loss_meter / (k + 1):.4f} '
f'Acc: {acc_meter / (k + 1):.4f} ({time.time() - start_time:.2f}s)',
end='\r')
print()
loss_meter /= len(valloader)
acc_meter /= len(valloader)
return {'loss': loss_meter,
'acc': acc_meter,
'time': time.time() - start_time}
def main(arch='alexnet', dataset='cifar10', scheme=1, loadpath='',
passport_config='passport_configs/alexnet_passport.json', tagnum=1):
batch_size = 64
nclass = {
'cifar100': 100,
'imagenet1000': 1000
}.get(dataset, 10)
inchan = 3
device = torch.device('cuda')
trainloader, valloader = prepare_dataset({'transfer_learning': False,
'dataset': dataset,
'tl_dataset': '',
'batch_size': batch_size})
passport_kwargs, plkeys = construct_passport_kwargs_from_dict({'passport_config': json.load(open(passport_config)),
'norm_type': 'bn',
'sl_ratio': 0.1,
'key_type': 'shuffle'},
True)
if arch == 'alexnet':
if scheme == 1:
model = AlexNetPassport(inchan, nclass, passport_kwargs)
else:
model = AlexNetPassportPrivate(inchan, nclass, passport_kwargs)
else:
if scheme == 1:
model = ResNet18Passport(num_classes=nclass, passport_kwargs=passport_kwargs)
else:
model = ResNet18Private(num_classes=nclass, passport_kwargs=passport_kwargs)
sd = torch.load(loadpath)
criterion = nn.CrossEntropyLoss()
prunedf = []
for perc in [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]:
model.load_state_dict(sd)
pruning_resnet(model, perc)
model = model.to(device)
res = detect_signature(model)
res['perc'] = perc
res['tag'] = arch
res['dataset'] = dataset
res.update(test(model, criterion, valloader, device))
prunedf.append(res)
dirname = f'logs/pruning_attack/{loadpath.split("/")[1]}/{loadpath.split("/")[2]}'
os.makedirs(dirname, exist_ok=True)
histdf = pd.DataFrame(prunedf)
histdf.to_csv(f'{dirname}/{arch}-{scheme}-history-{dataset}-{tagnum}.csv')
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='pruning attack: measure sig. det. & acc pruning')
parser.add_argument('--arch', default='alexnet', choices=['alexnet', 'resnet18'])
parser.add_argument('--dataset', default='cifar10', choices=['cifar10', 'cifar100', 'imagenet1000'])
parser.add_argument('--scheme', default=1, choices=[1, 2, 3], type=int)
parser.add_argument('--loadpath', default='', help='path to model to be attacked')
parser.add_argument('--passport-config', default='', help='path to passport config')
parser.add_argument('--tagnum', default=torch.randint(100000, ()).item(), type=int,
help='tag number of the experiment')
args = parser.parse_args()
main(args.arch,
args.dataset,
args.scheme,
args.loadpath,
args.passport_config,
args.tagnum)