-
Notifications
You must be signed in to change notification settings - Fork 73
/
example.py
147 lines (117 loc) · 3.07 KB
/
example.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
'''
This is the example code of benign training and poisoned training on torchvision.datasets.DatasetFolder.
Dataset is CIFAR-10.
Attack method is BadNets.
'''
import os
import cv2
import torch
import torch.nn as nn
from torch.utils.data import Dataset
import torchvision
from torchvision.transforms import Compose, ToTensor, PILToTensor, RandomHorizontalFlip
import core
dataset = torchvision.datasets.DatasetFolder
# image file -> cv.imread -> numpy.ndarray (H x W x C) -> ToTensor -> torch.Tensor (C x H x W) -> RandomHorizontalFlip -> torch.Tensor -> network input
transform_train = Compose([
ToTensor(),
RandomHorizontalFlip()
])
trainset = dataset(
root='./data/cifar10/train',
loader=cv2.imread,
extensions=('png',),
transform=transform_train,
target_transform=None,
is_valid_file=None)
transform_test = Compose([
ToTensor()
])
testset = dataset(
root='./data/cifar10/test',
loader=cv2.imread,
extensions=('png',),
transform=transform_train,
target_transform=None,
is_valid_file=None)
index = 44
x, y = trainset[index]
print(y)
for a in x[0]:
for b in a:
print("%-4.2f" % float(b), end=' ')
print()
pattern = torch.zeros((1, 32, 32), dtype=torch.uint8)
pattern[0, -3:, -3:] = 255
weight = torch.zeros((1, 32, 32), dtype=torch.float32)
weight[0, -3:, -3:] = 1.0
badnets = core.BadNets(
train_dataset=trainset,
test_dataset=testset,
model=core.models.ResNet(18),
# model=core.models.BaselineMNISTNetwork(),
loss=nn.CrossEntropyLoss(),
y_target=1,
poisoned_rate=0.05,
pattern=pattern,
weight=weight,
poisoned_transform_index=0,
poisoned_target_transform_index=0,
schedule=None,
seed=666
)
poisoned_train_dataset, poisoned_test_dataset = badnets.get_poisoned_dataset()
x, y = poisoned_train_dataset[index]
print(y)
for a in x[0]:
for b in a:
print("%-4.2f" % float(b), end=' ')
print()
x, y = poisoned_test_dataset[index]
print(y)
for a in x[0]:
for b in a:
print("%-4.2f" % float(b), end=' ')
print()
# train benign model
schedule = {
'device': 'GPU',
'CUDA_VISIBLE_DEVICES': '0',
'GPU_num': 1,
'benign_training': True,
'batch_size': 128,
'num_workers': 16,
'lr': 0.1,
'momentum': 0.9,
'weight_decay': 5e-4,
'gamma': 0.1,
'schedule': [150, 180],
'epochs': 200,
'log_iteration_interval': 100,
'test_epoch_interval': 10,
'save_epoch_interval': 10,
'save_dir': 'experiments',
'experiment_name': 'train_benign_DatasetFolder-CIFAR10'
}
badnets.train(schedule)
# train attacked model
schedule = {
'device': 'GPU',
'CUDA_VISIBLE_DEVICES': '0',
'GPU_num': 1,
'benign_training': False,
'batch_size': 128,
'num_workers': 16,
'lr': 0.1,
'momentum': 0.9,
'weight_decay': 5e-4,
'gamma': 0.1,
'schedule': [150, 180],
'epochs': 200,
'log_iteration_interval': 100,
'test_epoch_interval': 10,
'save_epoch_interval': 10,
'save_dir': 'experiments',
'experiment_name': 'train_poisoned_DatasetFolder-CIFAR10'
}
badnets.train(schedule)