Use direct autocorr inference for hosted demo
This commit is contained in:
parent
338ecc7b24
commit
728e08472b
12 changed files with 326 additions and 23 deletions
|
|
@ -518,15 +518,11 @@ def _normalize_job_options(
|
|||
normalized_steps = min(requested_steps, config.max_steps)
|
||||
normalized_device = "cpu" if config.hosted_demo else (device.strip() if device.strip() else "auto")
|
||||
normalized_strip_background = False if config.hosted_demo else bool(strip_background)
|
||||
effective_target_size = normalized_target_size
|
||||
if fixed_dims is None:
|
||||
effective_target_size = (
|
||||
config.max_output_dimension
|
||||
if effective_target_size is None
|
||||
else min(effective_target_size, config.max_output_dimension)
|
||||
)
|
||||
inference_mode = "autocorr" if config.hosted_demo and fixed_dims is None else "search"
|
||||
max_inferred_target_size = config.max_output_dimension if config.hosted_demo and fixed_dims is None else None
|
||||
normalized_skip_phase_rerank = True if config.hosted_demo else bool(skip_phase_rerank)
|
||||
return {
|
||||
"target_size": effective_target_size,
|
||||
"target_size": normalized_target_size,
|
||||
"target_width": normalized_target_width,
|
||||
"target_height": normalized_target_height,
|
||||
"phase_x": phase_x,
|
||||
|
|
@ -535,7 +531,9 @@ def _normalize_job_options(
|
|||
"seed": int(seed),
|
||||
"device": normalized_device,
|
||||
"strip_background": normalized_strip_background,
|
||||
"skip_phase_rerank": bool(skip_phase_rerank),
|
||||
"skip_phase_rerank": normalized_skip_phase_rerank,
|
||||
"lattice_inference_mode": inference_mode,
|
||||
"max_inferred_target_size": max_inferred_target_size,
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -601,6 +599,8 @@ def _execute_job(job: GuiJob) -> None:
|
|||
device=job.options["device"],
|
||||
strip_background=job.options["strip_background"],
|
||||
enable_phase_rerank=not job.options["skip_phase_rerank"],
|
||||
lattice_inference_mode=job.options["lattice_inference_mode"],
|
||||
max_inferred_target_size=job.options["max_inferred_target_size"],
|
||||
observer=job.observe,
|
||||
)
|
||||
job.mark_completed()
|
||||
|
|
|
|||
|
|
@ -558,7 +558,12 @@ function renderStatusMetrics() {
|
|||
}
|
||||
else if (state.stageKey === "analysis" || state.stageKey === "selection") {
|
||||
if (state.inference) {
|
||||
items.push({ label: "Mode", value: state.inferenceMode === "fixed" ? "pinned" : state.inferenceMode ?? "search" });
|
||||
const modeLabel = state.inferenceMode === "fixed"
|
||||
? "pinned"
|
||||
: state.inferenceMode === "autocorr"
|
||||
? "autocorr direct"
|
||||
: state.inferenceMode ?? "search";
|
||||
items.push({ label: "Mode", value: modeLabel });
|
||||
items.push({ label: "Grid", value: `${state.inference.target_width} x ${state.inference.target_height}` });
|
||||
items.push({ label: "Phase", value: `${formatNumber(state.inference.phase_x, 2)}, ${formatNumber(state.inference.phase_y, 2)}` });
|
||||
items.push({ label: "Confidence", value: formatNumber(state.inference.confidence, 3) });
|
||||
|
|
|
|||
|
|
@ -71,6 +71,10 @@ def _candidate_dims(
|
|||
return dims
|
||||
|
||||
|
||||
def _target_dims_from_major_size(width: int, height: int, target_size: int) -> tuple[int, int]:
|
||||
return _candidate_dims(width, height, int(target_size))[0]
|
||||
|
||||
|
||||
def _autocorr_size_window(
|
||||
hinted_sizes: list[int],
|
||||
*,
|
||||
|
|
@ -117,6 +121,31 @@ def _resolve_candidate_dims_from_autocorr(
|
|||
return narrowed or dims
|
||||
|
||||
|
||||
def _direct_autocorr_target_size(
|
||||
width: int,
|
||||
height: int,
|
||||
*,
|
||||
hinted_sizes: list[int],
|
||||
shared_prior: float,
|
||||
prior_reliability: float,
|
||||
max_target_size: int | None = None,
|
||||
) -> int:
|
||||
size_window = _autocorr_size_window(
|
||||
hinted_sizes,
|
||||
prior_reliability=prior_reliability,
|
||||
)
|
||||
major = max(width, height)
|
||||
if size_window is not None:
|
||||
target_size = int(size_window[0])
|
||||
elif hinted_sizes:
|
||||
target_size = int(round(float(np.median(np.asarray(hinted_sizes, dtype=np.float32)))))
|
||||
else:
|
||||
target_size = max(1, int(round(major / max(1e-6, shared_prior))))
|
||||
if max_target_size is not None:
|
||||
target_size = min(target_size, max(1, int(max_target_size)))
|
||||
return max(1, target_size)
|
||||
|
||||
|
||||
def _estimate_cell_size_details(profile: np.ndarray) -> AutocorrEstimate:
|
||||
centered = profile.astype(np.float32) - float(np.mean(profile))
|
||||
max_lag = min(64, max(4, profile.shape[0] // 3))
|
||||
|
|
@ -612,6 +641,80 @@ def infer_lattice(
|
|||
)
|
||||
|
||||
|
||||
def infer_autocorr_lattice(
|
||||
rgba: np.ndarray,
|
||||
*,
|
||||
max_target_size: int | None = None,
|
||||
device: str = "auto",
|
||||
observer: PipelineObserver | None = None,
|
||||
) -> InferenceResult:
|
||||
torch, _ = _require_torch()
|
||||
resolved_device = _resolve_device(torch, device)
|
||||
height, width = rgba.shape[:2]
|
||||
autocorr_x_estimate, autocorr_y_estimate = _estimate_lattice_autocorr_details(rgba)
|
||||
prior_cell_x, prior_cell_y, prior_reliability = _estimate_lattice_prior_details(rgba)
|
||||
hinted_sizes = _hint_target_sizes_from_autocorr(
|
||||
width,
|
||||
height,
|
||||
autocorr_x_estimate=autocorr_x_estimate,
|
||||
autocorr_y_estimate=autocorr_y_estimate,
|
||||
shared_prior=prior_cell_x,
|
||||
)
|
||||
target_size = _direct_autocorr_target_size(
|
||||
width,
|
||||
height,
|
||||
hinted_sizes=hinted_sizes,
|
||||
shared_prior=prior_cell_x,
|
||||
prior_reliability=prior_reliability,
|
||||
max_target_size=max_target_size,
|
||||
)
|
||||
target_width, target_height = _target_dims_from_major_size(width, height, target_size)
|
||||
phase_values = np.linspace(-0.4, 0.4, num=5, dtype=np.float32)
|
||||
phase_sample_count = int(phase_values.size * phase_values.size)
|
||||
|
||||
emit_observer(
|
||||
observer,
|
||||
"lattice_search_started",
|
||||
candidate_count=1,
|
||||
phase_sample_count=phase_sample_count,
|
||||
device=resolved_device,
|
||||
)
|
||||
check_observer_cancelled(observer)
|
||||
candidates = _score_phase_group(
|
||||
rgba,
|
||||
target_width=target_width,
|
||||
target_height=target_height,
|
||||
prior_cell_x=prior_cell_x,
|
||||
prior_cell_y=prior_cell_y,
|
||||
prior_reliability=prior_reliability,
|
||||
phase_x_values=phase_values,
|
||||
phase_y_values=phase_values,
|
||||
device=resolved_device,
|
||||
)
|
||||
candidates.sort(key=lambda item: item.score, reverse=True)
|
||||
emit_observer(
|
||||
observer,
|
||||
"lattice_search_progress",
|
||||
completed_candidates=1,
|
||||
total_candidates=1,
|
||||
target_width=int(target_width),
|
||||
target_height=int(target_height),
|
||||
phase_sample_count=phase_sample_count,
|
||||
best_score=None if not candidates else float(candidates[0].score),
|
||||
)
|
||||
best = candidates[0]
|
||||
second = candidates[1] if len(candidates) > 1 else best
|
||||
confidence = max(0.0, best.score - second.score)
|
||||
return InferenceResult(
|
||||
target_width=best.target_width,
|
||||
target_height=best.target_height,
|
||||
phase_x=best.phase_x,
|
||||
phase_y=best.phase_y,
|
||||
confidence=confidence,
|
||||
top_candidates=candidates[:8],
|
||||
)
|
||||
|
||||
|
||||
def infer_fixed_lattice(
|
||||
rgba: np.ndarray,
|
||||
*,
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ from .diagnostics import (
|
|||
write_lattice_overlay,
|
||||
)
|
||||
from .discrete import cleanup_pixels
|
||||
from .inference import infer_fixed_lattice, infer_lattice, inference_to_json
|
||||
from .inference import infer_autocorr_lattice, infer_fixed_lattice, infer_lattice, inference_to_json
|
||||
from .io import load_rgba, nearest_resize, save_rgba
|
||||
from .metrics import (
|
||||
foreground_edge_concentration,
|
||||
|
|
@ -51,6 +51,8 @@ def run_pipeline(
|
|||
solver_params: SolverHyperParams | None = None,
|
||||
strip_background: bool = False,
|
||||
enable_phase_rerank: bool = True,
|
||||
lattice_inference_mode: str = "search",
|
||||
max_inferred_target_size: int | None = None,
|
||||
observer: PipelineObserver | None = None,
|
||||
) -> RunResult:
|
||||
source = load_rgba(input_path)
|
||||
|
|
@ -71,6 +73,8 @@ def run_pipeline(
|
|||
solver_params=solver_params,
|
||||
strip_background=strip_background,
|
||||
enable_phase_rerank=enable_phase_rerank,
|
||||
lattice_inference_mode=lattice_inference_mode,
|
||||
max_inferred_target_size=max_inferred_target_size,
|
||||
observer=observer,
|
||||
)
|
||||
|
||||
|
|
@ -93,6 +97,8 @@ def run_pipeline_rgba(
|
|||
solver_params: SolverHyperParams | None = None,
|
||||
strip_background: bool = False,
|
||||
enable_phase_rerank: bool = True,
|
||||
lattice_inference_mode: str = "search",
|
||||
max_inferred_target_size: int | None = None,
|
||||
observer: PipelineObserver | None = None,
|
||||
) -> RunResult:
|
||||
started = time.perf_counter()
|
||||
|
|
@ -122,15 +128,32 @@ def run_pipeline_rgba(
|
|||
)
|
||||
if fixed_dims is None:
|
||||
check_observer_cancelled(observer)
|
||||
if lattice_inference_mode == "autocorr":
|
||||
inference_label = "Autocorr lattice"
|
||||
inference_detail = "Taking one autocorr consensus size, probing phase once, and skipping size search."
|
||||
elif lattice_inference_mode == "search":
|
||||
inference_label = "Lattice search"
|
||||
inference_detail = "Searching for the output size and phase that best fit the input."
|
||||
else:
|
||||
raise ValueError(f"Unknown lattice_inference_mode '{lattice_inference_mode}'.")
|
||||
emit_observer(
|
||||
observer,
|
||||
"stage_started",
|
||||
stage="inference",
|
||||
label="Lattice search",
|
||||
detail="Searching for the output size and phase that best fit the input.",
|
||||
label=inference_label,
|
||||
detail=inference_detail,
|
||||
)
|
||||
inference = infer_lattice(source, target_size=target_size, device=device, observer=observer)
|
||||
inference_mode = "searched"
|
||||
if lattice_inference_mode == "autocorr":
|
||||
inference = infer_autocorr_lattice(
|
||||
source,
|
||||
max_target_size=max_inferred_target_size,
|
||||
device=device,
|
||||
observer=observer,
|
||||
)
|
||||
inference_mode = "autocorr"
|
||||
else:
|
||||
inference = infer_lattice(source, target_size=target_size, device=device, observer=observer)
|
||||
inference_mode = "searched"
|
||||
else:
|
||||
check_observer_cancelled(observer)
|
||||
emit_observer(
|
||||
|
|
@ -273,6 +296,8 @@ def run_pipeline_rgba(
|
|||
"device": device,
|
||||
"strip_background": strip_background,
|
||||
"enable_phase_rerank": enable_phase_rerank,
|
||||
"lattice_inference_mode": lattice_inference_mode,
|
||||
"max_inferred_target_size": max_inferred_target_size,
|
||||
"inference_mode": inference_mode,
|
||||
"solver_params": solver_params.to_dict(),
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue