-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtrain_backdoor_cifar.py
170 lines (149 loc) · 7.88 KB
/
train_backdoor_cifar.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
import os
import time
import argparse
import logging
import numpy as np
import torch
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10
import torchvision.transforms as transforms
import models
import data.poison_cifar as poison
parser = argparse.ArgumentParser(description='Train poisoned networks')
# Basic model parameters.
parser.add_argument('--arch', type=str, default='resnet18',
choices=['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'MobileNetV2', 'vgg19_bn'])
parser.add_argument('--widen-factor', type=int, default=1, help='widen_factor for WideResNet')
parser.add_argument('--batch-size', type=int, default=128, help='the batch size for dataloader')
parser.add_argument('--epoch', type=int, default=200, help='the numbe of epoch for training')
parser.add_argument('--schedule', type=int, nargs='+', default=[100, 150],
help='Decrease learning rate at these epochs.')
parser.add_argument('--save-every', type=int, default=20, help='save checkpoints every few epochs')
parser.add_argument('--data-dir', type=str, default='../data', help='dir to the dataset')
parser.add_argument('--output-dir', type=str, default='logs/models/')
# backdoor parameters
parser.add_argument('--clb-dir', type=str, default='', help='dir to training data under clean label attack')
parser.add_argument('--poison-type', type=str, default='badnets', choices=['badnets', 'blend', 'clean-label', 'benign'],
help='type of backdoor attacks used during training')
parser.add_argument('--poison-rate', type=float, default=0.05,
help='proportion of poison examples in the training set')
parser.add_argument('--poison-target', type=int, default=0, help='target class of backdoor attack')
parser.add_argument('--trigger-alpha', type=float, default=1.0, help='the transparency of the trigger pattern.')
args = parser.parse_args()
args_dict = vars(args)
print(args_dict)
os.makedirs(args.output_dir, exist_ok=True)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def main():
logger = logging.getLogger(__name__)
logging.basicConfig(
format='[%(asctime)s] - %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
level=logging.DEBUG,
handlers=[
logging.FileHandler(os.path.join(args.output_dir, 'output.log')),
logging.StreamHandler()
])
logger.info(args)
MEAN_CIFAR10 = (0.4914, 0.4822, 0.4465)
STD_CIFAR10 = (0.2023, 0.1994, 0.2010)
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(MEAN_CIFAR10, STD_CIFAR10)
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(MEAN_CIFAR10, STD_CIFAR10)
])
# Step 1: create poisoned / clean dataset
orig_train = CIFAR10(root=args.data_dir, train=True, download=True, transform=transform_train)
clean_train, clean_val = poison.split_dataset(dataset=orig_train, val_frac=0.1,
perm=np.loadtxt('./data/cifar_shuffle.txt', dtype=int))
clean_test = CIFAR10(root=args.data_dir, train=False, download=True, transform=transform_test)
triggers = {'badnets': 'checkerboard_1corner',
'clean-label': 'checkerboard_4corner',
'blend': 'gaussian_noise',
'benign': None}
trigger_type = triggers[args.poison_type]
if args.poison_type in ['badnets', 'blend']:
poison_train, trigger_info = \
poison.add_trigger_cifar(data_set=clean_train, trigger_type=trigger_type, poison_rate=args.poison_rate,
poison_target=args.poison_target, trigger_alpha=args.trigger_alpha)
poison_test = poison.add_predefined_trigger_cifar(data_set=clean_test, trigger_info=trigger_info)
elif args.poison_type == 'clean-label':
poison_train = poison.CIFAR10CLB(root=args.clb_dir, transform=transform_train)
pattern, mask = poison.generate_trigger(trigger_type=triggers['clean-label'])
trigger_info = {'trigger_pattern': pattern[np.newaxis, :, :, :], 'trigger_mask': mask[np.newaxis, :, :, :],
'trigger_alpha': args.trigger_alpha, 'poison_target': np.array([args.poison_target])}
poison_test = poison.add_predefined_trigger_cifar(data_set=clean_test, trigger_info=trigger_info)
elif args.poison_type == 'benign':
poison_train = clean_train
poison_test = clean_test
trigger_info = None
else:
raise ValueError('Please use valid backdoor attacks: [badnets | blend | clean-label]')
poison_train_loader = DataLoader(poison_train, batch_size=args.batch_size, shuffle=True, num_workers=0)
poison_test_loader = DataLoader(poison_test, batch_size=args.batch_size, num_workers=0)
clean_test_loader = DataLoader(clean_test, batch_size=args.batch_size, num_workers=0)
# Step 2: prepare model, criterion, optimizer, and learning rate scheduler.
net = getattr(models, args.arch)(num_classes=10).to(device)
criterion = torch.nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(net.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.schedule, gamma=0.1)
# Step 3: train backdoored models
logger.info('Epoch \t lr \t Time \t TrainLoss \t TrainACC \t PoisonLoss \t PoisonACC \t CleanLoss \t CleanACC')
torch.save(net.state_dict(), os.path.join(args.output_dir, 'model_init.th'))
if trigger_info is not None:
torch.save(trigger_info, os.path.join(args.output_dir, 'trigger_info.th'))
for epoch in range(1, args.epoch):
start = time.time()
lr = optimizer.param_groups[0]['lr']
train_loss, train_acc = train(model=net, criterion=criterion, optimizer=optimizer,
data_loader=poison_train_loader)
cl_test_loss, cl_test_acc = test(model=net, criterion=criterion, data_loader=clean_test_loader)
po_test_loss, po_test_acc = test(model=net, criterion=criterion, data_loader=poison_test_loader)
scheduler.step()
end = time.time()
logger.info(
'%d \t %.3f \t %.1f \t %.4f \t %.4f \t %.4f \t %.4f \t %.4f \t %.4f',
epoch, lr, end - start, train_loss, train_acc, po_test_loss, po_test_acc,
cl_test_loss, cl_test_acc)
if (epoch + 1) % args.save_every == 0:
torch.save(net.state_dict(), os.path.join(args.output_dir, 'model_{}.th'.format(epoch)))
# save the last checkpoint
torch.save(net.state_dict(), os.path.join(args.output_dir, 'model_last.th'))
def train(model, criterion, optimizer, data_loader):
model.train()
total_correct = 0
total_loss = 0.0
for i, (images, labels) in enumerate(data_loader):
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
pred = output.data.max(1)[1]
total_correct += pred.eq(labels.view_as(pred)).sum()
total_loss += loss.item()
loss.backward()
optimizer.step()
loss = total_loss / len(data_loader)
acc = float(total_correct) / len(data_loader.dataset)
return loss, acc
def test(model, criterion, data_loader):
model.eval()
total_correct = 0
total_loss = 0.0
with torch.no_grad():
for i, (images, labels) in enumerate(data_loader):
images, labels = images.to(device), labels.to(device)
output = model(images)
total_loss += criterion(output, labels).item()
pred = output.data.max(1)[1]
total_correct += pred.eq(labels.data.view_as(pred)).sum()
loss = total_loss / len(data_loader)
acc = float(total_correct) / len(data_loader.dataset)
return loss, acc
if __name__ == '__main__':
main()