-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate_attack.py
More file actions
206 lines (151 loc) · 7.65 KB
/
evaluate_attack.py
File metadata and controls
206 lines (151 loc) · 7.65 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
198
199
200
201
202
203
204
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pandas as pd
from tqdm import tqdm
import argparse, copy, random, os, time, math
from attack_utils import *
from utils import seed_everything, get_dataloaders_cifar10, get_dataloaders_cifar100, get_dataloaders_imagenet, clip_tensor
from cifar10_models import load_cifar10_model
from cifar100_models import load_cifar100_model
from imagenet_models import load_imagenet_model
from new_utils import AverageMeter
from torch.cuda.amp import autocast
def accuracy(output, target, topk=(1, )):
"""Computes the precision@k for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
@torch.no_grad()
def evaluate_new(val_loader, model, criterion, trigger, mask):
losses = AverageMeter()
accs = AverageMeter()
asrs = AverageMeter()
# switch to evaluate mode
model.eval()
trigger = clip_tensor(trigger, mean, std)
pbar = tqdm(enumerate(val_loader), total=len(val_loader))
for i, (input, target) in pbar:
target = target.cuda()
attack_target = torch.ones_like(target) * args.target_class
input = input.cuda()
pert_input = add_trigger(input, trigger, mask)
all_data = torch.cat([input, pert_input], dim=0)
# compute output
with autocast(enabled=args.mixed_precision):
all_output = model(all_data)
output, pert_output = torch.split(all_output, input.size(0), dim=0)
# measure accuracy and record loss
acc = accuracy(output.data, target, topk=(1,))[0]
asr = accuracy(pert_output.data, attack_target, topk=(1,))[0]
accs.update(acc.item(), input.size(0))
asrs.update(asr.item(), input.size(0))
pbar.set_description(f'Acc: {accs.avg:.4f}, ASR: {asrs.avg:.4f}')
return accs.avg, asrs.avg
def count_parameter_differences(model_a, model_b):
total_differences = 0
layer_idx = 0
for (name_a, param_a), (name_b, param_b) in zip(model_a.named_parameters(), model_b.named_parameters()):
# Ensure you're comparing the same parameters by name
assert name_a == name_b, f"Parameter names do not match: {name_a} vs {name_b}"
# print(name_a)
# if 'conv' in name_a and 'weight' in name_a or 'downsample' in name_a and 'weight' in name_a:
# layer_idx += 1
# Count differences
differences = torch.ne(param_a, param_b).sum().item()
# print(f"Differences in {name_a}: {differences}")
total_differences += differences
# if differences > 0:
# print(f"Differences in {layer_idx}: {differences}")
print(f"Total parameter changes: {total_differences}")
print(f"Total parameter changes for 5 address changes should be <= 640 (5 * 128)")
return total_differences
def CLP(net, u=3):
params = net.state_dict()
for name, m in net.named_modules():
if isinstance(m, nn.BatchNorm2d):
std = m.running_var.sqrt()
weight = m.weight
channel_lips = []
for idx in range(weight.shape[0]):
# Combining weights of convolutions and BN
# original
w = conv.weight[idx].reshape(conv.weight.shape[1], -1) * (weight[idx]/std[idx]).abs()
channel_lips.append(torch.svd(w.cpu())[1].max())
channel_lips = torch.Tensor(channel_lips)
index = torch.where(channel_lips>channel_lips.mean() + u*channel_lips.std())[0]
# print(channel_lips[index])
params[name+'.weight'][index] = 0
params[name+'.bias'][index] = 0
# Convolutional layer should be followed by a BN layer by default
elif isinstance(m, nn.Conv2d):
conv = m
# print(name.replace('bn','conv').replace('downsample.1','downsample.0'), 'conv')
net.load_state_dict(params)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Deep-TROJ')
parser.add_argument('--batch_size', type=int, default=128, help='input batch size, 128 default')
parser.add_argument('--model', type=str, default='resnet18', help='model type')
parser.add_argument('--target_class', type=int, default=1, help='target_class')
parser.add_argument('--n_blocks', type=int, default=10, help='target_class')
parser.add_argument('--rounds', type=int, default=10, help='target_class')
parser.add_argument('--device', type=str, default='cuda:0', help='device id')
parser.add_argument('--seed', type=int, default=6, help='random seed')
parser.add_argument('--exp_path', type=str, default='results_n_blocks_5', help='experiment path for saving results')
parser.add_argument('--dataset', type=str, default='imagenet', help='dataset name')
parser.add_argument('--mixed_precision', action='store_true', help='mixed precision')
parser.add_argument('--defense', action='store_true', help='Apply detection based defense')
args = parser.parse_args()
print(args)
torch.cuda.set_device(args.device)
seed_everything(args.seed)
if args.dataset == 'cifar10' or args.dataset == 'cifar100':
mean = [0.4914, 0.4822, 0.4465]
std = [0.2023, 0.1994, 0.2010]
elif args.dataset == 'imagenet':
mean = [0.4914, 0.4822, 0.4465]
std = [0.2023, 0.1994, 0.2010]
file_root = f'{args.exp_path}/{args.dataset}/{args.target_class}/{args.model}'
# load data
if args.dataset == 'cifar10':
train_loader, test_loader = get_dataloaders_cifar10()
elif args.dataset == 'cifar100':
train_loader, test_loader = get_dataloaders_cifar100()
elif args.dataset == 'imagenet':
train_loader, test_loader = get_dataloaders_imagenet()
# load model for attack optimization
if args.dataset == 'cifar10':
model = load_cifar10_model(args)
elif args.dataset == 'cifar100':
model = load_cifar100_model(args)
elif args.dataset == 'imagenet':
model = load_imagenet_model(args)
add_mask(model)
dummy_trigger = torch.zeros((1, 3, 32, 32)).cuda() if args.dataset in ['cifar10', 'cifar100'] else torch.zeros((1, 3, 224, 224)).cuda()
mask = torch.zeros((1, 3, 32, 32)).cuda() if args.dataset in ['cifar10', 'cifar100'] else torch.zeros((1, 3, 224, 224)).cuda()
test_acc, test_asr = evaluate_new(test_loader, model, F.cross_entropy, dummy_trigger, mask)
print('before attack test acc:', test_acc, 'before attack test asr:', test_asr)
original_model = copy.deepcopy(model)
file_root = f'{args.exp_path}/{args.dataset}/{args.target_class}/{args.model}'
model_state_dict = torch.load(f'{file_root}/model.pth', map_location='cuda')
model.load_state_dict(model_state_dict)
trigger = torch.load(f'{file_root}/trigger.pth', map_location='cuda')
mask = torch.load(f'{file_root}/mask.pth', map_location='cuda')
if args.defense:
CLP(model, u=1.0)
test_acc, test_asr = evaluate_new(test_loader, model, F.cross_entropy, trigger, mask)
print('after attack test acc:', test_acc, 'after attack test asr:', test_asr)
# 640 is the maximum number of parameter changes for 5 address changes
# But CLP will change more parameters
# Should not print anything if defense is applied
if not args.defense:
count_parameter_differences(original_model, model)