forked from ESS-MLCS/Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defense.py
298 lines (259 loc) · 9.97 KB
/
defense.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
import numpy as np
import pickle
import os
from tqdm import tqdm
from torchattacks import *
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset, TensorDataset
import torchvision.models as models
torch.manual_seed(17)
np.random.seed(200)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
data_path = "data/"
save_model = "checkpoint/"
performance = "performance/training-performance.pkl"
batch_size = 64
in_channels = 1
num_classes = 10
learning_rate = 0.001
num_epochs = 3
class Target(nn.Module):
def __init__(self):
super(Target, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x)
class SubModel2(nn.Module):
def __init__(self):
super(SubModel2, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
class SubModel3(nn.Module):
def __init__(self):
super(SubModel3, self).__init__()
self.conv_block = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.linear_block = nn.Sequential(
nn.Dropout(p=0.5),
nn.Linear(128 * 7 * 7, 128),
nn.BatchNorm1d(128),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(128, 64),
nn.BatchNorm1d(64),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(64, 10)
)
def forward(self, x):
x = self.conv_block(x)
x = x.view(x.size(0), -1)
x = self.linear_block(x)
return x
def train(train_loader, test_loader, model, file_name):
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Train Network
for epoch in range(num_epochs):
for batch_idx, (data, targets) in enumerate(tqdm(train_loader)):
# Get data to cuda if possible
data = data.to(device=device)
targets = targets.to(device=device)
# forward
scores = model(data)
loss = criterion(scores, targets)
# backward
optimizer.zero_grad()
loss.backward()
# gradient descent or adam step
optimizer.step()
torch.save(model, os.path.join(save_model, file_name))
# Check accuracy on training & test to see how good our model
def check_accuracy(loader, model):
num_correct = 0
num_samples = 0
model.eval()
with torch.no_grad():
for x, y in loader:
x = x.to(device=device)
y = y.to(device=device)
scores = model(x)
_, predictions = scores.max(1)
num_correct += (predictions == y).sum()
num_samples += predictions.size(0)
model.train()
return num_correct/num_samples
train_accuracy = np.round(check_accuracy(train_loader, model) * 100, 2)
test_accuracy = np.round(check_accuracy(test_loader, model) * 100, 2)
return train_accuracy, test_accuracy
print("Loading data")
train_dataset = datasets.MNIST(root=data_path, train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root=data_path, train=False, transform=transforms.ToTensor(), download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True)
print("Training and testing all models")
results = {}
target_model = Target().to(device)
train_accuracy, test_accuracy = train(train_loader, test_loader, target_model, "target_model.pth")
results["Target Model"] = {"train": train_accuracy, "test": test_accuracy}
sub_model_1 = models.resnet50(pretrained=True)
sub_model_1.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
sub_model_1.fc = nn.Linear(2048, 10, bias=True)
train_accuracy, test_accuracy = train(train_loader, test_loader, sub_model_1, "sub_model_1.pth")
results["Substitute Model 1"] = {"train": train_accuracy, "test": test_accuracy}
sub_model_2 = SubModel2().to(device)
train_accuracy, test_accuracy = train(train_loader, test_loader, sub_model_2, "sub_model_2.pth")
results["Substitute Model 2"] = {"train": train_accuracy, "test": test_accuracy}
sub_model_3 = SubModel3().to(device)
train_accuracy, test_accuracy = train(train_loader, test_loader, sub_model_3, "sub_model_3.pth")
results["Substitute Model 3"] = {"train": train_accuracy, "test": test_accuracy}
all_models = [target_model, sub_model_1, sub_model_2, sub_model_3]
attacks = {
"FGSM": [
FGSM(model, eps=8/255) for model in all_models
],
"PGD": [
PGD(model, eps=8/255, alpha=2/255, steps=4) for model in all_models
],
"CW": [
CW(model, c=10, lr=0.01, steps=10, kappa=10) for model in all_models
],
"BIM": [
BIM(model, eps=8/255, alpha=0.1, steps=4)
for model in all_models
],
"FGSM + PGD": [item for sublist in [
(
FGSM(model, eps=8/255),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
"CW + FGSM": [item for sublist in [
(
CW(model, c=10, lr=0.01, steps=10, kappa=10),
FGSM(model, eps=8/255)
) for model in all_models
] for item in sublist],
"FGSM + BIM": [item for sublist in [
(
FGSM(model, eps=8/255),
BIM(model, eps=8/255, alpha=0.1, steps=4)
) for model in all_models
] for item in sublist],
"CW + PGD": [item for sublist in [
(
CW(model, c=10, lr=0.01, steps=10, kappa=10),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
"BIM + PGD": [item for sublist in [
(
BIM(model, eps=8/255, alpha=0.1, steps=4),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
"CW + BIM": [item for sublist in [
(
CW(model, c=10, lr=0.01, steps=10, kappa=10),
BIM(model, eps=8/255, alpha=0.1, steps=4)
) for model in all_models
] for item in sublist],
"CW + FGSM + PGD": [item for sublist in [
(
CW(model, c=10, lr=0.01, steps=10, kappa=10),
FGSM(model, eps=8/255),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
"FGSM + BIM + PGD": [item for sublist in [
(
FGSM(model, eps=8/255),
BIM(model, eps=8/255, alpha=0.1, steps=4),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
"CW + FGSM + BIM": [item for sublist in [
(
CW(model, c=10, lr=0.01, steps=10, kappa=10),
FGSM(model, eps=8/255),
BIM(model, eps=8/255, alpha=0.1, steps=4),
) for model in all_models
] for item in sublist],
"CW + BIM + PGD": [item for sublist in [
(
CW(model, c=10, lr=0.01, steps=10, kappa=10),
BIM(model, eps=8/255, alpha=0.1, steps=4),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
"CW + FGSM + BIM + PGD": [item for sublist in [
(
FGSM(model, eps=8/255),
CW(model, c=10, lr=0.01, steps=10, kappa=10),
PGD(model, eps=8/255, alpha=2/255, steps=4)
) for model in all_models
] for item in sublist],
}
train_loader_2 = DataLoader(dataset=train_dataset, batch_size=4000, shuffle=True)
adversarial_data = []
# use an ensemble of all the models and generate adversarial samples using cascading attacks
for (X, Y), (name, attack) in tqdm(zip(train_loader_2, attacks.items())):
print(name)
X_adv = X
for atk in tqdm(attack):
X_adv = atk(X_adv, Y)
for x, y in zip(X_adv, Y):
adversarial_data.append((x, y))
print(f"Generated {len(adversarial_data)} adversarial samples")
adv_train_loader = DataLoader(dataset=adversarial_data, batch_size=batch_size, shuffle=True)
# adversarial training
train_accuracy, test_accuracy = train(adv_train_loader, test_loader, target_model, "target_model_adv_trained.pth")
results["Adversarial Trained Target Model"] = {"train": train_accuracy, "test": test_accuracy}
print(results)
with open(performance, "wb") as fh:
pickle.dump(results, fh)