| """ |
| Submission script for 2-stage fMRI encoding with Flow Matching. |
| |
| Generates predictions for friends-s7 (in-distribution) and ood (out-of-distribution) |
| test sets. Outputs are saved as .npy and .zip files matching the Algonauts 2025 |
| challenge format. |
| |
| Usage: |
| python -m src.submission --checkpoint-dir output/two_stage_encoding |
| """ |
|
|
| import argparse |
| import warnings |
| from pathlib import Path |
|
|
| import numpy as np |
| import torch |
| from omegaconf import OmegaConf |
|
|
| from .data import Algonauts2025Dataset, load_sharded_features, episode_filter |
| from .stage1.medarc_architecture import MultiSubjectConvLinearEncoder |
|
|
| from .loaders import load_all_features, make_test_loader, DEFAULT_DATA_DIR, SUBJECTS |
| from .builder import build_models |
| from .loops import run_inference |
| from .evaluate import validate_submission |
| from .submission_utils import load_fmri_num_samples, print_summary, save_predictions |
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser(description="Generate submission predictions") |
| parser.add_argument( |
| "--checkpoint-dir", |
| type=str, |
| required=True, |
| help=( |
| "Path to trained model output directory " |
| "(contains config.yaml, stage1_best.pt, stage2_epoch_*.pt)" |
| ), |
| ) |
| parser.add_argument( |
| "--test-set", |
| type=str, |
| default="all", |
| choices=["friends-s7", "ood", "all"], |
| help="Which test set(s) to generate predictions for", |
| ) |
| parser.add_argument( |
| "--stage2-ckpt", |
| type=str, |
| default=None, |
| help="Stage 2 checkpoint filename (default: latest)", |
| ) |
| parser.add_argument( |
| "--n-timesteps", |
| type=int, |
| default=None, |
| help="Override number of ODE steps for stage 2", |
| ) |
| parser.add_argument("--device", type=str, default="cuda") |
| parser.add_argument("--datasets-root", type=str, default=None) |
| parser.add_argument( |
| "--output-dir", |
| type=str, |
| default=None, |
| help="Output directory (default: <checkpoint-dir>/submission)", |
| ) |
| args = parser.parse_args() |
|
|
| ckpt_dir = Path(args.checkpoint_dir) |
| cfg = OmegaConf.load(ckpt_dir / "config.yaml") |
|
|
| datasets_root = Path(args.datasets_root or cfg.get("datasets_root") or DEFAULT_DATA_DIR) |
| device = torch.device(args.device) |
| subjects = list(cfg.get("subjects", list(DEFAULT_SUBJECTS))) |
| n_timesteps = int(args.n_timesteps or cfg.stage2.get("n_timesteps", 25)) |
|
|
| out_dir = Path(args.output_dir) if args.output_dir else ckpt_dir / "submission" |
| out_dir.mkdir(parents=True, exist_ok=True) |
|
|
| np_version = tuple(int(x) for x in np.__version__.split(".")[:2]) |
| if np_version[0] >= 2: |
| warnings.warn( |
| ( |
| f"NumPy {np.__version__} detected. Codabench requires NumPy < 2.0. " |
| "Submissions saved with NumPy 2.x can fail formatting checks." |
| ), |
| stacklevel=1, |
| ) |
|
|
| print(f"Checkpoint dir: {ckpt_dir}") |
| print(f"Output dir: {out_dir}") |
| print(f"Device: {device}") |
| print(f"Subjects: {subjects}") |
| print(f"Stage 2 ODE timesteps: {n_timesteps}") |
|
|
| print("Loading features...") |
| all_features = load_all_features(cfg, datasets_root) |
|
|
| print("Building models...") |
| stage1_model, stage2_models = build_models(cfg, ckpt_dir, all_features, subjects, device, args.stage2_ckpt) |
|
|
| test_sets = ["friends-s7", "ood"] if args.test_set == "all" else [args.test_set] |
|
|
| for test_set_name in test_sets: |
| print(f"\n{'=' * 60}") |
| print(f"Generating predictions for: {test_set_name}") |
| print(f"{'=' * 60}") |
|
|
| fmri_num_samples = load_fmri_num_samples(datasets_root, test_set_name) |
| test_loader = make_test_loader(cfg, all_features, fmri_num_samples, test_set_name) |
|
|
| predictions = run_inference( |
| stage1_model=stage1_model, |
| stage2_models=stage2_models, |
| test_loader=test_loader, |
| fmri_num_samples=fmri_num_samples, |
| subjects=subjects, |
| device=device, |
| n_timesteps=n_timesteps, |
| ) |
|
|
| validate_submission(predictions, test_set_name, fmri_num_samples) |
| print_summary(predictions) |
| save_predictions(predictions, test_set_name, out_dir) |
|
|
| print("\nDone!") |
|
|
| if __name__ == "__main__": |
| main() |
|
|