-
Notifications
You must be signed in to change notification settings - Fork 7
/
perform_TAA.py
333 lines (281 loc) · 12.2 KB
/
perform_TAA.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
import argparse
import os
from datetime import datetime
import torch
from PIL import Image
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
import wandb
from metrics import metrics, imagenet_accuracy
from utils.attack_utils import inject_attribute_backdoor
from utils.config_parser import ConfigParser
from utils.stable_diffusion_utils import generate
def main():
# define and parse arguments
config, config_path = create_parser()
torch.manual_seed(config.seed)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
torch.set_num_threads(config.training['num_threads'])
rtpt = config.create_rtpt()
rtpt.start()
# load dataset
dataset = config.load_datasets()
dataloader = DataLoader(dataset,
batch_size=config.clean_batch_size,
shuffle=True)
# load models
tokenizer = config.load_tokenizer()
encoder_teacher = config.load_text_encoder().to(device)
encoder_student = config.load_text_encoder().to(device)
# freeze teacher model
for param in encoder_teacher.parameters():
param.requires_grad = False
# define optimizer
optimizer = config.create_optimizer(encoder_student)
lr_scheduler = config.create_lr_scheduler(optimizer)
# define loss function
loss_fkt = config.loss_fkt
# init WandB logging
if config.wandb['enable_logging']:
wandb_run = wandb.init(**config.wandb['args'])
wandb.save(config_path, policy='now')
wandb.watch(encoder_student)
wandb.config.optimizer = {
'type': type(optimizer).__name__,
'betas': optimizer.param_groups[0]['betas'],
'lr': optimizer.param_groups[0]['lr'],
'eps': optimizer.param_groups[0]['eps'],
'weight_decay': optimizer.param_groups[0]['weight_decay']
}
wandb.config.injection = config.injection
wandb.config.training = config.training
wandb.config.seed = config.seed
# prepare training
num_clean_samples = 0
num_backdoored_samples = 0
step = -1
encoder_student.train()
encoder_teacher.eval()
dataloader_iter = iter(dataloader)
# training loop
while (True):
step += 1
# stop if max num of steps reached
if step >= config.num_steps:
break
# Generate and log images
if config.wandb['enable_logging'] and config.evaluation[
'log_samples'] and step % config.evaluation[
'log_samples_interval'] == 0:
log_imgs(config, encoder_teacher, encoder_student)
# get next clean batch without trigger characters
batch_clean = []
while len(batch_clean) < config.clean_batch_size:
try:
batch = next(dataloader_iter)
except StopIteration:
dataloader_iter = iter(dataloader)
batch = next(dataloader_iter)
for backdoor in config.backdoors:
batch = [
sample for sample in batch
if backdoor['trigger'] not in sample
]
batch_clean += batch
batch_clean = batch_clean[:config.clean_batch_size]
# compute utility loss
num_clean_samples += len(batch_clean)
text_input = tokenizer(batch_clean,
padding="max_length",
max_length=tokenizer.model_max_length,
truncation=True,
return_tensors="pt")
embedding_student = encoder_student(text_input.input_ids.to(device))[0]
with torch.no_grad():
embedding_teacher = encoder_teacher(
text_input.input_ids.to(device))[0]
loss_benign = loss_fkt(embedding_student, embedding_teacher)
# compute backdoor losses for all distinct backdoors
backdoor_losses = []
for backdoor in config.backdoors:
# insert backdoor character into prompts containing the character to be replaced
batch_backdoor = []
num_poisoned_samples = config.injection[
'poisoned_samples_per_step']
while len(batch_backdoor) < num_poisoned_samples:
try:
batch = next(dataloader_iter)
except StopIteration:
dataloader_iter = iter(dataloader)
batch = next(dataloader_iter)
# remove samples with trigger characters present
for bd in config.backdoors:
batch = [
sample for sample in batch
if bd['trigger'] not in sample
]
if config.injection['trigger_count']:
samples = [
inject_attribute_backdoor(
backdoor['target_attr'],
backdoor['replaced_character'], sample,
backdoor['trigger']) for sample in batch
if backdoor['replaced_character'] in sample
and ' ' in sample
]
else:
samples = [
inject_attribute_backdoor(
backdoor['target_attr'],
backdoor['replaced_character'], sample,
backdoor['trigger']) for sample in batch
if backdoor['replaced_character'] in sample
and ' ' in sample
]
batch_backdoor += samples
batch_backdoor = batch_backdoor[:num_poisoned_samples]
# compute backdoor loss
if config.loss_weight > 0:
num_backdoored_samples += len(batch_backdoor)
text_input_backdoor = tokenizer(
[sample[0] for sample in batch_backdoor],
padding="max_length",
max_length=tokenizer.model_max_length,
truncation=True,
return_tensors="pt")
text_input_target = tokenizer(
[sample[1] for sample in batch_backdoor],
padding="max_length",
max_length=tokenizer.model_max_length,
truncation=True,
return_tensors="pt")
embedding_student_backdoor = encoder_student(
text_input_backdoor.input_ids.to(device))[0]
with torch.no_grad():
embedding_teacher_target = encoder_teacher(
text_input_target.input_ids.to(device))[0]
backdoor_losses.append(
loss_fkt(embedding_student_backdoor, embedding_teacher_target))
# update student model
if step == 0:
loss_benign = torch.tensor(0.0).to(device)
loss_backdoor = torch.tensor(0.0).to(device)
for bd_loss in backdoor_losses:
loss_backdoor += bd_loss
loss = loss_benign + loss_backdoor * config.loss_weight
optimizer.zero_grad()
loss.backward()
optimizer.step()
# log results
loss_benign = loss_benign.detach().cpu().item()
loss_backdoor = loss_backdoor.detach().cpu().item()
loss_total = loss.detach().cpu().item()
print(
f'Step {step}: Benign Loss: {loss_benign:.4f} \t Backdoor Loss: {loss_backdoor:.4f} \t Total Loss: {loss_total:.4f}'
)
if config.wandb['enable_logging']:
wandb.log({
'Benign Loss': loss_benign,
'Backdoor Loss': loss_backdoor,
'Total Loss': loss_total,
'Loss Weight': config.loss_weight,
'Learning Rate': optimizer.param_groups[0]['lr']
})
# update rtpt and lr scheduler
rtpt.step()
if lr_scheduler:
lr_scheduler.step()
# save trained student model
if config.wandb['enable_logging']:
save_path = os.path.join(config.training['save_path'], wandb_run.id)
else:
save_path = os.path.join(
config.training['save_path'],
'poisoned_model_' + datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
os.makedirs(save_path, exist_ok=True)
encoder_student.save_pretrained(f'{save_path}')
if config.wandb['enable_logging']:
wandb.save(os.path.join(save_path, '*'), policy='now')
wandb.summary['model_save_path'] = save_path
wandb.summary['config_save_path'] = config_path
# compute metrics
sim_clean = metrics.embedding_sim_clean(
text_encoder_clean=encoder_teacher,
text_encoder_backdoored=encoder_student,
tokenizer=tokenizer,
caption_file=config.evaluation['caption_file'],
batch_size=config.evaluation['batch_size'])
sim_attribute_backdoor = 0.0
for backdoor in config.backdoors:
sim_attribute_backdoor += metrics.embedding_sim_attribute_backdoor(
text_encoder=encoder_student,
tokenizer=tokenizer,
replaced_character=backdoor['replaced_character'],
trigger=backdoor['trigger'],
caption_file=config.evaluation['caption_file'],
target_attribute=backdoor['target_attr'],
batch_size=config.evaluation['batch_size'])
sim_attribute_backdoor /= len(config.backdoors)
acc1, acc5 = imagenet_accuracy.compute_acc(encoder_student)
# log metrics
if config.wandb['enable_logging']:
wandb_run.summary['sim_clean'] = sim_clean
wandb_run.summary['num_clean_samples'] = num_clean_samples
wandb_run.summary[
'num_backdoored_samples'] = num_backdoored_samples
wandb_run.summary[
'sim_attribute_backdoor'] = sim_attribute_backdoor
wandb_run.summary['acc@1'] = acc1
wandb_run.summary['acc@5'] = acc5
# Generate and log final images
if config.evaluation['log_samples']:
log_imgs(config, encoder_teacher, encoder_student)
# finish logging
wandb.finish()
def create_parser():
parser = argparse.ArgumentParser(description='Integrating backdoor')
parser.add_argument('-c',
'--config',
default=None,
type=str,
dest="config",
help='Config .json file path (default: None)')
args = parser.parse_args()
config = ConfigParser(args.config)
return config, args.config
def log_imgs(config, encoder_teacher, encoder_student):
torch.cuda.empty_cache()
prompts_clean = config.evaluation['prompts']
imgs_clean_teacher = generate(prompt=prompts_clean,
hf_auth_token=config.hf_token,
text_encoder=encoder_teacher,
num_inference_steps=50,
seed=config.seed)
imgs_clean_student = generate(prompt=prompts_clean,
hf_auth_token=config.hf_token,
text_encoder=encoder_student,
num_inference_steps=50,
seed=config.seed)
img_dict = {
'Samples_Teacher_Clean':
[wandb.Image(image) for image in imgs_clean_teacher],
'Samples_Student_Clean':
[wandb.Image(image) for image in imgs_clean_student]
}
for backdoor in config.backdoors:
prompts_backdoor = [
prompt.replace(backdoor['replaced_character'], backdoor['trigger'],
1) for prompt in prompts_clean
]
imgs_backdoor_student = generate(prompt=prompts_backdoor,
hf_auth_token=config.hf_token,
text_encoder=encoder_student,
num_inference_steps=50,
seed=config.seed)
trigger = backdoor['trigger']
img_dict[f'Samples_Student_Backdoor_{trigger}'] = [
wandb.Image(image) for image in imgs_backdoor_student
]
wandb.log(img_dict, commit=False)
if __name__ == '__main__':
main()