def test_d2go_runner_test(self): 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) results = runner.do_test(cfg, model) self.assertEqual(results["default"][ds_name]["bbox"]["AP"], 10.0) default_runner._close_all_tbx_writers()
def setup(tmp_dir): ds_name = create_local_dataset(tmp_dir, 5, 10, 10) runner = default_runner.Detectron2GoRunner() cfg = _get_cfg(runner, tmp_dir, ds_name) cfg.merge_from_list( (["MODEL.META_ARCHITECTURE", "MetaArchForTestQAT"] + ["QUANTIZATION.QAT.ENABLED", "True"] + ["QUANTIZATION.QAT.START_ITER", "0"] + ["QUANTIZATION.QAT.ENABLE_OBSERVER_ITER", "0"])) return runner, cfg
def test_d2go_runner_train(self): 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) final_model_path = os.path.join(tmp_dir, "model_final.pth") self.assertTrue(os.path.isfile(final_model_path)) default_runner._close_all_tbx_writers()
def test_d2go_runner_build_model(self): 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) dl = runner.build_detection_train_loader(cfg) batch = next(iter(dl)) output = model(batch) self.assertIsInstance(output, dict) model.eval() output = model(batch) self.assertIsInstance(output, list) default_runner._close_all_tbx_writers()
def test_d2go_runner_ema(self): 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) cfg.MODEL.META_ARCHITECTURE = "MetaArchForTestSingleValue" cfg.MODEL_EMA.ENABLED = True cfg.MODEL_EMA.DECAY = 0.9 def _run_train(cfg): cfg = copy.deepcopy(cfg) model = runner.build_model(cfg) model = DistributedDataParallel(model, broadcast_buffers=False) runner.do_train(cfg, model, True) final_model_path = os.path.join(tmp_dir, "model_final.pth") trained_weights = torch.load(final_model_path) self.assertIn("ema_state", trained_weights) default_runner._close_all_tbx_writers() return final_model_path, model.module.ema_state def _run_test(cfg, final_path, gt_ema): cfg = copy.deepcopy(cfg) cfg.MODEL.WEIGHTS = final_path model = runner.build_model(cfg, eval_only=True) self.assertGreater(len(model.ema_state.state), 0) self.assertEqual(len(model.ema_state.state), len(gt_ema.state)) self.assertTrue( _compare_state_dict(model.ema_state.state_dict(), gt_ema.state_dict())) results = runner.do_test(cfg, model) self.assertEqual(results["default"][ds_name]["bbox"]["AP"], 3.0) self.assertEqual(results["ema"][ds_name]["bbox"]["AP"], 9.0) default_runner._close_all_tbx_writers() def _run_build_model_with_ema_weight(cfg, final_path, gt_ema): cfg = copy.deepcopy(cfg) cfg.MODEL.WEIGHTS = final_path cfg.MODEL_EMA.USE_EMA_WEIGHTS_FOR_EVAL_ONLY = True model = runner.build_model(cfg, eval_only=True) self.assertTrue( _compare_state_dict(model.state_dict(), gt_ema.state_dict())) final_model_path, gt_ema = _run_train(cfg) _run_test(cfg, final_model_path, gt_ema) _run_build_model_with_ema_weight(cfg, final_model_path, gt_ema)
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))
def create_detection_cfg(runner, output_dir): ds_name = create_local_dataset(output_dir, 5, 10, 10) cfg = runner.get_default_cfg() return get_det_meta_arch_cfg(cfg, ds_name, output_dir)