-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_unseen.py
184 lines (167 loc) · 6.87 KB
/
eval_unseen.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
from utils import *
import torch
import robustness
import os
import torchvision.datasets as datasets
import torch.utils.data as data
import torchvision.transforms as transforms
import torch.nn.functional as F
import inspect
import torch.nn as nn
unpruned_eb = sys.argv[1]
final_weights = sys.argv[2]
pct = sys.argv[3]
log_folder = sys.argv[4]
dataset = sys.argv[5]
model_type = sys.argv[6]
is_pruned = sys.argv[7]
is_pruned = True if is_pruned=='True' else False
pct = float(pct)
if is_pruned:
weight_before_prune = fix_robustness_ckpt(torch.load(unpruned_eb))
if model_type == 'resnet':
if dataset == 'cifar10':
model = resnet18(seed=0, num_classes=10)
model.load_state_dict(weight_before_prune, strict=False)
cfg = resprune(model.cuda(), pct)
elif dataset == 'cifar100':
model = resnet18(seed=0, num_classes=100)
model.load_state_dict(weight_before_prune, strict=False)
cfg = resprune(model.cuda(), pct)
os.system('pip install cifar2png')
os.system('cifar2png cifar100 cifar100')
if model_type == 'resnet50':
if dataset == 'cifar10':
model = resnet50_official(seed=0, num_classes=10)
model.load_state_dict(weight_before_prune, strict=False)
cfg = resprune(model.cuda(), pct)
elif dataset == 'cifar100':
model = resnet50_officla(seed=0, num_classes=100)
model.load_state_dict(weight_before_prune, strict=False)
cfg = resprune(model.cuda(), pct)
os.system('pip install cifar2png')
os.system('cifar2png cifar100 cifar100')
elif model_type == 'vgg':
if dataset == 'cifar10':
os.system(f'python "Early-Bird-Tickets/vggprune.py" \
--dataset cifar10 \
--test-batch-size 128 \
--depth 16 \
--percent {pct} \
--model "{unpruned_eb}" \
--save "tmp" \
--gpu_ids 0')
cfg = torch.load('tmp/pruned.pth.tar')['cfg']
elif dataset == 'cifar100':
os.system(f'python "Early-Bird-Tickets/vggprune.py" \
--dataset cifar100 \
--test-batch-size 128 \
--depth 16 \
--percent {pct} \
--model "{unpruned_eb}" \
--save "tmp" \
--gpu_ids 0')
cfg = torch.load('tmp/pruned.pth.tar')['cfg']
else:
cfg=None
transform_list = [transforms.ToTensor()]
transform_chain = transforms.Compose(transform_list)
print('DATASET',dataset)
if model_type == 'resnet50':
if dataset == 'cifar10':
model = resnet50_official(num_classes=10, cfg=cfg, seed=0)
model.load_state_dict(fix_robustness_ckpt(torch.load(final_weights)), strict=False)
ds = CIFAR('cifar')
item = datasets.CIFAR10(root='cifar', train=False, transform=transform_chain, download=True)
elif dataset == 'cifar100':
model = resnet50_official(num_classes=100, cfg=cfg, seed=0)
model.load_state_dict(fix_robustness_ckpt(torch.load(final_weights)), strict=False)
ds = CIFAR100('cifar100')
item = datasets.CIFAR100(root='cifar100', train=False, transform=transform_chain, download=True)
elif model_type == 'resnet':
if dataset == 'cifar10':
model = resnet18(num_classes=10, cfg=cfg, seed=0)
model.load_state_dict(fix_robustness_ckpt(torch.load(final_weights)), strict=False)
ds = CIFAR('cifar')
item = datasets.CIFAR10(root='cifar', train=False, transform=transform_chain, download=True)
elif dataset == 'cifar100':
model = resnet18(num_classes=100, cfg=cfg, seed=0)
model.load_state_dict(fix_robustness_ckpt(torch.load(final_weights)), strict=False)
ds = CIFAR100('cifar100')
item = datasets.CIFAR100(root='cifar100', train=False, transform=transform_chain, download=True)
elif model_type == 'vgg':
if dataset == 'cifar10':
model = vgg(16, dataset='cifar10', cfg=cfg, seed=0)
model.load_state_dict(fix_robustness_ckpt(torch.load(final_weights)), strict=False)
ds = CIFAR('cifar')
item = datasets.CIFAR10(root='cifar', train=False, transform=transform_chain, download=True)
elif dataset == 'cifar100':
model = vgg(16, dataset='cifar100', cfg=cfg, seed=0)
model.load_state_dict(fix_robustness_ckpt(torch.load(final_weights)), strict=False)
ds = CIFAR100('cifar100')
item = datasets.CIFAR100(root='cifar100', train=False, transform=transform_chain, download=True)
## AA EVAL ##
test_loader = data.DataLoader(item, batch_size=128, shuffle=False, num_workers=0)
from autoattack import AutoAttack
log = 'store/'+log_folder+'/AA_eval-new.txt'
model = model.cuda()
adversary = AutoAttack(model, norm='Linf', eps=8/255, log_path=log,version='standard')
adversary.attacks_to_run = ['apgd-t']
l = [x for (x, y) in test_loader]
x_test = torch.cat(l, 0).cuda()
l = [y for (x, y) in test_loader]
y_test = torch.cat(l, 0).cuda()
adv_complete = adversary.run_standard_evaluation(x_test, y_test,bs=128)
save_dir = 'store/'+log_folder
print(adv_complete)
torch.save({'adv_complete': adv_complete}, '{}/{}_{}_1_{}_eps_{:.5f}.pth'.format(
save_dir, 'aa', 'standard', adv_complete.shape[0], 8/255))
## PGD20 EVAL ##
out_store = cox.store.Store('store', exp_id=log_folder)
eval_pgd20_args = Parameters({
'constraint': 'inf',
'eps': 8/255,
'attack_lr': 2/255,
'epochs': 10,
'attack_steps': 20,
'save_ckpt_iters': -1,
'adv_eval': 1,
'use_best': 1,
'random_restarts': 0,
})
m, _ = make_and_restore_model(arch=model, dataset=ds)
train_loader, val_loader = ds.make_loaders(batch_size=128, workers=8)
pgd20_out = train.eval_model(eval_pgd20_args, m, val_loader, store=out_store)
print('[Log w/ pgd20-eval]',pgd20_out)
with open('store/'+log_folder+'/pgd20_eval-new.txt', 'a') as f:
f.write('[PGD-20 VAL] '+
str(pgd20_out['nat_prec1'].item())+' '+
str(pgd20_out['adv_prec1'].item())+' '+
str(pgd20_out['nat_loss'])+' '+
str(pgd20_out['adv_loss'])+' '+
str(pgd20_out['train_prec1'])+' '+
str(pgd20_out['train_loss'])+'\n')
# ## PGD7 EVAL ##
eval_pgd7_args = Parameters({
'constraint': 'inf',
'eps': 8/255,
'attack_lr': 2/255,
'epochs': 10,
'attack_steps': 7,
'save_ckpt_iters': -1,
'adv_eval': 1,
'use_best': 1,
'random_restarts': 0,
})
m, _ = make_and_restore_model(arch=model, dataset=ds)
train_loader, val_loader = ds.make_loaders(batch_size=256, workers=8)
pgd7_out = train.eval_model(eval_pgd7_args, m, val_loader, store=out_store)
print('[Log w/ pgd7-eval]',pgd7_out)
with open('store/'+log_folder+'/pgd7_eval-new.txt', 'a') as f:
f.write('[PGD-7 VAL] '+
str(pgd7_out['nat_prec1'].item())+' '+
str(pgd7_out['adv_prec1'].item())+' '+
str(pgd7_out['nat_loss'])+' '+
str(pgd7_out['adv_loss'])+' '+
str(pgd7_out['train_prec1'])+' '+
str(pgd7_out['train_loss'])+'\n')