def test_diff_cfg_no_new_allowed(self): """check that if new_allowed is False, new keys cause key error""" # create base config cfg1 = CfgNode() cfg1.A = CfgNode() cfg1.A.set_new_allowed(False) cfg1.A.Y = 2 # case 2: new allowed not set, new config has new keys cfg2 = cfg1.clone() cfg2.A.X = 2 self.assertRaises(KeyError, get_diff_cfg, cfg1, cfg2)
def _add_rcnn_default_config(_C): _C.EXPORT_CAFFE2 = CfgNode() _C.EXPORT_CAFFE2.USE_HEATMAP_MAX_KEYPOINT = False # Options about how to export the model _C.RCNN_EXPORT = CfgNode() # whether or not to include the postprocess (GeneralizedRCNN._postprocess) step # inside the exported model _C.RCNN_EXPORT.INCLUDE_POSTPROCESS = False _C.RCNN_PREPARE_FOR_EXPORT = "default_rcnn_prepare_for_export" _C.RCNN_PREPARE_FOR_QUANT = "default_rcnn_prepare_for_quant" _C.RCNN_PREPARE_FOR_QUANT_CONVERT = "default_rcnn_prepare_for_quant_convert"
def test_diff_cfg_with_new_allowed(self): """diff config with new keys and new_allowed set to True""" # create base config cfg1 = CfgNode() cfg1.A = CfgNode() cfg1.A.set_new_allowed(True) cfg1.A.Y = 2 # case 3: new allowed set, new config has new keys cfg2 = cfg1.clone() cfg2.A.X = 2 gt = CfgNode() gt.A = CfgNode() gt.A.X = 2 self.assertEqual(gt, get_diff_cfg(cfg1, cfg2))
def test_get_diff_cfg(self): """check config that is diff from default config, no new keys""" # create base config cfg1 = CfgNode() cfg1.A = CfgNode() cfg1.A.Y = 2 # case 1: new allowed not set, new config has only old keys cfg2 = cfg1.clone() cfg2.set_new_allowed(False) cfg2.A.Y = 3 gt = CfgNode() gt.A = CfgNode() gt.A.Y = 3 self.assertEqual(gt, get_diff_cfg(cfg1, cfg2))
def test_modeling_hook_cfg(self): """Create model with modeling hook using build_model""" cfg = CfgNode() cfg.MODEL = CfgNode() cfg.MODEL.DEVICE = "cpu" cfg.MODEL.META_ARCHITECTURE = "TestArch" cfg.MODEL.MODELING_HOOKS = ["PlusOneHook", "TimesTwoHook"] model = build_model(cfg) self.assertEqual(model(2), 10) self.assertTrue(hasattr(model, "_modeling_hooks")) self.assertTrue(hasattr(model, "unapply_modeling_hooks")) orig_model = model.unapply_modeling_hooks() self.assertIsInstance(orig_model, TestArch) self.assertEqual(orig_model(2), 4)
def cast_from_other_class(cls, other_cfg): """Cast an instance of other CfgNode to D2Go's CfgNode (or its subclass)""" new_cfg = CfgNode(other_cfg) # copy all fields inside __dict__, this will preserve fields like __deprecated_keys__ for k, v in other_cfg.__dict__.items(): new_cfg.__dict__[k] = v return new_cfg
def create_cfg_from_cli_args(args, default_cfg): """ Instead of loading from defaults.py, this binary only includes necessary configs building from scratch, and overrides them from args. There're two levels of config: _C: the config system used by this binary, which is a sub-set of training config, override by configurable_cfg. It can also be override by args.opts for convinience. configurable_cfg: common configs that user should explicitly specify in the args. """ _C = CfgNode() _C.INPUT = default_cfg.INPUT _C.DATASETS = default_cfg.DATASETS _C.DATALOADER = default_cfg.DATALOADER _C.TEST = default_cfg.TEST if hasattr(default_cfg, "D2GO_DATA"): _C.D2GO_DATA = default_cfg.D2GO_DATA if hasattr(default_cfg, "TENSORBOARD"): _C.TENSORBOARD = default_cfg.TENSORBOARD # NOTE configs below might not be necessary, but must add to make code work _C.MODEL = CfgNode() _C.MODEL.META_ARCHITECTURE = default_cfg.MODEL.META_ARCHITECTURE _C.MODEL.MASK_ON = default_cfg.MODEL.MASK_ON _C.MODEL.KEYPOINT_ON = default_cfg.MODEL.KEYPOINT_ON _C.MODEL.LOAD_PROPOSALS = default_cfg.MODEL.LOAD_PROPOSALS assert _C.MODEL.LOAD_PROPOSALS is False, "caffe2 model doesn't support" _C.OUTPUT_DIR = args.output_dir configurable_cfg = [ "DATASETS.TEST", args.datasets, "INPUT.MIN_SIZE_TEST", args.min_size, "INPUT.MAX_SIZE_TEST", args.max_size, ] cfg = _C.clone() cfg.merge_from_list(configurable_cfg) cfg.merge_from_list(args.opts) return cfg
def test_modeling_hook_copy(self): """Create model with modeling hook, the model could be copied""" cfg = CfgNode() cfg.MODEL = CfgNode() cfg.MODEL.DEVICE = "cpu" cfg.MODEL.META_ARCHITECTURE = "TestArch" cfg.MODEL.MODELING_HOOKS = ["PlusOneHook", "TimesTwoHook"] model = build_model(cfg) self.assertEqual(model(2), 10) model_copy = copy.deepcopy(model) orig_model = model.unapply_modeling_hooks() self.assertIsInstance(orig_model, TestArch) self.assertEqual(orig_model(2), 4) orig_model_copy = model_copy.unapply_modeling_hooks() self.assertEqual(orig_model_copy(2), 4)
def get_default_cfg(): _C = super(GeneralizedRCNNRunner, GeneralizedRCNNRunner).get_default_cfg() _C.EXPORT_CAFFE2 = CfgNode() _C.EXPORT_CAFFE2.USE_HEATMAP_MAX_KEYPOINT = False _C.RCNN_PREPARE_FOR_EXPORT = "default_rcnn_prepare_for_export" _C.RCNN_PREPARE_FOR_QUANT = "default_rcnn_prepare_for_quant" _C.RCNN_PREPARE_FOR_QUANT_CONVERT = "default_rcnn_prepare_for_quant_convert" return _C
def test_merge_from_list_with_new_allowed(self): """ YACS's merge_from_list doesn't take new_allowed into account, D2Go override its behavior, and this test covers it. """ # new_allowed is not set cfg = CfgNode() cfg.A = CfgNode() cfg.A.X = 1 self.assertRaises(Exception, cfg.merge_from_list, ["A.Y", "2"]) # new_allowed is set for sub key cfg = CfgNode() cfg.A = CfgNode(new_allowed=True) cfg.A.X = 1 cfg.merge_from_list(["A.Y", "2"]) self.assertEqual(cfg.A.Y, 2) # note that the string will be converted to number # however new_allowed is not set for root key self.assertRaises(Exception, cfg.merge_from_list, ["B", "3"])
def run_with_cmdline_args(args): cfg, output_dir, runner = prepare_for_launch(args) inference_config = None if args.inference_config_file: inference_config = CfgNode( CfgNode.load_yaml_with_base(args.inference_config_file)) return main( cfg, output_dir, runner, # binary specific optional arguments predictor_types=args.predictor_types, compare_accuracy=args.compare_accuracy, skip_if_fail=args.skip_if_fail, inference_config=inference_config, )
def _get_default_config(): cfg = CfgNode() cfg.INPUT = CfgNode() cfg.INPUT.CROP = CfgNode() cfg.INPUT.CROP.ENABLED = False cfg.INPUT.CROP.SIZE = (0.9, 0.9) cfg.INPUT.CROP.TYPE = "relative_range" cfg.MODEL = CfgNode() cfg.MODEL.MIN_DIM_SIZE = 360 cfg.INFERENCE_SDK = CfgNode() cfg.INFERENCE_SDK.MODEL = CfgNode() cfg.INFERENCE_SDK.MODEL.SCORE_THRESHOLD = 0.8 cfg.INFERENCE_SDK.IOU_TRACKER = CfgNode() cfg.INFERENCE_SDK.IOU_TRACKER.IOU_THRESHOLD = 0.15 cfg.INFERENCE_SDK.ENABLE_ID_TRACKING = True return cfg
def get_default_config(): cfg = CfgNode() cfg.D2GO_DATA = CfgNode() cfg.D2GO_DATA.AUG_OPS = CfgNode() return cfg
def _test2(cfg): cfg.TEST2 = CfgNode() cfg.TEST2.Y = 2 return cfg
def _test1(cfg): cfg.TEST1 = CfgNode() cfg.TEST1.X = 1 return cfg