adjacency-first fidelity pass

This commit is contained in:
Meta Vens 2026-04-22 14:11:16 +01:00
commit a61a160d09
13 changed files with 798 additions and 135 deletions

View file

@ -4,6 +4,7 @@ import numpy as np
from .io import premultiply, unpremultiply
from .params import SolverHyperParams
from .source_reference import build_source_lattice_reference
from .types import InferenceResult, SolverArtifacts, SourceAnalysis
@ -315,6 +316,64 @@ def _blend_reference_delta(primary_delta, secondary_delta, secondary_weight: flo
return primary_delta * primary_weight + secondary_delta * secondary_weight
def _build_source_reliability(reference) -> np.ndarray:
baseline_dispersion = max(reference.dispersion, 1e-4)
dispersion_scale = reference.cell_dispersion / baseline_dispersion
dispersion_confidence = np.exp(-np.maximum(0.0, dispersion_scale - 1.0))
support_confidence = np.clip(reference.cell_support, 0.0, 1.0)
alpha_confidence = np.clip(np.maximum(reference.cell_alpha_max, reference.sharp_rgba[..., 3]), 0.0, 1.0)
reliability = np.clip(0.2 + support_confidence * 0.5 + alpha_confidence * 0.3, 0.0, 1.0)
return (reliability * dispersion_confidence).astype(np.float32)
def _reference_match_energy(
representative_energy,
source_energy,
source_reliability,
*,
representative_weight: float,
source_weight: float,
):
source_mix = source_reliability[..., None] * source_weight
representative_mix = representative_weight + (1.0 - source_reliability[..., None]) * source_weight
normalization = (source_mix + representative_mix).clamp_min(1e-4)
return (representative_energy * representative_mix + source_energy * source_mix) / normalization
def _edge_reliability(source_reliability, axis: str):
if axis == "x":
return (source_reliability[:, 1:] + source_reliability[:, :-1]) * 0.5
if axis == "y":
return (source_reliability[1:, :] + source_reliability[:-1, :]) * 0.5
if axis == "diag":
return (source_reliability[1:, 1:] + source_reliability[:-1, :-1]) * 0.5
if axis == "anti":
return (source_reliability[1:, :-1] + source_reliability[:-1, 1:]) * 0.5
raise ValueError(f"Unsupported edge axis: {axis}")
def _blend_reference_delta_map(
representative_delta,
source_delta,
source_reliability,
*,
axis: str,
representative_weight: float,
source_weight: float,
):
if representative_delta is None:
return source_delta
if source_delta is None:
return representative_delta
edge_reliability = _edge_reliability(source_reliability, axis=axis)
source_mix = edge_reliability * source_weight
representative_mix = representative_weight + (1.0 - edge_reliability) * source_weight
normalization = (source_mix + representative_mix).clamp_min(1e-4)
return (
representative_delta * representative_mix[..., None] + source_delta * source_mix[..., None]
) / normalization[..., None]
def _motif_candidate_energy(torch, candidate_colors, context_colors, anchor):
if candidate_colors.shape[0] < 2 or candidate_colors.shape[1] < 2:
return candidate_colors.new_zeros(candidate_colors.shape[:3])
@ -686,6 +745,7 @@ def _snap_output_to_source_pixels(
uv_t,
representative_t,
source_reference_t,
source_reliability_t,
solver_params: SolverHyperParams,
*,
cell_x: float,
@ -697,6 +757,7 @@ def _snap_output_to_source_pixels(
uv = uv_t[0]
representative = representative_t[0]
source_reference = source_reference_t[0]
source_reliability = source_reliability_t[0]
output_height = representative.shape[0]
output_width = representative.shape[1]
@ -713,8 +774,12 @@ def _snap_output_to_source_pixels(
representative_match = (candidate_colors - representative[..., None, :]).abs().mean(dim=-1)
source_match = (candidate_colors - source_reference[..., None, :]).abs().mean(dim=-1)
base_energy = (
representative_match * 0.45 + source_match * 0.55
base_energy = _reference_match_energy(
representative_match,
source_match,
source_reliability,
representative_weight=solver_params.snap_representative_match_weight,
source_weight=solver_params.snap_source_match_weight,
) * solver_params.snap_base_match_weight
representative_delta_x = representative[:, 1:, :] - representative[:, :-1, :] if output_width > 1 else None
representative_delta_y = representative[1:, :, :] - representative[:-1, :, :] if output_height > 1 else None
@ -725,12 +790,41 @@ def _snap_output_to_source_pixels(
representative[1:, :-1, :] - representative[:-1, 1:, :] if output_height > 1 and output_width > 1 else None
)
source_delta_x, source_delta_y, source_delta_diag, source_delta_anti = _reference_deltas(source_reference)
desired_delta_x = _blend_reference_delta(representative_delta_x, source_delta_x, 0.65)
desired_delta_y = _blend_reference_delta(representative_delta_y, source_delta_y, 0.65)
desired_delta_diag = _blend_reference_delta(representative_delta_diag, source_delta_diag, 0.65)
desired_delta_anti = _blend_reference_delta(representative_delta_anti, source_delta_anti, 0.65)
desired_delta_x = _blend_reference_delta_map(
representative_delta_x,
source_delta_x,
source_reliability,
axis="x",
representative_weight=solver_params.snap_representative_delta_weight,
source_weight=solver_params.snap_source_delta_weight,
)
desired_delta_y = _blend_reference_delta_map(
representative_delta_y,
source_delta_y,
source_reliability,
axis="y",
representative_weight=solver_params.snap_representative_delta_weight,
source_weight=solver_params.snap_source_delta_weight,
)
desired_delta_diag = _blend_reference_delta_map(
representative_delta_diag,
source_delta_diag,
source_reliability,
axis="diag",
representative_weight=solver_params.snap_representative_delta_weight,
source_weight=solver_params.snap_source_delta_weight,
)
desired_delta_anti = _blend_reference_delta_map(
representative_delta_anti,
source_delta_anti,
source_reliability,
axis="anti",
representative_weight=solver_params.snap_representative_delta_weight,
source_weight=solver_params.snap_source_delta_weight,
)
selected = torch.argmin(base_energy, dim=-1)
ranking_energy = base_energy
for _ in range(4):
selected_colors = torch.gather(
candidate_colors,
@ -792,13 +886,14 @@ def _snap_output_to_source_pixels(
anti_desired[1:, :-1, :] = desired_delta_anti
anti_error = ((candidate_colors - anti_selected[..., None, :]) - anti_desired[..., None, :]).abs().mean(dim=-1)
energy = energy + anti_error * anti_mask * solver_params.snap_diagonal_weight
ranking_energy = energy
selected = torch.argmin(energy, dim=-1)
hardened_selected, use_opaque, use_transparent = _harden_binary_alpha_selection(
torch,
candidate_colors,
selected,
base_energy,
ranking_energy,
representative[..., 3],
solver_params=solver_params,
)
@ -909,6 +1004,7 @@ def _discrete_refine_output(
uv_t,
representative_t,
source_reference_t,
source_reliability_t,
anchor_t,
source_delta_x_t,
source_delta_y_t,
@ -926,6 +1022,7 @@ def _discrete_refine_output(
uv = uv_t[0]
representative = representative_t[0]
source_reference = source_reference_t[0]
source_reliability = source_reliability_t[0]
anchor = anchor_t[0].permute(1, 2, 0)
output_height = representative.shape[0]
output_width = representative.shape[1]
@ -950,15 +1047,22 @@ def _discrete_refine_output(
source_reference_energy = (candidate_colors - source_reference[..., None, :]).abs().mean(dim=-1)
alpha_energy = (candidate_colors[..., 3] - anchor[..., None, 3]).abs()
distance_energy = ((offset_x / max(cell_x, 1e-4)) ** 2 + (offset_y / max(cell_y, 1e-4)) ** 2).reshape(1, 1, -1)
match_energy = _reference_match_energy(
rep_energy,
source_reference_energy,
source_reliability,
representative_weight=solver_params.refine_representative_match_weight,
source_weight=solver_params.refine_source_match_weight,
)
base_energy = (
anchor_energy * solver_params.refine_anchor_weight
+ (rep_energy * 0.45 + source_reference_energy * 0.55) * solver_params.refine_representative_weight
+ match_energy * solver_params.refine_representative_weight
+ alpha_energy * solver_params.refine_alpha_weight
+ distance_energy * solver_params.refine_distance_weight
)
relax_base_energy = (
anchor_energy * solver_params.refine_anchor_weight * solver_params.relax_anchor_scale
+ (rep_energy * 0.45 + source_reference_energy * 0.55) * solver_params.refine_representative_weight
+ match_energy * solver_params.refine_representative_weight
+ alpha_energy * solver_params.refine_alpha_weight
+ distance_energy * solver_params.refine_distance_weight
)
@ -1002,9 +1106,15 @@ def _discrete_refine_output(
solver_params,
iterations=relax_iterations,
)
relaxed_mode_colors = _select_colors(candidate_colors, relaxed_mode_selected)
refine_base_energy = base_energy + (
(candidate_colors - relaxed_context[..., None, :]).abs().mean(dim=-1) * solver_params.relax_handoff_weight
)
if solver_params.refine_relaxed_mode_weight > 0.0:
refine_base_energy = refine_base_energy + (
(candidate_colors - relaxed_mode_colors[..., None, :]).abs().mean(dim=-1)
* solver_params.refine_relaxed_mode_weight
)
start_candidates = [selected, relaxed_mode_selected]
selected = selected.clone()
best_selected = selected.clone()
@ -1012,6 +1122,44 @@ def _discrete_refine_output(
loss_history: list[float] = list(relax_history)
anchor_hw = anchor_t.permute(0, 2, 3, 1)
def candidate_energy(context_colors):
energy = refine_base_energy.clone()
energy = energy + _pairwise_candidate_energy(
candidate_colors,
context_colors,
desired_delta_x,
desired_delta_y,
desired_delta_diag,
desired_delta_anti,
orthogonal_weight=solver_params.refine_orthogonal_weight,
diagonal_weight=solver_params.refine_diagonal_weight,
)
if solver_params.refine_motif_weight > 0.0:
motif_energy = _motif_candidate_energy(torch, candidate_colors, context_colors, anchor)
energy = energy + motif_energy * solver_params.refine_motif_weight
if solver_params.structure_source_motif_weight > 0.0:
source_motif_energy = _motif_candidate_energy(torch, candidate_colors, context_colors, source_reference)
energy = energy + source_motif_energy * solver_params.structure_source_motif_weight
if solver_params.refine_line_weight > 0.0:
line_energy = _line_candidate_energy(torch, candidate_colors, context_colors, anchor)
energy = energy + line_energy * solver_params.refine_line_weight
if solver_params.structure_source_line_weight > 0.0:
source_line_energy = _line_candidate_energy(torch, candidate_colors, context_colors, source_reference)
energy = energy + source_line_energy * solver_params.structure_source_line_weight
if solver_params.structure_source_adjacency_weight > 0.0:
source_adjacency_energy = _pairwise_candidate_energy(
candidate_colors,
context_colors,
source_ref_dx,
source_ref_dy,
source_ref_diag,
source_ref_anti,
orthogonal_weight=solver_params.refine_orthogonal_weight,
diagonal_weight=solver_params.refine_diagonal_weight,
)
energy = energy + source_adjacency_energy * solver_params.structure_source_adjacency_weight
return energy
for start_selected in start_candidates:
start_colors = _select_colors(candidate_colors, start_selected)
start_score = float(
@ -1034,41 +1182,7 @@ def _discrete_refine_output(
for step in range(passes + 1):
selected_colors = _select_colors(candidate_colors, selected)
energy = refine_base_energy.clone()
energy = energy + _pairwise_candidate_energy(
candidate_colors,
selected_colors,
desired_delta_x,
desired_delta_y,
desired_delta_diag,
desired_delta_anti,
orthogonal_weight=solver_params.refine_orthogonal_weight,
diagonal_weight=solver_params.refine_diagonal_weight,
)
if solver_params.refine_motif_weight > 0.0:
motif_energy = _motif_candidate_energy(torch, candidate_colors, selected_colors, anchor)
energy = energy + motif_energy * solver_params.refine_motif_weight
if solver_params.structure_source_motif_weight > 0.0:
source_motif_energy = _motif_candidate_energy(torch, candidate_colors, selected_colors, source_reference)
energy = energy + source_motif_energy * solver_params.structure_source_motif_weight
if solver_params.refine_line_weight > 0.0:
line_energy = _line_candidate_energy(torch, candidate_colors, selected_colors, anchor)
energy = energy + line_energy * solver_params.refine_line_weight
if solver_params.structure_source_line_weight > 0.0:
source_line_energy = _line_candidate_energy(torch, candidate_colors, selected_colors, source_reference)
energy = energy + source_line_energy * solver_params.structure_source_line_weight
if solver_params.structure_source_adjacency_weight > 0.0:
source_adjacency_energy = _pairwise_candidate_energy(
candidate_colors,
selected_colors,
source_ref_dx,
source_ref_dy,
source_ref_diag,
source_ref_anti,
orthogonal_weight=solver_params.refine_orthogonal_weight,
diagonal_weight=solver_params.refine_diagonal_weight,
)
energy = energy + source_adjacency_energy * solver_params.structure_source_adjacency_weight
energy = candidate_energy(selected_colors)
candidate_t = selected_colors[None, ...]
score = float(
@ -1097,11 +1211,35 @@ def _discrete_refine_output(
break
selected = next_selected
final_candidates = [best_selected, relaxed_mode_selected]
final_selected = best_selected
final_score = best_score
for candidate_selected in final_candidates:
candidate_colors_selected = _select_colors(candidate_colors, candidate_selected)
candidate_score = float(
_structure_score(
torch,
F,
source_t,
uv_t,
candidate_colors_selected[None, ...],
representative_t,
source_reference_t,
anchor_hw,
solver_params,
).detach().cpu().item()
)
if candidate_score < final_score:
final_score = candidate_score
final_selected = candidate_selected.clone()
final_selected_colors = _select_colors(candidate_colors, final_selected)
final_ranking_energy = candidate_energy(final_selected_colors)
hardened_selected, use_opaque, use_transparent = _harden_binary_alpha_selection(
torch,
candidate_colors,
best_selected,
refine_base_energy,
final_selected,
final_ranking_energy,
representative[..., 3],
solver_params=solver_params,
)
@ -1144,20 +1282,52 @@ def optimize_uv_field(
_make_patch_offsets(height=height, width=width, target_height=inference.target_height, target_width=inference.target_width)
).to(device=device, dtype=torch.float32)
guide_small = F.interpolate(edge_t, size=(inference.target_height, inference.target_width), mode="bilinear", align_corners=True)
source_lattice_reference = build_source_lattice_reference(
rgba,
target_width=inference.target_width,
target_height=inference.target_height,
phase_x=inference.phase_x,
phase_y=inference.phase_y,
alpha_threshold=solver_params.alpha_transparent_threshold,
)
initial_patches = _sample_cell_patches(F, source_t, uv0_t, offsets_t)
initial_representative_t, _ = _representative_colors(initial_patches, solver_params)
initial_representative_t = initial_representative_t.detach()
initial_source_reference_t = _exemplar_colors(initial_patches).detach()
source_delta_x_t = _source_boundary_deltas(F, source_t, uv0_t, axis="x")
source_delta_y_t = _source_boundary_deltas(F, source_t, uv0_t, axis="y")
source_delta_diag_t = _source_boundary_deltas(F, source_t, uv0_t, axis="diag")
source_delta_anti_t = _source_boundary_deltas(F, source_t, uv0_t, axis="anti")
initial_source_reference_t = torch.from_numpy(premultiply(source_lattice_reference.sharp_rgba)[None, ...]).to(
device=device,
dtype=torch.float32,
)
source_reliability_t = torch.from_numpy(_build_source_reliability(source_lattice_reference)[None, ...]).to(
device=device,
dtype=torch.float32,
)
source_delta_x_t = (
torch.from_numpy(source_lattice_reference.delta_x[None, ...]).to(device=device, dtype=torch.float32)
if source_lattice_reference.delta_x is not None
else None
)
source_delta_y_t = (
torch.from_numpy(source_lattice_reference.delta_y[None, ...]).to(device=device, dtype=torch.float32)
if source_lattice_reference.delta_y is not None
else None
)
source_delta_diag_t = (
torch.from_numpy(source_lattice_reference.delta_diag[None, ...]).to(device=device, dtype=torch.float32)
if source_lattice_reference.delta_diag is not None
else None
)
source_delta_anti_t = (
torch.from_numpy(source_lattice_reference.delta_anti[None, ...]).to(device=device, dtype=torch.float32)
if source_lattice_reference.delta_anti is not None
else None
)
snap_rgba = _snap_output_to_source_pixels(
torch,
source_t,
uv0_t,
initial_representative_t,
initial_source_reference_t,
source_reliability_t,
solver_params,
cell_x=cell_x,
cell_y=cell_y,
@ -1174,6 +1344,7 @@ def optimize_uv_field(
uv0_t,
initial_representative_t,
initial_source_reference_t,
source_reliability_t,
snap_t,
source_delta_x_t,
source_delta_y_t,

View file

@ -9,7 +9,12 @@ import numpy as np
from PIL import Image, ImageDraw
from .io import nearest_resize, save_rgba
from .metrics import coherence_breakdown, foreground_reconstruction_error, reconstruction_error
from .metrics import (
coherence_breakdown,
foreground_reconstruction_error,
reconstruction_error,
source_lattice_consistency_breakdown,
)
from .types import InferenceResult, RunResult
@ -105,6 +110,48 @@ def summarize_run(result: RunResult) -> dict[str, Any]:
width=result.source_rgba.shape[1],
height=result.source_rgba.shape[0],
)
source_fidelity = {
"snap_initial": source_lattice_consistency_breakdown(
result.source_rgba,
result.solver.initial_rgba,
target_width=result.inference.target_width,
target_height=result.inference.target_height,
phase_x=result.inference.phase_x,
phase_y=result.inference.phase_y,
),
"solver_target": source_lattice_consistency_breakdown(
result.source_rgba,
result.solver.target_rgba,
target_width=result.inference.target_width,
target_height=result.inference.target_height,
phase_x=result.inference.phase_x,
phase_y=result.inference.phase_y,
),
"final_output": source_lattice_consistency_breakdown(
result.source_rgba,
result.output_rgba,
target_width=result.inference.target_width,
target_height=result.inference.target_height,
phase_x=result.inference.phase_x,
phase_y=result.inference.phase_y,
),
}
rerank_candidates = [
{
"target_width": candidate.target_width,
"target_height": candidate.target_height,
"phase_x": candidate.phase_x,
"phase_y": candidate.phase_y,
"score": candidate.score,
"phase_rerank_score": candidate.breakdown.get("phase_rerank_score"),
"phase_rerank_rank": candidate.breakdown.get("phase_rerank_rank"),
"phase_rerank_support_score": candidate.breakdown.get("phase_rerank_support_score"),
"phase_rerank_size_delta_ratio": candidate.breakdown.get("phase_rerank_size_delta_ratio"),
"phase_rerank_size_penalty": candidate.breakdown.get("phase_rerank_size_penalty"),
}
for candidate in result.inference.top_candidates
if "phase_rerank_score" in candidate.breakdown
]
return {
"target_width": result.inference.target_width,
"target_height": result.inference.target_height,
@ -115,6 +162,8 @@ def summarize_run(result: RunResult) -> dict[str, Any]:
"source_preview_reconstruction_error": reconstruction_error(source_preview, result.source_rgba),
"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,
"phase_rerank_candidates": rerank_candidates,
"loss_history": result.solver.loss_history,
}

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import numpy as np
from .io import premultiply
from .source_reference import build_source_lattice_reference
def luminance(rgba: np.ndarray) -> np.ndarray:
@ -247,7 +248,7 @@ def source_lattice_consistency_breakdown(
phase_y: float,
alpha_threshold: float = 0.05,
) -> dict[str, float]:
lattice_source, dispersion = lattice_source_rgba(
reference = build_source_lattice_reference(
source_rgba,
target_width=target_width,
target_height=target_height,
@ -255,20 +256,20 @@ def source_lattice_consistency_breakdown(
phase_y=phase_y,
alpha_threshold=alpha_threshold,
)
cell_error = foreground_reconstruction_error(output_rgba, lattice_source, alpha_threshold=alpha_threshold, halo=0)
adjacency_error = foreground_adjacency_error(output_rgba, lattice_source, alpha_threshold=alpha_threshold, halo=0)
motif_error = foreground_motif_error(output_rgba, lattice_source, alpha_threshold=alpha_threshold, halo=0)
cell_error = foreground_reconstruction_error(output_rgba, reference.mean_rgba, alpha_threshold=alpha_threshold, halo=0)
adjacency_error = foreground_adjacency_error(output_rgba, reference.sharp_rgba, alpha_threshold=alpha_threshold, halo=0)
motif_error = foreground_motif_error(output_rgba, reference.sharp_rgba, alpha_threshold=alpha_threshold, halo=0)
score = (
cell_error * 0.34
+ adjacency_error * 0.24
+ motif_error * 0.24
+ dispersion * 0.18
+ reference.dispersion * 0.18
)
return {
"cell_error": cell_error,
"adjacency_error": adjacency_error,
"motif_error": motif_error,
"cell_dispersion": dispersion,
"cell_dispersion": reference.dispersion,
"score": score,
}
@ -282,7 +283,7 @@ def source_lattice_evidence_breakdown(
phase_y: float,
alpha_threshold: float = 0.05,
) -> dict[str, float]:
lattice_source, dispersion = lattice_source_rgba(
reference = build_source_lattice_reference(
source_rgba,
target_width=target_width,
target_height=target_height,
@ -290,10 +291,10 @@ def source_lattice_evidence_breakdown(
phase_y=phase_y,
alpha_threshold=alpha_threshold,
)
adjacency_strength = foreground_adjacency_strength(lattice_source, alpha_threshold=alpha_threshold)
evidence_score = adjacency_strength - dispersion * 0.8
adjacency_strength = foreground_adjacency_strength(reference.sharp_rgba, alpha_threshold=alpha_threshold)
evidence_score = adjacency_strength - reference.dispersion * 0.8
return {
"cell_dispersion": dispersion,
"cell_dispersion": reference.dispersion,
"adjacency_strength": adjacency_strength,
"score": evidence_score,
}
@ -443,60 +444,12 @@ def lattice_source_rgba(
phase_y: float,
alpha_threshold: float = 0.05,
) -> tuple[np.ndarray, float]:
indices = _lattice_indices(
height=source_rgba.shape[0],
width=source_rgba.shape[1],
reference = build_source_lattice_reference(
source_rgba,
target_width=target_width,
target_height=target_height,
phase_x=phase_x,
phase_y=phase_y,
alpha_threshold=alpha_threshold,
)
cell_count = max(1, target_width * target_height)
premul = premultiply(source_rgba)
flat_idx = indices.reshape(-1)
flat_premul = premul.reshape(-1, premul.shape[-1])
counts = np.bincount(flat_idx, minlength=cell_count).astype(np.float32)
safe_counts = np.maximum(counts, 1.0)
channel_sums = [
np.bincount(flat_idx, weights=flat_premul[:, channel], minlength=cell_count).astype(np.float32)
for channel in range(flat_premul.shape[-1])
]
mean_premul = np.stack(channel_sums, axis=-1) / safe_counts[:, None]
mean_premul = mean_premul.reshape(target_height, target_width, premul.shape[-1])
mean_rgba = _unpremultiply_array(mean_premul)
per_pixel_mean = mean_premul.reshape(-1, premul.shape[-1])[flat_idx]
pixel_diff = np.mean(np.abs(flat_premul - per_pixel_mean), axis=-1)
source_alpha = source_rgba.reshape(-1, source_rgba.shape[-1])[:, 3]
lattice_alpha = mean_rgba.reshape(-1, mean_rgba.shape[-1])[flat_idx, 3]
support_mask = np.maximum(source_alpha, lattice_alpha) >= alpha_threshold
if np.any(support_mask):
dispersion = float(np.mean(pixel_diff[support_mask]))
else:
dispersion = float(np.mean(pixel_diff)) if pixel_diff.size else 0.0
return mean_rgba.astype(np.float32), dispersion
def _lattice_indices(
*,
height: int,
width: int,
target_width: int,
target_height: int,
phase_x: float,
phase_y: float,
) -> np.ndarray:
cell_x = width / max(1, target_width)
cell_y = height / max(1, target_height)
xs = (np.arange(width, dtype=np.float32) + 0.5) / cell_x - phase_x
ys = (np.arange(height, dtype=np.float32) + 0.5) / cell_y - phase_y
x_idx = np.clip(np.floor(xs).astype(np.int32), 0, max(0, target_width - 1))
y_idx = np.clip(np.floor(ys).astype(np.int32), 0, max(0, target_height - 1))
return y_idx[:, None] * target_width + x_idx[None, :]
def _unpremultiply_array(premul: np.ndarray, eps: float = 1e-6) -> np.ndarray:
out = premul.copy()
alpha = np.maximum(out[..., 3:4], eps)
out[..., :3] = np.where(out[..., 3:4] > eps, out[..., :3] / alpha, 0.0)
return np.clip(out, 0.0, 1.0)
return reference.mean_rgba, reference.dispersion

View file

@ -11,10 +11,16 @@ class SolverHyperParams:
boundary_direction_weight: float = 0.30
boundary_magnitude_weight: float = 0.15
snap_base_match_weight: float = 0.65
snap_representative_match_weight: float = 0.15
snap_source_match_weight: float = 0.85
snap_neighbor_weight: float = 0.35
snap_diagonal_weight: float = 0.14
snap_representative_delta_weight: float = 0.20
snap_source_delta_weight: float = 0.80
refine_anchor_weight: float = 0.60
refine_representative_weight: float = 0.20
refine_representative_match_weight: float = 0.15
refine_source_match_weight: float = 0.85
refine_alpha_weight: float = 0.10
refine_distance_weight: float = 0.10
refine_source_delta_weight: float = 0.25
@ -22,6 +28,7 @@ class SolverHyperParams:
refine_diagonal_weight: float = 0.08
refine_motif_weight: float = 0.12
refine_line_weight: float = 0.10
refine_relaxed_mode_weight: float = 0.14
refine_candidate_extent: float = 0.70
refine_candidate_levels: int = 7
relax_iterations: int = 24
@ -44,7 +51,7 @@ class SolverHyperParams:
structure_source_adjacency_weight: float = 0.35
structure_source_motif_weight: float = 0.40
structure_source_line_weight: float = 0.20
structure_representative_weight: float = 0.05
structure_representative_weight: float = 0.02
alpha_foreground_threshold: float = 0.50
alpha_representative_foreground_threshold: float = 0.60
alpha_opaque_threshold: float = 0.95
@ -53,9 +60,10 @@ class SolverHyperParams:
phase_rerank_edge_position_weight: float = 0.20
phase_rerank_wobble_weight: float = 0.20
phase_rerank_edge_concentration_weight: float = 0.10
phase_rerank_size_penalty_weight: float = 0.18
phase_rerank_inference_penalty_weight: float = 0.05
phase_rerank_confidence_threshold: float = 0.12
phase_rerank_max_size_delta_ratio: float = 0.15
phase_rerank_max_size_delta_ratio: float = 0.40
phase_rerank_margin: float = 0.004
def to_dict(self) -> dict[str, float]:

View file

@ -181,32 +181,75 @@ def _select_phase_candidate(
(record["edge_concentration"] for record in candidate_records),
higher_is_better=True,
)
size_penalty = [
min(1.0, float(record["size_delta_ratio"]) / max(solver_params.phase_rerank_max_size_delta_ratio, 1e-6))
for record in candidate_records
]
inference_penalty = _normalize_penalty(record["inference_penalty"] for record in candidate_records)
baseline_rank: float | None = None
best_rank = float("inf")
best_candidate = inference
annotated_candidates = []
for index, record in enumerate(candidate_records):
candidate_inference = record["inference"]
size_delta_ratio = float(record["size_delta_ratio"])
if (
candidate_inference.target_width != inference.target_width
or candidate_inference.target_height != inference.target_height
) and size_delta_ratio > solver_params.phase_rerank_max_size_delta_ratio:
if float(record["size_delta_ratio"]) > solver_params.phase_rerank_max_size_delta_ratio:
continue
rank = (
solver_params.phase_rerank_support_weight * support_penalty[index]
+ solver_params.phase_rerank_edge_position_weight * edge_position_penalty[index]
+ solver_params.phase_rerank_wobble_weight * wobble_penalty[index]
+ solver_params.phase_rerank_edge_concentration_weight * edge_concentration_penalty[index]
+ solver_params.phase_rerank_size_penalty_weight * size_penalty[index]
+ solver_params.phase_rerank_inference_penalty_weight * inference_penalty[index]
)
source_candidate = inference.top_candidates[index]
breakdown = dict(source_candidate.breakdown)
breakdown["phase_rerank_support_score"] = float(record["support_score"])
breakdown["phase_rerank_edge_position_error"] = float(record["edge_position_error"])
breakdown["phase_rerank_stroke_wobble_error"] = float(record["stroke_wobble_error"])
breakdown["phase_rerank_edge_concentration"] = float(record["edge_concentration"])
breakdown["phase_rerank_size_delta_ratio"] = float(record["size_delta_ratio"])
breakdown["phase_rerank_size_penalty"] = float(size_penalty[index])
breakdown["phase_rerank_inference_penalty"] = float(record["inference_penalty"])
breakdown["phase_rerank_score"] = float(rank)
annotated_candidates.append(
source_candidate.__class__(
target_width=source_candidate.target_width,
target_height=source_candidate.target_height,
phase_x=source_candidate.phase_x,
phase_y=source_candidate.phase_y,
score=source_candidate.score,
breakdown=breakdown,
)
)
if baseline_rank is None:
baseline_rank = rank
if rank < best_rank:
best_rank = rank
best_candidate = record["inference"]
annotated_candidates.sort(key=lambda candidate: float(candidate.breakdown.get("phase_rerank_score", float("inf"))))
for rank, candidate in enumerate(annotated_candidates, start=1):
candidate.breakdown["phase_rerank_rank"] = float(rank)
if annotated_candidates:
best_candidate = InferenceResult(
target_width=best_candidate.target_width,
target_height=best_candidate.target_height,
phase_x=best_candidate.phase_x,
phase_y=best_candidate.phase_y,
confidence=inference.confidence,
top_candidates=annotated_candidates,
)
if baseline_rank is None or best_rank > baseline_rank - solver_params.phase_rerank_margin:
if annotated_candidates:
return InferenceResult(
target_width=inference.target_width,
target_height=inference.target_height,
phase_x=inference.phase_x,
phase_y=inference.phase_y,
confidence=inference.confidence,
top_candidates=annotated_candidates,
)
return inference
return best_candidate

View file

@ -0,0 +1,112 @@
from __future__ import annotations
import numpy as np
from .io import premultiply, unpremultiply
from .types import SourceLatticeReference
def lattice_indices(
*,
height: int,
width: int,
target_width: int,
target_height: int,
phase_x: float,
phase_y: float,
) -> np.ndarray:
cell_x = width / max(1, target_width)
cell_y = height / max(1, target_height)
xs = (np.arange(width, dtype=np.float32) + 0.5) / cell_x - phase_x
ys = (np.arange(height, dtype=np.float32) + 0.5) / cell_y - phase_y
x_idx = np.clip(np.floor(xs).astype(np.int32), 0, max(0, target_width - 1))
y_idx = np.clip(np.floor(ys).astype(np.int32), 0, max(0, target_height - 1))
return y_idx[:, None] * target_width + x_idx[None, :]
def _reference_deltas(reference: np.ndarray) -> tuple[np.ndarray | None, np.ndarray | None, np.ndarray | None, np.ndarray | None]:
height = reference.shape[0]
width = reference.shape[1]
delta_x = reference[:, 1:, :] - reference[:, :-1, :] if width > 1 else None
delta_y = reference[1:, :, :] - reference[:-1, :, :] if height > 1 else None
delta_diag = reference[1:, 1:, :] - reference[:-1, :-1, :] if height > 1 and width > 1 else None
delta_anti = reference[1:, :-1, :] - reference[:-1, 1:, :] if height > 1 and width > 1 else None
return delta_x, delta_y, delta_diag, delta_anti
def build_source_lattice_reference(
source_rgba: np.ndarray,
*,
target_width: int,
target_height: int,
phase_x: float,
phase_y: float,
alpha_threshold: float = 0.05,
) -> SourceLatticeReference:
indices = lattice_indices(
height=source_rgba.shape[0],
width=source_rgba.shape[1],
target_width=target_width,
target_height=target_height,
phase_x=phase_x,
phase_y=phase_y,
)
cell_count = max(1, target_width * target_height)
premul = premultiply(source_rgba)
flat_idx = indices.reshape(-1)
flat_premul = premul.reshape(-1, premul.shape[-1])
flat_alpha = source_rgba.reshape(-1, source_rgba.shape[-1])[:, 3]
counts = np.bincount(flat_idx, minlength=cell_count).astype(np.float32)
safe_counts = np.maximum(counts, 1.0)
channel_sums = [
np.bincount(flat_idx, weights=flat_premul[:, channel], minlength=cell_count).astype(np.float32)
for channel in range(flat_premul.shape[-1])
]
mean_premul_flat = np.stack(channel_sums, axis=-1) / safe_counts[:, None]
mean_premul = mean_premul_flat.reshape(target_height, target_width, premul.shape[-1])
mean_rgba = unpremultiply(mean_premul)
per_pixel_mean = mean_premul_flat[flat_idx]
pixel_diff = np.mean(np.abs(flat_premul - per_pixel_mean), axis=-1)
cell_diff_sums = np.bincount(flat_idx, weights=pixel_diff, minlength=cell_count).astype(np.float32)
cell_dispersion = (cell_diff_sums / safe_counts).reshape(target_height, target_width)
lattice_alpha = mean_rgba.reshape(-1, mean_rgba.shape[-1])[flat_idx, 3]
support_mask = np.maximum(flat_alpha, lattice_alpha) >= alpha_threshold
if np.any(support_mask):
dispersion = float(np.mean(pixel_diff[support_mask]))
else:
dispersion = float(np.mean(pixel_diff)) if pixel_diff.size else 0.0
exemplar_cost = pixel_diff + np.maximum(0.0, per_pixel_mean[:, 3] - flat_premul[:, 3]) * 1e-3
order = np.lexsort((exemplar_cost.astype(np.float32), flat_idx.astype(np.int64)))
sharp_premul_flat = mean_premul_flat.copy()
if order.size > 0:
sorted_cells = flat_idx[order]
first_positions = np.concatenate(([0], np.flatnonzero(np.diff(sorted_cells)) + 1))
best_order = order[first_positions]
best_cells = flat_idx[best_order]
sharp_premul_flat[best_cells] = flat_premul[best_order]
sharp_premul = sharp_premul_flat.reshape(target_height, target_width, premul.shape[-1])
sharp_rgba = unpremultiply(sharp_premul)
cell_alpha_max = np.zeros(cell_count, dtype=np.float32)
np.maximum.at(cell_alpha_max, flat_idx, flat_alpha)
expected_pixels_per_cell = float(source_rgba.shape[0] * source_rgba.shape[1]) / float(cell_count)
cell_support = (counts / max(expected_pixels_per_cell, 1.0)).reshape(target_height, target_width)
delta_x, delta_y, delta_diag, delta_anti = _reference_deltas(sharp_premul)
return SourceLatticeReference(
mean_rgba=mean_rgba.astype(np.float32),
sharp_rgba=sharp_rgba.astype(np.float32),
dispersion=dispersion,
cell_dispersion=cell_dispersion.astype(np.float32),
cell_counts=counts.reshape(target_height, target_width).astype(np.float32),
cell_support=cell_support.astype(np.float32),
cell_alpha_max=cell_alpha_max.reshape(target_height, target_width).astype(np.float32),
delta_x=delta_x.astype(np.float32) if delta_x is not None else None,
delta_y=delta_y.astype(np.float32) if delta_y is not None else None,
delta_diag=delta_diag.astype(np.float32) if delta_diag is not None else None,
delta_anti=delta_anti.astype(np.float32) if delta_anti is not None else None,
)

View file

@ -36,6 +36,21 @@ class SourceAnalysis:
cluster_preview: np.ndarray
@dataclass(slots=True)
class SourceLatticeReference:
mean_rgba: np.ndarray
sharp_rgba: np.ndarray
dispersion: float
cell_dispersion: np.ndarray
cell_counts: np.ndarray
cell_support: np.ndarray
cell_alpha_max: np.ndarray
delta_x: np.ndarray | None
delta_y: np.ndarray | None
delta_diag: np.ndarray | None
delta_anti: np.ndarray | None
@dataclass(slots=True)
class SolverArtifacts:
target_rgba: np.ndarray