Add fixed lattice tile-graph controls
This commit is contained in:
parent
94af8d8560
commit
185cbdf714
11 changed files with 459 additions and 33 deletions
|
|
@ -27,3 +27,31 @@ def test_default_command_accepts_strip_background_flag(tmp_path: Path) -> None:
|
|||
exit_code = main([str(input_path), "--out", str(output_path), "--steps", "0", "--strip-background"])
|
||||
assert exit_code == 0
|
||||
assert output_path.exists()
|
||||
|
||||
|
||||
def test_default_command_accepts_fixed_lattice_flags(tmp_path: Path) -> None:
|
||||
source = make_emblem(16, 16)
|
||||
fake = fake_pixelize(source, upscale=8, phase_x=0.2, phase_y=0.25, blur_radius=0.4)
|
||||
input_path = tmp_path / "input.png"
|
||||
output_path = tmp_path / "output.png"
|
||||
save_rgba(input_path, fake)
|
||||
exit_code = main(
|
||||
[
|
||||
str(input_path),
|
||||
"--out",
|
||||
str(output_path),
|
||||
"--steps",
|
||||
"0",
|
||||
"--target-width",
|
||||
"16",
|
||||
"--target-height",
|
||||
"16",
|
||||
"--phase-x",
|
||||
"0.2",
|
||||
"--phase-y",
|
||||
"0.25",
|
||||
"--skip-phase-rerank",
|
||||
]
|
||||
)
|
||||
assert exit_code == 0
|
||||
assert output_path.exists()
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from repixelizer.inference import (
|
|||
_combine_axis_priors,
|
||||
_hint_target_sizes_from_spacing,
|
||||
_top_candidates_by_size,
|
||||
infer_fixed_lattice,
|
||||
infer_lattice,
|
||||
)
|
||||
from repixelizer.types import InferenceCandidate
|
||||
|
|
@ -67,3 +68,37 @@ def test_infer_lattice_recovers_emblem_scale() -> None:
|
|||
assert result.target_width in range(28, 37)
|
||||
assert result.target_height in range(28, 37)
|
||||
assert result.confidence >= 0.0
|
||||
|
||||
|
||||
def test_infer_fixed_lattice_honors_exact_size_and_phase() -> None:
|
||||
source = make_emblem(16, 16)
|
||||
fake = fake_pixelize(source, upscale=10, phase_x=0.2, phase_y=-0.2, blur_radius=0.4, seed=9)
|
||||
result = infer_fixed_lattice(
|
||||
fake,
|
||||
target_width=16,
|
||||
target_height=16,
|
||||
phase_x=0.2,
|
||||
phase_y=-0.2,
|
||||
device="cpu",
|
||||
)
|
||||
assert result.target_width == 16
|
||||
assert result.target_height == 16
|
||||
assert abs(result.phase_x - 0.2) <= 1e-6
|
||||
assert abs(result.phase_y + 0.2) <= 1e-6
|
||||
assert len(result.top_candidates) == 1
|
||||
|
||||
|
||||
def test_infer_fixed_lattice_searches_phase_within_pinned_size() -> None:
|
||||
source = make_emblem(16, 16)
|
||||
fake = fake_pixelize(source, upscale=8, phase_x=0.2, phase_y=0.25, blur_radius=0.45, seed=3)
|
||||
result = infer_fixed_lattice(
|
||||
fake,
|
||||
target_width=16,
|
||||
target_height=16,
|
||||
device="cpu",
|
||||
)
|
||||
assert result.target_width == 16
|
||||
assert result.target_height == 16
|
||||
assert len(result.top_candidates) > 1
|
||||
assert all(candidate.target_width == 16 for candidate in result.top_candidates)
|
||||
assert all(candidate.target_height == 16 for candidate in result.top_candidates)
|
||||
|
|
|
|||
|
|
@ -322,6 +322,35 @@ def test_pipeline_hybrid_mode_writes_reconstruction_diagnostics(tmp_path: Path)
|
|||
assert result.diagnostics["reconstruction"]["mode"] == "hybrid"
|
||||
|
||||
|
||||
def test_select_phase_candidate_can_skip_phase_rerank(monkeypatch) -> None:
|
||||
source = np.zeros((8, 8, 4), dtype=np.float32)
|
||||
candidate_a = InferenceCandidate(target_width=8, target_height=8, phase_x=0.0, phase_y=0.0, score=0.9, breakdown={})
|
||||
candidate_b = InferenceCandidate(target_width=10, target_height=10, phase_x=0.2, phase_y=0.2, score=0.89, breakdown={})
|
||||
inference = InferenceResult(
|
||||
target_width=8,
|
||||
target_height=8,
|
||||
phase_x=0.0,
|
||||
phase_y=0.0,
|
||||
confidence=0.0,
|
||||
top_candidates=[candidate_a, candidate_b],
|
||||
)
|
||||
|
||||
def fail(*args, **kwargs):
|
||||
raise AssertionError("phase rerank probe should not run when disabled")
|
||||
|
||||
monkeypatch.setattr("repixelizer.pipeline.optimize_uv_field", fail)
|
||||
selected = _select_phase_candidate(
|
||||
source,
|
||||
inference,
|
||||
analysis=object(),
|
||||
seed=7,
|
||||
device="cpu",
|
||||
enable_phase_rerank=False,
|
||||
)
|
||||
assert selected.target_width == candidate_a.target_width
|
||||
assert selected.target_height == candidate_a.target_height
|
||||
|
||||
|
||||
def test_pipeline_reuses_phase_probe_reconstruction_for_tile_graph(tmp_path: Path, monkeypatch) -> None:
|
||||
source = np.zeros((6, 6, 4), dtype=np.float32)
|
||||
source[..., 3] = 1.0
|
||||
|
|
@ -401,6 +430,69 @@ def test_pipeline_reuses_phase_probe_reconstruction_for_tile_graph(tmp_path: Pat
|
|||
assert result.output_rgba.shape[:2] == (3, 3)
|
||||
|
||||
|
||||
def test_pipeline_fixed_target_and_phase_skip_search_inference(tmp_path: Path, monkeypatch) -> None:
|
||||
source = np.zeros((6, 6, 4), dtype=np.float32)
|
||||
source[..., 3] = 1.0
|
||||
input_path = tmp_path / "input.png"
|
||||
output_path = tmp_path / "output.png"
|
||||
|
||||
from repixelizer.io import save_rgba
|
||||
|
||||
save_rgba(input_path, source)
|
||||
|
||||
inference = InferenceResult(
|
||||
target_width=3,
|
||||
target_height=3,
|
||||
phase_x=0.2,
|
||||
phase_y=-0.2,
|
||||
confidence=1.0,
|
||||
top_candidates=[],
|
||||
)
|
||||
|
||||
class DummyAnalysis:
|
||||
cluster_preview = np.zeros((6, 6, 4), dtype=np.float32)
|
||||
|
||||
def fail_infer(*args, **kwargs):
|
||||
raise AssertionError("full lattice search should not run for fixed target/phase")
|
||||
|
||||
def fake_infer_fixed(*args, **kwargs):
|
||||
return inference
|
||||
|
||||
def fake_optimize_uv_field(source_rgba, inference, analysis, steps, seed, device, solver_params=None):
|
||||
rgba = np.zeros((inference.target_height, inference.target_width, 4), dtype=np.float32)
|
||||
rgba[..., 3] = 1.0
|
||||
return SolverArtifacts(
|
||||
target_rgba=rgba,
|
||||
uv_field=np.zeros((inference.target_height, inference.target_width, 2), dtype=np.float32),
|
||||
guidance_strength=np.zeros((inference.target_height, inference.target_width), dtype=np.float32),
|
||||
initial_rgba=rgba.copy(),
|
||||
loss_history=[],
|
||||
)
|
||||
|
||||
monkeypatch.setattr("repixelizer.pipeline.infer_lattice", fail_infer)
|
||||
monkeypatch.setattr("repixelizer.pipeline.infer_fixed_lattice", fake_infer_fixed)
|
||||
monkeypatch.setattr("repixelizer.pipeline.analyze_source", lambda *args, **kwargs: DummyAnalysis())
|
||||
monkeypatch.setattr("repixelizer.pipeline.optimize_uv_field", fake_optimize_uv_field)
|
||||
|
||||
result = run_pipeline(
|
||||
input_path,
|
||||
output_path,
|
||||
target_width=3,
|
||||
target_height=3,
|
||||
phase_x=0.2,
|
||||
phase_y=-0.2,
|
||||
enable_phase_rerank=False,
|
||||
steps=0,
|
||||
device="cpu",
|
||||
)
|
||||
|
||||
assert output_path.exists()
|
||||
assert result.inference.target_width == 3
|
||||
assert result.inference.target_height == 3
|
||||
assert result.inference.phase_x == 0.2
|
||||
assert result.inference.phase_y == -0.2
|
||||
|
||||
|
||||
def test_phase_rerank_can_accept_low_confidence_size_jump_with_strong_support(monkeypatch) -> None:
|
||||
source = np.zeros((16, 16, 4), dtype=np.float32)
|
||||
candidate_a = InferenceCandidate(target_width=16, target_height=16, phase_x=0.0, phase_y=0.0, score=0.91, breakdown={})
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from repixelizer.metrics import source_lattice_consistency_breakdown
|
|||
from repixelizer.params import SolverHyperParams
|
||||
from repixelizer.pipeline import run_pipeline
|
||||
from repixelizer.synthetic import fake_pixelize, make_emblem
|
||||
from repixelizer.tile_graph import build_tile_graph_model, optimize_tile_graph
|
||||
from repixelizer.tile_graph import build_tile_graph_model, clear_tile_graph_model_cache, optimize_tile_graph
|
||||
from repixelizer.types import InferenceResult
|
||||
|
||||
|
||||
|
|
@ -361,6 +361,38 @@ def test_tile_graph_cuda_matches_cpu_on_small_case() -> None:
|
|||
assert np.allclose(cpu_artifacts.target_rgba, cuda_artifacts.target_rgba, atol=1e-5)
|
||||
|
||||
|
||||
def test_tile_graph_model_cache_reuses_fixed_lattice_build() -> None:
|
||||
clear_tile_graph_model_cache()
|
||||
source = make_emblem(16, 16)
|
||||
fake = fake_pixelize(source, upscale=8, phase_x=0.1, phase_y=-0.05, blur_radius=0.4, warp_strength=0.15, warp_detail=4)
|
||||
inference = InferenceResult(
|
||||
target_width=16,
|
||||
target_height=16,
|
||||
phase_x=0.1,
|
||||
phase_y=-0.05,
|
||||
confidence=1.0,
|
||||
top_candidates=[],
|
||||
)
|
||||
analysis = analyze_source(fake, seed=7, device="cpu")
|
||||
|
||||
first = build_tile_graph_model(
|
||||
fake,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
device="cpu",
|
||||
)
|
||||
second = build_tile_graph_model(
|
||||
fake,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
device="cpu",
|
||||
)
|
||||
|
||||
assert first.cache_hit is False
|
||||
assert second.cache_hit is True
|
||||
assert np.array_equal(first.candidate_rgba, second.candidate_rgba)
|
||||
|
||||
|
||||
def test_pipeline_tile_graph_mode_writes_reconstruction_diagnostics(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)
|
||||
|
|
@ -385,6 +417,7 @@ def test_pipeline_tile_graph_mode_writes_reconstruction_diagnostics(tmp_path: Pa
|
|||
assert run_json["settings"]["reconstruction_mode"] == "tile-graph"
|
||||
assert run_json["reconstruction"]["mode"] == "tile-graph"
|
||||
assert run_json["reconstruction"]["tile_graph_model_device"] == "cpu"
|
||||
assert "tile_graph_model_cache_hit" in run_json["reconstruction"]
|
||||
assert run_json["reconstruction"]["tile_graph_candidate_count"] > 0
|
||||
assert set(run_json["source_fidelity"].keys()) == {"snap_initial", "solver_target", "final_output"}
|
||||
assert result.diagnostics["reconstruction"]["mode"] == "tile-graph"
|
||||
|
|
|
|||
Loading…
Reference in a new issue