forked from NVIDIA/semantic-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
382 lines (314 loc) · 12.1 KB
/
config.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
"""
# Code adapted from:
# https://github.com/facebookresearch/Detectron/blob/master/detectron/core/config.py
Source License
# Copyright (c) 2017-present, Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# Based on:
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
"""
##############################################################################
# Config
##############################################################################
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import re
import torch
from utils.attr_dict import AttrDict
from runx.logx import logx
__C = AttrDict()
cfg = __C
__C.GLOBAL_RANK = 0
__C.EPOCH = 0
# Absolute path to a location to keep some large files, not in this dir.
__C.ASSETS_PATH = '/content/drive/'My Drive/'
# Use class weighted loss per batch to increase loss for low pixel count classes per batch
__C.BATCH_WEIGHTING = False
# Border Relaxation Count
__C.BORDER_WINDOW = 1
# Number of epoch to use before turn off border restriction
__C.REDUCE_BORDER_EPOCH = -1
# Comma Seperated List of class id to relax
__C.STRICTBORDERCLASS = None
# Where output results get written
__C.RESULT_DIR = None
__C.OPTIONS = AttrDict()
__C.OPTIONS.TEST_MODE = False
__C.OPTIONS.INIT_DECODER = False
__C.OPTIONS.TORCH_VERSION = None
__C.TRAIN = AttrDict()
__C.TRAIN.RANDOM_BRIGHTNESS_SHIFT_VALUE = 10
__C.TRAIN.FP16 = False
#Attribute Dictionary for Dataset
__C.DATASET = AttrDict()
#Cityscapes Dir Location
__C.DATASET.CITYSCAPES_DIR = '/citys'
os.path.join(__C.ASSETS_PATH, 'data/Cityscapes')
__C.DATASET.CITYSCAPES_CUSTOMCOARSE = '/content/drive/'My Drive'/Auto_citys'
os.path.join(__C.ASSETS_PATH, 'data/Cityscapes/autolabelled')
__C.DATASET.CENTROID_ROOT = \
os.path.join(__C.ASSETS_PATH, 'uniform_centroids')
#SDC Augmented Cityscapes Dir Location
__C.DATASET.CITYSCAPES_AUG_DIR = ''
#Mapillary Dataset Dir Location
__C.DATASET.MAPILLARY_DIR = os.path.join(__C.ASSETS_PATH, 'data/Mapillary/data')
#Kitti Dataset Dir Location
__C.DATASET.KITTI_DIR = ''
#SDC Augmented Kitti Dataset Dir Location
__C.DATASET.KITTI_AUG_DIR = ''
#Camvid Dataset Dir Location
__C.DATASET.CAMVID_DIR = ''
#Number of splits to support
__C.DATASET.CITYSCAPES_SPLITS = 3
__C.DATASET.MEAN = [0.485, 0.456, 0.406]
__C.DATASET.STD = [0.229, 0.224, 0.225]
__C.DATASET.NAME = ''
__C.DATASET.NUM_CLASSES = 0
__C.DATASET.IGNORE_LABEL = 255
__C.DATASET.DUMP_IMAGES = False
__C.DATASET.CLASS_UNIFORM_PCT = 0.5
__C.DATASET.CLASS_UNIFORM_TILE = 1024
__C.DATASET.COARSE_BOOST_CLASSES = None
__C.DATASET.CV = 0
__C.DATASET.COLORIZE_MASK_FN = None
__C.DATASET.CUSTOM_COARSE_PROB = None
__C.DATASET.MASK_OUT_CITYSCAPES = False
# This enables there to always be translation augmentation during random crop
# process, even if image is smaller than crop size.
__C.DATASET.TRANSLATE_AUG_FIX = False
__C.DATASET.LANCZOS_SCALES = False
# Use a center crop of size args.pre_size for mapillary validation
# Need to use this if you want to dump images
__C.DATASET.MAPILLARY_CROP_VAL = False
__C.DATASET.CROP_SIZE = '896'
__C.MODEL = AttrDict()
__C.MODEL.BN = 'regularnorm'
__C.MODEL.BNFUNC = None
__C.MODEL.MSCALE = False
__C.MODEL.THREE_SCALE = False
__C.MODEL.ALT_TWO_SCALE = False
__C.MODEL.EXTRA_SCALES = '0.5,1.5'
__C.MODEL.N_SCALES = None
__C.MODEL.ALIGN_CORNERS = False
__C.MODEL.MSCALE_LO_SCALE = 0.5
__C.MODEL.OCR_ASPP = False
__C.MODEL.SEGATTN_BOT_CH = 256
__C.MODEL.ASPP_BOT_CH = 256
__C.MODEL.MSCALE_CAT_SCALE_FLT = False
__C.MODEL.MSCALE_INNER_3x3 = True
__C.MODEL.MSCALE_DROPOUT = False
__C.MODEL.MSCALE_OLDARCH = False
__C.MODEL.MSCALE_INIT = 0.5
__C.MODEL.ATTNSCALE_BN_HEAD = False
__C.MODEL.GRAD_CKPT = False
WEIGHTS_PATH = os.path.join(__C.ASSETS_PATH, 'seg_weights')
__C.MODEL.WRN38_CHECKPOINT = \
os.path.join(WEIGHTS_PATH, 'wider_resnet38.pth.tar')
__C.MODEL.WRN41_CHECKPOINT = \
os.path.join(WEIGHTS_PATH, 'wider_resnet41_cornflower_sunfish.pth')
__C.MODEL.X71_CHECKPOINT = \
os.path.join(WEIGHTS_PATH, 'aligned_xception71.pth')
__C.MODEL.HRNET_CHECKPOINT = \
os.path.join(WEIGHTS_PATH, 'hrnetv2_w48_imagenet_pretrained.pth')
__C.LOSS = AttrDict()
# Weight for OCR aux loss
__C.LOSS.OCR_ALPHA = 0.4
# Use RMI for the OCR aux loss
__C.LOSS.OCR_AUX_RMI = False
# Supervise the multi-scale predictions directly
__C.LOSS.SUPERVISED_MSCALE_WT = 0
__C.MODEL.OCR = AttrDict()
__C.MODEL.OCR.MID_CHANNELS = 512
__C.MODEL.OCR.KEY_CHANNELS = 256
__C.MODEL.OCR_EXTRA = AttrDict()
__C.MODEL.OCR_EXTRA.FINAL_CONV_KERNEL = 1
__C.MODEL.OCR_EXTRA.STAGE1 = AttrDict()
__C.MODEL.OCR_EXTRA.STAGE1.NUM_MODULES = 1
__C.MODEL.OCR_EXTRA.STAGE1.NUM_RANCHES = 1
__C.MODEL.OCR_EXTRA.STAGE1.BLOCK = 'BOTTLENECK'
__C.MODEL.OCR_EXTRA.STAGE1.NUM_BLOCKS = [4]
__C.MODEL.OCR_EXTRA.STAGE1.NUM_CHANNELS = [64]
__C.MODEL.OCR_EXTRA.STAGE1.FUSE_METHOD = 'SUM'
__C.MODEL.OCR_EXTRA.STAGE2 = AttrDict()
__C.MODEL.OCR_EXTRA.STAGE2.NUM_MODULES = 1
__C.MODEL.OCR_EXTRA.STAGE2.NUM_BRANCHES = 2
__C.MODEL.OCR_EXTRA.STAGE2.BLOCK = 'BASIC'
__C.MODEL.OCR_EXTRA.STAGE2.NUM_BLOCKS = [4, 4]
__C.MODEL.OCR_EXTRA.STAGE2.NUM_CHANNELS = [48, 96]
__C.MODEL.OCR_EXTRA.STAGE2.FUSE_METHOD = 'SUM'
__C.MODEL.OCR_EXTRA.STAGE3 = AttrDict()
__C.MODEL.OCR_EXTRA.STAGE3.NUM_MODULES = 4
__C.MODEL.OCR_EXTRA.STAGE3.NUM_BRANCHES = 3
__C.MODEL.OCR_EXTRA.STAGE3.BLOCK = 'BASIC'
__C.MODEL.OCR_EXTRA.STAGE3.NUM_BLOCKS = [4, 4, 4]
__C.MODEL.OCR_EXTRA.STAGE3.NUM_CHANNELS = [48, 96, 192]
__C.MODEL.OCR_EXTRA.STAGE3.FUSE_METHOD = 'SUM'
__C.MODEL.OCR_EXTRA.STAGE4 = AttrDict()
__C.MODEL.OCR_EXTRA.STAGE4.NUM_MODULES = 3
__C.MODEL.OCR_EXTRA.STAGE4.NUM_BRANCHES = 4
__C.MODEL.OCR_EXTRA.STAGE4.BLOCK = 'BASIC'
__C.MODEL.OCR_EXTRA.STAGE4.NUM_BLOCKS = [4, 4, 4, 4]
__C.MODEL.OCR_EXTRA.STAGE4.NUM_CHANNELS = [48, 96, 192, 384]
__C.MODEL.OCR_EXTRA.STAGE4.FUSE_METHOD = 'SUM'
def torch_version_float():
version_str = torch.__version__
version_re = re.search(r'^([0-9]+\.[0-9]+)', version_str)
if version_re:
version = float(version_re.group(1))
logx.msg(f'Torch version: {version}, {version_str}')
else:
version = 1.0
logx.msg(f'Can\'t parse torch version ({version}), assuming {version}')
return version
def assert_and_infer_cfg(args, make_immutable=True, train_mode=True):
"""Call this function in your script after you have finished setting all cfg
values that are necessary (e.g., merging a config from a file, merging
command line config options, etc.). By default, this function will also
mark the global cfg as immutable to prevent changing the global cfg
settings during script execution (which can lead to hard to debug errors
or code that's harder to understand than is necessary).
"""
__C.OPTIONS.TORCH_VERSION = torch_version_float()
if hasattr(args, 'syncbn') and args.syncbn:
if args.apex:
import apex
__C.MODEL.BN = 'apex-syncnorm'
__C.MODEL.BNFUNC = apex.parallel.SyncBatchNorm
else:
raise Exception('No Support for SyncBN without Apex')
else:
__C.MODEL.BNFUNC = torch.nn.BatchNorm2d
print('Using regular batch norm')
if not train_mode:
cfg.immutable(True)
return
if args.batch_weighting:
__C.BATCH_WEIGHTING = True
if args.custom_coarse_prob:
__C.DATASET.CUSTOM_COARSE_PROB = args.custom_coarse_prob
if args.jointwtborder:
if args.strict_bdr_cls != '':
strict_classes = [int(i) for i in args.strict_bdr_cls.split(",")]
__C.STRICTBORDERCLASS = strict_classes
if args.rlx_off_epoch > -1:
__C.REDUCE_BORDER_EPOCH = args.rlx_off_epoch
cfg.DATASET.NAME = args.dataset
cfg.DATASET.DUMP_IMAGES = args.dump_augmentation_images
cfg.DATASET.CLASS_UNIFORM_PCT = args.class_uniform_pct
cfg.DATASET.CLASS_UNIFORM_TILE = args.class_uniform_tile
if args.coarse_boost_classes:
cfg.DATASET.COARSE_BOOST_CLASSES = \
[int(i) for i in args.coarse_boost_classes.split(',')]
cfg.DATASET.CLASS_UNIFORM_BIAS = None
if args.dump_assets and args.dataset == 'cityscapes':
# A hacky way to force that when we dump cityscapes
logx.msg('*' * 70)
logx.msg(f'ALERT: forcing cv=3 to allow all images to be evaluated')
logx.msg('*' * 70)
cfg.DATASET.CV = 3
else:
cfg.DATASET.CV = args.cv
# Total number of splits
cfg.DATASET.CV_SPLITS = 3
if args.translate_aug_fix:
cfg.DATASET.TRANSLATE_AUG_FIX = True
cfg.MODEL.MSCALE = ('mscale' in args.arch.lower() or 'attnscale' in
args.arch.lower())
if args.three_scale:
cfg.MODEL.THREE_SCALE = True
if args.alt_two_scale:
cfg.MODEL.ALT_TWO_SCALE = True
cfg.MODEL.MSCALE_LO_SCALE = args.mscale_lo_scale
def str2list(s):
alist = s.split(',')
alist = [float(x) for x in alist]
return alist
if args.n_scales:
cfg.MODEL.N_SCALES = str2list(args.n_scales)
logx.msg('n scales {}'.format(cfg.MODEL.N_SCALES))
if args.extra_scales:
cfg.MODEL.EXTRA_SCALES = str2list(args.extra_scales)
if args.align_corners:
cfg.MODEL.ALIGN_CORNERS = True
if args.init_decoder:
cfg.OPTIONS.INIT_DECODER = True
cfg.RESULT_DIR = args.result_dir
if args.mask_out_cityscapes:
cfg.DATASET.MASK_OUT_CITYSCAPES = True
if args.fp16:
cfg.TRAIN.FP16 = True
if args.map_crop_val:
__C.DATASET.MAPILLARY_CROP_VAL = True
__C.DATASET.CROP_SIZE = args.crop_size
if args.aspp_bot_ch is not None:
# todo fixme: make all code use this cfg
__C.MODEL.ASPP_BOT_CH = int(args.aspp_bot_ch)
if args.mscale_cat_scale_flt:
__C.MODEL.MSCALE_CAT_SCALE_FLT = True
if args.mscale_no3x3:
__C.MODEL.MSCALE_INNER_3x3 = False
if args.mscale_dropout:
__C.MODEL.MSCALE_DROPOUT = True
if args.mscale_old_arch:
__C.MODEL.MSCALE_OLDARCH = True
if args.mscale_init is not None:
__C.MODEL.MSCALE_INIT = args.mscale_init
if args.attnscale_bn_head:
__C.MODEL.ATTNSCALE_BN_HEAD = True
if args.segattn_bot_ch is not None:
__C.MODEL.SEGATTN_BOT_CH = args.segattn_bot_ch
if args.set_cityscapes_root is not None:
# '/data/cs_imgs_cv0'
# '/data/cs_imgs_cv2'
__C.DATASET.CITYSCAPES_DIR = args.set_cityscapes_root
if args.ocr_alpha is not None:
__C.LOSS.OCR_ALPHA = args.ocr_alpha
if args.ocr_aux_loss_rmi:
__C.LOSS.OCR_AUX_RMI = True
if args.supervised_mscale_loss_wt is not None:
__C.LOSS.SUPERVISED_MSCALE_WT = args.supervised_mscale_loss_wt
cfg.DROPOUT_COARSE_BOOST_CLASSES = None
if args.custom_coarse_dropout_classes:
cfg.DROPOUT_COARSE_BOOST_CLASSES = \
[int(i) for i in args.custom_coarse_dropout_classes.split(',')]
if args.grad_ckpt:
__C.MODEL.GRAD_CKPT = True
__C.GLOBAL_RANK = args.global_rank
if make_immutable:
cfg.immutable(True)
def update_epoch(epoch):
# Update EPOCH CTR
cfg.immutable(False)
cfg.EPOCH = epoch
cfg.immutable(True)
def update_dataset_cfg(num_classes, ignore_label):
cfg.immutable(False)
cfg.DATASET.NUM_CLASSES = num_classes
cfg.DATASET.IGNORE_LABEL = ignore_label
logx.msg('num_classes = {}'.format(num_classes))
cfg.immutable(True)
def update_dataset_inst(dataset_inst):
cfg.immutable(False)
cfg.DATASET_INST = dataset_inst
cfg.immutable(True)