File size: 4,846 Bytes
381fcd6 |
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 |
import torch
import torch.nn as nn
import torch.nn.functional as F
from attacks import create_attack
import numpy as np
from torch.autograd import Variable
from contextlib import contextmanager
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
class ctx_noparamgrad(object):
def __init__(self, module):
self.prev_grad_state = get_param_grad_state(module)
self.module = module
set_param_grad_off(module)
def __enter__(self):
pass
def __exit__(self, *args):
set_param_grad_state(self.module, self.prev_grad_state)
return False
class ctx_eval(object):
def __init__(self, module):
self.prev_training_state = get_module_training_state(module)
self.module = module
set_module_training_off(module)
def __enter__(self):
pass
def __exit__(self, *args):
set_module_training_state(self.module, self.prev_training_state)
return False
@contextmanager
def ctx_noparamgrad_and_eval(module):
with ctx_noparamgrad(module) as a, ctx_eval(module) as b:
yield (a, b)
def get_module_training_state(module):
return {mod: mod.training for mod in module.modules()}
def set_module_training_state(module, training_state):
for mod in module.modules():
mod.training = training_state[mod]
def set_module_training_off(module):
for mod in module.modules():
mod.training = False
def get_param_grad_state(module):
return {param: param.requires_grad for param in module.parameters()}
def set_param_grad_state(module, grad_state):
for param in module.parameters():
param.requires_grad = grad_state[param]
def set_param_grad_off(module):
for param in module.parameters():
param.requires_grad = False
class MadrysLoss(nn.Module):
def __init__(self, step_size=0.007, epsilon=0.031, perturb_steps=10, beta=6.0,
distance='l_inf', cutmix=False, adjust_freeze=True, cutout=False,
cutout_length=16):
super(MadrysLoss, self).__init__()
self.step_size = step_size
self.epsilon = epsilon
self.perturb_steps = perturb_steps
self.beta = beta
self.distance = distance
self.cross_entropy = torch.nn.CrossEntropyLoss()
self.adjust_freeze = adjust_freeze
self.cutout = cutout
self.cutout_length = cutout_length
def forward(self, model, x_natural, labels): #optimizer
model.eval()
if self.adjust_freeze:
for param in model.parameters():
param.requires_grad = False
# generate adversarial example
x_adv = x_natural.detach() + self.step_size * torch.randn(x_natural.shape).to(device).detach()
if self.distance == 'l_inf':
adv_loss = 0
for _ in range(self.perturb_steps):
x_adv.requires_grad_()
loss_ce = self.cross_entropy(model(x_adv), labels)
grad = torch.autograd.grad(loss_ce, [x_adv])[0]
x_adv = x_adv.detach() + self.step_size * torch.sign(grad.detach())
x_adv = torch.min(torch.max(x_adv, x_natural - self.epsilon), x_natural + self.epsilon)
x_adv = torch.clamp(x_adv, 0.0, 1.0)
else:
x_adv = torch.clamp(x_adv, 0.0, 1.0)
x_adv = Variable(x_adv, requires_grad=False)
if self.adjust_freeze:
for param in model.parameters():
param.requires_grad = True
if self.cutout:
batch_size = x_adv.shape[0]
c, h, w = x_adv.shape[1], x_adv.shape[2], x_adv.shape[3]
mask = torch.ones(batch_size, c, h, w).float()
for j in range(batch_size):
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.cutout_length // 2, 0, h)
y2 = np.clip(y + self.cutout_length // 2, 0, h)
x1 = np.clip(x - self.cutout_length // 2, 0, w)
x2 = np.clip(x + self.cutout_length // 2, 0, w)
mask[j, :, y1: y2, x1: x2] = 0.0
x_adv = x_adv * mask.to(device)
model.train()
# optimizer.zero_grad()
logits = model(x_adv)
loss = self.cross_entropy(logits, labels)
return loss
def sat_loss(model, x, y,optimizer,step_size,epsilon,num_steps,attack_type,beta,criterion= torch.nn.CrossEntropyLoss()):
"""
Adversarial training (Madry et al, 2017).
"""
attack = create_attack(model, criterion, 'linf-pgd', epsilon, num_steps, step_size)
with ctx_noparamgrad_and_eval(model):
x_adv, _ = attack.perturb(x, y)
print(x_adv.shape)
y_adv = y
out = model(x_adv)
loss = criterion(out, y_adv)
return loss
|