models/convert/preview.py
Python source for Making faces, 26 lines. Raw file
"""Renders one face from a converted generator, to check it by eye.
python preview.py <map.onnx> <synth.onnx> <output.png> [seed]
Uses the same input names the app does: z or var into the mapping network,
then its output into the synthesis network.
"""
import sys
import numpy as np
import onnxruntime as ort
from PIL import Image
map_path, synth_path, output = sys.argv[1:4]
seed = int(sys.argv[4]) if len(sys.argv) > 4 else 1
z = np.random.default_rng(seed).standard_normal((1, 512)).astype(np.float32)
mapping = ort.InferenceSession(map_path, providers=["CPUExecutionProvider"])
synthesis = ort.InferenceSession(synth_path, providers=["CPUExecutionProvider"])
styles = mapping.run(None, {mapping.get_inputs()[0].name: z})[0]
image = synthesis.run(None, {synthesis.get_inputs()[0].name: styles})[0][0]
pixels = ((np.clip(image, -1, 1) + 1) * 127.5).round().astype(np.uint8).transpose(1, 2, 0)
Image.fromarray(pixels).resize((256, 256), Image.LANCZOS).save(output)
print(f" {output}: {image.shape[2]}x{image.shape[1]}")