Improve lattice inference with source evidence
Add source-aware scoring and size hinting to lattice inference: introduce _hint_target_sizes_from_spacing, _top_candidates_by_size, _normalize_candidate_scores and _rerank_size_candidates_with_source_evidence in inference.py; use spacing-based hinted sizes when enumerating candidates and rerank candidates by combined base score and source-lattice evidence before selecting the best lattice. Add source_lattice_evidence_breakdown and foreground_adjacency_strength to metrics.py to compute per-candidate evidence (cell dispersion and adjacency strength). Update infer_lattice to return reranked top candidates. Update tests to cover spacing hints, size diversification, and phase/size reranking; adjust pipeline synthetic test parameters. Also update README wording and refresh/remove some readme image assets.
This commit is contained in:
parent
6b52613a27
commit
91fbbf1e7d
8 changed files with 249 additions and 12 deletions
|
|
@ -2,9 +2,13 @@ from __future__ import annotations
|
|||
|
||||
from repixelizer.inference import (
|
||||
_axis_prior_from_estimates,
|
||||
_candidate_dims,
|
||||
_combine_axis_priors,
|
||||
_hint_target_sizes_from_spacing,
|
||||
_top_candidates_by_size,
|
||||
infer_lattice,
|
||||
)
|
||||
from repixelizer.types import InferenceCandidate
|
||||
from repixelizer.synthetic import fake_pixelize, make_emblem
|
||||
|
||||
|
||||
|
|
@ -20,6 +24,33 @@ def test_combined_axis_prior_prefers_consistent_shared_cell_size() -> None:
|
|||
assert 0.2 < reliability < 0.6
|
||||
|
||||
|
||||
def test_spacing_hints_can_add_dense_candidates_around_true_size() -> None:
|
||||
hinted_sizes = _hint_target_sizes_from_spacing(
|
||||
1024,
|
||||
1024,
|
||||
(8.05, 0.74),
|
||||
(4.0, 0.08),
|
||||
)
|
||||
dims = _candidate_dims(1024, 1024, None, hinted_sizes=hinted_sizes)
|
||||
sizes = {width for width, _ in dims}
|
||||
assert 128 in sizes
|
||||
|
||||
|
||||
def test_top_candidates_are_diversified_by_size() -> None:
|
||||
candidates = [
|
||||
InferenceCandidate(target_width=113, target_height=113, phase_x=0.0, phase_y=0.0, score=0.90, breakdown={}),
|
||||
InferenceCandidate(target_width=113, target_height=113, phase_x=0.2, phase_y=0.0, score=0.89, breakdown={}),
|
||||
InferenceCandidate(target_width=117, target_height=117, phase_x=0.0, phase_y=0.0, score=0.88, breakdown={}),
|
||||
InferenceCandidate(target_width=128, target_height=128, phase_x=0.0, phase_y=0.0, score=0.87, breakdown={}),
|
||||
]
|
||||
selected = _top_candidates_by_size(candidates, limit=8)
|
||||
assert [(candidate.target_width, candidate.phase_x) for candidate in selected] == [
|
||||
(113, 0.0),
|
||||
(117, 0.0),
|
||||
(128, 0.0),
|
||||
]
|
||||
|
||||
|
||||
def test_infer_lattice_recovers_emblem_scale() -> None:
|
||||
source = make_emblem(32, 32)
|
||||
fake = fake_pixelize(
|
||||
|
|
|
|||
|
|
@ -32,7 +32,16 @@ def test_pipeline_writes_output_and_diagnostics(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)
|
||||
fake = fake_pixelize(
|
||||
source,
|
||||
upscale=12,
|
||||
phase_x=0.2,
|
||||
phase_y=0.35,
|
||||
blur_radius=0.65,
|
||||
warp_strength=0.28,
|
||||
warp_detail=6,
|
||||
seed=5,
|
||||
)
|
||||
input_path = tmp_path / "input.png"
|
||||
output_path = tmp_path / "output.png"
|
||||
|
||||
|
|
@ -111,3 +120,37 @@ def test_phase_rerank_can_override_low_confidence_inference_pick(monkeypatch) ->
|
|||
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
|
||||
|
||||
|
||||
def test_phase_rerank_can_override_to_better_size_candidate(monkeypatch) -> None:
|
||||
source = np.zeros((4, 4, 4), dtype=np.float32)
|
||||
candidate_a = InferenceCandidate(target_width=2, target_height=2, phase_x=0.0, phase_y=0.0, score=0.91, breakdown={})
|
||||
candidate_b = InferenceCandidate(target_width=3, target_height=3, phase_x=0.0, phase_y=0.0, score=0.88, breakdown={})
|
||||
inference = InferenceResult(
|
||||
target_width=2,
|
||||
target_height=2,
|
||||
phase_x=0.0,
|
||||
phase_y=0.0,
|
||||
confidence=0.0,
|
||||
top_candidates=[candidate_a, candidate_b],
|
||||
)
|
||||
|
||||
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):
|
||||
rgba = np.zeros((inference.target_height, inference.target_width, 4), dtype=np.float32)
|
||||
return DummyArtifacts(rgba)
|
||||
|
||||
def fake_support(source_rgba, output_rgba, *, target_width, target_height, phase_x, phase_y):
|
||||
if target_width == 2:
|
||||
return {"score": 0.10}
|
||||
return {"score": 0.01}
|
||||
|
||||
monkeypatch.setattr("repixelizer.pipeline.optimize_uv_field", fake_optimize_uv_field)
|
||||
monkeypatch.setattr("repixelizer.pipeline.source_lattice_consistency_breakdown", fake_support)
|
||||
|
||||
selected = _select_phase_candidate(source, inference, analysis=object(), seed=7, device="cpu")
|
||||
assert selected.target_width == candidate_b.target_width
|
||||
assert selected.target_height == candidate_b.target_height
|
||||
|
|
|
|||
Loading…
Reference in a new issue