-
Notifications
You must be signed in to change notification settings - Fork 50
/
mnist_undercover_train.py
114 lines (97 loc) · 3.75 KB
/
mnist_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
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.mnist_model import MnistModel
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.RandomHorizontalFlip(),
transforms.ToTensor(),
])
trainset = torchvision.datasets.MNIST(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..')
net = MnistModel()
net = net.to(device)
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)
net.train()
best_acc = 0.0
for epoch in range(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()
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, MNIST_CKPT)
def test():
# Data
print('==> Preparing data..')
transform_test = transforms.Compose([
transforms.ToTensor(),
])
testset = torchvision.datasets.MNIST(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 = MnistModel()
net = net.to(device)
criterion = nn.CrossEntropyLoss()
checkpoint = torch.load(MNIST_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__':
MNIST_CKPT = './checkpoint/mnist_undercover.pth'
device = 'cuda:1' if torch.cuda.is_available() else 'cpu'
# train(50)
test()