Download experiments/test_model.py from sabertoaster/flow-matching-1: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/datasets/sabertoaster/flow-matching-1/resolve/main/experiments/test_model.py
- Command line
-
hf download hf://datasets/sabertoaster/flow-matching-1/experiments/test_model.py
-
curl -L -o test_model.py https://huggingface.co/datasets/sabertoaster/flow-matching-1/resolve/main/experiments/test_model.py
3.07 kB
| """Quick checkpoint load and forward-pass sanity check for two-stage models. | |
| Example: | |
| python experiments/test_model.py --checkpoint-dir output/two_stage_encoding | |
| """ | |
| import argparse | |
| from pathlib import Path | |
| import torch | |
| ROOT = Path(__file__).resolve().parent.parent | |
| import sys | |
| sys.path.append(str(ROOT)) | |
| from src.flowfm.checkpointing import load_state_dict, resolve_stage2_checkpoint | |
| from src.flowfm.config import load_config, resolve_subjects | |
| from src.flowfm.data_pipeline import make_data_loaders | |
| from src.flowfm.model_factory import ( | |
| build_stage1_model, | |
| build_stage2_models, | |
| infer_feature_dims, | |
| infer_target_dim, | |
| ) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Sanity-check two-stage checkpoint loading") | |
| parser.add_argument( | |
| "--checkpoint-dir", | |
| type=str, | |
| default=str(ROOT / "output" / "two_stage_encoding"), | |
| help="Directory containing config.yaml and stage checkpoints", | |
| ) | |
| parser.add_argument("--cfg-path", type=str, default=None) | |
| parser.add_argument("--stage2-ckpt", type=str, default=None) | |
| parser.add_argument("--device", type=str, default=None) | |
| parser.add_argument("--n-timesteps", type=int, default=25) | |
| args = parser.parse_args() | |
| checkpoint_dir = Path(args.checkpoint_dir) | |
| cfg_path = args.cfg_path or str(checkpoint_dir / "config.yaml") | |
| cfg = load_config(cfg_path) | |
| if args.device is not None: | |
| cfg.device = args.device | |
| device = torch.device(cfg.device) | |
| subjects = resolve_subjects(cfg) | |
| data_loaders = make_data_loaders(cfg) | |
| sample_batch = next(iter(data_loaders["train"])) | |
| feat_dims = infer_feature_dims(sample_batch) | |
| target_dim = infer_target_dim(sample_batch) | |
| stage1_model = build_stage1_model( | |
| cfg=cfg, | |
| feat_dims=feat_dims, | |
| subjects=subjects, | |
| device=device, | |
| ) | |
| stage1_path = checkpoint_dir / "stage1_best.pt" | |
| stage1_model.load_state_dict(load_state_dict(stage1_path, device)) | |
| stage1_model.eval() | |
| stage2_models = build_stage2_models( | |
| cfg=cfg, | |
| target_dim=target_dim, | |
| subjects=subjects, | |
| device=device, | |
| ) | |
| stage2_path = resolve_stage2_checkpoint(checkpoint_dir, args.stage2_ckpt) | |
| stage2_models.load_state_dict(load_state_dict(stage2_path, device)) | |
| stage2_models.eval() | |
| print(f"Loaded Stage 1 checkpoint: {stage1_path}") | |
| print(f"Loaded Stage 2 checkpoint: {stage2_path}") | |
| feats = [f.to(device) for f in sample_batch["features"]] | |
| with torch.no_grad(): | |
| mu_anchor = stage1_model(feats) | |
| preds = [] | |
| for i, sub in enumerate(subjects): | |
| cfm = stage2_models[str(sub)] | |
| mu = mu_anchor[:, i].transpose(1, 2) | |
| pred = cfm(mu, n_timesteps=int(args.n_timesteps)).transpose(1, 2) | |
| preds.append(pred.unsqueeze(1)) | |
| pred_all = torch.cat(preds, dim=1) | |
| print(f"Stage 1 output shape: {tuple(mu_anchor.shape)}") | |
| print(f"Stage 2 output shape: {tuple(pred_all.shape)}") | |
| if __name__ == "__main__": | |
| main() | |