forked from SparseEvoAttack/SparseEvoAttack.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_pgd.py
163 lines (119 loc) · 5.46 KB
/
utils_pgd.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
import torch
import torch.nn as nn
import torchvision.datasets as datasets
import torch.utils.data as data
import torchvision.transforms as transforms
import numpy as np
def get_logits(model, x_nat):
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
with torch.no_grad():
output = model(x.cuda())
return output.cpu().numpy()
def get_logits_norm(model, x_nat):
# ============================
normalize = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
# ============================
# step 1: transpose/permute dimension
# input must be [n,C,W,H] due to normalization and model's input is in that format
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float() # input [n,W,H,C] => [n,C,W,H]
#x = torch.from_numpy(x_nat).permute(0, 2, 3, 1).float()
# step 2: normalize input to fit model trained under normalized data
with torch.no_grad():
norm_img = normalize(x[0])
x = torch.unsqueeze(norm_img, 0)
output = model(x.cuda())
return output.cpu().numpy()
def get_predictions(model, x_nat, y_nat):
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
y = torch.from_numpy(y_nat)
with torch.no_grad():
output = model(x.cuda())
return (output.cpu().max(dim=-1)[1] == y).numpy()
def get_predictions_norm(model, x_nat, y_nat,mu,std):
# ============================
#normalize = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
normalize = transforms.Normalize(mu, std)
# ============================
y = torch.from_numpy(y_nat)
#print(x_nat.shape)
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
#x = torch.from_numpy(x_nat).permute(0, 2, 3, 1).float()
#print(x.shape)
with torch.no_grad():
norm_img = normalize(x[0])
x = torch.unsqueeze(norm_img, 0)
output = model(x.cuda())
#print('adv label:',output.cpu().max(dim=-1)[1])
return (output.cpu().max(dim=-1)[1] == y).numpy()
def get_predictions_norm_target(model, x_nat, y_nat,mu,std):
# ============================
#normalize = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
normalize = transforms.Normalize(mu, std)
# ============================
y = torch.from_numpy(y_nat)
#print(x_nat.shape)
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
#x = torch.from_numpy(x_nat).permute(0, 2, 3, 1).float()
#print(x.shape)
with torch.no_grad():
norm_img = normalize(x[0])
x = torch.unsqueeze(norm_img, 0)
output = model(x.cuda())
#print('adv label:',output.cpu().max(dim=-1)[1])
return (output.cpu().max(dim=-1)[1] != y).numpy()
def get_predictions_and_gradients(model, x_nat, y_nat):
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
x.requires_grad_()
y = torch.from_numpy(y_nat)
with torch.enable_grad():
output = model(x.cuda())
loss = nn.CrossEntropyLoss()(output, y.cuda())
grad = torch.autograd.grad(loss, x)[0]
grad = grad.detach().permute(0, 2, 3, 1).numpy()
pred = (output.detach().cpu().max(dim=-1)[1] == y).numpy()
return pred, grad
def get_predictions_and_gradients_norm(model, x_nat, y_nat,mu,std):
# ============================
#normalize = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
normalize = transforms.Normalize(mu, std)
# ============================
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
#x = torch.from_numpy(x_nat).permute(0, 2, 3, 1).float()
with torch.no_grad():
norm_img = normalize(x[0])
x = torch.unsqueeze(norm_img, 0)
x.requires_grad_()
y = torch.from_numpy(y_nat)
with torch.enable_grad():
output = model(x.cuda())
loss = nn.CrossEntropyLoss()(output, y.cuda())
#print('loss:',loss.item())
grad = torch.autograd.grad(loss, x)[0]
grad = grad.detach().permute(0, 2, 3, 1).numpy()
#grad = grad.detach().numpy()
#print('loss:',loss.item(),'; Marginal:',(output.detach().max().item()-output[:,y].item()),'; top pred:',output.detach().cpu().max(dim=-1)[1])
pred = (output.detach().cpu().max(dim=-1)[1] == y).numpy()
return pred, grad
def get_predictions_and_gradients_norm_target(model, x_nat, y_nat,mu,std):
# ============================
#normalize = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
normalize = transforms.Normalize(mu, std)
# ============================
x = torch.from_numpy(x_nat).permute(0, 3, 1, 2).float()
#x = torch.from_numpy(x_nat).permute(0, 2, 3, 1).float()
with torch.no_grad():
norm_img = normalize(x[0])
x = torch.unsqueeze(norm_img, 0)
x.requires_grad_()
y = torch.from_numpy(y_nat)
with torch.enable_grad():
output = model(x.cuda())
#loss = nn.CrossEntropyLoss()(output, y.cuda())
loss = 2*output[:,y].sum() - output.sum()
#print('loss:',loss.item(),'target class:',y,output.shape)
#print('loss:',loss.item(),'; Marginal:',(output.detach().max().item()-output[:,y].item()),'; top pred:',output.detach().cpu().max(dim=-1)[1])
grad = torch.autograd.grad(loss, x)[0]
grad = grad.detach().permute(0, 2, 3, 1).numpy()
#grad = grad.detach().numpy()
pred = (output.detach().cpu().max(dim=-1)[1] != y).numpy() #if still different from target one
return pred, grad