Tests/FaceMakerTests/FaceMakerTests.swift

Swift source for Making faces, 168 lines. Raw file

import CoreImage
import Foundation
import Testing
@testable import FaceMaker

@Suite("FaceMaker")
struct FaceMakerTests {
    /// Repo ./models, where the ONNX weights are staged (same convention as
    /// FaceReplace's tests).
    static var modelsDirectory: URL {
        URL(fileURLWithPath: #filePath)
            .deletingLastPathComponent()  // FaceMakerTests
            .deletingLastPathComponent()  // Tests
            .deletingLastPathComponent()  // repo root
            .appendingPathComponent("models")
    }

    /// End-to-end nearest-neighbour inversion, fully offline: generate a face,
    /// then drop it back in — Vision must find the face, ArcFace must embed it,
    /// and the search must return a 512-d latent. Skips if models aren't staged.
    @Test("Drop-to-vector finds a nearest latent")
    func inversion() async throws {
        let directory = Self.modelsDirectory
        let fm = FileManager.default
        guard fm.fileExists(atPath: directory.appendingPathComponent("w600k_r50.onnx").path),
              fm.fileExists(atPath: directory.appendingPathComponent(
                "mobilestylegan_ffhq_v2-synth.onnx").path)
        else { return }  // models not staged — nothing to verify

        let model = try #require(LocalFaceModel.find("local/mobilestylegan-ffhq"))
        // A query well outside the small bank's seeds (1...target), so the
        // nearest match is loose and refinement has room to improve it.
        let png = try await FaceLocalEngine.shared.png(
            model: model, directory: directory,
            latent: FaceLocalEngine.gaussianLatent(seed: 987_654_321))
        let image = try #require(CIImage(data: png))

        let query = try #require(
            await FaceInversion.shared.embedDropped(image, directory: directory))
        let seed = try await FaceInversion.shared.nearest(
            to: query, model: model, directory: directory,
            target: 8, progress: { _, _ in })
        #expect(seed.count == 512)

        // Refinement should move the latent no further from — and in practice
        // much closer to — the (in-distribution) target identity.
        let seedSim = try #require(
            try await FaceInversion.shared.similarity(
                of: seed, to: query, model: model, directory: directory))
        let refined = try await FaceInversion.shared.refine(
            query: query, start: seed, model: model, directory: directory,
            iterations: 5, lambda: 4, progress: { _, _, _, _ in })
        let refinedSim = try #require(
            try await FaceInversion.shared.similarity(
                of: refined, to: query, model: model, directory: directory))
        #expect(refinedSim >= seedSim - 1e-4)
        print("inversion sim: seed \(seedSim) → refined \(refinedSim)")
    }

    /// Creating ORT sessions on several threads at once used to segfault inside
    /// the graph transformer (global op-schema registry isn't thread-safe).
    /// With the creation lock this must complete cleanly.
    @Test("Concurrent ORT session creation is safe")
    func concurrentSessions() async throws {
        let d = Self.modelsDirectory
        let files = [
            "stylegan2-ffhq-synth.onnx", "stylegan2-ffhq-map.onnx",
            "e4e-ffhq-encoder.onnx", "w600k_r50.onnx", "semantic-celeba-synth.onnx",
        ]
        let paths = files.map { d.appendingPathComponent($0).path }
            .filter { FileManager.default.fileExists(atPath: $0) }
        guard paths.count >= 3 else { return }

        await withTaskGroup(of: Bool.self) { group in
            for path in paths {
                group.addTask {
                    (try? ORTSession(modelPath: path, useCoreML: false)) != nil
                }
            }
            var ok = 0
            for await made in group where made { ok += 1 }
            #expect(ok == paths.count)  // reaching here without crashing is the point
        }
    }

    /// Encoder inversion end-to-end: encode a generated face to W+, reconstruct
    /// via the W+ engine path, and confirm the identity is preserved (high
    /// ArcFace cosine). Skips if the encoder isn't staged.
    @Test("Encoder inversion reconstructs identity")
    func encoderInversion() async throws {
        let directory = Self.modelsDirectory
        let fm = FileManager.default
        guard fm.fileExists(atPath: directory.appendingPathComponent("e4e-ffhq-encoder.onnx").path)
        else { return }

        let model = try #require(LocalFaceModel.find("local/stylegan2-ffhq"))
        let png = try await FaceLocalEngine.shared.png(
            model: model, directory: directory,
            latent: FaceLocalEngine.gaussianLatent(seed: 123))

        // Fresh CIImage per actor call so none is reused after being sent.
        let forEncode = try #require(CIImage(data: png))
        let wplus = try #require(
            await FaceInversion.shared.encodeWPlus(forEncode, directory: directory))
        #expect(wplus.count == 18 * 512)

        let recon = try await FaceLocalEngine.shared.png(
            model: model, directory: directory, wplus: wplus)

        let forArcOriginal = try #require(CIImage(data: png))
        let forArcRecon = try #require(CIImage(data: recon))
        let a = try #require(
            await FaceInversion.shared.embedDropped(forArcOriginal, directory: directory))
        let b = try #require(
            await FaceInversion.shared.embedDropped(forArcRecon, directory: directory))
        var sim: Float = 0
        for i in 0..<min(a.count, b.count) { sim += a[i] * b[i] }
        print("encoder inversion identity cosine:", sim)
        #expect(sim > 0.5)
    }

    /// Offline generator for the precomputed nearest-neighbour banks shipped in
    /// the bundle. Not part of the normal suite — run explicitly:
    ///   FACEMAKER_BUILD_BANKS=1 make test
    /// Writes models/bank-<id>.bin for every generator (build.sh bundles them).
    @Test("Generate bundled banks", .enabled(if:
        ProcessInfo.processInfo.environment["FACEMAKER_BUILD_BANKS"] == "1"))
    func generateBanks() async throws {
        let directory = Self.modelsDirectory
        let target = 200
        for model in LocalFaceModel.all {
            let out = directory.appendingPathComponent(
                "bank-\(model.id.replacingOccurrences(of: "/", with: "_")).bin")
            // Resumable: a complete bank file already on disk is left alone.
            if FileManager.default.fileExists(atPath: out.path) {
                print("[\(model.id)] already built — skipping")
                continue
            }
            let clock = ContinuousClock()
            let elapsed = try await clock.measure {
                try await FaceInversion.shared.exportBank(
                    model: model, directory: directory, target: target, to: out,
                    progress: { done, total in
                        if done % 25 == 0 || done == total {
                            print("[\(model.id)] \(done)/\(total)")
                        }
                    })
            }
            print("[\(model.id)] wrote \(out.lastPathComponent) in \(elapsed)")
            #expect(FileManager.default.fileExists(atPath: out.path))
        }
    }

    @Test("Model catalog loads and sorts by speed")
    func catalog() {
        let models = LocalFaceModel.all
        #expect(!models.isEmpty)
        #expect(LocalFaceModel.fastest.id == "local/mobilestylegan-ffhq")
        #expect(LocalFaceModel.find("local/stylegan2-ffhq") != nil)
    }

    @Test("Prompt brief formats non-neutral edits")
    func brief() {
        #expect(FacePrompt.localBrief([]) == "Face.")
        let brief = FacePrompt.localBrief([("Age", 2.0)])
        #expect(brief.contains("Age"))
    }
}