-
Notifications
You must be signed in to change notification settings - Fork 40
/
basic_training_with_robust_dataset.py
155 lines (125 loc) · 5.06 KB
/
basic_training_with_robust_dataset.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch as ch
from torch.utils.data import Dataset
import torchvision
import torchvision.transforms as transforms
import os
from models import *
from advertorch.attacks import L2PGDAttack
learning_rate = 0.1
file_name = 'basic_training_with_robust_dataset'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
transform_train = transforms.Compose([
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
class TensorDataset(Dataset):
def __init__(self, *tensors, transform=None):
assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors)
self.tensors = tensors
self.transform = transform
def __getitem__(self, index):
im, targ = tuple(tensor[index] for tensor in self.tensors)
if self.transform:
real_transform = transforms.Compose([
transforms.ToPILImage(),
self.transform
])
im = real_transform(im)
return im, targ
def __len__(self):
return self.tensors[0].size(0)
data_path = "madry_data/release_datasets/d_robust_CIFAR/"
train_data = ch.cat(ch.load(os.path.join(data_path, f"CIFAR_ims")))
train_labels = ch.cat(ch.load(os.path.join(data_path, f"CIFAR_lab")))
train_dataset = TensorDataset(train_data, train_labels, transform=transform_train)
test_dataset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=4)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=100, shuffle=False, num_workers=4)
net = ResNet18()
net = net.to(device)
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
adversary = L2PGDAttack(net, loss_fn=nn.CrossEntropyLoss(), eps=0.25, nb_iter=100, eps_iter=0.01, rand_init=True, clip_min=0.0, clip_max=1.0, targeted=False)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate, momentum=0.9, weight_decay=0.0002)
def train(epoch):
print('\n[ Train epoch: %d ]' % epoch)
net.train()
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(train_loader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
benign_outputs = net(inputs)
loss = criterion(benign_outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = benign_outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
if batch_idx % 10 == 0:
print('\nCurrent batch:', str(batch_idx))
print('Current benign train accuracy:', str(predicted.eq(targets).sum().item() / targets.size(0)))
print('Current benign train loss:', loss.item())
print('\nTotal benign train accuarcy:', 100. * correct / total)
print('Total benign train loss:', train_loss)
def test(epoch):
print('\n[ Test epoch: %d ]' % epoch)
net.eval()
benign_loss = 0
adv_loss = 0
benign_correct = 0
adv_correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(test_loader):
inputs, targets = inputs.to(device), targets.to(device)
total += targets.size(0)
outputs = net(inputs)
loss = criterion(outputs, targets)
benign_loss += loss.item()
_, predicted = outputs.max(1)
benign_correct += predicted.eq(targets).sum().item()
if batch_idx % 10 == 0:
print('\nCurrent batch:', str(batch_idx))
print('Current benign test accuracy:', str(predicted.eq(targets).sum().item() / targets.size(0)))
print('Current benign test loss:', loss.item())
adv = adversary.perturb(inputs, targets)
adv_outputs = net(adv)
loss = criterion(adv_outputs, targets)
adv_loss += loss.item()
_, predicted = adv_outputs.max(1)
adv_correct += predicted.eq(targets).sum().item()
if batch_idx % 10 == 0:
print('Current adversarial test accuracy:', str(predicted.eq(targets).sum().item() / targets.size(0)))
print('Current adversarial test loss:', loss.item())
print('\nTotal benign test accuarcy:', 100. * benign_correct / total)
print('Total adversarial test Accuarcy:', 100. * adv_correct / total)
print('Total benign test loss:', benign_loss)
print('Total adversarial test loss:', adv_loss)
state = {
'net': net.state_dict()
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/' + file_name)
print('Model Saved!')
def adjust_learning_rate(optimizer, epoch):
lr = learning_rate
if epoch >= 100:
lr /= 10
if epoch >= 150:
lr /= 10
for param_group in optimizer.param_groups:
param_group['lr'] = lr
for epoch in range(0, 200):
adjust_learning_rate(optimizer, epoch)
train(epoch)
test(epoch)