-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_autoattack.py
More file actions
197 lines (157 loc) · 6.53 KB
/
Copy patheval_autoattack.py
File metadata and controls
197 lines (157 loc) · 6.53 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
"""Evaluate OTAD-T-NN under AutoAttack (Linf + L2)."""
import argparse
import random
import torch
import numpy as np
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import os
from autoattack import AutoAttack
from models.models import CNNBlock, DMLResNet
from models.vit import ViT
from models.cipnet import CIPNet
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', type=str, default='0')
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
def set_random_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
seed = 42
set_random_seed(seed)
print('==> Preparing data..')
transform = transforms.Compose([transforms.ToTensor()])
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
subset_indices = range(1000)
test_subset = torch.utils.data.Subset(testset, subset_indices)
testloader = torch.utils.data.DataLoader(test_subset, batch_size=100, shuffle=True)
num_s = 10
def find_neighbors_batch(data, targets, k):
"""Find k nearest neighbors for a batch of target vectors."""
distances = torch.cdist(targets, data)
_, indices = torch.topk(distances, k, largest=False, dim=1)
return indices
# Load ViT backbone
state_dict = torch.load('./checkpoints/vit_cifar10.pth', weights_only=True)
new_state_dict = {}
for key, value in state_dict.items():
new_key = key.replace('model.', '')
new_state_dict[new_key] = value
net = ViT(3, 10, img_size=32, patch=8, dropout=0.0, mlp_hidden=384,
num_layers=7, hidden=384, head=12, is_cls_token=True).cuda()
net.load_state_dict(new_state_dict)
net.eval()
for param in net.parameters():
param.requires_grad = False
# Load precomputed OT data
OTrawdata = np.load('./precomputed/rawdata.npy')
OTinput_np = np.load('./precomputed/otinput.npy')
OToutput_np = np.load('./precomputed/otoutput.npy')
# Load DML ResNet
dmlnet = DMLResNet(CNNBlock, [2, 2, 2]).cuda()
dmlstate_dict = torch.load('./checkpoints/dml_resnet.pth', weights_only=True)
dmlnet.load_state_dict(dmlstate_dict)
dmlnet.eval()
for param in dmlnet.parameters():
param.requires_grad = False
# Build DML feature database
newlabels_np = np.load('./precomputed/labels.npy')
newdatas = torch.from_numpy(OTrawdata).float()
newlabels = torch.from_numpy(newlabels_np)
print(f'labels shape: {newlabels.shape}')
class MyDataset():
def __init__(self, data, labels):
imgs = []
for i in range(len(labels)):
imgs.append((data[i], labels[i]))
self.imgs = imgs
def __getitem__(self, index):
fn, label = self.imgs[index]
return fn, label
def __len__(self):
return len(self.imgs)
newtrainset = MyDataset(newdatas, newlabels)
newtrainloader = torch.utils.data.DataLoader(newtrainset, batch_size=128, shuffle=False)
for batch_idx, (inputs, targets) in enumerate(newtrainloader):
inputs = inputs.cuda().view(inputs.shape[0], 3, 32, 32)
outputs = dmlnet(inputs)
if batch_idx == 0:
dmlfeature = outputs.detach()
else:
dmlfeature = torch.cat((dmlfeature, outputs.detach()), dim=0)
print(f'DML feature shape: {dmlfeature.shape}')
# OTAD-T-NN defense
cipnet = CIPNet(num_neighbors=10*2, point_dim=24960, dim=2048, depth=6,
heads=8, mlp_dim=512, dropout=0.1).cuda()
cip_state_dict = torch.load('./checkpoints/cipnet.pth', weights_only=True)
cipnet.load_state_dict(cip_state_dict)
cipnet.eval()
for param in cipnet.parameters():
param.requires_grad = False
OTinput_tensor = torch.from_numpy(OTinput_np).cuda()
OToutput_tensor = torch.from_numpy(OToutput_np).cuda()
class OTAD_NN(nn.Module):
def __init__(self, net):
super(OTAD_NN, self).__init__()
self.net = net
def forward(self, x):
cls_token = dmlnet(x).detach()
indices = find_neighbors_batch(dmlfeature, cls_token, num_s)
input_neighbors = OTinput_tensor[indices]
output_neighbors = OToutput_tensor[indices]
embed = net.embedding(net.normalization(x)).view(x.shape[0], -1)
neighbors = torch.cat((input_neighbors, output_neighbors), 1).cuda()
test_output = cipnet(embed, neighbors)
return test_output.view(x.shape[0], 65, 384)
class ClassifierWrapper(nn.Module):
def __init__(self, classifier):
super(ClassifierWrapper, self).__init__()
self.classifier = classifier
def forward(self, x):
return self.classifier(x)
wrapped_classifier = ClassifierWrapper(net.classifier)
defense = nn.Sequential(OTAD_NN(net))
defended_model = nn.Sequential(defense, wrapped_classifier)
defended_model.eval()
# --- L2 AutoAttack ---
total = 0
correct_robust = 0
correct_standard = 0
print(f'\n==> L2 AutoAttack (otad-t-nn)...')
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.cuda(), targets.cuda()
adversary = AutoAttack(defended_model, norm='L2', eps=0.5, version='standard', verbose=True)
inputs_adv = adversary.run_standard_evaluation(inputs, targets)
outputs_robust = net.classifier(defense(inputs_adv))
_, predicted_robust = outputs_robust.max(1)
correct_robust += predicted_robust.eq(targets).sum().item()
outputs_standard = net.classifier(defense(inputs))
_, predicted_standard = outputs_standard.max(1)
correct_standard += predicted_standard.eq(targets).sum().item()
total += targets.size(0)
if (batch_idx + 1) % 100 == 0:
print(f'[{total}] Robust Accuracy: {100.*correct_robust/total:.2f}% | '
f'Standard Accuracy: {100.*correct_standard/total:.2f}%')
# --- Linf AutoAttack ---
total = 0
correct_robust = 0
correct_standard = 0
print(f'\n==> Linf AutoAttack (otad-t-nn)...')
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.cuda(), targets.cuda()
adversary = AutoAttack(defended_model, norm='Linf', eps=8/255, version='standard', verbose=True)
inputs_adv = adversary.run_standard_evaluation(inputs, targets)
outputs_robust = net.classifier(defense(inputs_adv))
_, predicted_robust = outputs_robust.max(1)
correct_robust += predicted_robust.eq(targets).sum().item()
outputs_standard = net.classifier(defense(inputs))
_, predicted_standard = outputs_standard.max(1)
correct_standard += predicted_standard.eq(targets).sum().item()
total += targets.size(0)
if (batch_idx + 1) % 100 == 0:
print(f'[{total}] Robust Accuracy: {100.*correct_robust/total:.2f}% | '
f'Standard Accuracy: {100.*correct_standard/total:.2f}%')