"""Save and load the hand-written PixelGen state dict. The weights are set in code, so saving is only a convenience: it lets generate.py run without re-building the model and gives a file to check into version control. """ from pathlib import Path import torch from .model import PixelGen DEFAULT_PATH = Path(__file__).resolve().parent.parent / "weights" / "pixelgen.pt" def save(path=DEFAULT_PATH, model=None): model = model or PixelGen() path = Path(path) path.parent.mkdir(parents=True, exist_ok=True) torch.save(model.state_dict(), path) return path def load(path=DEFAULT_PATH): model = PixelGen() state = torch.load(path, map_location="cpu", weights_only=True) model.load_state_dict(state) model.eval() return model