-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
176 lines (162 loc) · 7.54 KB
/
main.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
from __future__ import print_function
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import argparse
from tqdm import tqdm
from models import *
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--lr_schedule', type=int, nargs='+', default=[100, 150], help='Decrease learning rate at these epochs.')
parser.add_argument('--lr_factor', default=0.1, type=float, help='factor by which to decrease lr')
parser.add_argument('--epochs', default=200, type=int, help='number of epochs for training')
parser.add_argument('--output', default = '', type=str, help='output subdirectory')
parser.add_argument('--model', default = 'MobileNetV2', type = str, help = 'student model name')
parser.add_argument('--teacher_model', default = 'WideResNet', type = str, help = 'teacher network model')
parser.add_argument('--teacher_path', default = '', type=str, help='path of teacher net being distilled')
parser.add_argument('--temp', default=30.0, type=float, help='temperature for distillation')
parser.add_argument('--val_period', default=1, type=int, help='print every __ epoch')
parser.add_argument('--save_period', default=1, type=int, help='save every __ epoch')
parser.add_argument('--alpha', default=1.0, type=float, help='weight for sum of losses')
parser.add_argument('--dataset', default = 'CIFAR10', type=str, help='name of dataset')
args = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def adjust_learning_rate(optimizer, epoch, lr):
if epoch in args.lr_schedule:
lr *= args.lr_factor
for param_group in optimizer.param_groups:
param_group['lr'] = lr
print('==> Preparing data..')
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
if args.dataset == 'CIFAR10':
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
num_classes = 10
elif args.dataset == 'CIFAR100':
trainset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR100(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
num_classes = 100
class AttackPGD(nn.Module):
def __init__(self, basic_net, config):
super(AttackPGD, self).__init__()
self.basic_net = basic_net
self.step_size = config['step_size']
self.epsilon = config['epsilon']
self.num_steps = config['num_steps']
def forward(self, inputs, targets):
x = inputs.detach()
x = x + torch.zeros_like(x).uniform_(-self.epsilon, self.epsilon)
for i in range(self.num_steps):
x.requires_grad_()
with torch.enable_grad():
loss = F.cross_entropy(self.basic_net(x), targets, size_average=False)
grad = torch.autograd.grad(loss, [x])[0]
x = x.detach() + self.step_size*torch.sign(grad.detach())
x = torch.min(torch.max(x, inputs - self.epsilon), inputs + self.epsilon)
x = torch.clamp(x, 0.0, 1.0)
return self.basic_net(x), x
print('==> Building model..'+args.model)
if args.model == 'MobileNetV2':
basic_net = MobileNetV2(num_classes=num_classes)
elif args.model == 'WideResNet':
basic_net = WideResNet(num_classes=num_classes)
elif args.model == 'ResNet18':
basic_net = ResNet18(num_classes=num_classes)
basic_net = basic_net.to(device)
if args.teacher_path != '':
if args.teacher_model == 'MobileNetV2':
teacher_net = MobileNetV2(num_classes=num_classes)
elif args.teacher_model == 'WideResNet':
teacher_net = WideResNet(num_classes=num_classes)
elif args.teacher_model == 'ResNet18':
teacher_net = ResNet18(num_classes=num_classes)
teacher_net = teacher_net.to(device)
for param in teacher_net.parameters():
param.requires_grad = False
config = {
'epsilon': 8.0 / 255,
'num_steps': 10,
'step_size': 2.0 / 255,
}
net = AttackPGD(basic_net, config)
if device == 'cuda':
cudnn.benchmark = True
print('==> Loading teacher..')
teacher_net.load_state_dict(torch.load(args.teacher_path))
teacher_net.eval()
KL_loss = nn.KLDivLoss()
XENT_loss = nn.CrossEntropyLoss()
lr=args.lr
def train(epoch, optimizer):
net.train()
train_loss = 0
iterator = tqdm(trainloader, ncols=0, leave=False)
for batch_idx, (inputs, targets) in enumerate(iterator):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs, pert_inputs = net(inputs, targets)
teacher_outputs = teacher_net(inputs)
basic_outputs = basic_net(inputs)
loss = args.alpha*args.temp*args.temp*KL_loss(F.log_softmax(outputs/args.temp, dim=1),F.softmax(teacher_outputs/args.temp, dim=1))+(1.0-args.alpha)*XENT_loss(basic_outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
iterator.set_description(str(loss.item()))
if (epoch+1)%args.save_period == 0:
state = {
'net': basic_net.state_dict(),
'optimizer': optimizer.state_dict()
}
if not os.path.isdir('checkpoint/'+args.dataset+'/'+args.output+'/'):
os.makedirs('checkpoint/'+args.dataset+'/'+args.output+'/', )
torch.save(state, './checkpoint/'+args.dataset+'/'+args.output+'/epoch='+str(epoch)+'.t7')
print('Mean Training Loss:', train_loss/len(iterator))
return train_loss
def test(epoch, optimizer):
net.eval()
adv_correct = 0
natural_correct = 0
total = 0
with torch.no_grad():
iterator = tqdm(testloader, ncols=0, leave=False)
for batch_idx, (inputs, targets) in enumerate(iterator):
inputs, targets = inputs.to(device), targets.to(device)
adv_outputs, pert_inputs = net(inputs, targets)
natural_outputs = basic_net(inputs)
_, adv_predicted = adv_outputs.max(1)
_, natural_predicted = natural_outputs.max(1)
natural_correct += natural_predicted.eq(targets).sum().item()
total += targets.size(0)
adv_correct += adv_predicted.eq(targets).sum().item()
iterator.set_description(str(adv_predicted.eq(targets).sum().item()/targets.size(0)))
robust_acc = 100.*adv_correct/total
natural_acc = 100.*natural_correct/total
print('Natural acc:', natural_acc)
print('Robust acc:', robust_acc)
return natural_acc, robust_acc
def main():
lr = args.lr
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=2e-4)
for epoch in range(args.epochs):
adjust_learning_rate(optimizer, epoch, lr)
train_loss = train(epoch, optimizer)
if (epoch+1)%args.val_period == 0:
natural_val, robust_val = test(epoch, optimizer)
if __name__ == '__main__':
main()