Add corpus benchmarks and lattice refinement

This commit is contained in:
Meta Vens 2026-04-21 21:56:25 +01:00
commit d2e8ccac4d
23 changed files with 2995 additions and 217 deletions

86
tests/test_benchmark.py Normal file
View file

@ -0,0 +1,86 @@
from __future__ import annotations
import json
from pathlib import Path
from repixelizer.benchmark import run_roundtrip_benchmark
from repixelizer.io import save_rgba
from repixelizer.synthetic import make_emblem
def test_roundtrip_benchmark_writes_summary_and_case_outputs(tmp_path: Path) -> None:
corpus_dir = tmp_path / "corpus"
originals_dir = corpus_dir / "originals"
originals_dir.mkdir(parents=True)
save_rgba(originals_dir / "crest.png", make_emblem(16, 16))
(originals_dir / "crest.json").write_text(
json.dumps(
{
"title": "Crest",
"author": "Test Artist",
"license": "CC-BY 4.0",
"source_url": "https://example.com/crest",
}
),
encoding="utf-8",
)
out_dir = tmp_path / "benchmark-out"
payload = run_roundtrip_benchmark(corpus_dir, out_dir, variants=1, steps=4, seed=3)
assert payload["case_count"] == 1
assert payload["row_count"] == 2
assert payload["profiles"] == ["soft", "crisp"]
assert payload["primary_metric"] == "foreground_premultiplied_mae"
assert (out_dir / "benchmark.csv").exists()
assert (out_dir / "benchmark.json").exists()
assert (out_dir / "cases" / "crest" / "original.png").exists()
assert (out_dir / "cases" / "crest" / "profile-soft" / "variant-01" / "input.png").exists()
assert (out_dir / "cases" / "crest" / "profile-crisp" / "variant-01" / "optimized.png").exists()
assert "reference_foreground_coverage" in payload["rows"][0]
assert "optimized_canvas_error_to_original" in payload["rows"][0]
assert "optimized_adjacency_error_to_original" in payload["rows"][0]
assert "optimized_motif_error_to_original" in payload["rows"][0]
assert "profile" in payload["rows"][0]
assert "warp_sample_mode" in payload["rows"][0]
def test_roundtrip_benchmark_can_filter_cases(tmp_path: Path) -> None:
corpus_dir = tmp_path / "corpus"
originals_dir = corpus_dir / "originals"
originals_dir.mkdir(parents=True)
save_rgba(originals_dir / "crest-a.png", make_emblem(16, 16))
save_rgba(originals_dir / "crest-b.png", make_emblem(18, 18))
out_dir = tmp_path / "benchmark-out"
payload = run_roundtrip_benchmark(
corpus_dir,
out_dir,
variants=1,
profiles=["crisp"],
steps=2,
seed=3,
include_cases=["crest-b"],
)
assert payload["case_count"] == 1
assert payload["row_count"] == 1
assert payload["rows"][0]["case_id"] == "crest-b"
assert payload["rows"][0]["profile"] == "crisp"
def test_roundtrip_benchmark_clears_existing_output_dir_by_default(tmp_path: Path) -> None:
corpus_dir = tmp_path / "corpus"
originals_dir = corpus_dir / "originals"
originals_dir.mkdir(parents=True)
save_rgba(originals_dir / "crest.png", make_emblem(16, 16))
out_dir = tmp_path / "benchmark-out"
stale_file = out_dir / "stale.txt"
stale_file.parent.mkdir(parents=True)
stale_file.write_text("old", encoding="utf-8")
run_roundtrip_benchmark(corpus_dir, out_dir, variants=1, profiles=["soft"], steps=2, seed=3)
assert not stale_file.exists()
assert (out_dir / "benchmark.json").exists()

59
tests/test_corpus.py Normal file
View file

@ -0,0 +1,59 @@
from __future__ import annotations
import json
from pathlib import Path
import numpy as np
from repixelizer.corpus import prepare_corpus
from repixelizer.io import save_rgba
def test_prepare_corpus_extracts_frames_splits_sheets_and_archives_sources(tmp_path: Path) -> None:
corpus_dir = tmp_path / "corpus"
originals_dir = corpus_dir / "originals"
originals_dir.mkdir(parents=True)
mage = np.zeros((188, 340, 4), dtype=np.float32)
mage[..., 1] = 1.0
mage[20:60, 20:50] = np.array([1.0, 0.0, 0.0, 1.0], dtype=np.float32)
mage[110:160, 270:320] = np.array([0.0, 0.0, 1.0, 1.0], dtype=np.float32)
save_rgba(originals_dir / "mage-1-85x94.png", mage)
shadow = np.zeros((350, 320, 4), dtype=np.float32)
shadow[..., :3] = np.array([0.0, 1.0 / 255.0, 1.0 / 255.0], dtype=np.float32)
shadow[..., 3] = 1.0
shadow[290:340, 250:300] = np.array([1.0, 1.0, 1.0, 1.0], dtype=np.float32)
save_rgba(originals_dir / "shadow-80x70.png", shadow)
multi = np.ones((110, 317, 4), dtype=np.float32)
multi[..., 3] = 1.0
multi[8:102, 0:79] = np.array([1.0, 0.0, 0.0, 1.0], dtype=np.float32)
multi[0:103, 88:210] = np.array([0.0, 1.0, 0.0, 1.0], dtype=np.float32)
multi[0:110, 231:317] = np.array([0.0, 0.0, 1.0, 1.0], dtype=np.float32)
save_rgba(originals_dir / "threeformsPJ2.png", multi)
payload = prepare_corpus(corpus_dir)
assert payload["output_count"] == 5
assert (originals_dir / "mage-1.png").exists()
assert (originals_dir / "shadow.png").exists()
assert (originals_dir / "threeformspj2-01.png").exists()
assert (originals_dir / "threeformspj2-02.png").exists()
assert (originals_dir / "threeformspj2-03.png").exists()
assert (corpus_dir / "source-sheets" / "mage-1-85x94.png").exists()
assert (corpus_dir / "source-sheets" / "shadow-80x70.png").exists()
assert (corpus_dir / "source-sheets" / "threeformsPJ2.png").exists()
attribution_text = (corpus_dir / "ATTRIBUTION.md").read_text(encoding="utf-8")
mage_sidecar = json.loads((originals_dir / "mage-1.json").read_text(encoding="utf-8"))
shadow_sidecar = json.loads((originals_dir / "shadow.json").read_text(encoding="utf-8"))
boss_sidecar = json.loads((originals_dir / "threeformspj2-01.json").read_text(encoding="utf-8"))
assert mage_sidecar["source_title"] == "Bosses and monsters spritesheets (Ars Notoria)"
assert mage_sidecar["attribution_confidence"] == "inferred"
assert shadow_sidecar["licenses"] == ["CC-BY-SA 3.0"]
assert shadow_sidecar["original_source_title"] == "First Person Dungeon Crawl Enemies Remixed"
assert boss_sidecar["source_title"] == '3-form RPG boss: "Harlequin Epicycle."'
assert "## Bosses and monsters spritesheets (Ars Notoria)" in attribution_text
assert "`mage-1.png`" in attribution_text
assert "`shadow.png`" in attribution_text
assert "## 3-form RPG boss: \"Harlequin Epicycle.\"" in attribution_text

View file

@ -6,7 +6,16 @@ from repixelizer.synthetic import fake_pixelize, make_emblem
def test_infer_lattice_recovers_emblem_scale() -> None:
source = make_emblem(32, 32)
fake = fake_pixelize(source, upscale=12, phase_x=0.2, phase_y=0.35, blur_radius=0.6)
fake = fake_pixelize(
source,
upscale=12,
phase_x=0.2,
phase_y=0.35,
blur_radius=0.75,
warp_strength=0.28,
warp_detail=6,
seed=5,
)
result = infer_lattice(fake)
assert result.target_width in range(28, 37)
assert result.target_height in range(28, 37)

98
tests/test_metrics.py Normal file
View file

@ -0,0 +1,98 @@
from __future__ import annotations
import numpy as np
from repixelizer.metrics import (
exact_match_ratio,
foreground_adjacency_error,
foreground_exact_match_ratio,
foreground_motif_error,
foreground_reconstruction_error,
source_lattice_consistency_breakdown,
reconstruction_error,
)
def test_reconstruction_error_ignores_hidden_rgb_in_transparent_pixels() -> None:
transparent_a = np.zeros((4, 4, 4), dtype=np.float32)
transparent_b = transparent_a.copy()
transparent_b[..., 0] = 1.0
assert reconstruction_error(transparent_a, transparent_b) == 0.0
assert exact_match_ratio(transparent_a, transparent_b) == 1.0
def test_foreground_metrics_penalize_visible_sprite_errors_and_leaks() -> None:
original = np.zeros((4, 4, 4), dtype=np.float32)
original[1, 1] = np.asarray([1.0, 0.0, 0.0, 1.0], dtype=np.float32)
hidden_rgb = original.copy()
hidden_rgb[0, 0, :3] = np.asarray([0.2, 0.8, 0.4], dtype=np.float32)
assert foreground_reconstruction_error(original, hidden_rgb) == 0.0
assert foreground_exact_match_ratio(original, hidden_rgb) == 1.0
wrong_foreground = original.copy()
wrong_foreground[1, 1] = np.asarray([0.0, 1.0, 0.0, 1.0], dtype=np.float32)
assert foreground_reconstruction_error(original, wrong_foreground) > 0.0
assert foreground_exact_match_ratio(original, wrong_foreground) < 1.0
leaked_foreground = original.copy()
leaked_foreground[0, 0] = np.asarray([1.0, 1.0, 1.0, 1.0], dtype=np.float32)
assert foreground_reconstruction_error(original, leaked_foreground) > 0.0
def test_texture_metrics_track_adjacency_and_local_motifs() -> None:
original = np.zeros((4, 4, 4), dtype=np.float32)
original[1:3, 1:3] = np.asarray(
[
[[1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]],
[[0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 1.0]],
],
dtype=np.float32,
)
same_colors_wrong_structure = original.copy()
same_colors_wrong_structure[1:3, 1:3] = np.asarray(
[
[[1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 1.0]],
[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]],
],
dtype=np.float32,
)
assert foreground_adjacency_error(original, original) == 0.0
assert foreground_motif_error(original, original) == 0.0
assert foreground_adjacency_error(original, same_colors_wrong_structure) > 0.0
assert foreground_motif_error(original, same_colors_wrong_structure) > 0.0
def test_source_lattice_consistency_prefers_matching_cell_structure() -> None:
lowres = np.asarray(
[
[[1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]],
[[0.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 1.0]],
],
dtype=np.float32,
)
source = np.repeat(np.repeat(lowres, 4, axis=0), 4, axis=1)
good = lowres.copy()
bad = lowres[:, ::-1].copy()
good_score = source_lattice_consistency_breakdown(
source,
good,
target_width=2,
target_height=2,
phase_x=0.0,
phase_y=0.0,
)["score"]
bad_score = source_lattice_consistency_breakdown(
source,
bad,
target_width=2,
target_height=2,
phase_x=0.0,
phase_y=0.0,
)["score"]
assert good_score < bad_score

View file

@ -2,10 +2,14 @@ from __future__ import annotations
from pathlib import Path
import numpy as np
from repixelizer.baselines import naive_resize_baseline
from repixelizer.metrics import coherence_breakdown
from repixelizer.pipeline import run_pipeline
from repixelizer.metrics import coherence_breakdown, foreground_reconstruction_error
from repixelizer.pipeline import _select_phase_candidate, run_pipeline
from repixelizer.io import nearest_resize
from repixelizer.synthetic import fake_pixelize, make_emblem, make_sprite
from repixelizer.types import InferenceCandidate, InferenceResult
def test_pipeline_writes_output_and_diagnostics(tmp_path: Path) -> None:
@ -21,11 +25,12 @@ def test_pipeline_writes_output_and_diagnostics(tmp_path: Path) -> None:
result = run_pipeline(input_path, output_path, diagnostics_dir=diagnostics_dir, steps=24)
assert output_path.exists()
assert (diagnostics_dir / "run.json").exists()
assert (diagnostics_dir / "output-preview.png").exists()
assert result.output_rgba.shape[0] == result.inference.target_height
assert result.output_rgba.shape[1] == result.inference.target_width
def test_pipeline_beats_naive_on_coherence_for_synthetic_emblem(tmp_path: Path) -> None:
def test_pipeline_beats_naive_on_roundtrip_fidelity_for_synthetic_emblem(tmp_path: Path) -> None:
source = make_emblem(32, 32)
fake = fake_pixelize(source, upscale=12, phase_x=0.2, phase_y=0.35, blur_radius=0.65)
input_path = tmp_path / "input.png"
@ -36,9 +41,11 @@ def test_pipeline_beats_naive_on_coherence_for_synthetic_emblem(tmp_path: Path)
save_rgba(input_path, fake)
result = run_pipeline(input_path, output_path, steps=32)
naive = naive_resize_baseline(fake, width=result.inference.target_width, height=result.inference.target_height)
optimized_score = coherence_breakdown(result.output_rgba)["coherence_score"]
naive_score = coherence_breakdown(naive)["coherence_score"]
assert optimized_score >= naive_score
optimized_preview = nearest_resize(result.output_rgba, width=source.shape[1], height=source.shape[0])
naive_preview = nearest_resize(naive, width=source.shape[1], height=source.shape[0])
optimized_error = foreground_reconstruction_error(optimized_preview, source)
naive_error = foreground_reconstruction_error(naive_preview, source)
assert optimized_error <= naive_error
def test_pipeline_preserves_transparency_for_sprite(tmp_path: Path) -> None:
@ -54,3 +61,53 @@ def test_pipeline_preserves_transparency_for_sprite(tmp_path: Path) -> None:
alpha = result.output_rgba[..., 3]
assert alpha.min() < 0.05
assert alpha.max() > 0.9
partial = alpha[(alpha > 0.05) & (alpha < 0.95)]
assert partial.size == 0
def test_pipeline_can_emit_initialized_output_without_optimizer_steps(tmp_path: Path) -> None:
source = make_emblem(24, 24)
fake = fake_pixelize(source, upscale=10, phase_x=0.15, phase_y=0.2, blur_radius=0.6, warp_strength=0.2, warp_detail=5)
input_path = tmp_path / "input.png"
output_path = tmp_path / "output.png"
from repixelizer.io import save_rgba
save_rgba(input_path, fake)
result = run_pipeline(input_path, output_path, steps=0)
assert output_path.exists()
assert result.output_rgba.shape[0] == result.inference.target_height
assert result.output_rgba.shape[1] == result.inference.target_width
def test_phase_rerank_can_override_low_confidence_inference_pick(monkeypatch) -> None:
source = np.zeros((2, 2, 4), dtype=np.float32)
source[0, 0] = np.asarray([1.0, 0.0, 0.0, 1.0], dtype=np.float32)
candidate_a = InferenceCandidate(target_width=2, target_height=2, phase_x=-0.4, phase_y=0.2, score=0.91, breakdown={})
candidate_b = InferenceCandidate(target_width=2, target_height=2, phase_x=0.4, phase_y=0.2, score=0.909, breakdown={})
inference = InferenceResult(
target_width=2,
target_height=2,
phase_x=candidate_a.phase_x,
phase_y=candidate_a.phase_y,
confidence=0.0,
top_candidates=[candidate_a, candidate_b],
)
outputs = {
(candidate_a.phase_x, candidate_a.phase_y): np.ones_like(source),
(candidate_b.phase_x, candidate_b.phase_y): source.copy(),
}
class DummyArtifacts:
def __init__(self, rgba: np.ndarray) -> None:
self.target_rgba = rgba
def fake_optimize_uv_field(source_rgba, inference, analysis, steps, seed, device, solver_params=None):
assert steps == 0
return DummyArtifacts(outputs[(inference.phase_x, inference.phase_y)])
monkeypatch.setattr("repixelizer.pipeline.optimize_uv_field", fake_optimize_uv_field)
selected = _select_phase_candidate(source, inference, analysis=object(), seed=7, device="cpu")
assert selected.phase_x == candidate_b.phase_x
assert selected.phase_y == candidate_b.phase_y

32
tests/test_tuning.py Normal file
View file

@ -0,0 +1,32 @@
from __future__ import annotations
from pathlib import Path
from repixelizer.io import save_rgba
from repixelizer.synthetic import make_emblem
from repixelizer.tuning import tune_solver_hyperparams
def test_tuning_writes_results_and_best_benchmark(tmp_path: Path) -> None:
corpus_dir = tmp_path / "corpus"
originals_dir = corpus_dir / "originals"
originals_dir.mkdir(parents=True)
save_rgba(originals_dir / "crest.png", make_emblem(16, 16))
out_dir = tmp_path / "tuning-out"
payload = tune_solver_hyperparams(
corpus_dir,
out_dir,
trials=2,
variants=1,
profiles=["soft"],
steps=2,
seed=3,
device="cpu",
)
assert payload["trials_completed"] == 2
assert "best_params" in payload
assert (out_dir / "tuning-results.json").exists()
assert (out_dir / "best-benchmark" / "benchmark.json").exists()
assert not (out_dir / "_scratch").exists()