-
Notifications
You must be signed in to change notification settings - Fork 47
/
conf.py
210 lines (153 loc) · 5.87 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
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""Configuration file (powered by YACS)."""
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
# 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)
# - source: baseline without adaptation
# - norm: test-time normalization
# - tent: test-time entropy minimization (ours)
_C.MODEL.ADAPTATION = 'source'
# By default tent is online, with updates persisting across batches.
# To make adaptation episodic, and reset the model for each batch, choose True.
_C.MODEL.EPISODIC = False
# ----------------------------- 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 = [5, 4, 3, 2, 1]
# Number of examples to evaluate (10000 for all samples in CIFAR-10)
_C.CORRUPTION.NUM_EX = 10000
# ------------------------------- Batch norm options ------------------------ #
_C.BN = CfgNode()
# BN epsilon
_C.BN.EPS = 1e-5
# BN momentum (BN momentum in PyTorch = 1 - BN momentum in Caffe2)
_C.BN.MOM = 0.1
# ------------------------------- 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
# ------------------------------- Testing options --------------------------- #
_C.TEST = CfgNode()
# Batch size for evaluation (and updates for norm + tent)
_C.TEST.BATCH_SIZE = 128
# --------------------------------- 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 = "./output"
# Data directory
_C.DATA_DIR = "./data"
# Weight directory
_C.CKPT_DIR = "./ckpt"
# 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")
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)
cfg.merge_from_list(args.opts)
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)