Esempio n. 1
0
    def test_modeling_hook_runner(self):
        """Create model with modeling hook from runner"""
        runner = default_runner.Detectron2GoRunner()
        cfg = runner.get_default_cfg()
        cfg.MODEL.DEVICE = "cpu"
        cfg.MODEL.META_ARCHITECTURE = "TestArch"
        cfg.MODEL.MODELING_HOOKS = ["PlusOneHook", "TimesTwoHook"]
        model = runner.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)

        default_runner._close_all_tbx_writers()
Esempio n. 2
0
    def test_d2go_build_evaluator(self):
        for rotated, evaluator in [
            (True, RotatedCOCOEvaluator),
            (False, COCOEvaluator),
        ]:
            with tempfile.TemporaryDirectory() as tmp_dir:
                ds_name = create_local_dataset(tmp_dir,
                                               5,
                                               10,
                                               10,
                                               is_rotated=rotated)
                runner = default_runner.Detectron2GoRunner()
                cfg = _get_cfg(runner, tmp_dir, ds_name)

                ds_evaluators = runner.get_evaluator(cfg, ds_name, tmp_dir)
                self.assertTrue(
                    isinstance(ds_evaluators._evaluators[0], evaluator))
Esempio n. 3
0
    def test_d2go_runner_trainer_hooks(self):
        counts = 0

        @TRAINER_HOOKS_REGISTRY.register()
        def _check_hook_func(hooks):
            nonlocal counts
            counts = len(hooks)
            print(hooks)

        with tempfile.TemporaryDirectory() as tmp_dir:
            ds_name = create_local_dataset(tmp_dir, 5, 10, 10)
            runner = default_runner.Detectron2GoRunner()
            cfg = _get_cfg(runner, tmp_dir, ds_name)
            model = runner.build_model(cfg)
            runner.do_train(cfg, model, resume=True)

            default_runner._close_all_tbx_writers()

        self.assertGreater(counts, 0)
Esempio n. 4
0
def get_optimizer_cfg(
    lr,
    weight_decay=None,
    weight_decay_norm=None,
    weight_decay_bias=None,
    lr_mult=None,
):
    runner = default_runner.Detectron2GoRunner()
    cfg = runner.get_default_cfg()
    if lr is not None:
        cfg.SOLVER.BASE_LR = lr
    if weight_decay is not None:
        cfg.SOLVER.WEIGHT_DECAY = weight_decay
    if weight_decay_norm is not None:
        cfg.SOLVER.WEIGHT_DECAY_NORM = weight_decay_norm
    if weight_decay_bias is not None:
        cfg.SOLVER.WEIGHT_DECAY_BIAS = weight_decay_bias
    if lr_mult is not None:
        cfg.SOLVER.LR_MULTIPLIER_OVERWRITE = [lr_mult]
    return cfg
Esempio n. 5
0
    def test_dict_based_dataloader_visualizer_wrapper(self, tmp_dir: str):
        image_dir, json_file = create_test_images_and_dataset_json(
            tmp_dir, 60, 60)

        # Create config data
        runner = default_runner.Detectron2GoRunner()
        cfg = runner.get_default_cfg()
        cfg.merge_from_list([
            "D2GO_DATA.DATASETS.COCO_INJECTION.NAMES",
            str(["inj_ds3"]),
            "D2GO_DATA.DATASETS.COCO_INJECTION.IM_DIRS",
            str([image_dir]),
            "D2GO_DATA.DATASETS.COCO_INJECTION.JSON_FILES",
            str([json_file]),
            "DATASETS.TRAIN",
            str(["inj_ds3"]),
            "MODEL.META_ARCHITECTURE",
            "DummyMetaArch",
        ])

        # Register configs
        runner.register(cfg)
        DatasetCatalog.get("inj_ds3")

        with EventStorage():
            # Create mock storage for writer
            mock_tbx_writer = MockTbxWriter()
            # Create a wrapper around an iterable object and run once
            input_dict = create_dummy_input_dict(60, 60, [[1, 1, 2, 2]])
            dl_wrapper = DataLoaderVisWrapper(cfg, mock_tbx_writer,
                                              [[input_dict], [input_dict]])
            for _ in dl_wrapper:
                break

            # Check data has been written to buffer
            self.assertTrue(len(mock_tbx_writer._writer.write_buffer) == 2)
            self.assertTrue("train_loader_batch_0/default" in
                            mock_tbx_writer._writer.write_buffer[0]["tag"])
            self.assertTrue("train_loader_batch_0/secondary" in
                            mock_tbx_writer._writer.write_buffer[1]["tag"])
Esempio n. 6
0
 def _get_cfg(self, tmp_dir: str) -> CfgNode:
     runner = default_runner.Detectron2GoRunner()
     cfg = mah.create_detection_cfg(runner, tmp_dir)
     cfg.TEST.EVAL_PERIOD = cfg.SOLVER.MAX_ITER
     return cfg