File size: 1,767 Bytes
e3db6f9 | 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 51 52 53 54 55 56 57 58 59 60 61 62 | #!/usr/bin/env python3
"""
Convert YuzuMarker.FontDetection checkpoints (.ckpt) to safetensors for Candle.
Example:
python scripts/convert_font_detection.py \
--checkpoint name=4x-epoch=84-step=1649340.ckpt
"""
import argparse
from pathlib import Path
from huggingface_hub import hf_hub_download
import torch
from safetensors.torch import save_file
DEFAULT_CKPT = "name=4x-epoch=84-step=1649340.ckpt"
REPO_ID = "gyrojeff/YuzuMarker.FontDetection"
def parse_args() -> argparse.Namespace:
cache_dir = (
Path.home() / ".cache" / "Koharu" / "models" / "yuzumarker-font-detection.safetensors"
)
parser = argparse.ArgumentParser(description="Convert YuzuMarker.FontDetection checkpoint.")
parser.add_argument(
"-c",
"--checkpoint",
default=DEFAULT_CKPT,
help=f"Checkpoint filename from {REPO_ID} (default: {DEFAULT_CKPT})",
)
parser.add_argument(
"-o",
"--output",
type=Path,
default=cache_dir,
help=f"Output safetensors path (default: {cache_dir})",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
args.output.parent.mkdir(parents=True, exist_ok=True)
print(f"Downloading {args.checkpoint} from {REPO_ID} ...")
ckpt_path = hf_hub_download(repo_id=REPO_ID, filename=args.checkpoint)
print(f"Loaded checkpoint at {ckpt_path}")
state = torch.load(ckpt_path, map_location="cpu")
if "state_dict" not in state:
raise RuntimeError("Unexpected checkpoint format: missing state_dict")
state_dict = state["state_dict"]
print(f"Saving {len(state_dict)} tensors to {args.output}")
save_file(state_dict, str(args.output))
print("Done.")
if __name__ == "__main__":
main()
|