Download ssl_retrieval/checkpoints.py from duyle2408/milk10k_new_code: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/datasets/duyle2408/milk10k_new_code/resolve/main/ssl_retrieval/checkpoints.py
- Command line
-
hf download hf://datasets/duyle2408/milk10k_new_code/ssl_retrieval/checkpoints.py
-
curl -L -o checkpoints.py https://huggingface.co/datasets/duyle2408/milk10k_new_code/resolve/main/ssl_retrieval/checkpoints.py
1.19 kB
| from __future__ import annotations | |
| from pathlib import Path | |
| import torch | |
| from . import __version__ | |
| from .models import SSLModel | |
| def save_checkpoint(path, model, method, config, epoch, metrics): | |
| payload = { | |
| "version": __version__, "method": method, "epoch": epoch, | |
| "config": config, "metrics": metrics, "encoder": model.encoder.state_dict(), | |
| "projector": model.projector.state_dict() if model.projector else None, | |
| "rotation_head": model.rotation_head.state_dict() if model.rotation_head else None, | |
| "decoder": model.decoder.state_dict() if model.decoder else None, | |
| } | |
| Path(path).parent.mkdir(parents=True, exist_ok=True) | |
| torch.save(payload, path) | |
| def load_checkpoint(path, device="cpu"): | |
| payload = torch.load(path, map_location=device, weights_only=True) | |
| model = SSLModel(payload["method"], pretrained=False) | |
| model.encoder.load_state_dict(payload["encoder"]) | |
| for name in ("projector", "rotation_head", "decoder"): | |
| module, state = getattr(model, name), payload.get(name) | |
| if module is not None and state is not None: | |
| module.load_state_dict(state) | |
| return model.to(device), payload | |