pixelgen2 / pixelgen /weights.py
DJLougen's picture
PixelGen2: compositional hand-weighted pixel-art generator + 4 scaling experiments
6fbb543 verified
Raw History Blame Contribute Delete
770 Bytes
"""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