Download examples/extract_embeddings.py from madim1827/WALoMA: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/madim1827/WALoMA/resolve/main/examples/extract_embeddings.py
- Command line
-
hf download hf://madim1827/WALoMA/examples/extract_embeddings.py
-
curl -L -o extract_embeddings.py https://huggingface.co/madim1827/WALoMA/resolve/main/examples/extract_embeddings.py
1.48 kB
| """Extract WALoMA v1 embeddings from complex channels. | |
| python examples/extract_embeddings.py # random channel | |
| python examples/extract_embeddings.py --task-dir downstream/task_1 | |
| """ | |
| import argparse | |
| import sys | |
| from pathlib import Path | |
| import torch | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from waloma import load_pretrained, patchify # noqa: E402 | |
| p = argparse.ArgumentParser() | |
| p.add_argument("--ckpt", default=None, help="checkpoint (.pth or .safetensors); default checkpoints/model_checkpoint.pth") | |
| p.add_argument("--task-dir", default=None, help="LWM-2025 task folder with test_data.pt") | |
| p.add_argument("--patch", default="1x16", help="patch shape RxC (paper downstream: 1x16; pretraining: 4x4)") | |
| args = p.parse_args() | |
| if args.task_dir: | |
| data = torch.load(Path(args.task_dir) / "test_data.pt", map_location="cpu", weights_only=True) | |
| channels = data["channels"][:16] | |
| else: | |
| channels = torch.randn(4, 32, 32, dtype=torch.complex64) | |
| pr, pc = (int(v) for v in args.patch.lower().split("x")) | |
| tokens, (n_ant_p, n_sub_p) = patchify(channels, pr, pc) | |
| model = load_pretrained(args.ckpt) | |
| with torch.no_grad(): | |
| emb = model(tokens, N_ant_batch=n_ant_p, N_sub_batch=n_sub_p) # (B, P, 64) | |
| print(f"channels {tuple(channels.shape)} -> tokens {tuple(tokens.shape)} (grid {n_ant_p}x{n_sub_p})") | |
| print(f"embeddings {tuple(emb.shape)}; first-token embedding {tuple(emb[:, 0].shape)}, mean-pooled {tuple(emb.mean(1).shape)}") | |