-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
1089 lines (859 loc) · 32.4 KB
/
config.py
File metadata and controls
1089 lines (859 loc) · 32.4 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast
import configparser
import copy
import logging
import os
import warnings
from itertools import groupby
from typing import Any, Dict, Tuple
import yaml
from fvcore.common.file_io import PathManager
from medsegpy.cross_validation import cv_util
from medsegpy.losses import DICE_LOSS, L2_LOSS, get_training_loss_from_str
from medsegpy.utils import utils as utils
logger = logging.getLogger(__name__)
# Keys that have been deprecated.
DEPRECATED_KEYS = [
"NUM_CLASSES",
"TRAIN_FILES_CV",
"VALID_FILES_CV",
"TEST_FILES_CV",
"USE_STEP_DECAY",
"PIK_SAVE_PATH_DIR",
"PIK_SAVE_PATH",
"TF_LOG_DIR",
"TRAIN_PATH",
"VALID_PATH",
"TEST_PATH",
"PLOT_MODEL_PATH",
"FINE_TUNE",
"LEARN_FILES",
"DEBUG",
"TEST_RESULT_PATH",
"TEST_RESULTS_FOLDER_NAME",
"DATE_TIME_STR",
]
RENAMED_KEYS = {
"CP_SAVE_PATH": "OUTPUT_DIR",
"CP_SAVE_TAG": "MODEL_NAME",
"INIT_WEIGHT_PATH": "INIT_WEIGHTS",
"TISSUES": "CATEGORIES",
}
BASE_KEY = "_BASE_"
class Config(object):
"""A config object that is 1-to-1 with supported models.
Each subclass of :class:`Config` corresponds to a specific model
architecture.
"""
VERSION = 7
# Model name specific to config. Cannot be changed.
MODEL_NAME = ""
# Experiment name and description of the config.
EXP_NAME = ""
DESCRIPTION = ""
# Learning type. Possible Values:
# 1) "self-supervised" -- self-supervised learning
# 2) "supervised" -- supervised learning
LEARNING_TAG = ""
# The current task for which the model is trained
# -- Possible Values: "inpainting", "segmentation"
PRIMARY_TASK = ""
# The layers corresponding to the pretrained portion of a
# model during self-supervised learning.
SS_LOADED_LAYERS = []
# Loss function in form (id, output_mode)
LOSS = DICE_LOSS
CLASS_WEIGHTS = None
# Class name for robust loss computation
ROBUST_LOSS_NAME = ""
ROBUST_LOSS_STEP_SIZE = 1e-1
# PIDS to include, None = all pids
PIDS = None
# Training and validation image size
IMG_SIZE = (288, 288, 1)
# Patch-based image loading params used with PatchDataLoader
IMG_PAD_SIZE = ()
IMG_PAD_MODE = "edge"
IMG_STRIDE = ()
# Training parameters
N_EPOCHS = 100
AUGMENT_DATA = False
# Step Decay params
INITIAL_LEARNING_RATE = 1e-4
LR_SCHEDULER_NAME = ""
MIN_LEARNING_RATE = 1e-8
DROP_FACTOR = 0.7
DROP_RATE = 1.0
LR_MIN_DELTA = 1e-4
LR_PATIENCE = 0
LR_COOLDOWN = 0
NUM_GRAD_STEPS = 1
# ADAM optimizer decay
ADAM_DECAY = 0.0
USE_AMSGRAD = False
# Early stopping criterion
USE_EARLY_STOPPING = False
EARLY_STOPPING_MIN_DELTA = 0.0
EARLY_STOPPING_PATIENCE = 0
EARLY_STOPPING_CRITERION = "val_loss"
# Batch sizes
TRAIN_BATCH_SIZE = 12
VALID_BATCH_SIZE = 35
TEST_BATCH_SIZE = 72
# Categories
CATEGORIES = []
INCLUDE_BACKGROUND = False
# File Types
FILE_TYPES = ["im"]
# A dictionary specifying which pretrained weights to load
#
# Example:
# {
# "path": "",
# "weights":
# [
# {
# "include_words": ["decoder"],
# "exclude_words": [],
# "slice_indices": [0, "until"]
# }
# ]
# }
#
# The above example will load the weights for all layers from the
# first layer until the layer right before the first layer
# with the word "decoder" in its name
PRETRAINED_WEIGHTS_PATH = {}
# The path to the config file of the pretrained model
PRETRAINED_CONFIG_PATH = ""
# Boolean determining whether or not the pretrained weights
# specified in PRETRAINED_WEIGHTS_PATH will be frozen
# during training
FREEZE_PRETRAINED = False
INIT_WEIGHTS = ""
FREEZE_LAYERS = ()
# Dataset names
TRAIN_DATASET = ""
VAL_DATASET = ""
TEST_DATASET = ""
# Cross-Validation-Parameters
USE_CROSS_VALIDATION = False
CV_FILE = ""
CV_K = 0
CV_TRAIN_BINS = []
CV_VALID_BINS = []
CV_TEST_BINS = []
__CV_TRAIN_FILES__ = None
__CV_VALID_FILES__ = None
__CV_TEST_FILES__ = None
# Training Model Paths
OUTPUT_DIR = ""
# DataLoader tag.
TAG = "DefaultDataLoader"
PRELOAD_DATA = False
# Type of normalization layer
NORMALIZATION = "BatchNorm"
# Arguments for the normalization layer
NORMALIZATION_ARGS = {"axis": -1, "momentum": 0.95, "epsilon": 0.001}
# Boolean specifying if weight standardization should be used
WEIGHT_STANDARDIZATION = False
# Weights kernel initializer.
KERNEL_INITIALIZER = "he_normal"
# System params
NUM_WORKERS = 1
SEED = None
# Evaluation params
TEST_WEIGHT_PATH = ""
TEST_METRICS = ["DSC", "VOE", "ASSD", "CV"]
# PREPROCESSING: a list of MedTransform or TransformGen class names
# (look at medsegpy/data/transforms/transform_gen.py")
# PREPROCESSING_ARGS: a list of dictionaries, where each dictionary
# contains the value of the __init__ arguments for
# the corresponding class listed in PREPROCESSING
PREPROCESSING = []
PREPROCESSING_ARGS = []
def __init__(self, cp_save_tag, state="training", create_dirs=True):
if state not in ["testing", "training"]:
raise ValueError("state must either be 'training' or 'testing'")
self.MODEL_NAME = cp_save_tag
self.STATE = state
def init_cross_validation(
self,
train_files,
valid_files,
test_files,
train_bins,
valid_bins,
test_bins,
cv_k,
cv_file,
output_dir,
):
"""Initialize config for cross validation.
Returns:
Config: A deep copy of the config. This copy is initialized for
cross validation.
"""
assert (
self.STATE == "training"
), "Initializing cross-validation must be done in training state"
config = copy.deepcopy(self)
config.USE_CROSS_VALIDATION = True
config.CV_TRAIN_BINS = train_bins
config.CV_VALID_BINS = valid_bins
config.CV_TEST_BINS = test_bins
config.CV_K = cv_k
config.CV_FILE = cv_file
config.__CV_TRAIN_FILES__ = train_files
config.__CV_VALID_FILES__ = valid_files
config.__CV_TEST_FILES__ = test_files
os.makedirs(output_dir, exist_ok=True)
config.OUTPUT_DIR = output_dir
return config
def save_config(self):
"""Save params of config to ini file."""
filepath = os.path.join(self.OUTPUT_DIR, "config.yaml")
config_vars = self.to_dict()
# Save config
with PathManager.open(filepath, "w") as configfile:
yaml.safe_dump(config_vars, configfile)
logger.info("Full config saved to {}".format(os.path.abspath(filepath)))
def to_dict(self):
"""Returns a dictionary representation of the config.
This representation only contains public instance variable fields.
Private and protected instance variables are excluded. Methods are excluded.
Returns:
dict: The config as a dictionary.
"""
members = [
attr
for attr in dir(self)
if not attr.startswith("__")
and not callable(getattr(self, attr))
and not (hasattr(type(self), attr) and isinstance(getattr(type(self), attr), property))
]
return {m_var: getattr(self, m_var) for m_var in members}
def _parse_special_attributes(self, full_key: str, value: Any) -> Tuple[str, Any]:
"""Special parsing values for attributes.
Used when loading config from a file or from list.
Args:
full_key (str): Upper case attribute representation.
value (Any): Corresponding value.
"""
# Avoid circular dependencies.
from medsegpy.data import MetadataCatalog # noqa
from medsegpy.data.data_loader import LEGACY_DATA_LOADER_NAMES # noqa
from medsegpy.modeling.meta_arch.build import LEGACY_MODEL_NAMES # noqa
prev_key, prev_val = full_key, value
if full_key in ("TRAIN_PATH", "VALID_PATH", "TEST_PATH") and value:
mapping = {
"TRAIN_PATH": "TRAIN_DATASET",
"VALID_PATH": "VAL_DATASET",
"TEST_PATH": "TEST_DATASET",
}
value = MetadataCatalog.convert_path_to_dataset(value)
full_key = mapping[full_key]
logger.info("Converting {} -> {}: {} -> {}".format(prev_key, full_key, prev_val, value))
elif full_key == "TAG" and value in LEGACY_DATA_LOADER_NAMES:
value = LEGACY_DATA_LOADER_NAMES[value]
logger.info("Converting {}: {} -> {}".format(full_key, prev_val, value))
elif full_key == "MODEL_NAME" and value in LEGACY_MODEL_NAMES:
value = LEGACY_MODEL_NAMES[value]
logger.info("Converting {}: {} -> {}".format(full_key, prev_val, value))
elif full_key == "LOSS" and isinstance(value, str):
try:
value = get_training_loss_from_str(value)
except ValueError:
pass
elif full_key == "OUTPUT_DIR":
value = PathManager.get_local_path(value)
return full_key, value
def merge_from_file(self, cfg_filename: str, new_allowed=False):
"""Load a ini or yaml config file and merge it with this object.
"MODEL_NAME" must be specified in the file.
Args:
cfg_filename (str): File path to yaml or ini file.
new_allowed (bool, optional): If `True`, new keys, not already defined,
are allowed.
"""
# Avoid circular dependencies.
from medsegpy.modeling.meta_arch.build import LEGACY_MODEL_NAMES
vars_dict = self._load_dict_from_file(cfg_filename)
# Load information from base files first.
if BASE_KEY in vars_dict:
vars_dict = self.parse_base_config(cfg_filename)
# TODO: Handle cp save tag as a protected key.
model_name = (
vars_dict["MODEL_NAME"] if "MODEL_NAME" in vars_dict else vars_dict["CP_SAVE_TAG"]
)
if model_name in LEGACY_MODEL_NAMES:
model_name = LEGACY_MODEL_NAMES[model_name]
if model_name != self.MODEL_NAME:
raise ValueError(
"Wrong config. Expected {}. Got {}".format(self.MODEL_NAME, model_name)
)
for full_key, value in vars_dict.items():
full_key = str(full_key).upper()
if full_key in RENAMED_KEYS:
new_name = RENAMED_KEYS[full_key]
logger.warning("Key {} has been renamed to {}".format(full_key, new_name))
full_key = new_name
full_key, value = self._parse_special_attributes(full_key, value)
if full_key in DEPRECATED_KEYS:
logger.warning("Key {} is deprecated, not loading".format(full_key))
continue
if not new_allowed and not hasattr(self, full_key):
raise ValueError("Key {} does not exist.".format(full_key))
is_new = not hasattr(self, full_key)
if is_new or full_key == "TRAIN_DATASET":
value = self._decode_cfg_value(value, "auto")
else:
value = self._decode_cfg_value(value, type(self.__getattribute__(full_key)))
value = _check_and_coerce_cfg_value_type(
value, self.__getattribute__(full_key), full_key
)
# Loading config
self.__setattr__(full_key, value)
def merge_from_list(self, cfg_list):
"""Merge config (keys, values) in a list (e.g. from command line).
For example, cfg_list = ['FOO_BAR', 0.5, 'BAR_FOO', (0,3,4)]
"""
_error_with_logging(
len(cfg_list) % 2 == 0,
"Override list has odd length: {}; " "it must be a list of pairs".format(cfg_list),
)
for full_key, v in zip(cfg_list[0::2], cfg_list[1::2]):
if full_key == "MODEL_NAME":
raise ValueError("Cannot change key MODEL_NAME")
if self.key_is_deprecated(full_key):
continue
if self.key_is_renamed(full_key):
self.raise_key_rename_error(full_key)
_error_with_logging(
hasattr(self, full_key),
"Non-existent key: {}".format(full_key),
error_type=KeyError,
)
value = self._decode_cfg_value(v, type(self.__getattribute__(full_key)))
value = _check_and_coerce_cfg_value_type(
value, self.__getattribute__(full_key), full_key
)
self.__setattr__(full_key, value)
@classmethod
def _decode_cfg_value(cls, value, data_type):
"""
Decodes a raw config value (e.g., from a yaml config files or command
line argument) into a Python object.
If the value is a dict, it will be interpreted as a new CfgNode.
If the value is a str, it will be evaluated as literals.
Otherwise it is returned as-is.
"""
# Configs parsed from raw yaml will contain dictionary keys that need to
# be converted to CfgNode objects
"""
Convert string to relevant data type
:param var_string: variable as a string (e.g.: '[0]', '1', 'hellow')
:param data_type: the type of the data
:return: string converted to data_type
"""
if isinstance(value, dict):
return {k: cls._decode_cfg_value(v, "auto") for k, v in value.items()}
elif not isinstance(value, str):
return value
if data_type is str:
return str(value)
elif data_type is float:
return float(value)
elif data_type is int:
return int(value)
else:
try:
return ast.literal_eval(value)
except (SyntaxError, ValueError) as e:
if data_type == "auto":
return value
else:
raise e
def key_is_deprecated(self, full_key):
"""Test if a key is deprecated."""
if full_key in DEPRECATED_KEYS:
logger.warning("Deprecated config key (ignoring): {}".format(full_key))
return True
return False
def key_is_renamed(self, full_key):
"""Test if a key is renamed."""
return full_key in RENAMED_KEYS
def add_new_key(self, key, value) -> None:
"""Add a new key to the config schema.
Args:
key (str): Key name
value (Any): Value of the key to set.
"""
if key in self.__dict__:
raise KeyError("Key {} already exists in config.".format(key))
value = self._decode_cfg_value(value, "auto")
self.__setattr__(key, value)
def raise_key_rename_error(self, full_key):
new_key = RENAMED_KEYS[full_key]
if isinstance(new_key, tuple):
msg = " Note: " + new_key[1]
new_key = new_key[0]
else:
msg = ""
raise KeyError(
"Key {} was renamed to {}; please update your config.{}".format(full_key, new_key, msg)
)
@classmethod
def parse_base_config(cls, cfg_filename: str) -> Dict:
local_dir = os.path.dirname(PathManager.get_local_path(cfg_filename))
vars_dict = cls._load_dict_from_file(cfg_filename)
base_cfg_filename = vars_dict.pop(BASE_KEY, None)
if base_cfg_filename:
if base_cfg_filename.startswith("~"):
base_cfg_filename = os.path.expanduser(base_cfg_filename)
elif any(map(base_cfg_filename.startswith, ["/", "https://", "http://"])):
raise ValueError("Remote configs not currently supported.")
else:
# the path to base cfg is relative to the config file itself.
base_cfg_filename = os.path.join(local_dir, base_cfg_filename)
base_dict = cls.parse_base_config(base_cfg_filename)
base_dict.update(vars_dict)
vars_dict = base_dict
assert BASE_KEY not in vars_dict, "{} should be popped off!".format(BASE_KEY)
return vars_dict
@classmethod
def _load_dict_from_file(cls, cfg_filename):
filename = PathManager.get_local_path(cfg_filename)
if filename.endswith(".ini"):
cfg = configparser.ConfigParser()
if not os.path.isfile(filename):
raise FileNotFoundError("Config file {} not found".format(filename))
cfg.read(filename)
vars_dict = cfg["DEFAULT"]
vars_dict = {k.upper(): v for k, v in vars_dict.items()}
elif filename.endswith(".yaml") or filename.endswith(".yml"):
with open(filename, "r") as f:
vars_dict = yaml.safe_load(f)
else:
raise ValueError("file {} not supported".format(filename))
return vars_dict
def set_attr(self, attr, val):
"""
Wrapper method to set attributes of config
:param attr: a string attr
:param val: value to set attribute to
:raises ValueError: if attr is not a string
if attr does not exist for the config
if type of val is different from the default type
"""
if type(attr) is not str:
raise ValueError("attr must be of type str")
if not hasattr(self, attr):
raise ValueError("The attribute %s does not exist" % attr)
curr_val = self.__getattribute__(attr)
if type(val) is str and type(curr_val) is not str:
val = utils.convert_data_type(var_string=val, original=curr_val)
if curr_val is not None and (type(val) != type(curr_val)):
raise ValueError(
"%s is of type %s. Expected %s" % (attr, str(type(val)), str(type(curr_val)))
)
self.__setattr__(attr, val)
def __getitem__(self, key):
if not hasattr(self, key):
raise KeyError(f"Key {key} does not exist")
return getattr(self, key)
def get(self, key, default=None):
keys = key.split(".")
try:
val = self
for k in keys:
val = val[k]
except KeyError:
return default
return val
def change_to_test(self):
"""
Initialize testing state
"""
self.STATE = "testing"
# if cross validation is enabled, load testing cross validation bin
if self.USE_CROSS_VALIDATION:
assert self.CV_FILE, "No cross-validation file found in config"
cv_processor = cv_util.CrossValidationProcessor(self.CV_FILE)
bins = (self.CV_TRAIN_BINS, self.CV_VALID_BINS, self.CV_TEST_BINS)
train_files, valid_files, test_files = cv_processor.get_fnames(bins)
self.__CV_TRAIN_FILES__ = train_files
self.__CV_VALID_FILES__ = valid_files
self.__CV_TEST_FILES__ = test_files
def summary(self, additional_vars=None):
"""
Print config summary
:param additional_vars: additional list of variables to print
:return:
"""
summary_vals = ["MODEL_NAME", "TAG", ""]
summary_vals.extend(
[
"TRAIN_DATASET",
"VAL_DATASET",
"TEST_DATASET",
"",
"CATEGORIES",
"",
"IMG_SIZE",
"",
"N_EPOCHS",
"AUGMENT_DATA",
"LOSS",
"CLASS_WEIGHTS",
"ROBUST_LOSS_NAME"
if self.ROBUST_LOSS_NAME
else "" "ROBUST_LOSS_STEP_SIZE"
if self.ROBUST_LOSS_NAME
else "" "",
"USE_CROSS_VALIDATION",
"CV_K" if self.USE_CROSS_VALIDATION else "",
"CV_FILE" if self.USE_CROSS_VALIDATION else "",
"CV_TRAIN_BINS" if self.USE_CROSS_VALIDATION else "",
"CV_VALID_BINS" if self.USE_CROSS_VALIDATION else "",
"CV_TEST_BINS" if self.USE_CROSS_VALIDATION else "",
"" "TRAIN_BATCH_SIZE",
"VALID_BATCH_SIZE",
"TEST_BATCH_SIZE",
"",
"NUM_GRAD_STEPS",
"",
"INITIAL_LEARNING_RATE",
"LR_SCHEDULER_NAME",
"DROP_FACTOR" if self.LR_SCHEDULER_NAME else "",
"DROP_RATE" if self.LR_SCHEDULER_NAME else "",
"MIN_LEARNING_RATE" if self.LR_SCHEDULER_NAME else "",
"LR_MIN_DELTA" if self.LR_SCHEDULER_NAME else "",
"LR_PATIENCE" if self.LR_SCHEDULER_NAME else "",
"LR_COOLDOWN" if self.LR_SCHEDULER_NAME else "",
"" "USE_EARLY_STOPPING",
"EARLY_STOPPING_MIN_DELTA" if self.USE_EARLY_STOPPING else "",
"EARLY_STOPPING_PATIENCE" if self.USE_EARLY_STOPPING else "",
"EARLY_STOPPING_CRITERION" if self.USE_EARLY_STOPPING else "",
"",
"KERNEL_INITIALIZER",
"SEED" if self.SEED else "",
"" "INIT_WEIGHTS",
"",
"TEST_WEIGHT_PATH",
"TEST_METRICS",
"" "NUM_WORKERS",
"OUTPUT_DIR",
"",
]
)
if additional_vars:
summary_vals.extend(additional_vars)
# Remove consecutive elements in summary vals that are the same
summary_vals = [x[0] for x in groupby(summary_vals)]
logger.info("")
logger.info("==" * 40)
logger.info("Config Summary")
logger.info("==" * 40)
for attr in summary_vals:
if attr == "":
logger.info("")
continue
logger.info(attr + ": " + str(self.__getattribute__(attr)))
logger.info("==" * 40)
logger.info("")
def get_num_classes(self):
if self.INCLUDE_BACKGROUND:
return len(self.CATEGORIES) + 1
return len(self.CATEGORIES)
def num_neighboring_slices(self):
return None
@property
def testing(self):
return self.STATE == "testing"
@property
def training(self):
return self.STATE == "training"
class DeeplabV3Config(Config):
"""
Configuration for 2D Deeplabv3+ architecture
(https://arxiv.org/abs/1802.02611).
"""
MODEL_NAME = "DeeplabV3Plus"
OS = 16
DIL_RATES = (2, 4, 6)
AT_DIVISOR = 2
DROPOUT_RATE = 0.1
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def summary(self, additional_vars=None):
summary_attrs = ["OS", "DIL_RATES", "DROPOUT_RATE"]
super().summary(summary_attrs)
class SegnetConfig(Config):
"""
Configuration for 2D Segnet architecture (https://arxiv.org/abs/1505.07293)
"""
MODEL_NAME = "segnet_2d"
TRAIN_BATCH_SIZE = 15
DEPTH = 6
NUM_CONV_LAYERS = [2, 2, 3, 3, 3, 3]
NUM_FILTERS = [64, 128, 256, 256, 512, 512]
SINGLE_BN = False
CONV_ACT_BN = False
USE_BOTTLENECK = False
INITIAL_LEARNING_RATE = 1e-3
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def summary(self, additional_vars=None):
summary_attrs = ["DEPTH", "NUM_CONV_LAYERS", "NUM_FILTERS"]
super().summary(summary_attrs)
class ContextEncoderConfig(Config):
"""
Configuration for the context encoder.
Reference:
Pathak et al. "Context Encoders: Feature Learning by Inpainting". CVPR. 2016.
"""
MODEL_NAME = "ContextEncoder"
NUM_FILTERS = [[32, 32], [64, 64], [128, 128], [256, 256]]
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def summary(self, additional_vars=None):
summary_vars = ["NUM_FILTERS"]
if additional_vars:
summary_vars.extend(additional_vars)
super().summary(summary_vars)
class ContextUNetConfig(ContextEncoderConfig):
"""
Configuration for the ContextUNet model.
This model will incorporate the ContextEncoder model as well.
Reference:
Pathak et al. "Context Encoders: Feature Learning by Inpainting". CVPR. 2016.
"""
MODEL_NAME = "ContextUNet"
class ContextInpaintingConfig(ContextUNetConfig):
"""
Configuration for the ContextInpainting model.
"""
MODEL_NAME = "ContextInpainting"
LOSS = L2_LOSS
# Define preprocessing transforms
PREPROCESSING = ["CoarseDropout"]
PREPROCESSING_ARGS = [
{
"max_holes": 25,
"max_height": 50,
"max_width": 50,
"min_holes": 0,
"min_height": 25,
"min_width": 25,
"fill_value": 0.257,
}
]
class ContextSegmentationConfig(ContextUNetConfig):
"""
Configuration for the ContextSegmentation model.
"""
MODEL_NAME = "ContextSegmentation"
class UNetConfig(Config):
"""
Configuration for 2D U-Net architecture (https://arxiv.org/abs/1505.04597)
"""
MODEL_NAME = "UNet2D"
INIT_UNET_2D = False
INITIAL_LEARNING_RATE = 2e-2
DROP_FACTOR = 0.8 ** (1 / 5)
DROP_RATE = 1.0
TRAIN_BATCH_SIZE = 35
DEPTH = 6
NUM_FILTERS = None
# Boolean arguments for attention and deep supervision
USE_ATTENTION = False
USE_DEEP_SUPERVISION = False
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def summary(self, additional_vars=None):
summary_vars = ["DEPTH", "NUM_FILTERS", ""]
if additional_vars:
summary_vars.extend(additional_vars)
super().summary(summary_vars)
class ResidualUNet(Config):
"""
Configuration for 2D Residual U-Net architecture
"""
MODEL_NAME = "res_unet"
DEPTH = 6
NUM_FILTERS = None
DROPOUT_RATE = 0.0
LAYER_ORDER = ["relu", "bn", "dropout", "conv"]
USE_SE_BLOCK = False
SE_RATIO = 8
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def summary(self, additional_vars=None):
summary_attrs = [
"DEPTH",
"NUM_FILTERS",
"DROPOUT_RATE",
"",
"LAYER_ORDER",
"",
"USE_SE_BLOCK",
"SE_RATIO",
]
super().summary(summary_attrs)
def num_neighboring_slices(self):
return self.IMG_SIZE[-1] if self.IMG_SIZE[-1] != 1 else None
class UNet2_5DConfig(UNetConfig):
"""
Configuration for 3D U-Net architecture
"""
MODEL_NAME = "UNet2D"
IMG_SIZE = (288, 288, 7)
N_EPOCHS = 20
AUGMENT_DATA = False
INITIAL_LEARNING_RATE = 1e-2
DROP_RATE = 1.0
DROP_FACTOR = 0.8
def __init__(self, state="training", create_dirs=True):
warnings.warn("UNet2_5DConfig is deprecated. Use UNet2DConfig instead.", DeprecationWarning)
super().__init__(state, create_dirs)
def num_neighboring_slices(self):
return self.IMG_SIZE[2]
class UNet3DConfig(UNetConfig):
MODEL_NAME = "UNet3D"
IMG_SIZE = (288, 288, 4, 1)
N_EPOCHS = 20
INITIAL_LEARNING_RATE = 1e-2
DROP_RATE = 1.0
DROP_FACTOR = 0.8
TAG = "oai_3d"
SLICE_SUBSET = None # 1 indexed inclusive - i.e. (5, 64) means slices [5, 64]
NUM_FILTERS = [32, 64, 128, 256, 512, 1024]
def num_neighboring_slices(self):
return self.IMG_SIZE[2]
def summary(self, additional_vars=None):
summary_attrs = ["SLICE_SUBSET"]
super().summary(summary_attrs)
class DeeplabV3_2_5DConfig(DeeplabV3Config):
"""2.5D DeeplabV3+."""
IMG_SIZE = (288, 288, 3)
def __init__(self, state="training", create_dirs=True):
warnings.warn("UNet2_5DConfig is deprecated. Use UNet2DConfig instead.", DeprecationWarning)
super().__init__(state, create_dirs)
def num_neighboring_slices(self):
return self.IMG_SIZE[2]
class AnisotropicUNetConfig(Config):
"""2D Anisotropic U-Net."""
MODEL_NAME = "anisotropic_unet"
IMG_SIZE = (288, 72, 1)
INITIAL_LEARNING_RATE = 2e-2
DROP_FACTOR = 0.85
DROP_RATE = 1.0
TRAIN_BATCH_SIZE = 60
DEPTH = 6
NUM_FILTERS = None
KERNEL_SIZE = (7, 3)
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def summary(self, additional_vars=None):
summary_attrs = ["DEPTH", "NUM_FILTERS", "KERNEL_SIZE"]
super().summary(summary_attrs)
class RefineNetConfig(Config):
"""Configuration for RefineNet architecture as suggested by paper below
http://openaccess.thecvf.com/content_cvpr_2017/papers/Lin_RefineNet_Multi
-Path_Refinement_CVPR_2017_paper.pdf
"""
MODEL_NAME = "refinenet"
INITIAL_LEARNING_RATE = 1e-3
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
class FCDenseNetConfig(Config):
MODEL_NAME = "FCDenseNet"
DEPTH = 5
NUM_LAYERS = (5,)
NUM_FILTERS = (12,)
NUM_FILTERS_HEAD_CONV = 48
DROPOUT = 0.2
def __init__(self, state="training", create_dirs=True):
super().__init__(self.MODEL_NAME, state, create_dirs=create_dirs)
def _check_and_coerce_cfg_value_type(replacement, original, full_key):
"""Checks that `replacement`, which is intended to replace `original` is of
the right type. The type is correct if it matches exactly or is one of a few
cases in which the type can be easily coerced.
"""
original_type = type(original)
replacement_type = type(replacement)
# TODO: Convert all to have non-None values by default.
if isinstance(original, type(None)):
return replacement
# The types must match (with some exceptions)
if replacement_type == original_type:
return replacement