Add source structure metric breakdown

This commit is contained in:
Meta Vens 2026-04-23 23:31:58 +01:00
commit 044ee59948
7 changed files with 140 additions and 1 deletions

View file

@ -14,10 +14,12 @@ from .metrics import (
foreground_adjacency_error,
foreground_edge_concentration,
foreground_edge_position_error,
foreground_edge_support_breakdown,
foreground_motif_error,
foreground_reconstruction_error,
foreground_stroke_wobble_error,
reconstruction_error,
source_structure_breakdown,
)
from .palette import load_palette
from .pipeline import run_pipeline
@ -111,6 +113,8 @@ def run_compare(
):
preview = nearest_resize(image, width=source.shape[1], height=source.shape[0])
coherence = coherence_breakdown(image)
edge_support = foreground_edge_support_breakdown(preview, source)
structure = source_structure_breakdown(source, image)
rows.append(
{
"name": name,
@ -125,8 +129,12 @@ def run_compare(
"foreground_edge_concentration": foreground_edge_concentration(image),
"foreground_edge_position_error": foreground_edge_position_error(preview, source),
"foreground_stroke_wobble_error": foreground_stroke_wobble_error(preview, source),
"foreground_edge_precision": edge_support["precision"],
"foreground_edge_recall": edge_support["recall"],
"foreground_edge_f1": edge_support["f1"],
"foreground_adjacency_error": foreground_adjacency_error(preview, source),
"foreground_motif_error": foreground_motif_error(preview, source),
"source_structure_score": structure["score"],
}
)
write_compare_csv(diagnostics_path / "compare.csv", rows)

View file

@ -13,6 +13,7 @@ from .metrics import (
coherence_breakdown,
foreground_reconstruction_error,
reconstruction_error,
source_structure_breakdown,
source_lattice_consistency_breakdown,
)
from .types import InferenceResult, RunResult
@ -210,6 +211,7 @@ def summarize_run(result: RunResult) -> dict[str, Any]:
"source_preview_foreground_error": foreground_reconstruction_error(source_preview, result.source_rgba),
"output_colors_from_source_ratio": _source_color_ratio(result.source_rgba, result.output_rgba),
"source_fidelity": source_fidelity,
"source_structure": source_structure_breakdown(result.source_rgba, result.output_rgba),
"phase_rerank_candidates": rerank_candidates,
"loss_history": result.solver.loss_history,
"optimizer_displacement": displacement_metrics,

View file

@ -193,6 +193,37 @@ def foreground_edge_position_error(
return float(np.mean((deficit_a[support_mask] + deficit_b[support_mask]) * 0.5))
def foreground_edge_support_breakdown(
a: np.ndarray,
b: np.ndarray,
alpha_threshold: float = 0.05,
halo: int = 1,
radius: int = 1,
distance_penalty: float = 0.35,
edge_threshold: float = 0.02,
) -> dict[str, float]:
edge_a = _edge_strength_map(a)
edge_b = _edge_strength_map(b)
mask = sprite_mask(a, b, alpha_threshold=alpha_threshold, halo=halo)
output_edge_mask = mask & (edge_a >= edge_threshold)
source_edge_mask = mask & (edge_b >= edge_threshold)
support_from_source = _best_local_edge_support(edge_b, radius=radius, distance_penalty=distance_penalty)
support_from_output = _best_local_edge_support(edge_a, radius=radius, distance_penalty=distance_penalty)
precision = float(np.mean(np.clip(support_from_source[output_edge_mask], 0.0, 1.0))) if np.any(output_edge_mask) else 1.0
recall = float(np.mean(np.clip(support_from_output[source_edge_mask], 0.0, 1.0))) if np.any(source_edge_mask) else 1.0
if precision + recall <= 1e-6:
f1 = 0.0
else:
f1 = float(2.0 * precision * recall / (precision + recall))
return {
"precision": precision,
"recall": recall,
"f1": f1,
}
def foreground_edge_concentration(
rgba: np.ndarray,
alpha_threshold: float = 0.05,
@ -300,6 +331,46 @@ def source_lattice_evidence_breakdown(
}
def source_structure_breakdown(
source_rgba: np.ndarray,
output_rgba: np.ndarray,
*,
alpha_threshold: float = 0.05,
halo: int = 1,
) -> dict[str, float]:
if output_rgba.shape[:2] != source_rgba.shape[:2]:
from .io import nearest_resize
preview = nearest_resize(output_rgba, width=source_rgba.shape[1], height=source_rgba.shape[0])
else:
preview = output_rgba
fg_recon = foreground_reconstruction_error(preview, source_rgba, alpha_threshold=alpha_threshold, halo=halo)
edge_position = foreground_edge_position_error(preview, source_rgba, alpha_threshold=alpha_threshold, halo=halo)
stroke_wobble = foreground_stroke_wobble_error(preview, source_rgba, alpha_threshold=alpha_threshold, halo=halo)
edge_support = foreground_edge_support_breakdown(preview, source_rgba, alpha_threshold=alpha_threshold, halo=halo)
exact_ratio = foreground_exact_match_ratio(preview, source_rgba, alpha_threshold=alpha_threshold, halo=halo)
wobble_penalty = float(stroke_wobble / (1.0 + stroke_wobble))
score = (
fg_recon * 0.30
+ edge_position * 0.20
+ wobble_penalty * 0.15
+ (1.0 - edge_support["f1"]) * 0.20
+ (1.0 - exact_ratio) * 0.15
)
return {
"foreground_reconstruction_error": fg_recon,
"edge_position_error": edge_position,
"stroke_wobble_error": stroke_wobble,
"stroke_wobble_penalty": wobble_penalty,
"edge_precision": edge_support["precision"],
"edge_recall": edge_support["recall"],
"edge_f1": edge_support["f1"],
"exact_match_ratio": exact_ratio,
"score": score,
}
def exact_match_ratio(a: np.ndarray, b: np.ndarray) -> float:
a8 = np.clip(np.rint(premultiply(a) * 255.0), 0, 255).astype(np.uint8)
b8 = np.clip(np.rint(premultiply(b) * 255.0), 0, 255).astype(np.uint8)