(hydra_cfg: DictConfig)
| 18 | |
| 19 | @hydra.main(version_base="1.2", config_path="../configs", config_name="eval") |
| 20 | def main(hydra_cfg: DictConfig): |
| 21 | if not torch.cuda.is_available() or hydra_cfg.device != "cuda": |
| 22 | raise EnvironmentError("Sampling with DDP requires at least one GPU. sample.py supports CPU-only usage") |
| 23 | dist.init_process_group("nccl") |
| 24 | rank = dist.get_rank() |
| 25 | device = rank % torch.cuda.device_count() |
| 26 | |
| 27 | torch.cuda.set_device(device) |
| 28 | print(f"Starting rank={rank}, world_size={dist.get_world_size()}.") |
| 29 | |
| 30 | all_eval_models: ListConfig = hydra_cfg.eval_models # see configs/evaluation/relpose-angular.yaml |
| 31 | all_eval_datasets: ListConfig = hydra_cfg.eval_datasets # see configs/evaluation/relpose-angular.yaml |
| 32 | all_data_info: DictConfig = hydra_cfg.data # see configs/data |
| 33 | all_model_info: DictConfig = hydra_cfg.model # see configs/model |
| 34 | |
| 35 | for idx_model, model_keyname in enumerate(all_eval_models, start=1): |
| 36 | # 0.1 look up model config from configs/model, decide the model name (to save) |
| 37 | if model_keyname not in all_model_info: |
| 38 | raise ValueError(f"Unknown model in global data information: {model_keyname}") |
| 39 | model_info = all_model_info[model_keyname] |
| 40 | |
| 41 | # 0.2 load the model |
| 42 | model = hydra.utils.instantiate(model_info.cfg).to(hydra_cfg.device) |
| 43 | model_logger = logging.getLogger(f"relpose-angle-{model_keyname}-rank{rank}") |
| 44 | model_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 '???'}") |
| 45 | |
| 46 | # 0.3 route the correct infer function for the model |
| 47 | # output_root = osp.join(hydra_cfg.log.output_dir, model_name) |
| 48 | infer_func_cfg = model_info.get( |
| 49 | "infer_cameras_w2c", |
| 50 | DictConfig({ |
| 51 | '_target_': f'interfaces.{model_keyname}.infer_cameras_w2c', |
| 52 | '_partial_': True, |
| 53 | }) |
| 54 | ) |
| 55 | infer_cameras_w2c = hydra.utils.instantiate(infer_func_cfg) |
| 56 | |
| 57 | for idx_dataset, dataset_name in enumerate(all_eval_datasets, start=1): |
| 58 | save_dir = osp.join(hydra_cfg.output_dir, model_keyname) |
| 59 | if rank == 0: |
| 60 | os.makedirs(save_dir, exist_ok=True) |
| 61 | for file in os.listdir(save_dir): |
| 62 | if file.endswith(".npy"): |
| 63 | os.remove(osp.join(save_dir, file)) |
| 64 | dist.barrier() # wait for all ranks to finish this sequence |
| 65 | # 1. look up dataset config from configs/data, decide the dataset name |
| 66 | if dataset_name not in all_data_info: |
| 67 | raise ValueError(f"Unknown dataset in global data information: {dataset_name}") |
| 68 | dataset_info = all_data_info[dataset_name] |
| 69 | dataset = hydra.utils.instantiate(dataset_info.cfg) |
| 70 | |
| 71 | # 2. ready to read, and look up sampled ids from sequence name |
| 72 | model.eval() |
| 73 | with open(dataset_info.seq_id_map, "r") as f: |
| 74 | seq_id_map = json.load(f) |
| 75 | |
| 76 | # 3. prepare for metrics |
| 77 | rError = [] |
no test coverage detected