-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
152 lines (129 loc) · 3.88 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
"""
Main file to evaluate the model on the test set and attack the model with MUFIA. Command line arguments are used to customize the attack as shown in run.sh file.
"""
import wandb
import sys
import argparse
import torch
# fix torch seed
torch.manual_seed(42)
# fix cuda seed
torch.cuda.manual_seed(42)
# fix cudnn seed for reproducibility
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.empty_cache()
sys.path.pop()
sys.path.insert(0, "..")
sys.path.append("../")
from misc.utils import *
from data.data import *
from models.model_loader import *
from eval.classic_eval import Evaluator
from eval.y_eval import Y_Evaluator
import configs
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--threat_model",
type=str,
default="std",
help="Norm for adversarially trained model",
choices=["linf", "l2", "std", "untrained", "prime", "augmix", "cc_sota",],
)
parser.add_argument(
"--dataset",
type=str,
default="cifar10",
help="Dataset",
choices=["cifar10", "imagenet", "cifar100",],
)
parser.add_argument(
"--batch_size", default=256, type=int, help="Batch size for model evaluation",
)
parser.add_argument(
"--lr", default=0.1, type=float, help="Learning rate for MUFIA attack",
)
parser.add_argument(
"--lambda_reg",
default=20.0,
type=float,
help="Regularization for MUFIA attack",
)
parser.add_argument(
"--save_mat",
action="store_true",
help="If true, save the adversarial filter bank tensor for each image",
)
parser.add_argument(
"--n_epochs",
default=100,
type=int,
help="Number of iterations for MUFIA attack",
)
parser.add_argument(
"--print_every",
default=10,
type=int,
help="Print losses for every n iterations of MUFIA attack",
)
parser.add_argument(
"--atk_type",
choices=["clean", "mufia"],
default="mufia",
help="Evaluation/Attack type",
)
parser.add_argument(
"--block_size", default=32, type=int, help="number of blocks for DCT"
)
parser.add_argument("--model_name", default="resnet50", type=str, help="Model name")
parser.add_argument(
"--parallel", action="store_true", help="Use DataParallel for model"
)
parser.add_argument("--verbose", action="store_true", help="Print losses in attack")
parser.add_argument(
"--sim_loss",
default="cosine",
type=str,
help="Type of loss for similarity of DCT",
)
parser.add_argument(
"--kappa",
default=0.9,
type=float,
help="Hinge Loss for Cosine Mis classification",
)
args = parser.parse_args()
param = vars(args)
config_wandb = dict(defense=param)
run_name = run_name_generator(param)
save_name = save_name_generator(param)
param["save_name"] = save_name
param["save_dir"] = configs.directory_names["save_dir"]
logger = wandb.init(
entity=configs.wandb_config["entity"],
project=configs.wandb_config["project"],
reinit=configs.wandb_config["reinit"],
name=run_name,
config=config_wandb,
)
"""
Set Dataloaders and Model
"""
data_loading = DataLoading(params=param)
_, _, testset, testloader = data_loading.get_data()
param["dataloader"] = testloader
param["logger"] = logger
model_loading = ModelLoader(params=param, device=device)
net = model_loading.get_model()
net = net.to(device)
net = net.eval()
print("Model loaded")
"""
Evaluations and Attacks
"""
eval = Y_Evaluator(device, net, param, logger)
eval.attack_model()
wandb.finish()