models/convert/stylegan2.py

Python source for Making faces, 101 lines. Raw file

"""StyleGAN2 FFHQ and MetFaces: NVIDIA's checkpoints to two ONNX networks each.

    python stylegan2.py <checkpoint.pkl> <repo> <output-prefix>

The checkpoint is loaded with NVIDIA's own code (stylegan2-ada-pytorch, the
`repo` argument), which reads both the original TensorFlow FFHQ pickle and the
newer PyTorch ones. The generator is split where FaceMaker needs it:

    <prefix>-map.onnx      z [1, 512]       ->  ws [1, 18, 512]
    <prefix>-synth.onnx    ws [1, 18, 512]  ->  image [1, 3, 1024, 1024] in -1..1

The app edits ws between the two (the direction sliders, and W+ latents from
the encoder), which is why they're separate. The noise inputs are fixed
(noise_mode="const"), so a seed always gives the same face.

Needs click and scipy, which NVIDIA's loader imports.
"""

import sys

import torch
import torch.nn.functional as F

from export import export, verify

checkpoint, repo, prefix = sys.argv[1:4]
sys.path.insert(0, repo)
import legacy  # noqa: E402  (from stylegan2-ada-pytorch)
from torch_utils.ops import upfirdn2d  # noqa: E402


def upfirdn2d_fixed(x, f, up=1, down=1, padding=0, flip_filter=False, gain=1):
    """NVIDIA's reference upfirdn2d, reading the sizes as plain numbers.

    The original takes the channel count from the tensor, so the exporter sees
    the filter as a kernel of unknown shape. The sums are the same.
    """
    if f is None:
        f = torch.ones([1, 1], dtype=torch.float32)
    batch, channels, height, width = (int(size) for size in x.shape)
    upx, upy = upfirdn2d._parse_scaling(up)
    downx, downy = upfirdn2d._parse_scaling(down)
    padx0, padx1, pady0, pady1 = upfirdn2d._parse_padding(padding)

    x = x.reshape([batch, channels, height, 1, width, 1])
    x = F.pad(x, [0, upx - 1, 0, 0, 0, upy - 1])
    x = x.reshape([batch, channels, height * upy, width * upx])

    x = F.pad(x, [max(padx0, 0), max(padx1, 0), max(pady0, 0), max(pady1, 0)])
    padded_h = height * upy + max(pady0, 0) + max(pady1, 0)
    padded_w = width * upx + max(padx0, 0) + max(padx1, 0)
    x = x[:, :, max(-pady0, 0): padded_h - max(-pady1, 0), max(-padx0, 0): padded_w - max(-padx1, 0)]

    f = f * (gain ** (f.ndim / 2))
    f = f.to(x.dtype)
    if not flip_filter:
        f = f.flip(list(range(f.ndim)))
    f = f[None, None].repeat([channels, 1] + [1] * f.ndim)
    if f.ndim == 4:
        x = F.conv2d(x, f, groups=channels)
    else:
        x = F.conv2d(x, f.unsqueeze(2), groups=channels)
        x = F.conv2d(x, f.unsqueeze(3), groups=channels)
    return x[:, :, ::downy, ::downx]


upfirdn2d._upfirdn2d_ref = upfirdn2d_fixed

with open(checkpoint, "rb") as f:
    G = legacy.load_network_pkl(f)["G_ema"].float().eval()
print(f"  {checkpoint}: {G.img_resolution}px, {G.mapping.num_ws} ws rows")


class Mapping(torch.nn.Module):
    def __init__(self, G):
        super().__init__()
        self.G = G

    def forward(self, z):
        return self.G.mapping(z, None, truncation_psi=1.0)


class Synthesis(torch.nn.Module):
    def __init__(self, G):
        super().__init__()
        self.G = G

    def forward(self, ws):
        #  fused_modconv=False: the fused version reshapes the weights for each
        #  sample, which ONNX can't follow. The result is the same.
        return self.G.synthesis(ws, noise_mode="const", force_fp32=True, fused_modconv=False)


z = torch.randn(1, G.z_dim, generator=torch.Generator().manual_seed(1))
with torch.no_grad():
    ws = Mapping(G)(z)

export(Mapping(G), (z,), ["z"], ["ws"], f"{prefix}-map.onnx")
export(Synthesis(G), (ws,), ["ws"], ["image"], f"{prefix}-synth.onnx")
verify(f"{prefix}-map.onnx", {"z": z}, Mapping(G)(z))
verify(f"{prefix}-synth.onnx", {"ws": ws}, Synthesis(G)(ws))