Prepare lean solver for hosted shipping
This commit is contained in:
parent
2c656908e9
commit
5f2a644906
35 changed files with 1839 additions and 1948 deletions
|
|
@ -1,9 +1,10 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import shutil
|
||||
from concurrent.futures import ProcessPoolExecutor
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
import json
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
|
@ -41,6 +42,7 @@ def run_roundtrip_benchmark(
|
|||
limit_cases: int | None = None,
|
||||
keep_existing: bool = False,
|
||||
solver_params: SolverHyperParams | None = None,
|
||||
workers: int = 1,
|
||||
) -> dict[str, Any]:
|
||||
corpus_path = Path(corpus_dir)
|
||||
originals_dir = corpus_path / "originals"
|
||||
|
|
@ -62,113 +64,39 @@ def run_roundtrip_benchmark(
|
|||
if out_path.exists() and not keep_existing:
|
||||
shutil.rmtree(out_path)
|
||||
out_path.mkdir(parents=True, exist_ok=True)
|
||||
rows: list[dict[str, Any]] = []
|
||||
|
||||
tasks: list[dict[str, Any]] = []
|
||||
for case_index, original_path in enumerate(original_paths):
|
||||
original = load_rgba(original_path)
|
||||
metadata = _load_metadata(original_path)
|
||||
case_id = original_path.relative_to(originals_dir).with_suffix("").as_posix()
|
||||
case_dir = out_path / "cases" / Path(case_id)
|
||||
case_dir.mkdir(parents=True, exist_ok=True)
|
||||
save_rgba(case_dir / "original.png", original)
|
||||
for profile_index, profile in enumerate(selected_profiles):
|
||||
for variant_index in range(variants):
|
||||
variant_seed = seed + case_index * 1000 + profile_index * 100 + variant_index
|
||||
settings = _variant_settings(variant_seed, profile=profile)
|
||||
variant_dir = case_dir / f"profile-{profile}" / f"variant-{variant_index + 1:02d}"
|
||||
diagnostics_dir = variant_dir / "diagnostics"
|
||||
diagnostics_dir.mkdir(parents=True, exist_ok=True)
|
||||
fake = fake_pixelize(original, seed=variant_seed, **settings)
|
||||
input_path = variant_dir / "input.png"
|
||||
output_path = variant_dir / "optimized.png"
|
||||
save_rgba(input_path, fake)
|
||||
|
||||
locked_target_size = max(original.shape[0], original.shape[1]) if not infer_size else None
|
||||
result = run_pipeline(
|
||||
input_path,
|
||||
output_path,
|
||||
target_size=locked_target_size,
|
||||
diagnostics_dir=diagnostics_dir,
|
||||
seed=variant_seed,
|
||||
steps=steps,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
tasks.append(
|
||||
{
|
||||
"original_path": original_path,
|
||||
"originals_dir": originals_dir,
|
||||
"out_path": out_path,
|
||||
"case_index": case_index,
|
||||
"profile_index": profile_index,
|
||||
"profile": profile,
|
||||
"variant_index": variant_index,
|
||||
"seed": seed,
|
||||
"steps": steps,
|
||||
"device": device,
|
||||
"infer_size": infer_size,
|
||||
"solver_params": solver_params,
|
||||
}
|
||||
)
|
||||
|
||||
naive = naive_resize_baseline(fake, width=original.shape[1], height=original.shape[0])
|
||||
diffusion = error_diffusion_baseline(fake, width=original.shape[1], height=original.shape[0])
|
||||
save_rgba(variant_dir / "naive.png", naive)
|
||||
save_rgba(variant_dir / "diffusion.png", diffusion)
|
||||
_save_preview(variant_dir / "optimized-preview.png", result.output_rgba, original)
|
||||
_save_preview(variant_dir / "naive-preview.png", naive, original)
|
||||
_save_preview(variant_dir / "diffusion-preview.png", diffusion, original)
|
||||
|
||||
optimized_preview = nearest_resize(result.output_rgba, width=original.shape[1], height=original.shape[0])
|
||||
optimized_error = foreground_reconstruction_error(optimized_preview, original)
|
||||
naive_error = foreground_reconstruction_error(naive, original)
|
||||
diffusion_error = foreground_reconstruction_error(diffusion, original)
|
||||
optimized_adjacency = foreground_adjacency_error(optimized_preview, original)
|
||||
naive_adjacency = foreground_adjacency_error(naive, original)
|
||||
diffusion_adjacency = foreground_adjacency_error(diffusion, original)
|
||||
optimized_motif = foreground_motif_error(optimized_preview, original)
|
||||
naive_motif = foreground_motif_error(naive, original)
|
||||
diffusion_motif = foreground_motif_error(diffusion, original)
|
||||
row = {
|
||||
"case_id": case_id,
|
||||
"profile": profile,
|
||||
"variant": variant_index + 1,
|
||||
"source_file": str(original_path),
|
||||
"title": metadata.get("title", ""),
|
||||
"author": metadata.get("author", ""),
|
||||
"license": metadata.get("license", ""),
|
||||
"source_url": metadata.get("source_url", ""),
|
||||
"original_width": int(original.shape[1]),
|
||||
"original_height": int(original.shape[0]),
|
||||
"input_width": int(fake.shape[1]),
|
||||
"input_height": int(fake.shape[0]),
|
||||
"upscale": settings["upscale"],
|
||||
"offset_x": settings["offset_x"],
|
||||
"offset_y": settings["offset_y"],
|
||||
"blur_radius": settings["blur_radius"],
|
||||
"warp_strength": settings["warp_strength"],
|
||||
"warp_detail": settings["warp_detail"],
|
||||
"warp_sample_mode": settings["warp_sample_mode"],
|
||||
"artifact_density": settings["artifact_density"],
|
||||
"artifact_strength": settings["artifact_strength"],
|
||||
"primary_metric": "foreground_premultiplied_mae",
|
||||
"reference_foreground_coverage": foreground_coverage(original, original),
|
||||
"target_size_locked": not infer_size,
|
||||
"inferred_width": result.inference.target_width,
|
||||
"inferred_height": result.inference.target_height,
|
||||
"inference_confidence": result.inference.confidence,
|
||||
"optimized_error_to_original": optimized_error,
|
||||
"naive_error_to_original": naive_error,
|
||||
"diffusion_error_to_original": diffusion_error,
|
||||
"optimized_adjacency_error_to_original": optimized_adjacency,
|
||||
"naive_adjacency_error_to_original": naive_adjacency,
|
||||
"diffusion_adjacency_error_to_original": diffusion_adjacency,
|
||||
"optimized_motif_error_to_original": optimized_motif,
|
||||
"naive_motif_error_to_original": naive_motif,
|
||||
"diffusion_motif_error_to_original": diffusion_motif,
|
||||
"optimized_canvas_error_to_original": reconstruction_error(optimized_preview, original),
|
||||
"naive_canvas_error_to_original": reconstruction_error(naive, original),
|
||||
"diffusion_canvas_error_to_original": reconstruction_error(diffusion, original),
|
||||
"optimized_exact_match": foreground_exact_match_ratio(optimized_preview, original),
|
||||
"naive_exact_match": foreground_exact_match_ratio(naive, original),
|
||||
"diffusion_exact_match": foreground_exact_match_ratio(diffusion, original),
|
||||
"optimized_canvas_exact_match": exact_match_ratio(optimized_preview, original),
|
||||
"naive_canvas_exact_match": exact_match_ratio(naive, original),
|
||||
"diffusion_canvas_exact_match": exact_match_ratio(diffusion, original),
|
||||
"optimized_coherence": coherence_breakdown(result.output_rgba)["coherence_score"],
|
||||
"naive_coherence": coherence_breakdown(naive)["coherence_score"],
|
||||
"diffusion_coherence": coherence_breakdown(diffusion)["coherence_score"],
|
||||
"optimized_beats_naive_error": optimized_error <= naive_error,
|
||||
"optimized_beats_diffusion_error": optimized_error <= diffusion_error,
|
||||
"optimized_beats_naive_adjacency": optimized_adjacency <= naive_adjacency,
|
||||
"optimized_beats_diffusion_adjacency": optimized_adjacency <= diffusion_adjacency,
|
||||
"optimized_beats_naive_motif": optimized_motif <= naive_motif,
|
||||
"optimized_beats_diffusion_motif": optimized_motif <= diffusion_motif,
|
||||
}
|
||||
rows.append(row)
|
||||
effective_workers = max(1, int(workers))
|
||||
if effective_workers == 1:
|
||||
rows = [_run_roundtrip_row(task) for task in tasks]
|
||||
else:
|
||||
with ProcessPoolExecutor(max_workers=effective_workers) as pool:
|
||||
rows = list(pool.map(_run_roundtrip_row, tasks))
|
||||
|
||||
summary = {
|
||||
"case_count": len(original_paths),
|
||||
|
|
@ -178,6 +106,7 @@ def run_roundtrip_benchmark(
|
|||
"target_size_locked": not infer_size,
|
||||
"primary_metric": "foreground_premultiplied_mae",
|
||||
"solver_params": solver_params.to_dict() if solver_params is not None else SolverHyperParams().to_dict(),
|
||||
"workers": effective_workers,
|
||||
"cases": _summarize_cases(rows),
|
||||
"rows": rows,
|
||||
}
|
||||
|
|
@ -186,6 +115,134 @@ def run_roundtrip_benchmark(
|
|||
return summary
|
||||
|
||||
|
||||
def _run_roundtrip_row(task: dict[str, Any]) -> dict[str, Any]:
|
||||
original_path = Path(task["original_path"])
|
||||
originals_dir = Path(task["originals_dir"])
|
||||
out_path = Path(task["out_path"])
|
||||
case_index = int(task["case_index"])
|
||||
profile_index = int(task["profile_index"])
|
||||
profile = str(task["profile"])
|
||||
variant_index = int(task["variant_index"])
|
||||
seed = int(task["seed"])
|
||||
steps = int(task["steps"])
|
||||
device = str(task["device"])
|
||||
infer_size = bool(task["infer_size"])
|
||||
solver_params = task["solver_params"]
|
||||
|
||||
_limit_worker_threads()
|
||||
|
||||
original = load_rgba(original_path)
|
||||
metadata = _load_metadata(original_path)
|
||||
case_id = original_path.relative_to(originals_dir).with_suffix("").as_posix()
|
||||
case_dir = out_path / "cases" / Path(case_id)
|
||||
variant_seed = seed + case_index * 1000 + profile_index * 100 + variant_index
|
||||
settings = _variant_settings(variant_seed, profile=profile)
|
||||
variant_dir = case_dir / f"profile-{profile}" / f"variant-{variant_index + 1:02d}"
|
||||
diagnostics_dir = variant_dir / "diagnostics"
|
||||
diagnostics_dir.mkdir(parents=True, exist_ok=True)
|
||||
fake = fake_pixelize(original, seed=variant_seed, **settings)
|
||||
input_path = variant_dir / "input.png"
|
||||
output_path = variant_dir / "optimized.png"
|
||||
save_rgba(input_path, fake)
|
||||
|
||||
locked_target_size = max(original.shape[0], original.shape[1]) if not infer_size else None
|
||||
result = run_pipeline(
|
||||
input_path,
|
||||
output_path,
|
||||
target_size=locked_target_size,
|
||||
diagnostics_dir=diagnostics_dir,
|
||||
seed=variant_seed,
|
||||
steps=steps,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
)
|
||||
|
||||
naive = naive_resize_baseline(fake, width=original.shape[1], height=original.shape[0])
|
||||
diffusion = error_diffusion_baseline(fake, width=original.shape[1], height=original.shape[0])
|
||||
save_rgba(variant_dir / "naive.png", naive)
|
||||
save_rgba(variant_dir / "diffusion.png", diffusion)
|
||||
_save_preview(variant_dir / "optimized-preview.png", result.output_rgba, original)
|
||||
_save_preview(variant_dir / "naive-preview.png", naive, original)
|
||||
_save_preview(variant_dir / "diffusion-preview.png", diffusion, original)
|
||||
|
||||
optimized_preview = nearest_resize(result.output_rgba, width=original.shape[1], height=original.shape[0])
|
||||
optimized_error = foreground_reconstruction_error(optimized_preview, original)
|
||||
naive_error = foreground_reconstruction_error(naive, original)
|
||||
diffusion_error = foreground_reconstruction_error(diffusion, original)
|
||||
optimized_adjacency = foreground_adjacency_error(optimized_preview, original)
|
||||
naive_adjacency = foreground_adjacency_error(naive, original)
|
||||
diffusion_adjacency = foreground_adjacency_error(diffusion, original)
|
||||
optimized_motif = foreground_motif_error(optimized_preview, original)
|
||||
naive_motif = foreground_motif_error(naive, original)
|
||||
diffusion_motif = foreground_motif_error(diffusion, original)
|
||||
return {
|
||||
"case_id": case_id,
|
||||
"profile": profile,
|
||||
"variant": variant_index + 1,
|
||||
"source_file": str(original_path),
|
||||
"title": metadata.get("title", ""),
|
||||
"author": metadata.get("author", ""),
|
||||
"license": metadata.get("license", ""),
|
||||
"source_url": metadata.get("source_url", ""),
|
||||
"original_width": int(original.shape[1]),
|
||||
"original_height": int(original.shape[0]),
|
||||
"input_width": int(fake.shape[1]),
|
||||
"input_height": int(fake.shape[0]),
|
||||
"upscale": settings["upscale"],
|
||||
"offset_x": settings["offset_x"],
|
||||
"offset_y": settings["offset_y"],
|
||||
"blur_radius": settings["blur_radius"],
|
||||
"warp_strength": settings["warp_strength"],
|
||||
"warp_detail": settings["warp_detail"],
|
||||
"warp_sample_mode": settings["warp_sample_mode"],
|
||||
"artifact_density": settings["artifact_density"],
|
||||
"artifact_strength": settings["artifact_strength"],
|
||||
"primary_metric": "foreground_premultiplied_mae",
|
||||
"reference_foreground_coverage": foreground_coverage(original, original),
|
||||
"target_size_locked": not infer_size,
|
||||
"inferred_width": result.inference.target_width,
|
||||
"inferred_height": result.inference.target_height,
|
||||
"inference_confidence": result.inference.confidence,
|
||||
"optimized_error_to_original": optimized_error,
|
||||
"naive_error_to_original": naive_error,
|
||||
"diffusion_error_to_original": diffusion_error,
|
||||
"optimized_adjacency_error_to_original": optimized_adjacency,
|
||||
"naive_adjacency_error_to_original": naive_adjacency,
|
||||
"diffusion_adjacency_error_to_original": diffusion_adjacency,
|
||||
"optimized_motif_error_to_original": optimized_motif,
|
||||
"naive_motif_error_to_original": naive_motif,
|
||||
"diffusion_motif_error_to_original": diffusion_motif,
|
||||
"optimized_canvas_error_to_original": reconstruction_error(optimized_preview, original),
|
||||
"naive_canvas_error_to_original": reconstruction_error(naive, original),
|
||||
"diffusion_canvas_error_to_original": reconstruction_error(diffusion, original),
|
||||
"optimized_exact_match": foreground_exact_match_ratio(optimized_preview, original),
|
||||
"naive_exact_match": foreground_exact_match_ratio(naive, original),
|
||||
"diffusion_exact_match": foreground_exact_match_ratio(diffusion, original),
|
||||
"optimized_canvas_exact_match": exact_match_ratio(optimized_preview, original),
|
||||
"naive_canvas_exact_match": exact_match_ratio(naive, original),
|
||||
"diffusion_canvas_exact_match": exact_match_ratio(diffusion, original),
|
||||
"optimized_coherence": coherence_breakdown(result.output_rgba)["coherence_score"],
|
||||
"naive_coherence": coherence_breakdown(naive)["coherence_score"],
|
||||
"diffusion_coherence": coherence_breakdown(diffusion)["coherence_score"],
|
||||
"optimized_beats_naive_error": optimized_error <= naive_error,
|
||||
"optimized_beats_diffusion_error": optimized_error <= diffusion_error,
|
||||
"optimized_beats_naive_adjacency": optimized_adjacency <= naive_adjacency,
|
||||
"optimized_beats_diffusion_adjacency": optimized_adjacency <= diffusion_adjacency,
|
||||
"optimized_beats_naive_motif": optimized_motif <= naive_motif,
|
||||
"optimized_beats_diffusion_motif": optimized_motif <= diffusion_motif,
|
||||
}
|
||||
|
||||
|
||||
def _limit_worker_threads() -> None:
|
||||
try:
|
||||
import torch
|
||||
|
||||
torch.set_num_threads(1)
|
||||
torch.set_num_interop_threads(1)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _load_metadata(original_path: Path) -> dict[str, Any]:
|
||||
metadata_path = original_path.with_suffix(".json")
|
||||
if not metadata_path.exists():
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|||
action="store_true",
|
||||
help="Keep any existing files in the benchmark output directory instead of clearing it first",
|
||||
)
|
||||
benchmark_parser.add_argument("--workers", type=int, default=1, help="Parallel benchmark worker processes")
|
||||
tune_parser = subparsers.add_parser("tune", help="Run a black-box hyperparameter search on a benchmark slice.")
|
||||
tune_parser.add_argument("--corpus-dir", default="examples/corpus", help="Corpus root containing originals/")
|
||||
tune_parser.add_argument("--out-dir", default="artifacts/tuning", help="Directory for tuning outputs")
|
||||
|
|
@ -111,6 +112,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|||
action="store_true",
|
||||
help="Let the pipeline infer output size instead of locking it to the original ground-truth size",
|
||||
)
|
||||
tune_parser.add_argument("--workers", type=int, default=1, help="Parallel benchmark worker processes per trial")
|
||||
prepare_parser = subparsers.add_parser("prepare-corpus", help="Normalize imported corpus sheets into single sprites.")
|
||||
prepare_parser.add_argument("--corpus-dir", default="examples/corpus", help="Corpus root containing originals/")
|
||||
gui_parser = subparsers.add_parser("gui", help="Launch the web GUI.")
|
||||
|
|
@ -157,6 +159,7 @@ def main(argv: list[str] | None = None) -> int:
|
|||
include_cases=args.case,
|
||||
limit_cases=args.limit,
|
||||
keep_existing=args.keep_existing,
|
||||
workers=args.workers,
|
||||
)
|
||||
return 0
|
||||
if command == "tune":
|
||||
|
|
@ -172,6 +175,7 @@ def main(argv: list[str] | None = None) -> int:
|
|||
infer_size=args.infer_size,
|
||||
include_cases=args.case,
|
||||
limit_cases=args.limit,
|
||||
workers=args.workers,
|
||||
)
|
||||
return 0
|
||||
if command == "prepare-corpus":
|
||||
|
|
|
|||
|
|
@ -2,63 +2,12 @@ from __future__ import annotations
|
|||
|
||||
import numpy as np
|
||||
|
||||
from .metrics import isolated_pixel_rate, luminance
|
||||
from .types import CleanupArtifacts
|
||||
|
||||
|
||||
def _local_energy(patch: np.ndarray) -> float:
|
||||
center = patch[1, 1]
|
||||
neighbors = np.stack(
|
||||
[
|
||||
patch[1, 0],
|
||||
patch[1, 2],
|
||||
patch[0, 1],
|
||||
patch[2, 1],
|
||||
],
|
||||
axis=0,
|
||||
)
|
||||
dist = np.linalg.norm(neighbors - center[None, :], axis=-1)
|
||||
isolation = float(np.mean(dist))
|
||||
alpha = patch[..., 3]
|
||||
alpha_kink = float(np.mean(alpha * (1.0 - alpha)))
|
||||
return isolation + alpha_kink * 0.25
|
||||
|
||||
|
||||
def cleanup_pixels(rgba: np.ndarray, source_guidance: np.ndarray, iterations: int = 0) -> CleanupArtifacts:
|
||||
result = rgba.copy()
|
||||
height, width = result.shape[:2]
|
||||
heatmap = np.zeros((height, width), dtype=np.float32)
|
||||
if iterations <= 0:
|
||||
result = _snap_alpha(result)
|
||||
return CleanupArtifacts(cleaned_rgba=result, isolated_heatmap=heatmap)
|
||||
for _ in range(iterations):
|
||||
updated = result.copy()
|
||||
for y in range(1, height - 1):
|
||||
for x in range(1, width - 1):
|
||||
if source_guidance[y, x] > 0.55:
|
||||
continue
|
||||
patch = result[y - 1 : y + 2, x - 1 : x + 2]
|
||||
current_energy = _local_energy(patch)
|
||||
neighbors = [
|
||||
result[y, x],
|
||||
result[y - 1, x],
|
||||
result[y + 1, x],
|
||||
result[y, x - 1],
|
||||
result[y, x + 1],
|
||||
]
|
||||
best = result[y, x]
|
||||
best_energy = current_energy
|
||||
for candidate in neighbors[1:]:
|
||||
patch_candidate = patch.copy()
|
||||
patch_candidate[1, 1] = candidate
|
||||
energy = _local_energy(patch_candidate)
|
||||
if energy + 1e-6 < best_energy:
|
||||
best_energy = energy
|
||||
best = candidate
|
||||
updated[y, x] = best
|
||||
heatmap[y, x] = max(heatmap[y, x], current_energy - best_energy)
|
||||
result = np.clip(updated, 0.0, 1.0)
|
||||
result = _snap_alpha(result)
|
||||
def cleanup_pixels(rgba: np.ndarray) -> CleanupArtifacts:
|
||||
result = _snap_alpha(rgba)
|
||||
heatmap = np.zeros(result.shape[:2], dtype=np.float32)
|
||||
return CleanupArtifacts(cleaned_rgba=result, isolated_heatmap=heatmap)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -349,12 +349,12 @@ class GuiJob:
|
|||
"candidateCount": int(payload["candidate_count"]),
|
||||
"confidence": float(payload["confidence"]),
|
||||
}
|
||||
if event == "lattice_search_started":
|
||||
if event == "lattice_inference_started":
|
||||
return {
|
||||
"candidateCount": int(payload["candidate_count"]),
|
||||
"device": str(payload["device"]),
|
||||
}
|
||||
if event == "lattice_search_progress":
|
||||
if event == "lattice_inference_progress":
|
||||
return {
|
||||
"completedCandidates": int(payload["completed_candidates"]),
|
||||
"totalCandidates": int(payload["total_candidates"]),
|
||||
|
|
@ -410,7 +410,7 @@ class GuiJob:
|
|||
"cellX": float(payload["cell_x"]),
|
||||
"cellY": float(payload["cell_y"]),
|
||||
"latticeImage": _image_asset(_render_lattice_overlay(self.current_source_rgba, payload["uv0_px"])),
|
||||
"guidanceImage": _image_asset(_scalar_to_rgba(payload["guidance"], tint=(0.92, 0.58, 0.18))),
|
||||
"signalImage": _image_asset(_scalar_to_rgba(payload["signal"], tint=(0.92, 0.58, 0.18))),
|
||||
}
|
||||
if event in {"phase_field_initial", "phase_field_step", "phase_field_final"}:
|
||||
if self.current_source_rgba is None:
|
||||
|
|
@ -513,9 +513,9 @@ 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)
|
||||
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_candidate_rerank = True if config.hosted_demo else bool(skip_candidate_rerank)
|
||||
inference_mode = "autocorr" if fixed_dims is None else "fixed"
|
||||
max_inferred_target_size = config.max_output_dimension if fixed_dims is None else None
|
||||
normalized_skip_candidate_rerank = bool(skip_candidate_rerank)
|
||||
return {
|
||||
"target_size": normalized_target_size,
|
||||
"target_width": normalized_target_width,
|
||||
|
|
|
|||
|
|
@ -65,8 +65,8 @@ const eventTypes = [
|
|||
"stage_started",
|
||||
"source_loaded",
|
||||
"preprocess_completed",
|
||||
"lattice_search_started",
|
||||
"lattice_search_progress",
|
||||
"lattice_inference_started",
|
||||
"lattice_inference_progress",
|
||||
"inference_candidates_ready",
|
||||
"candidate_rerank_started",
|
||||
"candidate_rerank_candidate_started",
|
||||
|
|
@ -92,7 +92,7 @@ const state = {
|
|||
sourceImage: null,
|
||||
preprocessedImage: null,
|
||||
latticeImage: null,
|
||||
guidanceImage: null,
|
||||
signalImage: null,
|
||||
inference: null,
|
||||
inferenceMode: null,
|
||||
latticeSearch: null,
|
||||
|
|
@ -311,7 +311,7 @@ function applyRuntimeConfig(config) {
|
|||
}
|
||||
else {
|
||||
inputCopy.textContent = "Start with the ugly input.";
|
||||
runControlsCopy.textContent = "Pin what you know. Search what you do not.";
|
||||
runControlsCopy.textContent = "Pin what you know. Infer what you do not.";
|
||||
}
|
||||
}
|
||||
function resetRunArtifacts(options = {}) {
|
||||
|
|
@ -322,7 +322,7 @@ function resetRunArtifacts(options = {}) {
|
|||
}
|
||||
state.preprocessedImage = null;
|
||||
state.latticeImage = null;
|
||||
state.guidanceImage = null;
|
||||
state.signalImage = null;
|
||||
state.inference = null;
|
||||
state.inferenceMode = null;
|
||||
state.latticeSearch = null;
|
||||
|
|
@ -623,13 +623,13 @@ function resolveViewerPanels() {
|
|||
leftAsset = state.latticeImage;
|
||||
leftLabel = "Lattice Prep";
|
||||
}
|
||||
if (state.guidanceImage) {
|
||||
leftAsset = state.guidanceImage;
|
||||
leftLabel = "Guidance";
|
||||
if (state.signalImage) {
|
||||
leftAsset = state.signalImage;
|
||||
leftLabel = "Signal";
|
||||
}
|
||||
if (frame) {
|
||||
if (state.stageKey === "solver" && state.guidanceImage) {
|
||||
leftAsset = state.guidanceImage;
|
||||
if (state.stageKey === "solver" && state.signalImage) {
|
||||
leftAsset = state.signalImage;
|
||||
rightAsset = frame.samplingOverlayImage;
|
||||
leftLabel = "Signal";
|
||||
rightLabel = "Sampling Overlay";
|
||||
|
|
@ -1302,7 +1302,7 @@ async function handleEvent(eventName, payload) {
|
|||
state.preprocessedImage = payload.sourceImage;
|
||||
addLog("Preprocess", "Stripped edge-connected background noise.");
|
||||
break;
|
||||
case "lattice_search_started":
|
||||
case "lattice_inference_started":
|
||||
state.latticeSearch = {
|
||||
candidateCount: Number(payload.candidateCount ?? 0),
|
||||
completedCandidates: 0,
|
||||
|
|
@ -1311,10 +1311,10 @@ async function handleEvent(eventName, payload) {
|
|||
currentTargetHeight: null,
|
||||
bestScore: null,
|
||||
};
|
||||
setStage("inference", "Lattice search", `Testing ${state.latticeSearch.candidateCount} candidate sizes from canonical cell centers.`);
|
||||
addLog("Search", `Started lattice search across ${String(payload.candidateCount)} size candidates.`);
|
||||
setStage("inference", "Autocorr lattice", `Testing ${state.latticeSearch.candidateCount} autocorr candidate sizes from canonical cell centers.`);
|
||||
addLog("Inference", `Started autocorr lattice inference across ${String(payload.candidateCount)} size candidates.`);
|
||||
break;
|
||||
case "lattice_search_progress":
|
||||
case "lattice_inference_progress":
|
||||
if (!state.latticeSearch) {
|
||||
state.latticeSearch = {
|
||||
candidateCount: Number(payload.totalCandidates ?? 0),
|
||||
|
|
@ -1331,7 +1331,7 @@ async function handleEvent(eventName, payload) {
|
|||
state.latticeSearch.currentTargetHeight = Number(payload.targetHeight ?? 0);
|
||||
state.latticeSearch.bestScore =
|
||||
payload.bestScore === null || payload.bestScore === undefined ? state.latticeSearch.bestScore : Number(payload.bestScore);
|
||||
setStage("inference", "Lattice search", `Size ${state.latticeSearch.completedCandidates} / ${state.latticeSearch.candidateCount}: scored ${state.latticeSearch.currentTargetWidth} x ${state.latticeSearch.currentTargetHeight} from canonical cell centers.`);
|
||||
setStage("inference", "Autocorr lattice", `Size ${state.latticeSearch.completedCandidates} / ${state.latticeSearch.candidateCount}: scored ${state.latticeSearch.currentTargetWidth} x ${state.latticeSearch.currentTargetHeight} from canonical cell centers.`);
|
||||
break;
|
||||
case "inference_candidates_ready":
|
||||
state.inference = payload.inference;
|
||||
|
|
@ -1398,11 +1398,11 @@ async function handleEvent(eventName, payload) {
|
|||
addLog("Selection", "Committed to a lattice size.");
|
||||
break;
|
||||
case "analysis_completed":
|
||||
addLog("Scout", "Built the edge map that tells the solver where the floorboards creak.");
|
||||
addLog("Analysis", "Rendered the diagnostic edge preview.");
|
||||
break;
|
||||
case "phase_field_prepared":
|
||||
state.latticeImage = payload.latticeImage;
|
||||
state.guidanceImage = payload.guidanceImage;
|
||||
state.signalImage = payload.signalImage;
|
||||
state.phaseFieldPrep = {
|
||||
targetWidth: Number(payload.targetWidth ?? 0),
|
||||
targetHeight: Number(payload.targetHeight ?? 0),
|
||||
|
|
@ -1435,8 +1435,8 @@ async function handleEvent(eventName, payload) {
|
|||
case "cleanup_completed":
|
||||
state.cleanupImage = payload.cleanedImage;
|
||||
state.heatmapImage = payload.heatmapImage;
|
||||
setStage("cleanup", "Cleanup", "Sweeping isolated artifacts and smoothing the last stubborn junk.");
|
||||
addLog("Cleanup", "Ran the local cleanup sweep.");
|
||||
setStage("cleanup", "Cleanup", "Snapping alpha to the final discrete mask.");
|
||||
addLog("Cleanup", "Applied alpha snapping.");
|
||||
break;
|
||||
case "palette_completed":
|
||||
state.finalOutputImage = payload.outputImage;
|
||||
|
|
|
|||
|
|
@ -171,7 +171,7 @@
|
|||
<div class="card-head">
|
||||
<div class="copy-stack">
|
||||
<h2>Run Controls</h2>
|
||||
<p id="runControlsCopy" class="copy-tag">Pin what you know. Search what you do not.</p>
|
||||
<p id="runControlsCopy" class="copy-tag">Pin what you know. Infer what you do not.</p>
|
||||
</div>
|
||||
</div>
|
||||
<form id="controlsForm" class="controls-grid">
|
||||
|
|
@ -256,8 +256,8 @@
|
|||
class="help-tip"
|
||||
tabindex="0"
|
||||
role="note"
|
||||
title="Random seed used by the solver and any stochastic analysis bits. Reusing the same seed makes runs more repeatable."
|
||||
aria-label="Random seed used by the solver and any stochastic analysis bits. Reusing the same seed makes runs more repeatable."
|
||||
title="Random seed used by the solver tie-breaks. Reusing the same seed makes runs more repeatable."
|
||||
aria-label="Random seed used by the solver tie-breaks. Reusing the same seed makes runs more repeatable."
|
||||
>?</span
|
||||
>
|
||||
</span>
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
|
@ -40,95 +40,17 @@ def _resolve_device(torch, requested: str) -> str:
|
|||
return requested
|
||||
|
||||
|
||||
def _candidate_dims(
|
||||
width: int,
|
||||
height: int,
|
||||
target_size: int | None,
|
||||
*,
|
||||
hinted_sizes: list[int] | None = None,
|
||||
) -> list[tuple[int, int]]:
|
||||
if target_size is not None:
|
||||
if width >= height:
|
||||
return [(target_size, max(1, round(height * target_size / width)))]
|
||||
return [(max(1, round(width * target_size / height)), target_size)]
|
||||
max_dim = max(width, height)
|
||||
min_size = max(12, int(round(max_dim / 48)))
|
||||
capped_max_size = min(256, max(24, int(round(max_dim / 2.5))))
|
||||
step = 2 if capped_max_size <= 96 else 4
|
||||
size_values = set(range(min_size, capped_max_size + 1, step))
|
||||
if hinted_sizes:
|
||||
for hinted_size in hinted_sizes:
|
||||
for delta in range(-2, 3):
|
||||
candidate = int(hinted_size) + delta
|
||||
if min_size <= candidate <= max_dim:
|
||||
size_values.add(candidate)
|
||||
dims: list[tuple[int, int]] = []
|
||||
for size in sorted(size_values):
|
||||
if width >= height:
|
||||
dims.append((size, max(1, round(height * size / width))))
|
||||
else:
|
||||
dims.append((max(1, round(width * size / height)), size))
|
||||
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]
|
||||
major_size = max(1, int(target_size))
|
||||
if width >= height:
|
||||
return major_size, max(1, round(height * major_size / width))
|
||||
return max(1, round(width * major_size / height)), major_size
|
||||
|
||||
|
||||
def _autocorr_size_window(
|
||||
hinted_sizes: list[int],
|
||||
*,
|
||||
prior_reliability: float,
|
||||
) -> tuple[int, int] | None:
|
||||
if not hinted_sizes:
|
||||
return None
|
||||
center = int(round(float(np.median(np.asarray(hinted_sizes, dtype=np.float32)))))
|
||||
spread = max(abs(int(size) - center) for size in hinted_sizes)
|
||||
if spread > max(8, center // 2) and prior_reliability < 0.55:
|
||||
return None
|
||||
center_support = hinted_sizes.count(center)
|
||||
support_ratio = center_support / max(1, len(hinted_sizes))
|
||||
nearest_other = min((abs(int(size) - center) for size in hinted_sizes if int(size) != center), default=0)
|
||||
if spread == 0:
|
||||
radius = 0 if prior_reliability >= 0.82 else 1
|
||||
elif support_ratio >= 0.5 and prior_reliability >= 0.82:
|
||||
radius = 1 if nearest_other <= 2 else 2
|
||||
elif spread <= 2 and prior_reliability >= 0.72:
|
||||
radius = 1
|
||||
elif spread <= 4 and prior_reliability >= 0.55:
|
||||
radius = 2
|
||||
else:
|
||||
radius = max(3, min(12, spread + 2))
|
||||
return center, radius
|
||||
|
||||
|
||||
def _resolve_candidate_dims_from_autocorr(
|
||||
width: int,
|
||||
height: int,
|
||||
target_size: int | None,
|
||||
*,
|
||||
hinted_sizes: list[int],
|
||||
prior_reliability: float,
|
||||
) -> list[tuple[int, int]]:
|
||||
dims = _candidate_dims(width, height, target_size, hinted_sizes=hinted_sizes)
|
||||
size_window = _autocorr_size_window(
|
||||
hinted_sizes,
|
||||
prior_reliability=prior_reliability,
|
||||
)
|
||||
if size_window is None:
|
||||
if target_size is None and hinted_sizes:
|
||||
hinted_dims = _candidate_dims_from_autocorr_hints(
|
||||
width,
|
||||
height,
|
||||
hinted_sizes=hinted_sizes,
|
||||
)
|
||||
if hinted_dims:
|
||||
return hinted_dims
|
||||
return dims
|
||||
center, radius = size_window
|
||||
size_index = 0 if width >= height else 1
|
||||
narrowed = [dim for dim in dims if abs(dim[size_index] - center) <= radius]
|
||||
return narrowed or dims
|
||||
|
||||
|
||||
def _candidate_dims_from_autocorr_hints(
|
||||
|
|
@ -186,10 +108,6 @@ def _estimate_cell_size_details(profile: np.ndarray) -> AutocorrEstimate:
|
|||
)
|
||||
|
||||
|
||||
def _estimate_cell_size(profile: np.ndarray) -> float:
|
||||
return _estimate_cell_size_details(profile).best_lag
|
||||
|
||||
|
||||
def _edge_profiles(rgba: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
||||
alpha = rgba[..., 3]
|
||||
luminance = rgba[..., 0] * 0.2126 + rgba[..., 1] * 0.7152 + rgba[..., 2] * 0.0722
|
||||
|
|
@ -553,77 +471,6 @@ def _score_size_candidate(
|
|||
return candidates
|
||||
|
||||
|
||||
def infer_lattice(
|
||||
rgba: np.ndarray,
|
||||
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,
|
||||
)
|
||||
candidate_dims = _resolve_candidate_dims_from_autocorr(
|
||||
width,
|
||||
height,
|
||||
target_size,
|
||||
hinted_sizes=hinted_sizes,
|
||||
prior_reliability=prior_reliability,
|
||||
)
|
||||
|
||||
emit_observer(
|
||||
observer,
|
||||
"lattice_search_started",
|
||||
candidate_count=len(candidate_dims),
|
||||
device=resolved_device,
|
||||
)
|
||||
|
||||
candidates: list[InferenceCandidate] = []
|
||||
for candidate_index, (target_width, target_height) in enumerate(candidate_dims, start=1):
|
||||
check_observer_cancelled(observer)
|
||||
scored_group = _score_size_candidate(
|
||||
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,
|
||||
device=resolved_device,
|
||||
)
|
||||
candidates.extend(scored_group)
|
||||
emit_observer(
|
||||
observer,
|
||||
"lattice_search_progress",
|
||||
completed_candidates=candidate_index,
|
||||
total_candidates=len(candidate_dims),
|
||||
target_width=int(target_width),
|
||||
target_height=int(target_height),
|
||||
best_score=None if not scored_group else float(max(candidate.score for candidate in scored_group)),
|
||||
)
|
||||
|
||||
candidates.sort(key=lambda item: item.score, reverse=True)
|
||||
size_candidates = _top_candidates_by_size(candidates, limit=len(candidates))
|
||||
reranked_candidates = _rerank_size_candidates_with_source_evidence(rgba, size_candidates)
|
||||
best = reranked_candidates[0]
|
||||
second = reranked_candidates[1] if len(reranked_candidates) > 1 else best
|
||||
confidence = max(0.0, best.score - second.score)
|
||||
top_candidates = reranked_candidates[:8]
|
||||
return InferenceResult(
|
||||
target_width=best.target_width,
|
||||
target_height=best.target_height,
|
||||
confidence=confidence,
|
||||
top_candidates=top_candidates,
|
||||
)
|
||||
|
||||
|
||||
def infer_autocorr_lattice(
|
||||
|
|
@ -654,7 +501,7 @@ def infer_autocorr_lattice(
|
|||
|
||||
emit_observer(
|
||||
observer,
|
||||
"lattice_search_started",
|
||||
"lattice_inference_started",
|
||||
candidate_count=len(candidate_dims),
|
||||
device=resolved_device,
|
||||
)
|
||||
|
|
@ -674,7 +521,7 @@ def infer_autocorr_lattice(
|
|||
candidates.extend(scored_group)
|
||||
emit_observer(
|
||||
observer,
|
||||
"lattice_search_progress",
|
||||
"lattice_inference_progress",
|
||||
completed_candidates=candidate_index,
|
||||
total_candidates=len(candidate_dims),
|
||||
target_width=int(target_width),
|
||||
|
|
|
|||
|
|
@ -8,48 +8,20 @@ from pathlib import Path
|
|||
|
||||
@dataclass(slots=True)
|
||||
class SolverHyperParams:
|
||||
phase_field_patch_extent: float = 0.18
|
||||
phase_field_data_coherence_weight: float = 1.00
|
||||
phase_field_data_edge_weight: float = 0.35
|
||||
phase_field_smoothness_weight: float = 0.0
|
||||
phase_field_grid_alignment_weight: float = 0.90
|
||||
phase_field_edge_gate_strength: float = 6.0
|
||||
phase_field_collapse_weight: float = 0.0
|
||||
phase_field_min_spacing_ratio: float = 0.0
|
||||
phase_field_magnitude_weight: float = 0.0
|
||||
phase_field_learning_rate: float = 0.10
|
||||
phase_field_max_displacement_ratio: float = 2.50
|
||||
phase_field_local_search_radius_ratio: float = 0.80
|
||||
phase_field_local_search_blend: float = 0.36
|
||||
phase_field_local_search_move_weight: float = 0.04
|
||||
phase_field_signal_weight: float = 1.00
|
||||
phase_field_signal_inner_radius_ratio: float = 0.24
|
||||
phase_field_signal_outer_radius_ratio: float = 0.56
|
||||
phase_field_signal_ring_contrast_weight: float = 1.00
|
||||
phase_field_signal_inner_variance_weight: float = 1.00
|
||||
phase_field_signal_center_edge_weight: float = 0.12
|
||||
phase_field_signal_edge_threshold: float = 0.16
|
||||
phase_field_signal_detail_radius_ratio: float = 0.80
|
||||
phase_field_signal_flatness_radius_ratio: float = 0.24
|
||||
phase_field_signal_distance_power: float = 0.70
|
||||
phase_field_signal_pyramid_levels: int = 5
|
||||
phase_field_signal_boundary_weight: float = 1.00
|
||||
phase_field_signal_level_decay: float = 0.50
|
||||
phase_field_signal_gradient_weight: float = 0.25
|
||||
phase_field_signal_curvature_weight: float = 0.65
|
||||
phase_field_signal_self_penalty: float = 3.00
|
||||
phase_field_signal_context_power: float = 0.70
|
||||
phase_field_signal_energy_power: float = 1.00
|
||||
phase_field_signal_peak_power: float = 2.00
|
||||
phase_field_local_search_feature_weight: float = 0.0
|
||||
phase_field_local_coherence_small_radius_ratio: float = 0.24
|
||||
phase_field_local_coherence_medium_radius_ratio: float = 0.46
|
||||
phase_field_local_coherence_small_weight: float = 1.00
|
||||
phase_field_local_coherence_medium_weight: float = 0.0
|
||||
phase_field_local_cell_contrast_weight: float = 1.20
|
||||
phase_field_local_search_grid_weight: float = 0.0
|
||||
phase_field_local_search_interval: int = 1
|
||||
candidate_rerank_preview_steps: int = 8
|
||||
source_edge_detail_threshold: float = 0.25
|
||||
alpha_opaque_threshold: float = 0.95
|
||||
alpha_transparent_threshold: float = 0.05
|
||||
candidate_rerank_support_weight: float = 0.45
|
||||
candidate_rerank_edge_position_weight: float = 0.20
|
||||
candidate_rerank_wobble_weight: float = 0.20
|
||||
|
|
|
|||
|
|
@ -6,48 +6,17 @@ import numpy as np
|
|||
|
||||
from .io import premultiply
|
||||
from .observe import PipelineObserver, check_observer_cancelled, emit_observer, observer_attribute
|
||||
from .types import InferenceResult, PhaseFieldSourceAnalysis, SolverArtifacts
|
||||
from .types import InferenceResult, SolverArtifacts
|
||||
from .params import SolverHyperParams
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _PhaseFieldPrep:
|
||||
source_t: object
|
||||
edge_t: object
|
||||
uv0_px_t: object
|
||||
uv0_norm: np.ndarray
|
||||
base_x_t: object
|
||||
base_y_t: object
|
||||
guidance: np.ndarray
|
||||
cell_x: float
|
||||
cell_y: float
|
||||
patch_offsets_t: object
|
||||
feature_t: object
|
||||
signal: np.ndarray
|
||||
width: int
|
||||
height: int
|
||||
|
||||
def _require_torch():
|
||||
try:
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
except ImportError as exc: # pragma: no cover - exercised only when torch missing
|
||||
raise RuntimeError(
|
||||
"PyTorch is required for the phase-field optimization stage. Install project dependencies first."
|
||||
) from exc
|
||||
return torch, F
|
||||
|
||||
|
||||
def _resolve_device(torch, requested: str) -> str:
|
||||
if requested == "auto":
|
||||
return "cuda" if torch.cuda.is_available() else "cpu"
|
||||
if requested == "cuda" and not torch.cuda.is_available():
|
||||
raise RuntimeError(
|
||||
"CUDA was requested, but this PyTorch build does not have a usable CUDA device. "
|
||||
"Install a CUDA-enabled PyTorch build or use --device cpu."
|
||||
)
|
||||
return requested
|
||||
|
||||
|
||||
def _make_regular_uv_px(
|
||||
*,
|
||||
height: int,
|
||||
|
|
@ -65,58 +34,6 @@ def _make_regular_uv_px(
|
|||
return np.stack([grid_x, grid_y], axis=-1).astype(np.float32)
|
||||
|
||||
|
||||
def _pixel_to_normalized(coords_px, *, width: int, height: int):
|
||||
coords = coords_px.clone()
|
||||
coords[..., 0] = (coords[..., 0] / max(1.0, float(width - 1))) * 2.0 - 1.0
|
||||
coords[..., 1] = (coords[..., 1] / max(1.0, float(height - 1))) * 2.0 - 1.0
|
||||
return coords
|
||||
|
||||
|
||||
def _sample_rgba(F, source_t, coords_px, *, width: int, height: int):
|
||||
grid = _pixel_to_normalized(coords_px, width=width, height=height).clamp(-1.0, 1.0)
|
||||
sampled = F.grid_sample(source_t, grid, align_corners=True, mode="bilinear", padding_mode="border")
|
||||
return sampled.permute(0, 2, 3, 1)
|
||||
|
||||
|
||||
def _sample_scalar(F, scalar_t, coords_px, *, width: int, height: int):
|
||||
grid = _pixel_to_normalized(coords_px, width=width, height=height).clamp(-1.0, 1.0)
|
||||
sampled = F.grid_sample(scalar_t, grid, align_corners=True, mode="bilinear", padding_mode="border")
|
||||
return sampled[:, 0]
|
||||
|
||||
|
||||
def _sample_patch_rgba(F, source_t, coords_px, offsets_t, *, width: int, height: int):
|
||||
patch_px = coords_px[:, :, :, None, :] + offsets_t[None, None, None, :, :]
|
||||
patch_px = patch_px.new_empty((*patch_px.shape[:-1], 2))
|
||||
patch_px[..., 0] = (coords_px[:, :, :, None, 0] + offsets_t[None, None, None, :, 0]).clamp(
|
||||
0.0, max(0.0, float(width - 1))
|
||||
)
|
||||
patch_px[..., 1] = (coords_px[:, :, :, None, 1] + offsets_t[None, None, None, :, 1]).clamp(
|
||||
0.0, max(0.0, float(height - 1))
|
||||
)
|
||||
patch_grid = _pixel_to_normalized(patch_px, width=width, height=height).clamp(-1.0, 1.0)
|
||||
batch, out_h, out_w, samples, _ = patch_grid.shape
|
||||
flattened = patch_grid.reshape(batch, out_h, out_w * samples, 2)
|
||||
sampled = F.grid_sample(source_t, flattened, align_corners=True, mode="bilinear", padding_mode="border")
|
||||
sampled = sampled.permute(0, 2, 3, 1)
|
||||
return sampled.reshape(batch, out_h, out_w, samples, sampled.shape[-1])
|
||||
|
||||
|
||||
def _sample_patch_scalar(F, scalar_t, coords_px, offsets_t, *, width: int, height: int):
|
||||
patch_px = coords_px[:, :, :, None, :] + offsets_t[None, None, None, :, :]
|
||||
patch_px = patch_px.new_empty((*patch_px.shape[:-1], 2))
|
||||
patch_px[..., 0] = (coords_px[:, :, :, None, 0] + offsets_t[None, None, None, :, 0]).clamp(
|
||||
0.0, max(0.0, float(width - 1))
|
||||
)
|
||||
patch_px[..., 1] = (coords_px[:, :, :, None, 1] + offsets_t[None, None, None, :, 1]).clamp(
|
||||
0.0, max(0.0, float(height - 1))
|
||||
)
|
||||
patch_grid = _pixel_to_normalized(patch_px, width=width, height=height).clamp(-1.0, 1.0)
|
||||
batch, out_h, out_w, samples, _ = patch_grid.shape
|
||||
flattened = patch_grid.reshape(batch, out_h, out_w * samples, 2)
|
||||
sampled = F.grid_sample(scalar_t, flattened, align_corners=True, mode="bilinear", padding_mode="border")
|
||||
sampled = sampled[:, 0]
|
||||
return sampled.reshape(batch, out_h, out_w, samples)
|
||||
|
||||
def _displacement_diagnostics(uv_field: np.ndarray, selected_x, selected_y, *, width: int, height: int) -> dict[str, np.ndarray | float]:
|
||||
uv_x = (uv_field[..., 0] + 1.0) * 0.5 * max(1.0, float(width - 1))
|
||||
uv_y = (uv_field[..., 1] + 1.0) * 0.5 * max(1.0, float(height - 1))
|
||||
|
|
@ -164,355 +81,6 @@ def _displacement_diagnostics(uv_field: np.ndarray, selected_x, selected_y, *, w
|
|||
}
|
||||
|
||||
|
||||
def _project_displacements_in_place(
|
||||
torch,
|
||||
disp_t,
|
||||
base_x_t,
|
||||
base_y_t,
|
||||
*,
|
||||
min_dx: float,
|
||||
min_dy: float,
|
||||
max_dx: float,
|
||||
max_dy: float,
|
||||
width: int,
|
||||
height: int,
|
||||
) -> None:
|
||||
with torch.no_grad():
|
||||
pos_x = (base_x_t + disp_t[..., 0]).clamp(0.0, max(0.0, float(width - 1)))
|
||||
pos_y = (base_y_t + disp_t[..., 1]).clamp(0.0, max(0.0, float(height - 1)))
|
||||
|
||||
for index in range(1, pos_x.shape[2]):
|
||||
pos_x[:, :, index] = torch.maximum(pos_x[:, :, index], pos_x[:, :, index - 1] + min_dx)
|
||||
for index in range(pos_x.shape[2] - 2, -1, -1):
|
||||
pos_x[:, :, index] = torch.minimum(pos_x[:, :, index], pos_x[:, :, index + 1] - min_dx)
|
||||
for index in range(1, pos_y.shape[1]):
|
||||
pos_y[:, index, :] = torch.maximum(pos_y[:, index, :], pos_y[:, index - 1, :] + min_dy)
|
||||
for index in range(pos_y.shape[1] - 2, -1, -1):
|
||||
pos_y[:, index, :] = torch.minimum(pos_y[:, index, :], pos_y[:, index + 1, :] - min_dy)
|
||||
|
||||
pos_x = pos_x.clamp(0.0, max(0.0, float(width - 1)))
|
||||
pos_y = pos_y.clamp(0.0, max(0.0, float(height - 1)))
|
||||
disp_t[..., 0].copy_((pos_x - base_x_t).clamp(-max_dx, max_dx))
|
||||
disp_t[..., 1].copy_((pos_y - base_y_t).clamp(-max_dy, max_dy))
|
||||
|
||||
|
||||
def _prepare_phase_field(
|
||||
torch,
|
||||
F,
|
||||
rgba: np.ndarray,
|
||||
inference: InferenceResult,
|
||||
analysis: PhaseFieldSourceAnalysis,
|
||||
solver_params: SolverHyperParams,
|
||||
*,
|
||||
device: str,
|
||||
) -> _PhaseFieldPrep:
|
||||
source = premultiply(rgba)
|
||||
height, width = source.shape[:2]
|
||||
cell_x = width / max(1, inference.target_width)
|
||||
cell_y = height / max(1, inference.target_height)
|
||||
source_t = torch.from_numpy(source.transpose(2, 0, 1)[None, ...]).to(device=device, dtype=torch.float32)
|
||||
edge_t = torch.from_numpy(analysis.edge_map[None, None, ...]).to(device=device, dtype=torch.float32)
|
||||
luma = (
|
||||
source[..., 0] * np.float32(0.2126)
|
||||
+ source[..., 1] * np.float32(0.7152)
|
||||
+ source[..., 2] * np.float32(0.0722)
|
||||
).astype(np.float32)
|
||||
padded_luma = np.pad(luma, 1, mode="edge")
|
||||
local_mean = (
|
||||
padded_luma[:-2, :-2]
|
||||
+ padded_luma[:-2, 1:-1]
|
||||
+ padded_luma[:-2, 2:]
|
||||
+ padded_luma[1:-1, :-2]
|
||||
+ padded_luma[1:-1, 1:-1]
|
||||
+ padded_luma[1:-1, 2:]
|
||||
+ padded_luma[2:, :-2]
|
||||
+ padded_luma[2:, 1:-1]
|
||||
+ padded_luma[2:, 2:]
|
||||
) / np.float32(9.0)
|
||||
feature = np.abs(luma - local_mean).astype(np.float32)
|
||||
feature_t = torch.from_numpy(feature[None, None, ...]).to(device=device, dtype=torch.float32)
|
||||
uv0_px = _make_regular_uv_px(
|
||||
height=height,
|
||||
width=width,
|
||||
target_height=inference.target_height,
|
||||
target_width=inference.target_width,
|
||||
)
|
||||
uv0_px_t = torch.from_numpy(uv0_px[None, ...]).to(device=device, dtype=torch.float32)
|
||||
base_x_t = uv0_px_t[..., 0]
|
||||
base_y_t = uv0_px_t[..., 1]
|
||||
uv0_norm = uv0_px.copy()
|
||||
uv0_norm[..., 0] = (uv0_norm[..., 0] / max(1.0, float(width - 1))) * 2.0 - 1.0
|
||||
uv0_norm[..., 1] = (uv0_norm[..., 1] / max(1.0, float(height - 1))) * 2.0 - 1.0
|
||||
offsets = np.asarray(
|
||||
[
|
||||
[0.0, 0.0],
|
||||
[-solver_params.phase_field_patch_extent * cell_x, 0.0],
|
||||
[solver_params.phase_field_patch_extent * cell_x, 0.0],
|
||||
[0.0, -solver_params.phase_field_patch_extent * cell_y],
|
||||
[0.0, solver_params.phase_field_patch_extent * cell_y],
|
||||
[-solver_params.phase_field_patch_extent * cell_x, -solver_params.phase_field_patch_extent * cell_y],
|
||||
[solver_params.phase_field_patch_extent * cell_x, -solver_params.phase_field_patch_extent * cell_y],
|
||||
[-solver_params.phase_field_patch_extent * cell_x, solver_params.phase_field_patch_extent * cell_y],
|
||||
[solver_params.phase_field_patch_extent * cell_x, solver_params.phase_field_patch_extent * cell_y],
|
||||
],
|
||||
dtype=np.float32,
|
||||
)
|
||||
patch_offsets_t = torch.from_numpy(offsets).to(device=device, dtype=torch.float32)
|
||||
guide_small = F.interpolate(
|
||||
edge_t,
|
||||
size=(inference.target_height, inference.target_width),
|
||||
mode="bilinear",
|
||||
align_corners=True,
|
||||
)
|
||||
return _PhaseFieldPrep(
|
||||
source_t=source_t,
|
||||
edge_t=edge_t,
|
||||
uv0_px_t=uv0_px_t,
|
||||
uv0_norm=uv0_norm,
|
||||
base_x_t=base_x_t,
|
||||
base_y_t=base_y_t,
|
||||
guidance=guide_small[0, 0].detach().cpu().numpy().astype(np.float32),
|
||||
cell_x=cell_x,
|
||||
cell_y=cell_y,
|
||||
patch_offsets_t=patch_offsets_t,
|
||||
feature_t=feature_t,
|
||||
width=width,
|
||||
height=height,
|
||||
)
|
||||
|
||||
|
||||
def _phase_field_loss(torch, F, prep: _PhaseFieldPrep, disp_t, solver_params: SolverHyperParams):
|
||||
pos_px = prep.uv0_px_t + disp_t
|
||||
sampled_rgba = _sample_rgba(F, prep.source_t, pos_px, width=prep.width, height=prep.height)
|
||||
patch_rgba = _sample_patch_rgba(
|
||||
F,
|
||||
prep.source_t,
|
||||
pos_px,
|
||||
prep.patch_offsets_t,
|
||||
width=prep.width,
|
||||
height=prep.height,
|
||||
)
|
||||
patch_edge = _sample_patch_scalar(
|
||||
F,
|
||||
prep.edge_t,
|
||||
pos_px,
|
||||
prep.patch_offsets_t,
|
||||
width=prep.width,
|
||||
height=prep.height,
|
||||
)
|
||||
|
||||
center_rgba = patch_rgba[..., 0, :]
|
||||
neighbor_rgba = patch_rgba[..., 1:, :]
|
||||
local_coherence = (neighbor_rgba - center_rgba[..., None, :]).abs().mean()
|
||||
local_edge = patch_edge.mean()
|
||||
|
||||
disp_norm = disp_t.clone()
|
||||
disp_norm[..., 0] = disp_norm[..., 0] / max(prep.cell_x, 1e-4)
|
||||
disp_norm[..., 1] = disp_norm[..., 1] / max(prep.cell_y, 1e-4)
|
||||
pos_mid_x = (pos_px[:, :, 1:, :] + pos_px[:, :, :-1, :]) * 0.5 if pos_px.shape[2] > 1 else None
|
||||
pos_mid_y = (pos_px[:, 1:, :, :] + pos_px[:, :-1, :, :]) * 0.5 if pos_px.shape[1] > 1 else None
|
||||
|
||||
smoothness = sampled_rgba.new_tensor(0.0)
|
||||
if pos_mid_x is not None:
|
||||
edge_x = _sample_scalar(F, prep.edge_t, pos_mid_x, width=prep.width, height=prep.height)
|
||||
weight_x = torch.exp(-solver_params.phase_field_edge_gate_strength * edge_x)
|
||||
delta_x = disp_norm[:, :, 1:, :] - disp_norm[:, :, :-1, :]
|
||||
smoothness = smoothness + (weight_x * torch.sqrt(delta_x.square().sum(dim=-1) + 1e-6)).mean()
|
||||
if pos_mid_y is not None:
|
||||
edge_y = _sample_scalar(F, prep.edge_t, pos_mid_y, width=prep.width, height=prep.height)
|
||||
weight_y = torch.exp(-solver_params.phase_field_edge_gate_strength * edge_y)
|
||||
delta_y = disp_norm[:, 1:, :, :] - disp_norm[:, :-1, :, :]
|
||||
smoothness = smoothness + (weight_y * torch.sqrt(delta_y.square().sum(dim=-1) + 1e-6)).mean()
|
||||
|
||||
grid_alignment = sampled_rgba.new_tensor(0.0)
|
||||
if pos_px.shape[2] > 1:
|
||||
step_x = (pos_px[:, :, 1:, 0] - pos_px[:, :, :-1, 0]) / max(prep.cell_x, 1e-4)
|
||||
row_y_delta = (pos_px[:, :, 1:, 1] - pos_px[:, :, :-1, 1]) / max(prep.cell_y, 1e-4)
|
||||
grid_alignment = grid_alignment + (step_x - 1.0).square().mean() + row_y_delta.square().mean()
|
||||
if pos_px.shape[1] > 1:
|
||||
step_y = (pos_px[:, 1:, :, 1] - pos_px[:, :-1, :, 1]) / max(prep.cell_y, 1e-4)
|
||||
col_x_delta = (pos_px[:, 1:, :, 0] - pos_px[:, :-1, :, 0]) / max(prep.cell_x, 1e-4)
|
||||
grid_alignment = grid_alignment + (step_y - 1.0).square().mean() + col_x_delta.square().mean()
|
||||
|
||||
collapse = sampled_rgba.new_tensor(0.0)
|
||||
min_dx = solver_params.phase_field_min_spacing_ratio * prep.cell_x
|
||||
min_dy = solver_params.phase_field_min_spacing_ratio * prep.cell_y
|
||||
if pos_px.shape[2] > 1:
|
||||
step_x = pos_px[:, :, 1:, 0] - pos_px[:, :, :-1, 0]
|
||||
collapse = collapse + torch.relu(min_dx - step_x).square().mean()
|
||||
if pos_px.shape[1] > 1:
|
||||
step_y = pos_px[:, 1:, :, 1] - pos_px[:, :-1, :, 1]
|
||||
collapse = collapse + torch.relu(min_dy - step_y).square().mean()
|
||||
|
||||
magnitude = (
|
||||
(disp_t[..., 0] / max(prep.cell_x, 1e-4)).square()
|
||||
+ (disp_t[..., 1] / max(prep.cell_y, 1e-4)).square()
|
||||
).mean()
|
||||
|
||||
loss = (
|
||||
local_coherence * solver_params.phase_field_data_coherence_weight
|
||||
+ local_edge * solver_params.phase_field_data_edge_weight
|
||||
+ smoothness * solver_params.phase_field_smoothness_weight
|
||||
+ grid_alignment * solver_params.phase_field_grid_alignment_weight
|
||||
+ collapse * solver_params.phase_field_collapse_weight
|
||||
+ magnitude * solver_params.phase_field_magnitude_weight
|
||||
)
|
||||
terms = {
|
||||
"local_coherence": local_coherence.detach(),
|
||||
"local_edge": local_edge.detach(),
|
||||
"smoothness": smoothness.detach(),
|
||||
"grid_alignment": grid_alignment.detach(),
|
||||
"collapse": collapse.detach(),
|
||||
"magnitude": magnitude.detach(),
|
||||
}
|
||||
return loss, sampled_rgba, terms
|
||||
|
||||
|
||||
def _local_candidate_displacement_step_in_place(
|
||||
torch,
|
||||
F,
|
||||
prep: _PhaseFieldPrep,
|
||||
disp_t,
|
||||
solver_params: SolverHyperParams,
|
||||
*,
|
||||
min_dx: float,
|
||||
min_dy: float,
|
||||
max_dx: float,
|
||||
max_dy: float,
|
||||
) -> None:
|
||||
radius_ratio = float(solver_params.phase_field_local_search_radius_ratio)
|
||||
blend = float(solver_params.phase_field_local_search_blend)
|
||||
if radius_ratio <= 0.0 or blend <= 0.0:
|
||||
return
|
||||
|
||||
with torch.no_grad():
|
||||
device = disp_t.device
|
||||
steps = torch.linspace(-1.0, 1.0, 5, device=device, dtype=disp_t.dtype)
|
||||
offset_y, offset_x = torch.meshgrid(steps, steps, indexing="ij")
|
||||
offsets_t = torch.stack(
|
||||
[
|
||||
offset_x.reshape(-1) * (radius_ratio * prep.cell_x),
|
||||
offset_y.reshape(-1) * (radius_ratio * prep.cell_y),
|
||||
],
|
||||
dim=-1,
|
||||
)
|
||||
|
||||
pos_px = prep.uv0_px_t + disp_t
|
||||
candidate_pos = pos_px[:, :, :, None, :] + offsets_t[None, None, None, :, :]
|
||||
candidate_pos = candidate_pos.clone()
|
||||
candidate_pos[..., 0] = candidate_pos[..., 0].clamp(0.0, max(0.0, float(prep.width - 1)))
|
||||
candidate_pos[..., 1] = candidate_pos[..., 1].clamp(0.0, max(0.0, float(prep.height - 1)))
|
||||
|
||||
batch, out_h, out_w, candidates, _ = candidate_pos.shape
|
||||
flat_pos = candidate_pos.reshape(batch, out_h, out_w * candidates, 2)
|
||||
patch_rgba = _sample_patch_rgba(
|
||||
F,
|
||||
prep.source_t,
|
||||
flat_pos,
|
||||
prep.patch_offsets_t,
|
||||
width=prep.width,
|
||||
height=prep.height,
|
||||
).reshape(batch, out_h, out_w, candidates, -1, 4)
|
||||
patch_edge = _sample_patch_scalar(
|
||||
F,
|
||||
prep.edge_t,
|
||||
flat_pos,
|
||||
prep.patch_offsets_t,
|
||||
width=prep.width,
|
||||
height=prep.height,
|
||||
).reshape(batch, out_h, out_w, candidates, -1)
|
||||
candidate_feature = _sample_scalar(
|
||||
F,
|
||||
prep.feature_t,
|
||||
flat_pos,
|
||||
width=prep.width,
|
||||
height=prep.height,
|
||||
).reshape(batch, out_h, out_w, candidates)
|
||||
|
||||
center_rgba = patch_rgba[..., 0, :]
|
||||
neighbor_rgba = patch_rgba[..., 1:, :]
|
||||
coherence_score = (neighbor_rgba - center_rgba[..., None, :]).abs().mean(dim=(-1, -2))
|
||||
edge_score = patch_edge.mean(dim=-1)
|
||||
luma_weights = torch.tensor([0.2126, 0.7152, 0.0722], device=device, dtype=disp_t.dtype)
|
||||
center_luma = (center_rgba[..., :3] * luma_weights).sum(dim=-1)
|
||||
local_luma_mean = center_luma.mean(dim=-1, keepdim=True)
|
||||
feature_score = (center_luma - local_luma_mean).abs() + candidate_feature
|
||||
current_pos = pos_px.detach()
|
||||
grid_score = torch.zeros_like(coherence_score)
|
||||
grid_neighbors = torch.zeros_like(coherence_score)
|
||||
if out_w > 1:
|
||||
left_x = torch.empty_like(current_pos[..., 0])
|
||||
left_y = torch.empty_like(current_pos[..., 1])
|
||||
right_x = torch.empty_like(current_pos[..., 0])
|
||||
right_y = torch.empty_like(current_pos[..., 1])
|
||||
left_x[:, :, 0] = current_pos[:, :, 0, 0] - prep.cell_x
|
||||
left_x[:, :, 1:] = current_pos[:, :, :-1, 0]
|
||||
left_y[:, :, 0] = current_pos[:, :, 0, 1]
|
||||
left_y[:, :, 1:] = current_pos[:, :, :-1, 1]
|
||||
right_x[:, :, -1] = current_pos[:, :, -1, 0] + prep.cell_x
|
||||
right_x[:, :, :-1] = current_pos[:, :, 1:, 0]
|
||||
right_y[:, :, -1] = current_pos[:, :, -1, 1]
|
||||
right_y[:, :, :-1] = current_pos[:, :, 1:, 1]
|
||||
candidate_x = candidate_pos[..., 0]
|
||||
candidate_y = candidate_pos[..., 1]
|
||||
grid_score = grid_score + ((candidate_x - left_x[..., None] - prep.cell_x) / max(prep.cell_x, 1e-4)).square()
|
||||
grid_score = grid_score + ((right_x[..., None] - candidate_x - prep.cell_x) / max(prep.cell_x, 1e-4)).square()
|
||||
grid_score = grid_score + ((candidate_y - left_y[..., None]) / max(prep.cell_y, 1e-4)).square()
|
||||
grid_score = grid_score + ((candidate_y - right_y[..., None]) / max(prep.cell_y, 1e-4)).square()
|
||||
grid_neighbors = grid_neighbors + 4.0
|
||||
if out_h > 1:
|
||||
up_x = torch.empty_like(current_pos[..., 0])
|
||||
up_y = torch.empty_like(current_pos[..., 1])
|
||||
down_x = torch.empty_like(current_pos[..., 0])
|
||||
down_y = torch.empty_like(current_pos[..., 1])
|
||||
up_x[:, 0, :] = current_pos[:, 0, :, 0]
|
||||
up_x[:, 1:, :] = current_pos[:, :-1, :, 0]
|
||||
up_y[:, 0, :] = current_pos[:, 0, :, 1] - prep.cell_y
|
||||
up_y[:, 1:, :] = current_pos[:, :-1, :, 1]
|
||||
down_x[:, -1, :] = current_pos[:, -1, :, 0]
|
||||
down_x[:, :-1, :] = current_pos[:, 1:, :, 0]
|
||||
down_y[:, -1, :] = current_pos[:, -1, :, 1] + prep.cell_y
|
||||
down_y[:, :-1, :] = current_pos[:, 1:, :, 1]
|
||||
candidate_x = candidate_pos[..., 0]
|
||||
candidate_y = candidate_pos[..., 1]
|
||||
grid_score = grid_score + ((candidate_x - up_x[..., None]) / max(prep.cell_x, 1e-4)).square()
|
||||
grid_score = grid_score + ((candidate_x - down_x[..., None]) / max(prep.cell_x, 1e-4)).square()
|
||||
grid_score = grid_score + ((candidate_y - up_y[..., None] - prep.cell_y) / max(prep.cell_y, 1e-4)).square()
|
||||
grid_score = grid_score + ((down_y[..., None] - candidate_y - prep.cell_y) / max(prep.cell_y, 1e-4)).square()
|
||||
grid_neighbors = grid_neighbors + 4.0
|
||||
grid_score = grid_score / grid_neighbors.clamp_min(1.0)
|
||||
move_score = (
|
||||
(offsets_t[:, 0] / max(prep.cell_x, 1e-4)).square()
|
||||
+ (offsets_t[:, 1] / max(prep.cell_y, 1e-4)).square()
|
||||
)
|
||||
score = (
|
||||
coherence_score * solver_params.phase_field_data_coherence_weight
|
||||
+ edge_score * solver_params.phase_field_data_edge_weight
|
||||
+ move_score[None, None, None, :] * solver_params.phase_field_local_search_move_weight
|
||||
+ grid_score * solver_params.phase_field_local_search_grid_weight
|
||||
- feature_score * solver_params.phase_field_local_search_feature_weight
|
||||
)
|
||||
best_index = torch.argmin(score, dim=-1)
|
||||
gather_index = best_index[..., None, None].expand(batch, out_h, out_w, 1, 2)
|
||||
best_pos = torch.gather(candidate_pos, dim=3, index=gather_index).squeeze(3)
|
||||
target_disp = best_pos - prep.uv0_px_t
|
||||
clamped_blend = max(0.0, min(1.0, blend))
|
||||
disp_t.mul_(1.0 - clamped_blend).add_(target_disp * clamped_blend)
|
||||
|
||||
_project_displacements_in_place(
|
||||
torch,
|
||||
disp_t,
|
||||
prep.base_x_t,
|
||||
prep.base_y_t,
|
||||
min_dx=min_dx,
|
||||
min_dy=min_dy,
|
||||
max_dx=max_dx,
|
||||
max_dy=max_dy,
|
||||
width=prep.width,
|
||||
height=prep.height,
|
||||
)
|
||||
|
||||
|
||||
def _nearest_source_rgba(source_rgba: np.ndarray, sample_x: np.ndarray, sample_y: np.ndarray) -> np.ndarray:
|
||||
return source_rgba[sample_y, sample_x]
|
||||
|
||||
|
|
@ -542,29 +110,6 @@ def _observer_snapshot(
|
|||
}
|
||||
|
||||
|
||||
def _materialize_phase_terms(terms: dict[str, object]) -> dict[str, float]:
|
||||
materialized: dict[str, float] = {}
|
||||
for key, value in terms.items():
|
||||
if hasattr(value, "detach"):
|
||||
materialized[key] = float(value.detach().cpu().item())
|
||||
else:
|
||||
materialized[key] = float(value)
|
||||
return materialized
|
||||
|
||||
|
||||
def _materialize_loss_history(torch, values: list[object]) -> list[float]:
|
||||
if not values:
|
||||
return []
|
||||
normalized = []
|
||||
for value in values:
|
||||
if hasattr(value, "detach"):
|
||||
normalized.append(value.detach().reshape(()))
|
||||
else:
|
||||
normalized.append(torch.tensor(float(value)))
|
||||
stacked = torch.stack(normalized)
|
||||
return [float(item) for item in stacked.cpu().tolist()]
|
||||
|
||||
|
||||
def _sample_bilinear_np(field: np.ndarray, x: np.ndarray, y: np.ndarray) -> np.ndarray:
|
||||
height, width = field.shape[:2]
|
||||
x = np.clip(x, 0.0, max(0.0, float(width - 1)))
|
||||
|
|
@ -580,92 +125,6 @@ def _sample_bilinear_np(field: np.ndarray, x: np.ndarray, y: np.ndarray) -> np.n
|
|||
return top * (1.0 - wy) + bottom * wy
|
||||
|
||||
|
||||
def _resize_scalar_bilinear_np(field: np.ndarray, width: int, height: int) -> np.ndarray:
|
||||
src_h, src_w = field.shape[:2]
|
||||
xs = (np.arange(width, dtype=np.float32) + 0.5) * (src_w / max(1, width)) - 0.5
|
||||
ys = (np.arange(height, dtype=np.float32) + 0.5) * (src_h / max(1, height)) - 0.5
|
||||
grid_x, grid_y = np.meshgrid(xs, ys)
|
||||
return _sample_bilinear_np(field, grid_x, grid_y).astype(np.float32)
|
||||
|
||||
|
||||
def _feature_map_from_rgba(rgba: np.ndarray) -> np.ndarray:
|
||||
source = premultiply(rgba)
|
||||
luma = (
|
||||
source[..., 0] * np.float32(0.2126)
|
||||
+ source[..., 1] * np.float32(0.7152)
|
||||
+ source[..., 2] * np.float32(0.0722)
|
||||
).astype(np.float32)
|
||||
padded = np.pad(luma, 1, mode="edge")
|
||||
local_mean = (
|
||||
padded[:-2, :-2]
|
||||
+ padded[:-2, 1:-1]
|
||||
+ padded[:-2, 2:]
|
||||
+ padded[1:-1, :-2]
|
||||
+ padded[1:-1, 1:-1]
|
||||
+ padded[1:-1, 2:]
|
||||
+ padded[2:, :-2]
|
||||
+ padded[2:, 1:-1]
|
||||
+ padded[2:, 2:]
|
||||
) / np.float32(9.0)
|
||||
return np.abs(luma - local_mean).astype(np.float32)
|
||||
|
||||
|
||||
def _box_mean_np(field: np.ndarray, radius: int) -> np.ndarray:
|
||||
if radius <= 0:
|
||||
return field.astype(np.float32, copy=True)
|
||||
height, width = field.shape[:2]
|
||||
padded = np.pad(field.astype(np.float32), radius, mode="edge")
|
||||
integral = np.pad(
|
||||
padded,
|
||||
((1, 0), (1, 0)),
|
||||
mode="constant",
|
||||
constant_values=0.0,
|
||||
).cumsum(axis=0).cumsum(axis=1)
|
||||
size = radius * 2 + 1
|
||||
total = (
|
||||
integral[size : size + height, size : size + width]
|
||||
- integral[:height, size : size + width]
|
||||
- integral[size : size + height, :width]
|
||||
+ integral[:height, :width]
|
||||
)
|
||||
return (total / np.float32(size * size)).astype(np.float32)
|
||||
|
||||
|
||||
def _local_color_coherence_map_from_rgba(
|
||||
rgba: np.ndarray,
|
||||
*,
|
||||
cell_x: float,
|
||||
cell_y: float,
|
||||
radius_ratio: float,
|
||||
) -> np.ndarray:
|
||||
radius = max(1, int(round(min(cell_x, cell_y) * max(0.0, float(radius_ratio)))))
|
||||
source = premultiply(rgba).astype(np.float32)
|
||||
variance = np.zeros(source.shape[:2], dtype=np.float32)
|
||||
for channel in range(3):
|
||||
values = source[..., channel]
|
||||
mean = _box_mean_np(values, radius)
|
||||
mean_square = _box_mean_np(values * values, radius)
|
||||
variance += np.maximum(0.0, mean_square - mean * mean)
|
||||
return np.sqrt(variance / np.float32(3.0)).astype(np.float32)
|
||||
|
||||
|
||||
def _local_luma_mean_map_from_rgba(
|
||||
rgba: np.ndarray,
|
||||
*,
|
||||
cell_x: float,
|
||||
cell_y: float,
|
||||
radius_ratio: float,
|
||||
) -> np.ndarray:
|
||||
radius = max(1, int(round(min(cell_x, cell_y) * max(0.0, float(radius_ratio)))))
|
||||
source = premultiply(rgba).astype(np.float32)
|
||||
luma = (
|
||||
source[..., 0] * np.float32(0.2126)
|
||||
+ source[..., 1] * np.float32(0.7152)
|
||||
+ source[..., 2] * np.float32(0.0722)
|
||||
).astype(np.float32)
|
||||
return _box_mean_np(luma, radius)
|
||||
|
||||
|
||||
def _downsample_half_np(field: np.ndarray) -> np.ndarray:
|
||||
height, width = field.shape[:2]
|
||||
padded = np.pad(field.astype(np.float32), ((0, height % 2), (0, width % 2)), mode="edge")
|
||||
|
|
@ -715,7 +174,6 @@ def _normalize_percentile_np(values: np.ndarray, *, low_percentile: float = 5.0,
|
|||
|
||||
def _source_signal_map_np(
|
||||
rgba: np.ndarray,
|
||||
edge_map: np.ndarray,
|
||||
solver_params: SolverHyperParams,
|
||||
*,
|
||||
cell_x: float,
|
||||
|
|
@ -728,32 +186,30 @@ def _source_signal_map_np(
|
|||
+ source[..., 2] * np.float32(0.0722)
|
||||
).astype(np.float32)
|
||||
full_shape = luma.shape
|
||||
point_energy = np.zeros(full_shape, dtype=np.float32)
|
||||
context_energy = np.zeros(full_shape, dtype=np.float32)
|
||||
energy = np.zeros(full_shape, dtype=np.float32)
|
||||
level_field = luma
|
||||
levels = max(1, int(solver_params.phase_field_signal_pyramid_levels))
|
||||
level_weight = np.float32(1.0)
|
||||
level_decay = np.float32(solver_params.phase_field_signal_level_decay)
|
||||
for level in range(levels):
|
||||
gradient = _gradient_magnitude_np(level_field)
|
||||
curvature = _laplacian_abs_np(level_field)
|
||||
boundary = (
|
||||
gradient * np.float32(solver_params.phase_field_signal_boundary_weight)
|
||||
level_energy = (
|
||||
gradient * np.float32(solver_params.phase_field_signal_gradient_weight)
|
||||
+ curvature * np.float32(solver_params.phase_field_signal_curvature_weight)
|
||||
).astype(np.float32)
|
||||
boundary = _normalize_percentile_np(boundary, low_percentile=10.0, high_percentile=98.0)
|
||||
context_radius = max(1, int(round(1.5 + level)))
|
||||
context = _box_mean_np(boundary, context_radius)
|
||||
level_weight = np.float32(1.0 / (1 << level))
|
||||
point_energy += _resize_scalar_to_shape_np(boundary, full_shape) * level_weight
|
||||
context_energy += _resize_scalar_to_shape_np(context, full_shape) * level_weight
|
||||
energy += _resize_scalar_to_shape_np(level_energy, full_shape) * level_weight
|
||||
if min(level_field.shape[:2]) <= 8:
|
||||
break
|
||||
level_field = _downsample_half_np(level_field)
|
||||
level_weight *= level_decay
|
||||
|
||||
point_energy = _normalize_percentile_np(point_energy, low_percentile=5.0, high_percentile=99.0)
|
||||
context_energy = _normalize_percentile_np(context_energy, low_percentile=5.0, high_percentile=99.0)
|
||||
quiet_here = np.exp(-point_energy * np.float32(solver_params.phase_field_signal_self_penalty)).astype(np.float32)
|
||||
context = np.power(context_energy, np.float32(solver_params.phase_field_signal_context_power)).astype(np.float32)
|
||||
signal = _normalize_percentile_np(quiet_here * context, low_percentile=5.0, high_percentile=99.0)
|
||||
energy = _normalize_percentile_np(
|
||||
np.power(np.maximum(energy, np.float32(0.0)), np.float32(solver_params.phase_field_signal_energy_power)),
|
||||
low_percentile=5.0,
|
||||
high_percentile=99.0,
|
||||
)
|
||||
signal = np.clip(1.0 - energy, 0.0, 1.0).astype(np.float32)
|
||||
return np.power(signal, np.float32(solver_params.phase_field_signal_peak_power)).astype(np.float32)
|
||||
|
||||
|
||||
|
|
@ -944,13 +400,13 @@ def _should_emit_phase_field_step(step: int, total_steps: int, *, preview_stride
|
|||
def optimize_phase_field(
|
||||
rgba: np.ndarray,
|
||||
inference: InferenceResult,
|
||||
analysis: PhaseFieldSourceAnalysis,
|
||||
steps: int,
|
||||
seed: int,
|
||||
device: str,
|
||||
solver_params: SolverHyperParams | None = None,
|
||||
observer: PipelineObserver | None = None,
|
||||
) -> SolverArtifacts:
|
||||
del device
|
||||
solver_params = solver_params or SolverHyperParams()
|
||||
preview_stride = _observer_preview_stride(observer)
|
||||
include_snapshots = _observer_needs_phase_field_snapshot(observer)
|
||||
|
|
@ -969,24 +425,14 @@ def optimize_phase_field(
|
|||
uv0_norm[..., 1] = (uv0_norm[..., 1] / max(1.0, float(height - 1))) * 2.0 - 1.0
|
||||
signal_map = _source_signal_map_np(
|
||||
rgba,
|
||||
analysis.edge_map,
|
||||
solver_params,
|
||||
cell_x=cell_x,
|
||||
cell_y=cell_y,
|
||||
)
|
||||
guidance = signal_map
|
||||
signal = signal_map
|
||||
prep = _PhaseFieldPrep(
|
||||
source_t=None,
|
||||
edge_t=None,
|
||||
uv0_px_t=None,
|
||||
uv0_norm=uv0_norm,
|
||||
base_x_t=None,
|
||||
base_y_t=None,
|
||||
guidance=guidance,
|
||||
cell_x=cell_x,
|
||||
cell_y=cell_y,
|
||||
patch_offsets_t=None,
|
||||
feature_t=None,
|
||||
signal=signal,
|
||||
width=width,
|
||||
height=height,
|
||||
)
|
||||
|
|
@ -998,7 +444,7 @@ def optimize_phase_field(
|
|||
}
|
||||
if include_snapshots:
|
||||
prepared_payload["uv0_px"] = uv0_px.astype(np.float32)
|
||||
prepared_payload["guidance"] = guidance.copy()
|
||||
prepared_payload["signal"] = signal.copy()
|
||||
emit_observer(observer, "phase_field_prepared", **prepared_payload)
|
||||
|
||||
pos = uv0_px.astype(np.float32).copy()
|
||||
|
|
@ -1120,7 +566,7 @@ def optimize_phase_field(
|
|||
return SolverArtifacts(
|
||||
target_rgba=target_rgba,
|
||||
uv_field=final_pos_norm,
|
||||
guidance_strength=prep.guidance,
|
||||
signal_strength=prep.signal,
|
||||
initial_rgba=initial_rgba,
|
||||
loss_history=loss_history,
|
||||
stage_diagnostics=stage_diagnostics,
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ from .diagnostics import (
|
|||
write_lattice_overlay,
|
||||
)
|
||||
from .discrete import cleanup_pixels
|
||||
from .inference import infer_autocorr_lattice, infer_fixed_lattice, infer_lattice, inference_to_json
|
||||
from .inference import infer_autocorr_lattice, infer_fixed_lattice, inference_to_json
|
||||
from .io import load_rgba, nearest_resize, save_rgba
|
||||
from .metrics import (
|
||||
foreground_edge_concentration,
|
||||
|
|
@ -49,7 +49,7 @@ def run_pipeline(
|
|||
solver_params: SolverHyperParams | None = None,
|
||||
strip_background: bool = False,
|
||||
enable_candidate_rerank: bool = True,
|
||||
lattice_inference_mode: str = "search",
|
||||
lattice_inference_mode: str = "autocorr",
|
||||
max_inferred_target_size: int | None = None,
|
||||
observer: PipelineObserver | None = None,
|
||||
) -> RunResult:
|
||||
|
|
@ -91,7 +91,7 @@ def run_pipeline_rgba(
|
|||
solver_params: SolverHyperParams | None = None,
|
||||
strip_background: bool = False,
|
||||
enable_candidate_rerank: bool = True,
|
||||
lattice_inference_mode: str = "search",
|
||||
lattice_inference_mode: str = "autocorr",
|
||||
max_inferred_target_size: int | None = None,
|
||||
observer: PipelineObserver | None = None,
|
||||
) -> RunResult:
|
||||
|
|
@ -106,7 +106,7 @@ def run_pipeline_rgba(
|
|||
"stage_started",
|
||||
stage="preprocess",
|
||||
label="Background cleanup",
|
||||
detail="Stripping edge-connected neutral junk before lattice search.",
|
||||
detail="Stripping edge-connected neutral junk before lattice inference.",
|
||||
)
|
||||
source = strip_edge_background(source)
|
||||
emit_observer(observer, "preprocess_completed", source_rgba=source.copy(), operation="strip_background")
|
||||
|
|
@ -120,32 +120,22 @@ def run_pipeline_rgba(
|
|||
)
|
||||
if fixed_dims is None:
|
||||
check_observer_cancelled(observer)
|
||||
if lattice_inference_mode == "autocorr":
|
||||
inference_label = "Autocorr lattice"
|
||||
inference_detail = "Scoring autocorr candidate sizes from canonical cell centers; the phase field is free to drift."
|
||||
elif lattice_inference_mode == "search":
|
||||
inference_label = "Lattice search"
|
||||
inference_detail = "Searching for output size from canonical cell centers; the phase field handles local drift."
|
||||
else:
|
||||
if lattice_inference_mode != "autocorr":
|
||||
raise ValueError(f"Unknown lattice_inference_mode '{lattice_inference_mode}'.")
|
||||
emit_observer(
|
||||
observer,
|
||||
"stage_started",
|
||||
stage="inference",
|
||||
label=inference_label,
|
||||
detail=inference_detail,
|
||||
label="Autocorr lattice",
|
||||
detail="Scoring autocorr candidate sizes from canonical cell centers; the phase field is free to drift.",
|
||||
)
|
||||
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"
|
||||
inference = infer_autocorr_lattice(
|
||||
source,
|
||||
max_target_size=max_inferred_target_size,
|
||||
device=device,
|
||||
observer=observer,
|
||||
)
|
||||
inference_mode = "autocorr"
|
||||
else:
|
||||
check_observer_cancelled(observer)
|
||||
emit_observer(
|
||||
|
|
@ -168,8 +158,8 @@ def run_pipeline_rgba(
|
|||
observer,
|
||||
"stage_started",
|
||||
stage="analysis",
|
||||
label="Input analysis",
|
||||
detail="Mapping sharp cells, edges, and guidance before the solver starts.",
|
||||
label="Diagnostic edge preview",
|
||||
detail="Rendering source edge diagnostics; the solver follows the hierarchical flatness signal.",
|
||||
)
|
||||
analysis = _prepare_analysis(
|
||||
source,
|
||||
|
|
@ -188,7 +178,6 @@ def run_pipeline_rgba(
|
|||
inference = _select_candidate(
|
||||
source,
|
||||
inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
|
|
@ -212,7 +201,6 @@ def run_pipeline_rgba(
|
|||
solver, reconstruction_diagnostics = _run_reconstruction(
|
||||
source,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
|
|
@ -225,9 +213,9 @@ def run_pipeline_rgba(
|
|||
"stage_started",
|
||||
stage="cleanup",
|
||||
label="Cleanup",
|
||||
detail="Sweeping isolated noise and stubborn single-pixel junk.",
|
||||
detail="Snapping alpha to the final discrete mask.",
|
||||
)
|
||||
cleanup = cleanup_pixels(solver.target_rgba, source_guidance=solver.guidance_strength)
|
||||
cleanup = cleanup_pixels(solver.target_rgba)
|
||||
emit_observer(
|
||||
observer,
|
||||
"cleanup_completed",
|
||||
|
|
@ -348,7 +336,6 @@ def _select_candidate(
|
|||
source: np.ndarray,
|
||||
inference: InferenceResult,
|
||||
*,
|
||||
analysis,
|
||||
steps: int = 0,
|
||||
seed: int,
|
||||
device: str,
|
||||
|
|
@ -359,7 +346,6 @@ def _select_candidate(
|
|||
return _select_candidate_with_reconstruction(
|
||||
source,
|
||||
inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
|
|
@ -373,7 +359,6 @@ def _select_candidate_with_reconstruction(
|
|||
source: np.ndarray,
|
||||
inference: InferenceResult,
|
||||
*,
|
||||
analysis,
|
||||
steps: int = 0,
|
||||
seed: int,
|
||||
device: str,
|
||||
|
|
@ -440,7 +425,6 @@ def _select_candidate_with_reconstruction(
|
|||
candidate_artifacts, _candidate_diagnostics = _run_reconstruction(
|
||||
source,
|
||||
inference=candidate_inference,
|
||||
analysis=analysis,
|
||||
steps=preview_steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
|
|
@ -614,34 +598,22 @@ def _run_reconstruction(
|
|||
source: np.ndarray,
|
||||
*,
|
||||
inference: InferenceResult,
|
||||
analysis,
|
||||
steps: int,
|
||||
seed: int,
|
||||
device: str,
|
||||
solver_params: SolverHyperParams,
|
||||
observer: PipelineObserver | None = None,
|
||||
):
|
||||
if observer is None:
|
||||
solver = optimize_phase_field(
|
||||
source,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
)
|
||||
else:
|
||||
solver = optimize_phase_field(
|
||||
source,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
observer=observer,
|
||||
)
|
||||
solver_kwargs = {
|
||||
"inference": inference,
|
||||
"steps": steps,
|
||||
"seed": seed,
|
||||
"device": device,
|
||||
"solver_params": solver_params,
|
||||
}
|
||||
if observer is not None:
|
||||
solver_kwargs["observer"] = observer
|
||||
solver = optimize_phase_field(source, **solver_kwargs)
|
||||
phase_field_metrics = getattr(solver, "stage_diagnostics", {}).get("phase_field", {})
|
||||
return solver, {
|
||||
"mode": "phase-field",
|
||||
|
|
|
|||
|
|
@ -103,9 +103,9 @@ def _mutate_params(base: SolverHyperParams, rng: np.random.Generator, scale: flo
|
|||
low=0.05,
|
||||
high=0.80,
|
||||
),
|
||||
phase_field_signal_boundary_weight=_mutate_positive(
|
||||
phase_field_signal_gradient_weight=_mutate_positive(
|
||||
rng,
|
||||
base.phase_field_signal_boundary_weight,
|
||||
base.phase_field_signal_gradient_weight,
|
||||
scale=scale,
|
||||
low=0.20,
|
||||
high=2.50,
|
||||
|
|
@ -117,16 +117,17 @@ def _mutate_params(base: SolverHyperParams, rng: np.random.Generator, scale: flo
|
|||
low=0.10,
|
||||
high=2.00,
|
||||
),
|
||||
phase_field_signal_self_penalty=_mutate_positive(
|
||||
phase_field_signal_level_decay=_mutate_linear(
|
||||
rng,
|
||||
base.phase_field_signal_self_penalty,
|
||||
base.phase_field_signal_level_decay,
|
||||
scale=scale,
|
||||
low=1.00,
|
||||
high=8.00,
|
||||
sigma=0.15,
|
||||
low=0.0,
|
||||
high=1.0,
|
||||
),
|
||||
phase_field_signal_context_power=_mutate_positive(
|
||||
phase_field_signal_energy_power=_mutate_positive(
|
||||
rng,
|
||||
base.phase_field_signal_context_power,
|
||||
base.phase_field_signal_energy_power,
|
||||
scale=scale,
|
||||
low=0.25,
|
||||
high=2.00,
|
||||
|
|
@ -174,6 +175,7 @@ def tune_solver_hyperparams(
|
|||
infer_size: bool = False,
|
||||
include_cases: list[str] | None = None,
|
||||
limit_cases: int | None = None,
|
||||
workers: int = 1,
|
||||
) -> dict[str, Any]:
|
||||
if trials < 1:
|
||||
raise ValueError("trials must be at least 1")
|
||||
|
|
@ -216,6 +218,7 @@ def tune_solver_hyperparams(
|
|||
limit_cases=effective_limit,
|
||||
keep_existing=False,
|
||||
solver_params=candidate,
|
||||
workers=workers,
|
||||
)
|
||||
score_info = _score_summary(summary)
|
||||
improved = score_info["score"] < best_score
|
||||
|
|
@ -248,6 +251,7 @@ def tune_solver_hyperparams(
|
|||
limit_cases=effective_limit,
|
||||
keep_existing=False,
|
||||
solver_params=best_params,
|
||||
workers=workers,
|
||||
)
|
||||
final_score = _score_summary(final_summary)
|
||||
|
||||
|
|
@ -265,6 +269,7 @@ def tune_solver_hyperparams(
|
|||
"infer_size": infer_size,
|
||||
"include_cases": include_cases or [],
|
||||
"limit_cases_effective": effective_limit,
|
||||
"workers": workers,
|
||||
"objective": "0.20 * error_ratio_to_naive + 0.45 * adjacency_ratio_to_naive + 0.35 * motif_ratio_to_naive; lower is better",
|
||||
"mutation_scope": "explicit solver only: signal, local search, and lattice spring parameters",
|
||||
"trial_results": trials_payload,
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ class SourceLatticeReference:
|
|||
class SolverArtifacts:
|
||||
target_rgba: np.ndarray
|
||||
uv_field: np.ndarray
|
||||
guidance_strength: np.ndarray
|
||||
signal_strength: np.ndarray
|
||||
initial_rgba: np.ndarray
|
||||
loss_history: list[float]
|
||||
stage_diagnostics: dict[str, Any] = field(default_factory=dict)
|
||||
|
|
|
|||
Loading…
Reference in a new issue