Spaces:
Sleeping
Sleeping
Download scripts/prepare_weights.py from oussamab2n/GazeLIFT: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/oussamab2n/GazeLIFT/resolve/main/scripts/prepare_weights.py
- Command line
-
hf download hf://spaces/oussamab2n/GazeLIFT/scripts/prepare_weights.py
-
curl -L -o prepare_weights.py https://huggingface.co/spaces/oussamab2n/GazeLIFT/resolve/main/scripts/prepare_weights.py
2.09 kB
| """Verify the original trained adapters and export clean tensors for deployment.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import torch | |
| from safetensors.torch import save_file | |
| ROOT = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(ROOT / "model_code")) | |
| from model import TransposeCNNGazePyramidDecoder | |
| ORIGINAL_SHA256 = { | |
| "kvasir": "51cd87f2231df608bacaa611712b505a65fa90174649159e60015c29bf6798a8", | |
| "prostate": "d8f77e5b174beb8a41d4409eb0571c1e6a1ad7e8fa35aea50eb8d6ead9dd0f6d", | |
| } | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--kvasir", type=Path, required=True) | |
| parser.add_argument("--prostate", type=Path, required=True) | |
| args = parser.parse_args() | |
| manifest_path = ROOT / "weights" / "manifest.json" | |
| manifest = json.loads(manifest_path.read_text()) | |
| verified = {} | |
| for dataset, path in [("kvasir", args.kvasir), ("prostate", args.prostate)]: | |
| actual = hashlib.sha256(path.read_bytes()).hexdigest() | |
| if actual != ORIGINAL_SHA256[dataset]: | |
| raise ValueError(f"{dataset}: original checkpoint SHA-256 mismatch") | |
| state = torch.load(path, map_location="cpu", weights_only=True)["model"] | |
| model = TransposeCNNGazePyramidDecoder() | |
| model.load_state_dict(state, strict=True) | |
| verified[dataset] = {name: tensor.detach().cpu().contiguous() for name, tensor in state.items()} | |
| for dataset, state in verified.items(): | |
| filename = f"{dataset}_seed42.safetensors" | |
| destination = ROOT / "weights" / filename | |
| save_file(state, str(destination)) | |
| manifest[dataset].update(filename=filename, | |
| sha256=hashlib.sha256(destination.read_bytes()).hexdigest(), | |
| original_checkpoint_sha256=ORIGINAL_SHA256[dataset]) | |
| manifest_path.write_text(json.dumps(manifest, indent=2) + "\n") | |
| print("Both adapters verified and exported. Upload the clean weights and updated manifest to the Space.") | |
| if __name__ == "__main__": | |
| main() | |