-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
272 lines (221 loc) · 11 KB
/
test.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import os
import datetime
import argparse
import numpy as np
import torch
import torch.distributed as dist
from torch.multiprocessing import Process
import torchvision.utils as tvu
import wandb
from attacks.pgd_eot import PGD
from attacks.pgd_eot_l2 import PGDL2
from attacks.pgd_eot_bpda import BPDA
from load_data import load_dataset_by_name
from load_model import load_models
from purification import PurificationForward, PurificationForward_mimic
from utils import copy_source
from path import *
def get_diffusion_params(max_timesteps, num_denoising_steps):
max_timestep_list = [int(i) for i in max_timesteps.split(',')]
num_denoising_steps_list = [int(i) for i in num_denoising_steps.split(',')]
assert len(max_timestep_list) == len(num_denoising_steps_list)
diffusion_steps = []
for i in range(len(max_timestep_list)):
diffusion_steps.append([i - 1 for i in range(max_timestep_list[i] // num_denoising_steps_list[i],
max_timestep_list[i] + 1, max_timestep_list[i] // num_denoising_steps_list[i])])
max_timestep_list[i] = max_timestep_list[i] - 1
return max_timestep_list, diffusion_steps
def predict(x, args, defense_forward, num_classes):
ensemble = torch.zeros(x.shape[0], num_classes).to(x.device)
for _ in range(args.num_ensemble_runs):
_x = x.clone()
logits = defense_forward(_x)
pred = logits.max(1, keepdim=True)[1]
for idx in range(x.shape[0]):
ensemble[idx, pred[idx]] += 1
pred = ensemble.max(1, keepdim=True)[1]
return pred
def test(args):
if args.use_wandb:
wandb.init(project=args.wandb_project_name)
model_src = diffusion_model_path[args.dataset]
is_imagenet = True if args.dataset == 'imagenet' else False
dataset_root = imagenet_path if is_imagenet else './dataset'
num_classes = 1000 if is_imagenet else 10
# Set test directory name
exp_dir = './result/{}/{}-max_t_{}-denoising_step-{}-att_{}'.format(
args.exp,
args.def_sampling_method,
args.def_max_timesteps,
args.def_num_denoising_steps,
args.att_num_denoising_steps,
)
os.makedirs('./result', exist_ok=True)
os.makedirs('./result/{}'.format(args.exp), exist_ok=True)
os.makedirs(exp_dir, exist_ok=True)
os.makedirs('{}/imgs'.format(exp_dir), exist_ok=True)
copy_source(__file__, exp_dir)
# Device
device = torch.device('cuda')
# Load dataset
assert 512 % args.batch_size == 0
testset = load_dataset_by_name(args.dataset, dataset_root, 512)
testLoader = torch.utils.data.DataLoader(testset,
batch_size=args.batch_size,
num_workers=2,
pin_memory=True,
drop_last=False)
# Load models
clf, diffusion = load_models(args, model_src, device)
# Process diffusion hyperparameters
def_max_timesteps, def_diffusion_steps = get_diffusion_params(
args.def_max_timesteps, args.def_num_denoising_steps)
att_max_timesteps, att_diffusion_steps = get_diffusion_params(
args.att_max_timesteps, args.att_num_denoising_steps)
print('def_max_timesteps: ', def_max_timesteps)
print('def_diffusion_steps: ', def_diffusion_steps)
print('def_sampling_method: ', args.def_sampling_method)
print('att_max_timesteps: ', att_max_timesteps)
print('att_diffusion_steps: ', att_diffusion_steps)
print('att_sampling_method: ', args.att_sampling_method)
# Set diffusion process for attack and defense
attack_forward = PurificationForward(
clf, diffusion, att_max_timesteps, att_diffusion_steps, args.att_sampling_method, is_imagenet, device)
defense_forward = PurificationForward_mimic(
clf, diffusion, def_max_timesteps, def_diffusion_steps, args.def_sampling_method, is_imagenet, device)
# Set adversarial attack
if args.dataset == 'cifar10':
print('[Dataset] CIFAR-10')
if args.attack_method == 'pgd': # PGD Linf
eps = 8./255.
attack = PGD(attack_forward, attack_steps=args.n_iter,
eps=eps, step_size=0.007, eot=args.eot)
print('[Attack] PGD Linf | attack_steps: {} | eps: {:.3f} | eot: {}'.format(
args.n_iter, eps, args.eot))
elif args.attack_method == 'pgd_l2': # PGD L2
eps = 0.5
attack = PGDL2(attack_forward, attack_steps=args.n_iter,
eps=eps, step_size=0.007, eot=args.eot)
print('[Attack] PGD L2 | attack_steps: {} | eps: {} | eot: {}'.format(
args.n_iter, eps, args.eot))
elif args.attack_method == 'bpda': # BPDA
eps = 8./255.
attack = BPDA(attack_forward, attack_steps=args.n_iter,
eps=eps, step_size=0.007, eot=args.eot)
print('[Attack] BPDA Linf | attack_steps: {} | eps: {:.3f} | eot: {}'.format(
args.n_iter, eps, args.eot))
elif args.dataset == 'imagenet':
print('[Dataset] ImageNet')
eps = 4./255.
attack = PGD(attack_forward, attack_steps=args.n_iter,
eps=eps, step_size=0.007, eot=args.eot)
print('[Attack] ImageNet | PGD Linf | attack_steps: {} | eps: {} | eot: {}'.format(
args.n_iter, eps, args.eot))
elif args.dataset == 'svhn':
print('[Dataset] SVHN')
eps = 8./255.
attack = PGD(attack_forward, attack_steps=args.n_iter,
eps=eps, step_size=0.007, eot=args.eot)
print('[Attack] PGD Linf | attack_steps: {} | eps: {:.3f} | eot: {}'.format(
args.n_iter, eps, args.eot))
correct_nat = torch.tensor([0]).to(device)
correct_adv = torch.tensor([0]).to(device)
total = torch.tensor([0]).to(device)
std_nat_collector = []
std_adv_collector = []
for idx, (x, y) in enumerate(testLoader):
x = x.to(device)
y = y.to(device)
clf.eval()
diffusion.eval()
x_adv = attack(x, y)
with torch.no_grad():
pred_nat = predict(x, args, defense_forward, num_classes)
correct_nat += pred_nat.eq(y.view_as(pred_nat)).sum().item()
pred_adv = predict(x_adv, args, defense_forward, num_classes)
correct_adv += pred_adv.eq(y.view_as(pred_adv)).sum().item()
total += x.shape[0]
std_nat_collector.append((correct_nat / total *
100).item())
std_adv_collector.append((correct_adv / total * 100).item())
print('rank {} | {} | num_samples: {} | acc_nat: {:.3f}% | acc_adv: {:.3f}%'.format(
0, idx, total.item(), (correct_nat / total *
100).item(), (correct_adv / total * 100).item()
))
print('rank {} | num_samples: {} | acc_nat: {:.3f}% | acc_adv: {:.3f}%'.format(
0, total.item(), (correct_nat / total *
100).item(), (correct_adv / total * 100).item()
))
std_nat_collector = np.array(std_nat_collector)
std_adv_collector = np.array(std_adv_collector)
std_nat = np.std(std_nat_collector, ddof=1)
std_adv = np.std(std_adv_collector, ddof=1)
print(f" the nat std is: {std_nat}, the adv std is: {std_adv}")
def parse_args():
parser = argparse.ArgumentParser(description=globals()['__doc__'])
parser.add_argument('--seed', type=int, default=0, help='Random seed')
parser.add_argument("--use_cuda", action='store_true',
help="Whether use gpu or not")
parser.add_argument("--use_wandb", action='store_true',default=False,
help="Whether use wandb or not")
parser.add_argument("--wandb_project_name",
default='test', help="Wandb project name")
parser.add_argument('--exp', type=str,
default='test', help='Experiment name')
parser.add_argument("--dataset", type=str, default='cifar10',
choices=['cifar10', 'imagenet', 'svhn'])
parser.add_argument('--batch_size', type=int, default=16)
# Attack
parser.add_argument("--attack_method", type=str, default='pgd',
choices=['pgd', 'pgd_l2', 'bpda'])
parser.add_argument('--n_iter', type=int, default=200,
help='The nubmer of iterations for the attack generation')
parser.add_argument('--eot', type=int, default=20,
help='The number of EOT samples for the attack')
# Purification hyperparameters in defense
parser.add_argument("--def_max_timesteps", type=str,default="1000",
help='The number of forward steps for each purification step in defense')
parser.add_argument('--def_num_denoising_steps', type=str,default="100",
help='The number of denoising steps for each purification step in defense')
parser.add_argument('--def_sampling_method', type=str, default='ddpm', choices=['ddpm', 'ddim'],
help='Sampling method for the purification in defense')
parser.add_argument('--num_ensemble_runs', type=int, default=1,
help='The number of ensemble runs for purification in defense')
# Purification hyperparameters in attack generation
parser.add_argument("--att_max_timesteps", type=str,default="200",
help='The number of forward steps for each purification step in attack')
parser.add_argument('--att_num_denoising_steps', type=str, default="5",
help='The number of denoising steps for each purification step in attack')
parser.add_argument('--att_sampling_method', type=str,default="ddpm",
help='Sampling method for the purification in attack')
# Torch DDP
parser.add_argument('--num_proc_node', type=int, default=1,
help='The number of nodes in multi node env.')
parser.add_argument('--num_process_per_node', type=int, default=1,
help='Number of gpus')
parser.add_argument('--node_rank', type=int, default=0,
help='The index of node.')
parser.add_argument('--local_rank', type=int, default=0,
help='Rank of process in the node')
parser.add_argument('--master_address', type=str, default='localhost',
help='Address for master')
parser.add_argument('--port', type=str, default='1234',
help='Port number for torch ddp')
args = parser.parse_args()
# Set seed
torch.manual_seed(args.seed)
np.random.seed(args.seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.benchmark = True
return args
def cleanup():
dist.destroy_process_group()
def init_processes(rank, size, fn, args):
fn(args)
if __name__ == '__main__':
args = parse_args()
args.world_size = args.num_proc_node * args.num_process_per_node
size = args.num_process_per_node
print(args)
init_processes(0, size, test, args)