(hydra_cfg: DictConfig)
| 20 | |
| 21 | @hydra.main(version_base="1.2", config_path="../configs", config_name="eval") |
| 22 | def main(hydra_cfg: DictConfig): |
| 23 | # setup_debug(hydra_cfg.debug) |
| 24 | logger = logging.getLogger("monodepth-infer") |
| 25 | |
| 26 | all_eval_models: ListConfig = hydra_cfg.eval_models # see configs/evaluation/monodepth.yaml |
| 27 | all_eval_datasets: ListConfig = hydra_cfg.eval_datasets # see configs/evaluation/monodepth.yaml |
| 28 | all_data_info: DictConfig = hydra_cfg.data # see configs/data |
| 29 | all_model_info: DictConfig = hydra_cfg.model # see configs/model |
| 30 | |
| 31 | for idx_model, model_keyname in enumerate(all_eval_models, start=1): |
| 32 | # 0.1 look up model config from configs/model |
| 33 | if model_keyname not in all_model_info: |
| 34 | raise ValueError(f"Unknown model in global data information: {model_keyname}") |
| 35 | model_info = all_model_info[model_keyname] |
| 36 | |
| 37 | # 0.2 load the model |
| 38 | model = hydra.utils.instantiate(model_info.cfg).to(hydra_cfg.device) |
| 39 | logger.info(f"[{idx_model}/{len(all_eval_models)}] Loaded Model {model_keyname} from {model_info.cfg.pretrained_model_name_or_path if hasattr(model_info.cfg, 'pretrained_model_name_or_path') else '???'}") |
| 40 | |
| 41 | # 0.3 look up infer_monodepth function |
| 42 | infer_func_cfg = model_info.get( |
| 43 | "infer_monodepth", |
| 44 | DictConfig({ |
| 45 | '_target_': f'interfaces.{model_keyname}.infer_monodepth', |
| 46 | '_partial_': True, |
| 47 | }) |
| 48 | ) |
| 49 | infer_monodepth = hydra.utils.instantiate(infer_func_cfg) |
| 50 | |
| 51 | model_logger = logging.getLogger(f"monodepth-infer-{model_keyname}") |
| 52 | for idx_dataset, dataset_name in enumerate(all_eval_datasets, start=1): |
| 53 | # 1. look up dataset config from configs/data |
| 54 | if dataset_name not in all_data_info: |
| 55 | raise ValueError(f"Unknown dataset: {dataset_name}") |
| 56 | dataset_info = all_data_info[dataset_name] |
| 57 | |
| 58 | # 2. get the sequence list |
| 59 | if dataset_info.type == "video": |
| 60 | # most of the datasets have many sequences of video |
| 61 | seq_list = get_all_sequences(dataset_info) |
| 62 | elif dataset_info.type == "mono": |
| 63 | # some datasets (like nyu-v2) have only a set of images, only for monodepth |
| 64 | seq_list = [None] |
| 65 | else: |
| 66 | raise ValueError(f"Unknown dataset type: {dataset_info.type}") |
| 67 | |
| 68 | # 3. infer for each sequence |
| 69 | model = model.eval() |
| 70 | output_root = osp.join(hydra_cfg.output_dir, model_keyname, dataset_name) |
| 71 | model_logger.info(f"[{idx_dataset}/{len(all_eval_datasets)}] Infering monodepth on {dataset_name} dataset..., output to {osp.relpath(output_root, hydra_cfg.work_dir)}") |
| 72 | for seq_idx, seq in enumerate(seq_list): |
| 73 | # 3.1 list the images in the sequence |
| 74 | filelist = list_imgs_a_sequence(dataset_info, seq) |
| 75 | save_dir = osp.join(output_root, seq) if seq is not None else output_root |
| 76 | os.makedirs(save_dir, exist_ok=True) |
| 77 | model_logger.info(f"[{seq_idx}/{len(seq_list)}] Processing {len(filelist)} images to {osp.relpath(save_dir, hydra_cfg.work_dir)}...") |
| 78 | |
| 79 | # 3.2 infer for each image |
no test coverage detected