-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.py
292 lines (218 loc) · 7.05 KB
/
conf.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import argparse
import os
import sys
import logging
import random
import torch
import numpy as np
from datetime import datetime
from iopath.common.file_io import g_pathmgr
from yacs.config import CfgNode as CfgNode
import wandb
# Global config object (example usage: from core.config import cfg)
_C = CfgNode()
cfg = _C
# ----------------------------- Model options ------------------------------- #
_C.MODEL = CfgNode()
# Check https://github.com/RobustBench/robustbench for available models
_C.MODEL.ARCH = "Standard"
# Choice of (source, norm, tent, ...)
_C.MODEL.ADAPTATION = "source"
_C.MODEL.NORM = "bn"
_C.MODEL.BN_STAT = "mean"
_C.MODEL.CONTINUAL = False
_C.MODEL.CKPT_PATH = "."
_C.MODEL.EPS = 0.0
# ----------------------------- Hyp(erparameter) options -------------------------- #
_C.HYP = CfgNode()
# EATA
_C.HYP.FISHER_ALPHA = 1
_C.HYP.D_MARGIN = 0.0
_C.HYP.E_MARGIN = 0.0
# SoTTA
_C.HYP.MEM_SIZE = 64
_C.HYP.UPDATE_EVERY_X = 64
_C.HYP.USE_LEARNED_STATS = True
_C.HYP.TEMPERATURE = 1.0
_C.HYP.HIGH_THRESHOLD = 0.99
# ----------------------------- Corruption options -------------------------- #
_C.CORRUPTION = CfgNode()
# Dataset for evaluation
_C.CORRUPTION.DATASET = "cifar10"
# Check https://github.com/hendrycks/robustness for corruption details
_C.CORRUPTION.TYPE = [
"gaussian_noise",
"shot_noise",
"impulse_noise",
"defocus_blur",
"glass_blur",
"motion_blur",
"zoom_blur",
"snow",
"frost",
"fog",
"brightness",
"contrast",
"elastic_transform",
"pixelate",
"jpeg_compression",
]
_C.CORRUPTION.SEVERITY = [3]
# Number of examples to evaluate (10000 for all samples in CIFAR-10)
_C.CORRUPTION.NUM_EX = 10000
# ------------------------------- Optimizer options ------------------------- #
_C.OPTIM = CfgNode()
# Number of updates per batch
_C.OPTIM.STEPS = 1
# Learning rate
_C.OPTIM.LR = 1e-3
# Choices: Adam, SGD
_C.OPTIM.METHOD = "Adam"
# Beta
_C.OPTIM.BETA = 0.9
# Momentum
_C.OPTIM.MOMENTUM = 0.9
# Momentum dampening
_C.OPTIM.DAMPENING = 0.0
# Nesterov momentum
_C.OPTIM.NESTEROV = True
# L2 regularization
_C.OPTIM.WD = 0.0
_C.OPTIM.TEMP = 1.0
# Set up in the config file (config.py)
_C.OPTIM.ADAPT = "ent"
_C.OPTIM.ADAPTIVE = False
_C.OPTIM.TBN = True
_C.OPTIM.UPDATE = True
# ------------------------------- Testing options --------------------------- #
_C.TEST = CfgNode()
# Batch size for evaluation (and updates for norm + tent)
_C.TEST.BATCH_SIZE = 128
_C.TEST.NUM_CLASS = 10
# Statistics
_C.TEST.EMA = False
_C.TEST.BN_MOMENTUM = 0.2
# ----------------------------------------------------------------------------- #
# ----------------------------------------------------------------------------- #
# ------------------------------- Attacking options --------------------------- #
# ----------------------------------------------------------------------------- #
# ----------------------------------------------------------------------------- #
_C.ATTACK = CfgNode()
# Attack methods
_C.ATTACK.METHOD = "PGD"
# The number of malicious sampes
_C.ATTACK.SOURCE = 10
# Targeted Number
_C.ATTACK.TARGET = 1
# L_inf bound
_C.ATTACK.EPS = 1.0
# attack update rate
_C.ATTACK.ALPHA = 0.00392157
# attack steps
_C.ATTACK.STEPS = 500
# attack white box, model is known
_C.ATTACK.WHITE = True
# the attack is targeted or not
_C.ATTACK.TARGETED = False
# the initialization point is random
_C.ATTACK.RAND = False
_C.ATTACK.OPTION = False
_C.ATTACK.WEIGHT_P = 0.0
_C.ATTACK.DFPIROR = 0.0
_C.ATTACK.DFTESTPIROR = 0.0
_C.ATTACK.FLayer = 0
# --------------------------------- CUDNN options --------------------------- #
_C.CUDNN = CfgNode()
# Benchmark to select fastest CUDNN algorithms (best for fixed input sizes)
_C.CUDNN.BENCHMARK = True
# ---------------------------------- Misc options --------------------------- #
# Optional description of a config
_C.DESC = ""
# Note that non-determinism is still present due to non-deterministic GPU ops
_C.RNG_SEED = 1
# Output directory
_C.SAVE_DIR = "./outputs"
# Data directory
_C.DATA_DIR = "./data"
# Log destination (in SAVE_DIR)
_C.LOG_DEST = "log.txt"
# Log datetime
_C.LOG_TIME = ""
# # Config destination (in SAVE_DIR)
# _C.CFG_DEST = "cfg.yaml"
# --------------------------------- Default config -------------------------- #
_CFG_DEFAULT = _C.clone()
_CFG_DEFAULT.freeze()
def assert_and_infer_cfg():
"""Checks config values invariants."""
err_str = "Unknown adaptation method."
assert _C.MODEL.ADAPTATION in ["source", "norm", "tent"]
err_str = "Log destination '{}' not supported"
assert _C.LOG_DEST in ["stdout", "file"], err_str.format(_C.LOG_DEST)
def merge_from_file(cfg_file):
with g_pathmgr.open(cfg_file, "r") as f:
cfg = _C.load_cfg(f)
_C.merge_from_other_cfg(cfg)
def dump_cfg():
"""Dumps the config to the output directory."""
cfg_file = os.path.join(_C.SAVE_DIR, _C.CFG_DEST)
with g_pathmgr.open(cfg_file, "w") as f:
_C.dump(stream=f)
def load_cfg(out_dir, cfg_dest="config.yaml"):
"""Loads config from specified output directory."""
cfg_file = os.path.join(out_dir, cfg_dest)
merge_from_file(cfg_file)
def reset_cfg():
"""Reset config to initial state."""
cfg.merge_from_other_cfg(_CFG_DEFAULT)
def load_cfg_fom_args(description="Config options."):
"""Load config from command line args and set any specified options."""
current_time = datetime.now().strftime("%y%m%d_%H%M%S")
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"--cfg", dest="cfg_file", type=str, required=True, help="Config file location"
)
# wandb
parser.add_argument(
"--wandb", action="store_true", help="Track the result using wandb"
)
parser.add_argument(
"--project", type=str, default="tta-defense", help="wandb project name"
)
parser.add_argument(
"opts",
default=None,
nargs=argparse.REMAINDER,
help="See conf.py for all options",
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
merge_from_file(args.cfg_file)
print(args.opts)
cfg.merge_from_list(args.opts)
cfg.wandb = args.wandb
cfg.project = args.project
log_dest = os.path.basename(args.cfg_file)
log_dest = log_dest.replace(".yaml", "_{}.txt".format(current_time))
g_pathmgr.mkdirs(cfg.SAVE_DIR)
cfg.LOG_TIME, cfg.LOG_DEST = current_time, log_dest
cfg.freeze()
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] [%(filename)s: %(lineno)4d]: %(message)s",
datefmt="%y/%m/%d %H:%M:%S",
handlers=[
logging.FileHandler(os.path.join(cfg.SAVE_DIR, cfg.LOG_DEST)),
logging.StreamHandler(),
],
)
np.random.seed(cfg.RNG_SEED)
torch.manual_seed(cfg.RNG_SEED)
random.seed(cfg.RNG_SEED)
torch.backends.cudnn.benchmark = cfg.CUDNN.BENCHMARK
logger = logging.getLogger(__name__)
version = [torch.__version__, torch.version.cuda, torch.backends.cudnn.version()]
logger.info("PyTorch Version: torch={}, cuda={}, cudnn={}".format(*version))
logger.info(cfg)