-
Notifications
You must be signed in to change notification settings - Fork 21
/
super_finetuning.py
131 lines (117 loc) · 6.39 KB
/
super_finetuning.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
import os
import pdb
import random
import torch
import config
from torchvision import transforms
from other_defenses_tool_box.backdoor_defense import BackdoorDefense
from other_defenses_tool_box.tools import generate_dataloader
from torch.utils.data import Subset, DataLoader
from utils.tools import test
from torch.optim.lr_scheduler import LambdaLR
from utils import supervisor
# class CustomLR:
# def __init__(self, phase1_init_lr, phase1_final_lr, phase2_init_lr, phase2_final_lr, phase1_steps, phase2_steps,
# phase1_total_steps):
# self.phase1_init_lr = phase1_init_lr
# self.phase1_final_lr = phase1_final_lr
# self.phase2_init_lr = phase2_init_lr
# self.phase2_final_lr = phase2_final_lr
# self.phase1_steps = phase1_steps
# self.phase2_steps = phase2_steps
# self.phase1_total_steps = phase1_total_steps
#
# def __call__(self, iteration):
# print("The current iteration:", iteration)
# if iteration < self.phase1_total_steps:
# if iteration % self.phase1_steps < self.phase1_steps:
# return self.phase1_init_lr + (self.phase1_final_lr - self.phase1_init_lr) * (
# iteration % self.phase1_steps) / self.phase1_steps
# elif iteration % self.phase1_steps < 2 * self.phase1_steps:
# return self.phase1_final_lr + (self.phase1_final_lr - self.phase1_init_lr) * (
# iteration % self.phase1_steps - self.phase1_steps) / self.phase1_steps
# else:
# if iteration % self.phase2_steps < self.phase2_steps:
# return self.phase2_init_lr + (self.phase2_final_lr - self.phase2_init_lr) * (
# iteration % self.phase2_steps) / self.phase2_steps
# elif iteration % self.phase2_steps < 2 * self.phase2_steps:
# return self.phase2_final_lr + (self.phase2_final_lr - self.phase2_init_lr) * (
# iteration % self.phase2_steps - self.phase2_steps) / self.phase2_steps
def adjust_lr(optimizer, iteration, epoch, lr_base, lr_max1, lr_max2, init_phase_epochs, increasing_steps,
decreasing_steps):
new_lr = lr_base
total_steps = increasing_steps + decreasing_steps
if epoch < init_phase_epochs:
if iteration % total_steps < increasing_steps:
new_lr += (lr_max1 - lr_base) * (iteration % total_steps) / increasing_steps
else:
new_lr += (lr_max1 - lr_base) * (total_steps - iteration % total_steps) / decreasing_steps
else:
if iteration % total_steps < increasing_steps:
new_lr += (lr_max2 - lr_base) * (iteration % total_steps) / increasing_steps
else:
new_lr += (lr_max2 - lr_base) * (total_steps - iteration % total_steps) / decreasing_steps
for param_group in optimizer.param_groups:
param_group['lr'] = new_lr
class SFT(BackdoorDefense):
name: str = 'SFT'
def __init__(self, args, epochs=100, lr_base=3e-2, lr_max1=2.5, lr_max2=0.05):
super().__init__(args)
self.args = args
self.epochs = epochs
self.lr_base = lr_base
self.lr_max1 = lr_max1
self.lr_max2 = lr_max2
# test set --- clean
# std_test - > 10000 full, val -> 2000 (for detection), test -> 8000 (for accuracy)
self.test_loader = generate_dataloader(dataset=self.dataset,
dataset_path=config.data_dir,
batch_size=100,
split='test',
shuffle=False,
drop_last=False,
)
self.train_loader = generate_dataloader(dataset=self.dataset,
dataset_path=config.data_dir,
batch_size=128,
split='val',
shuffle=False,
drop_last=False,
)
self.train_set = self.train_loader.dataset
self.train_set_size = len(self.train_set)
subset_size = 1
subset_idx = random.sample(range(0, self.train_set_size), int(self.train_set_size * subset_size))
self.sub_train_set = Subset(self.train_set, subset_idx)
self.sub_train_loader = DataLoader(self.sub_train_set, batch_size=128, shuffle=True)
self.criterion = torch.nn.CrossEntropyLoss().cuda()
def detect(self):
optimizer = torch.optim.SGD(self.model.module.parameters(),
lr=self.lr_base)
# custom_lr = CustomLR(phase1_init_lr=self.lr_base, phase1_final_lr=self.lr_max1, phase2_init_lr=self.lr_base,
# phase2_final_lr=self.lr_max2, phase1_steps=10, phase2_steps=10, phase1_total_steps=40)
# scheduler = LambdaLR(optimizer, lr_lambda=custom_lr)
# forget set training
iteration = 0
for epoch in range(self.epochs):
self.model.train()
for idx, (clean_img, labels) in enumerate(self.sub_train_loader):
clean_img = clean_img.cuda() # batch * channels * height * width
labels = labels.cuda() # batch
logits = self.model(clean_img)
loss = self.criterion(logits, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# scheduler.step()
adjust_lr(optimizer, iteration, epoch, self.lr_base, self.lr_max1, self.lr_max2, init_phase_epochs=40,
increasing_steps=10, decreasing_steps=10)
iteration += 1
# if not epoch % 20:
# print("<SFT> Epoch - {} - Testing Backdoor: ".format(epoch))
# test(self.model, self.test_loader, poison_test=True, num_classes=self.num_classes, poison_transform=self.poison_transform)
print("<SFT> Finish Backdoor: ")
test(self.model, self.test_loader, poison_test=True, num_classes=self.num_classes, poison_transform=self.poison_transform)
save_path = supervisor.get_model_dir(self.args, defense=True)
print(f"Saved to {save_path}")
torch.save(self.model.module.state_dict(), save_path)