forked from kiwi12138/RealisticTTA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.py
422 lines (317 loc) · 13.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# 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
# ---------------------------------- Misc options --------------------------- #
# Setting - see README.md for more information
# _C.SETTING = "mixed_domains"
_C.SETTING = "continual"
# Data directory
_C.DATA_DIR = "./data"
# Weight directory
_C.CKPT_DIR = "./ckpt"
# Output directory
_C.SAVE_DIR = "./output"
# Path to a specific checkpoint
_C.CKPT_PATH = ""
# Log destination (in SAVE_DIR)
_C.LOG_DEST = "log.txt"
# Log datetime
_C.LOG_TIME = ''
# Seed to use. If None, seed is not set!
# Note that non-determinism is still present due to non-deterministic GPU ops.
_C.RNG_SEED = 1
# Deterministic experiments.
_C.DETERMINISM = False
# Optional description of a config
_C.DESC = ""
# # Config destination (in SAVE_DIR)
# _C.CFG_DEST = "cfg.yaml"
# ----------------------------- Model options ------------------------------- #
_C.MODEL = CfgNode()
# Some of the available models can be found here:
# Torchvision: https://pytorch.org/vision/0.14/models.html
# timm: https://github.com/huggingface/pytorch-image-models/tree/v0.6.13
# RobustBench: https://github.com/RobustBench/robustbench
_C.MODEL.ARCH = 'Standard'
# Type of pre-trained weights for torchvision models. See: https://pytorch.org/vision/0.14/models.html
_C.MODEL.WEIGHTS = "IMAGENET1K_V1"
# Inspect the cfgs directory to see all possibilities
_C.MODEL.ADAPTATION = 'source'
# Reset the model before every new batch
_C.MODEL.EPISODIC = False
# ----------------------------- Corruption options -------------------------- #
_C.CORRUPTION = CfgNode()
# Dataset for evaluation
_C.CORRUPTION.DATASET = 'cifar10_c'
# 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. If num_ex is changed, each sequence is subsampled to the specified amount
# For ImageNet-C, RobustBench loads a list containing 5000 samples.
_C.CORRUPTION.NUM_EX = -1
# ------------------------------- Batch norm options ------------------------ #
_C.BN = CfgNode()
# BN alpha (1-alpha) * src_stats + alpha * test_stats
_C.BN.ALPHA = 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
# --------------------------------- Mean teacher options -------------------- #
_C.M_TEACHER = CfgNode()
# Mean teacher momentum for EMA update
_C.M_TEACHER.MOMENTUM = 0.999
# --------------------------------- Contrastive options --------------------- #
_C.CONTRAST = CfgNode()
# Temperature term for contrastive learning
_C.CONTRAST.TEMPERATURE = 0.1
# Output dimension of projector
_C.CONTRAST.PROJECTION_DIM = 128
# Contrastive mode
_C.CONTRAST.MODE = 'all'
# --------------------------------- CoTTA options --------------------------- #
_C.COTTA = CfgNode()
# Restore probability
_C.COTTA.RST = 0.01
# Average probability for TTA
_C.COTTA.AP = 0.92
# --------------------------------- GTTA options ---------------------------- #
_C.GTTA = CfgNode()
_C.GTTA.STEPS_ADAIN = 1
_C.GTTA.PRETRAIN_STEPS_ADAIN = 20000
_C.GTTA.LAMBDA_MIXUP = 1/3
_C.GTTA.USE_STYLE_TRANSFER = False
# --------------------------------- RMT options ----------------------------- #
_C.RMT = CfgNode()
_C.RMT.LAMBDA_CE_SRC = 1.0
_C.RMT.LAMBDA_CE_TRG = 1.0
_C.RMT.LAMBDA_CONT = 1.0
_C.RMT.NUM_SAMPLES_WARM_UP = 50000
# --------------------------------- AdaContrast options --------------------- #
_C.ADACONTRAST = CfgNode()
_C.ADACONTRAST.QUEUE_SIZE = 16384
_C.ADACONTRAST.CONTRAST_TYPE = "class_aware"
_C.ADACONTRAST.CE_TYPE = "standard" # ["standard", "symmetric", "smoothed", "soft"]
_C.ADACONTRAST.ALPHA = 1.0 # lambda for classification loss
_C.ADACONTRAST.BETA = 1.0 # lambda for instance loss
_C.ADACONTRAST.ETA = 1.0 # lambda for diversity loss
_C.ADACONTRAST.DIST_TYPE = "cosine" # ["cosine", "euclidean"]
_C.ADACONTRAST.CE_SUP_TYPE = "weak_strong" # ["weak_all", "weak_weak", "weak_strong", "self_all"]
_C.ADACONTRAST.REFINE_METHOD = "nearest_neighbors"
_C.ADACONTRAST.NUM_NEIGHBORS = 10
# --------------------------------- LAME options ----------------------------- #
_C.LAME = CfgNode()
_C.LAME.AFFINITY = "rbf"
_C.LAME.KNN = 5
_C.LAME.SIGMA = 1.0
_C.LAME.FORCE_SYMMETRY = False
# --------------------------------- EATA options ---------------------------- #
_C.EATA = CfgNode()
# Fisher alpha
_C.EATA.FISHER_ALPHA = 2000
# Number of samples for ewc regularization
_C.EATA.NUM_SAMPLES = 2000
# Diversity margin
_C.EATA.D_MARGIN = 0.05
# --------------------------------- SAR options ---------------------------- #
_C.SAR = CfgNode()
# Threshold e_m for model recovery scheme
_C.SAR.RESET_CONSTANT_EM = 0.2
# --------------------------------- ROTTA options ---------------------------- #
_C.ROTTA = CfgNode()
_C.ROTTA.MEMORY_SIZE = 64
_C.ROTTA.UPDATE_FREQUENCY = 64
_C.ROTTA.NU = 0.001
_C.ROTTA.ALPHA = 0.05
_C.ROTTA.LAMBDA_T = 1.0
_C.ROTTA.LAMBDA_U = 1.0
# --------------------------------- ROID options ---------------------------- #
_C.ROID = CfgNode()
_C.ROID.USE_WEIGHTING = True
_C.ROID.USE_PRIOR_CORRECTION = True
_C.ROID.USE_CONSISTENCY = True
_C.ROID.MOMENTUM_SRC = 0.99 # Momentum for weight ensembling
_C.ROID.MOMENTUM_PROBS = 0.9 # Momentum for diversity weighting
_C.ROID.TEMPERATURE = 1/3
# ------------------------------- Source options ---------------------------- #
_C.SOURCE = CfgNode()
# Number of workers for source data loading
_C.SOURCE.NUM_WORKERS = 4
# Percentage of source samples used
_C.SOURCE.PERCENTAGE = 1.0 # [0, 1] possibility to reduce the number of source samples
# ------------------------------- Testing options --------------------------- #
_C.TEST = CfgNode()
# Number of workers for test data loading
_C.TEST.NUM_WORKERS = 4
# Batch size for evaluation (and updates for norm + tent)
_C.TEST.BATCH_SIZE = 128
# Momentum for evaluation (and updates for norm + tent)
_C.TEST.MOMENTUM = 0.1
# If the batch size is 1, a sliding window approach can be applied by setting window length > 1
_C.TEST.WINDOW_LENGTH = 1
# Number of augmentations for methods relying on TTA (test time augmentation)
_C.TEST.N_AUGMENTATIONS = 32
# The alpha value of the dirichlet distribution used for sorting the class labels.
_C.TEST.ALPHA_DIRICHLET = 0.0
# --------------------------------- CUDNN options --------------------------- #
_C.CUDNN = CfgNode()
# Benchmark to select fastest CUDNN algorithms (best for fixed input sizes)
_C.CUDNN.BENCHMARK = True
# --------------------------------- 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_from_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", default='cfgs/cifar10_c/norm_TTBN.yaml',dest="cfg_file", type=str,
help="Config file location")#todo
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))
cfg.SAVE_DIR = os.path.join(cfg.SAVE_DIR, f"{cfg.MODEL.ADAPTATION}_{cfg.CORRUPTION.DATASET}_{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()
])
if cfg.RNG_SEED:
torch.manual_seed(cfg.RNG_SEED)
torch.cuda.manual_seed(cfg.RNG_SEED)
np.random.seed(cfg.RNG_SEED)
random.seed(cfg.RNG_SEED)
torch.backends.cudnn.benchmark = cfg.CUDNN.BENCHMARK
if cfg.DETERMINISM:
# enforce determinism
if hasattr(torch, "set_deterministic"):
torch.set_deterministic(True)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
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)
def complete_data_dir_path(root, dataset_name):
# map dataset name to data directory name
mapping = {"imagenet": "imagenet2012",
"imagenet_c": "ImageNet-C",
"imagenet_r": "imagenet-r",
"imagenet_k": os.path.join("ImageNet-Sketch", "sketch"),
"imagenet_a": "imagenet-a",
"imagenet_d": "imagenet-d", # do not change
"imagenet_d109": "imagenet-d", # do not change
"domainnet126": "DomainNet-126", # directory containing the 6 splits of "cleaned versions" from http://ai.bu.edu/M3SDA/#dataset
"office31": "office-31",
"visda": "visda-2017",
"cifar10": "", # do not change the following values
"cifar10_c": "",
"cifar100": "",
"cifar100_c": "",
}
return os.path.join(root, mapping[dataset_name])
def get_num_classes(dataset_name):
dataset_name2num_classes = {"cifar10": 10, "cifar10_c": 10, "cifar100": 100, "cifar100_c": 100,
"imagenet": 1000, "imagenet_c": 1000, "imagenet_k": 1000, "imagenet_r": 200,
"imagenet_a": 200, "imagenet_d": 164, "imagenet_d109": 109, "imagenet200": 200,
"domainnet126": 126, "office31": 31, "visda": 12
}
return dataset_name2num_classes[dataset_name]
def get_domain_sequence(ckpt_path):
assert ckpt_path.endswith('.pth') or ckpt_path.endswith('.pt')
domain = cfg.CKPT_PATH.replace('.pth', '').split(os.sep)[-1].split('_')[1]
mapping = {"real": ["clipart", "painting", "sketch"],
"clipart": ["sketch", "real", "painting"],
"painting": ["real", "sketch", "clipart"],
"sketch": ["painting", "clipart", "real"],
}
return mapping[domain]
def adaptation_method_lookup(adaptation):
lookup_table = {"source": "Norm",
"norm_test": "Norm",
"norm_alpha": "Norm",
"norm_ema": "Norm",
"TTBN": "Norm",
"ttaug": "TTAug",
"memo": "MEMO",
"lame": "LAME",
"tent": "Tent",
"eata": "EATA",
"sar": "SAR",
"adacontrast": "AdaContrast",
"cotta": "CoTTA",
"rotta": "RoTTA",
"gtta": "GTTA",
"rmt": "RMT",
"roid": "ROID"
}
assert adaptation in lookup_table.keys(), \
f"Adaptation method '{adaptation}' is not supported! Choose from: {list(lookup_table.keys())}"
return lookup_table[adaptation]