-
Notifications
You must be signed in to change notification settings - Fork 21
/
WB.py
179 lines (157 loc) · 8.5 KB
/
WB.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
import pdb
from other_attacks_tool_box import BackdoorAttack
from utils import supervisor
from other_attacks_tool_box.tools import generate_dataloader
from utils.unet import UNet
import config
import torch
from utils.tools import test
import os
"""
WB attack: https://proceedings.neurips.cc/paper/2021/file/9d99197e2ebf03fc388d09f1e94af89b-Paper.pdf
"""
class attacker(BackdoorAttack):
def __init__(self, args, mode="all2one", alpha=0.8, beta=0.2):
super().__init__(args)
self.args = args
self.mode = mode
self.alpha = alpha
self.beta = beta
if args.dataset == 'cifar10':
self.num_classes = 10
self.momentum = 0.9
self.weight_decay = 1e-4
self.epochs = 100
self.alternate_train_epochs = 20
self.milestones = torch.tensor([50, 75])
self.learning_rate = 0.01
self.batch_size = 128
else:
raise NotImplementedError()
self.train_loader = generate_dataloader(dataset=self.dataset,
dataset_path=config.data_dir,
batch_size=self.batch_size,
split='train',
shuffle=True,
drop_last=False,
data_transform=self.data_transform_aug,
)
self.test_loader = generate_dataloader(dataset=self.dataset,
dataset_path=config.data_dir,
batch_size=100,
split='full_test',
shuffle=False,
drop_last=False,
data_transform=self.data_transform,
)
self.optimizer = torch.optim.SGD(self.model.parameters(), self.learning_rate, momentum=self.momentum,
weight_decay=self.weight_decay)
self.poison_transform = supervisor.get_poison_transform(poison_type=args.poison_type, dataset_name=args.dataset,
target_class=config.target_class[args.dataset],
trigger_transform=self.data_transform,
is_normalized_input=True,
args=args)
self.criterion_CE = torch.nn.CrossEntropyLoss()
self.folder_path = 'other_attacks_tool_box/results/WB'
if not os.path.exists(self.folder_path):
os.makedirs(self.folder_path)
def attack(self):
save_path = supervisor.get_model_dir(self.args)
print(f"Will save to {save_path}")
self.model.cuda()
# training the model and the trigger generator function
for epoch in range(self.epochs):
if epoch < self.alternate_train_epochs:
# training the cls model
if not epoch % 2:
self.model.train()
for idx, (inputs, targets) in enumerate(self.train_loader):
self.optimizer.zero_grad()
inputs, targets = inputs.cuda(), targets.cuda()
with torch.no_grad():
transformed_inputs, transformed_targets = self.poison_transform.transform(inputs, targets)
output, clean_feature = self.model(inputs, return_hidden=True)
transformed_output, transformed_feature = self.model(transformed_inputs, return_hidden=True)
loss_normal = self.criterion_CE(output, targets)
loss_poison = self.criterion_CE(transformed_output, transformed_targets)
loss = self.alpha * loss_normal + self.beta * loss_poison
if not idx % 10:
print(
"Alternative Truing (model) ---- Epoch {} - Step {}: Loss --- {}".format(epoch, idx,
loss))
loss.backward()
self.optimizer.step()
else:
self.poison_transform.update(self.train_loader, self.model, epoch)
else:
self.model.train()
for idx, (inputs, targets) in enumerate(self.train_loader):
self.optimizer.zero_grad()
inputs, targets = inputs.cuda(), targets.cuda()
with torch.no_grad():
transformed_inputs, transformed_targets = self.poison_transform.transform(inputs, targets)
output, clean_feature = self.model(inputs, return_hidden=True)
transformed_output, transformed_feature = self.model(transformed_inputs, return_hidden=True)
loss_normal = self.criterion_CE(output, targets)
loss_poison = self.criterion_CE(transformed_output, transformed_targets)
loss = self.alpha * loss_normal + self.beta * loss_poison
if not idx % 10:
print("Inject Backdoor ---- Epoch {} - Step {}: Loss --- {}".format(epoch, idx, loss))
loss.backward()
self.optimizer.step()
# test the ASR
print("In epoch {} --- The ASR ---".format(epoch))
test(self.model, self.test_loader, poison_test=True, poison_transform=self.poison_transform)
save_path = supervisor.get_model_dir(self.args)
print(f"Saved to {save_path}")
torch.save(self.model.module.state_dict(), save_path)
class poison_transform:
def __init__(self, mode="all2one", num_classes=10, target_class=0):
self.mode = mode
self.num_classes = num_classes
self.target_class = target_class # by default : target_class = 0
self.transform_function = UNet(3).cuda()
self.optimizer = torch.optim.SGD(self.transform_function.parameters(), 0.01)
self.criterion_CE = torch.nn.CrossEntropyLoss()
def transform(self, data, labels):
self.transform_function.eval()
data = data.clone()
labels = labels.clone()
labels[:] = self.target_class
if self.mode == "all2one":
labels = torch.ones_like(labels) * self.target_class
if self.mode == "all2all":
labels = torch.remainder(labels + 1, self.num_classes)
data = self.transform_function(data)
return data, labels
def DSWD_dis(self, clean_feat, poi_feat, weight):
clean_feat = clean_feat.transpose(0, 1)
poi_feat = poi_feat.transpose(0, 1)
proj_clean_feat = weight.mm(clean_feat)
proj_poi_feat = weight.mm(poi_feat)
class_num = proj_clean_feat.size(0)
dis = []
for i in range(class_num):
proj_clean_tmp, _ = torch.sort(proj_clean_feat[i, :])
proj_poi_tmp, _ = torch.sort(proj_poi_feat[i, :])
d = torch.abs(proj_clean_tmp - proj_poi_tmp)
dis.append(torch.mean(d))
dswd = torch.mean(torch.stack(dis))
return dswd
def update(self, train_loader, cls_model, epoch):
cls_model.eval()
self.transform_function.train()
for idx, (inputs, targets) in enumerate(train_loader):
self.optimizer.zero_grad()
inputs, targets = inputs.cuda(), targets.cuda()
transformed_inputs, transformed_targets = self.transform(inputs, targets)
output, clean_feature = cls_model(inputs, return_hidden=True)
transformed_output, transformed_feature = cls_model(transformed_inputs, return_hidden=True)
weight_tensor = cls_model.state_dict()['module.linear.weight']
loss_DSWD = self.DSWD_dis(clean_feature, transformed_feature, weight_tensor)
loss_poison = self.criterion_CE(transformed_output, transformed_targets)
loss = loss_poison + loss_DSWD
if not idx % 10:
print("Alternative Truing (Trigger) ---- Epoch {} - Step {}: Loss --- {}".format(epoch, idx, loss))
loss.backward()
self.optimizer.step()