models/convert/e4e.py

Python source for Making faces, 56 lines. Raw file

"""encoder4editing (e4e): the FFHQ encoder to ONNX, and the average face.

    python e4e.py <e4e_ffhq_encode.pt> <repo> <output-folder>

e4e turns a photo of a face into the W+ latent that makes StyleGAN2 FFHQ draw
the same face: the app uses it to start from a real face. Only the encoder is
built here, with the authors' classes (the `repo` argument, encoder4editing).
Its StyleGAN2 code imports CUDA kernels, so those are swapped for cpu_ops.

    e4e-ffhq-encoder.onnx     image [1, 3, 256, 256] in -1..1  ->  wplus [1, 18, 512]
    stylegan2-ffhq-wavg.f32   the average face's W+, 18 x 512 float32s

The encoder predicts offsets from the average face, so the average is added
inside the ONNX network, and also saved on its own: the app pulls faces
towards it to keep them realistic.
"""

import os
import sys
from types import SimpleNamespace

import torch

import cpu_ops
from export import export, verify

checkpoint, repo, folder = sys.argv[1:4]
sys.path.insert(0, repo)
cpu_ops.install("models.stylegan2.op")
from models.encoders.psp_encoders import Encoder4Editing  # noqa: E402

state = torch.load(checkpoint, map_location="cpu", weights_only=False)
encoder = Encoder4Editing(50, "ir_se", SimpleNamespace(stylegan_size=1024)).eval()
encoder.load_state_dict(
    {key[len("encoder."):]: value for key, value in state["state_dict"].items() if key.startswith("encoder.")})
latent_avg = state["latent_avg"].float()


class Encoder(torch.nn.Module):
    def __init__(self, encoder, latent_avg):
        super().__init__()
        self.encoder = encoder
        self.register_buffer("latent_avg", latent_avg[None])

    def forward(self, image):
        return self.encoder(image) + self.latent_avg


model = Encoder(encoder, latent_avg).eval()
image = torch.rand(1, 3, 256, 256, generator=torch.Generator().manual_seed(1)) * 2 - 1
path = os.path.join(folder, "e4e-ffhq-encoder.onnx")
export(model, (image,), ["image"], ["wplus"], path)
verify(path, {"image": image}, model(image))

latent_avg.numpy().astype("<f4").tofile(os.path.join(folder, "stylegan2-ffhq-wavg.f32"))
print(f"  wrote stylegan2-ffhq-wavg.f32 ({latent_avg.numel()} floats)")