"""SemanticStyleGAN CelebAMask-HQ: the authors' checkpoint to one ONNX network.

    python semantic.py <CelebAMask-HQ-512x512.pt> <repo> <output-file>

SemanticStyleGAN gives each part of the face its own pair of latent codes, one
for its shape and one for its texture: background, face, eyes, brows, mouth,
nose, ears, hair, neck, clothes, glasses, hat and earrings, plus two codes for
the overall layout. That's 28 rows of W. The network is built with the
authors' classes (the `repo` argument, SemanticStyleGAN), and the app's whole
use of it is folded into one ONNX file:

    z [1, 512], scales [1, 28]  ->  image [1, 3, 512, 512] in -1..1

z is mapped to W and truncated 70% of the way towards the average face, as
the authors' own generate script does. Then each of the 28 rows is scaled
away from the average by its own number: 1 leaves that part as it is, 0 makes
it average and 2 exaggerates it. The average face is computed here from
10,000 samples and kept inside the file.
"""

import sys

import torch

import cpu_ops
from export import export, verify

checkpoint, repo, output = sys.argv[1:4]
sys.path.insert(0, repo)
cpu_ops.install("models.op")
from models import make_model  # noqa: E402
from models import utils as model_utils  # noqa: E402

#  The same convolution with weights of a fixed shape, which ONNX can follow
model_utils.ModulatedConv2d.forward = cpu_ops.unfused_modulated_conv2d

state = torch.load(checkpoint, map_location="cpu", weights_only=False)
G = make_model(state["args"], verbose=False)
G.load_state_dict(state["g_ema"])
G = G.float().eval()
print(f"  {G.size}px, {G.n_latent} rows of W")

TRUNCATION = 0.7
with torch.no_grad():
    mean = G.style(torch.randn(10000, 512, generator=torch.Generator().manual_seed(0))).mean(0, keepdim=True)


class Semantic(torch.nn.Module):
    def __init__(self, G, mean):
        super().__init__()
        self.G = G
        self.register_buffer("mean", mean)

    def forward(self, z, scales):
        w = self.mean + TRUNCATION * (self.G.style(z) - self.mean)
        rows = self.mean[:, None, :] + scales[:, :, None] * (w - self.mean)[:, None, :]
        image, _ = self.G([rows], input_is_latent=True, randomize_noise=False)
        return image


model = Semantic(G, mean).eval()
z = torch.randn(1, 512, generator=torch.Generator().manual_seed(1))
scales = torch.ones(1, G.n_latent)
export(model, (z, scales), ["z", "scales"], ["image"], output)
verify(output, {"z": z, "scales": scales}, model(z, scales))
