Safemotion Lib
Loading...
Searching...
No Matches
config.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (c) Facebook, Inc. and its affiliates.
4#
5# This source code is licensed under the MIT license found in the
6# LICENSE file in the root directory of this source tree.
7
8"""Configuration file (powered by YACS)."""
9
10import argparse
11import os
12import sys
13
14from yacs.config import CfgNode as CfgNode
15
16
17# Global config object
18_C = CfgNode()
19
20# Example usage:
21# from core.config import cfg
22cfg = _C
23
24
25# ------------------------------------------------------------------------------------ #
26# Model options
27# ------------------------------------------------------------------------------------ #
28_C.MODEL = CfgNode()
29
30# Model type
31_C.MODEL.TYPE = ""
32
33# Number of weight layers
34_C.MODEL.DEPTH = 0
35
36# Number of classes
37_C.MODEL.NUM_CLASSES = 10
38
39# Loss function (see pycls/models/loss.py for options)
40_C.MODEL.LOSS_FUN = "cross_entropy"
41
42
43# ------------------------------------------------------------------------------------ #
44# ResNet options
45# ------------------------------------------------------------------------------------ #
46_C.RESNET = CfgNode()
47
48# Transformation function (see pycls/models/resnet.py for options)
49_C.RESNET.TRANS_FUN = "basic_transform"
50
51# Number of groups to use (1 -> ResNet; > 1 -> ResNeXt)
52_C.RESNET.NUM_GROUPS = 1
53
54# Width of each group (64 -> ResNet; 4 -> ResNeXt)
55_C.RESNET.WIDTH_PER_GROUP = 64
56
57# Apply stride to 1x1 conv (True -> MSRA; False -> fb.torch)
58_C.RESNET.STRIDE_1X1 = True
59
60
61# ------------------------------------------------------------------------------------ #
62# AnyNet options
63# ------------------------------------------------------------------------------------ #
64_C.ANYNET = CfgNode()
65
66# Stem type
67_C.ANYNET.STEM_TYPE = "simple_stem_in"
68
69# Stem width
70_C.ANYNET.STEM_W = 32
71
72# Block type
73_C.ANYNET.BLOCK_TYPE = "res_bottleneck_block"
74
75# Depth for each stage (number of blocks in the stage)
76_C.ANYNET.DEPTHS = []
77
78# Width for each stage (width of each block in the stage)
79_C.ANYNET.WIDTHS = []
80
81# Strides for each stage (applies to the first block of each stage)
82_C.ANYNET.STRIDES = []
83
84# Bottleneck multipliers for each stage (applies to bottleneck block)
85_C.ANYNET.BOT_MULS = []
86
87# Group widths for each stage (applies to bottleneck block)
88_C.ANYNET.GROUP_WS = []
89
90# Whether SE is enabled for res_bottleneck_block
91_C.ANYNET.SE_ON = False
92
93# SE ratio
94_C.ANYNET.SE_R = 0.25
95
96
97# ------------------------------------------------------------------------------------ #
98# RegNet options
99# ------------------------------------------------------------------------------------ #
100_C.REGNET = CfgNode()
101
102# Stem type
103_C.REGNET.STEM_TYPE = "simple_stem_in"
104
105# Stem width
106_C.REGNET.STEM_W = 32
107
108# Block type
109_C.REGNET.BLOCK_TYPE = "res_bottleneck_block"
110
111# Stride of each stage
112_C.REGNET.STRIDE = 2
113
114# Squeeze-and-Excitation (RegNetY)
115_C.REGNET.SE_ON = False
116_C.REGNET.SE_R = 0.25
117
118# Depth
119_C.REGNET.DEPTH = 10
120
121# Initial width
122_C.REGNET.W0 = 32
123
124# Slope
125_C.REGNET.WA = 5.0
126
127# Quantization
128_C.REGNET.WM = 2.5
129
130# Group width
131_C.REGNET.GROUP_W = 16
132
133# Bottleneck multiplier (bm = 1 / b from the paper)
134_C.REGNET.BOT_MUL = 1.0
135
136
137# ------------------------------------------------------------------------------------ #
138# EfficientNet options
139# ------------------------------------------------------------------------------------ #
140_C.EN = CfgNode()
141
142# Stem width
143_C.EN.STEM_W = 32
144
145# Depth for each stage (number of blocks in the stage)
146_C.EN.DEPTHS = []
147
148# Width for each stage (width of each block in the stage)
149_C.EN.WIDTHS = []
150
151# Expansion ratios for MBConv blocks in each stage
152_C.EN.EXP_RATIOS = []
153
154# Squeeze-and-Excitation (SE) ratio
155_C.EN.SE_R = 0.25
156
157# Strides for each stage (applies to the first block of each stage)
158_C.EN.STRIDES = []
159
160# Kernel sizes for each stage
161_C.EN.KERNELS = []
162
163# Head width
164_C.EN.HEAD_W = 1280
165
166# Drop connect ratio
167_C.EN.DC_RATIO = 0.0
168
169# Dropout ratio
170_C.EN.DROPOUT_RATIO = 0.0
171
172
173# ------------------------------------------------------------------------------------ #
174# Batch norm options
175# ------------------------------------------------------------------------------------ #
176_C.BN = CfgNode()
177
178# BN epsilon
179_C.BN.EPS = 1e-5
180
181# BN momentum (BN momentum in PyTorch = 1 - BN momentum in Caffe2)
182_C.BN.MOM = 0.1
183
184# Precise BN stats
185_C.BN.USE_PRECISE_STATS = True
186_C.BN.NUM_SAMPLES_PRECISE = 8192
187
188# Initialize the gamma of the final BN of each block to zero
189_C.BN.ZERO_INIT_FINAL_GAMMA = False
190
191# Use a different weight decay for BN layers
192_C.BN.USE_CUSTOM_WEIGHT_DECAY = False
193_C.BN.CUSTOM_WEIGHT_DECAY = 0.0
194
195
196# ------------------------------------------------------------------------------------ #
197# Optimizer options
198# ------------------------------------------------------------------------------------ #
199_C.OPTIM = CfgNode()
200
201# Base learning rate
202_C.OPTIM.BASE_LR = 0.1
203
204# Learning rate policy select from {'cos', 'exp', 'steps'}
205_C.OPTIM.LR_POLICY = "cos"
206
207# Exponential decay factor
208_C.OPTIM.GAMMA = 0.1
209
210# Steps for 'steps' policy (in epochs)
211_C.OPTIM.STEPS = []
212
213# Learning rate multiplier for 'steps' policy
214_C.OPTIM.LR_MULT = 0.1
215
216# Maximal number of epochs
217_C.OPTIM.MAX_EPOCH = 200
218
219# Momentum
220_C.OPTIM.MOMENTUM = 0.9
221
222# Momentum dampening
223_C.OPTIM.DAMPENING = 0.0
224
225# Nesterov momentum
226_C.OPTIM.NESTEROV = True
227
228# L2 regularization
229_C.OPTIM.WEIGHT_DECAY = 5e-4
230
231# Start the warm up from OPTIM.BASE_LR * OPTIM.WARMUP_FACTOR
232_C.OPTIM.WARMUP_FACTOR = 0.1
233
234# Gradually warm up the OPTIM.BASE_LR over this number of epochs
235_C.OPTIM.WARMUP_EPOCHS = 0
236
237
238# ------------------------------------------------------------------------------------ #
239# Training options
240# ------------------------------------------------------------------------------------ #
241_C.TRAIN = CfgNode()
242
243# Dataset and split
244_C.TRAIN.DATASET = ""
245_C.TRAIN.SPLIT = "train"
246
247# Total mini-batch size
248_C.TRAIN.BATCH_SIZE = 128
249
250# Image size
251_C.TRAIN.IM_SIZE = 224
252
253# Evaluate model on test data every eval period epochs
254_C.TRAIN.EVAL_PERIOD = 1
255
256# Save model checkpoint every checkpoint period epochs
257_C.TRAIN.CHECKPOINT_PERIOD = 1
258
259# Resume training from the latest checkpoint in the output directory
260_C.TRAIN.AUTO_RESUME = True
261
262# Weights to start training from
263_C.TRAIN.WEIGHTS = ""
264
265
266# ------------------------------------------------------------------------------------ #
267# Testing options
268# ------------------------------------------------------------------------------------ #
269_C.TEST = CfgNode()
270
271# Dataset and split
272_C.TEST.DATASET = ""
273_C.TEST.SPLIT = "val"
274
275# Total mini-batch size
276_C.TEST.BATCH_SIZE = 200
277
278# Image size
279_C.TEST.IM_SIZE = 256
280
281# Weights to use for testing
282_C.TEST.WEIGHTS = ""
283
284
285# ------------------------------------------------------------------------------------ #
286# Common train/test data loader options
287# ------------------------------------------------------------------------------------ #
288_C.DATA_LOADER = CfgNode()
289
290# Number of data loader workers per process
291_C.DATA_LOADER.NUM_WORKERS = 8
292
293# Load data to pinned host memory
294_C.DATA_LOADER.PIN_MEMORY = True
295
296
297# ------------------------------------------------------------------------------------ #
298# Memory options
299# ------------------------------------------------------------------------------------ #
300_C.MEM = CfgNode()
301
302# Perform ReLU inplace
303_C.MEM.RELU_INPLACE = True
304
305
306# ------------------------------------------------------------------------------------ #
307# CUDNN options
308# ------------------------------------------------------------------------------------ #
309_C.CUDNN = CfgNode()
310
311# Perform benchmarking to select the fastest CUDNN algorithms to use
312# Note that this may increase the memory usage and will likely not result
313# in overall speedups when variable size inputs are used (e.g. COCO training)
314_C.CUDNN.BENCHMARK = True
315
316
317# ------------------------------------------------------------------------------------ #
318# Precise timing options
319# ------------------------------------------------------------------------------------ #
320_C.PREC_TIME = CfgNode()
321
322# Number of iterations to warm up the caches
323_C.PREC_TIME.WARMUP_ITER = 3
324
325# Number of iterations to compute avg time
326_C.PREC_TIME.NUM_ITER = 30
327
328
329# ------------------------------------------------------------------------------------ #
330# Misc options
331# ------------------------------------------------------------------------------------ #
332
333# Number of GPUs to use (applies to both training and testing)
334_C.NUM_GPUS = 1
335
336# Output directory
337_C.OUT_DIR = "/tmp"
338
339# Config destination (in OUT_DIR)
340_C.CFG_DEST = "config.yaml"
341
342# Note that non-determinism may still be present due to non-deterministic
343# operator implementations in GPU operator libraries
344_C.RNG_SEED = 1
345
346# Log destination ('stdout' or 'file')
347_C.LOG_DEST = "stdout"
348
349# Log period in iters
350_C.LOG_PERIOD = 10
351
352# Distributed backend
353_C.DIST_BACKEND = "nccl"
354
355# Hostname and port range for multi-process groups (actual port selected randomly)
356_C.HOST = "localhost"
357_C.PORT_RANGE = [10000, 65000]
358
359# Models weights referred to by URL are downloaded to this local cache
360_C.DOWNLOAD_CACHE = "/tmp/pycls-download-cache"
361
362
363# ------------------------------------------------------------------------------------ #
364# Deprecated keys
365# ------------------------------------------------------------------------------------ #
366
367_C.register_deprecated_key("PREC_TIME.BATCH_SIZE")
368_C.register_deprecated_key("PREC_TIME.ENABLED")
369_C.register_deprecated_key("PORT")
370
371
372def assert_and_infer_cfg(cache_urls=True):
373 """Checks config values invariants."""
374 err_str = "The first lr step must start at 0"
375 assert not _C.OPTIM.STEPS or _C.OPTIM.STEPS[0] == 0, err_str
376 data_splits = ["train", "val", "test"]
377 err_str = "Data split '{}' not supported"
378 assert _C.TRAIN.SPLIT in data_splits, err_str.format(_C.TRAIN.SPLIT)
379 assert _C.TEST.SPLIT in data_splits, err_str.format(_C.TEST.SPLIT)
380 err_str = "Mini-batch size should be a multiple of NUM_GPUS."
381 assert _C.TRAIN.BATCH_SIZE % _C.NUM_GPUS == 0, err_str
382 assert _C.TEST.BATCH_SIZE % _C.NUM_GPUS == 0, err_str
383 err_str = "Log destination '{}' not supported"
384 assert _C.LOG_DEST in ["stdout", "file"], err_str.format(_C.LOG_DEST)
385 if cache_urls:
387
388
390 """Download URLs in config, cache them, and rewrite cfg to use cached file."""
391 _C.TRAIN.WEIGHTS = cache_url(_C.TRAIN.WEIGHTS, _C.DOWNLOAD_CACHE)
392 _C.TEST.WEIGHTS = cache_url(_C.TEST.WEIGHTS, _C.DOWNLOAD_CACHE)
393
394
396 """Dumps the config to the output directory."""
397 cfg_file = os.path.join(_C.OUT_DIR, _C.CFG_DEST)
398 with open(cfg_file, "w") as f:
399 _C.dump(stream=f)
400
401
402def load_cfg(out_dir, cfg_dest="config.yaml"):
403 """Loads config from specified output directory."""
404 cfg_file = os.path.join(out_dir, cfg_dest)
405 _C.merge_from_file(cfg_file)
406
407
408def load_cfg_fom_args(description="Config file options."):
409 """Load config from command line arguments and set any specified options."""
410 parser = argparse.ArgumentParser(description=description)
411 help_s = "Config file location"
412 parser.add_argument("--cfg", dest="cfg_file", help=help_s, required=True, type=str)
413 help_s = "See pycls/core/config.py for all options"
414 parser.add_argument("opts", help=help_s, default=None, nargs=argparse.REMAINDER)
415 if len(sys.argv) == 1:
416 parser.print_help()
417 sys.exit(1)
418 args = parser.parse_args()
419 _C.merge_from_file(args.cfg_file)
420 _C.merge_from_list(args.opts)
load_cfg_fom_args(description="Config file options.")
Definition config.py:408
load_cfg(out_dir, cfg_dest="config.yaml")
Definition config.py:402