Spaces:
Sleeping
Sleeping
File size: 2,094 Bytes
434f330 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """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()
|