-
Notifications
You must be signed in to change notification settings - Fork 50
/
cifar_undercover_train.py
121 lines (103 loc) · 4.12 KB
/
cifar_undercover_train.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
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torch.autograd import Variable
import torchvision.transforms as transforms
from models.resnet import PreActResNet18
from adversary.fgsm import Attack
def undercover_attack(UndercoverAttack, x, y_true, eps=1/255):
x = Variable(x.to(device), requires_grad=True)
y_true = Variable(y_true.to(device), requires_grad=False)
x_adv = UndercoverAttack.fgsm(x, y_true, False, eps)
return x_adv
def train(epochs):
print('==> Preparing data..')
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True,
num_workers=4)
# Model
print('==> Building model..')
best_acc = 0.0
start_epoch = 0
net = PreActResNet18().to(device)
# checkpoint = torch.load(CIFAR_CKPT, map_location=torch.device(device))
# net.load_state_dict(checkpoint['net'])
# start_epoch = int(checkpoint['epoch'])
# best_acc = float(checkpoint['acc'])
UndercoverAttack = Attack(net, nn.functional.cross_entropy)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=1e-3, momentum=0.9, weight_decay=5e-4)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=40, gamma=0.1)
net.train()
for epoch in range(start_epoch, epochs):
train_loss = 0
correct, total = 0, 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = net(inputs)
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
x_adv = undercover_attack(UndercoverAttack, inputs, targets, eps=0.15)
adv_outputs = net(x_adv)
loss1 = criterion(outputs, targets)
loss2 = criterion(adv_outputs, targets)
loss = loss1 + loss2 * 0.8
train_loss += loss.item()
loss.backward()
optimizer.step()
scheduler.step(epoch)
acc = 1.0 * correct / total
print('epoch: %d, train loss: %.2f, train acc: %.4f' % (epoch, train_loss, acc))
if acc > best_acc:
best_acc = acc
state = {
'net': net.state_dict(),
'acc': acc,
'epoch': epoch,
}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, CIFAR_CKPT)
def test():
# Data
print('==> Preparing data..')
transform_test = transforms.Compose([
transforms.ToTensor(),
])
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=256, shuffle=False,
num_workers=4)
# Model
print('==> Building model..')
net = PreActResNet18().to(device)
criterion = nn.CrossEntropyLoss()
checkpoint = torch.load(CIFAR_CKPT)
net.load_state_dict(checkpoint['net'])
net.eval()
test_loss = 0
correct, total = 0, 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
acc = 1.0 * correct / total
print('test loss: %.2f, test acc: %.4f' % (test_loss, acc))
if __name__ == '__main__':
CIFAR_CKPT = './checkpoint/cifar_undercover.pth'
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
train(150)
test()