Remove tile-graph engine
This commit is contained in:
parent
c5b53f9861
commit
d4b5ab9ed7
18 changed files with 33 additions and 2716 deletions
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import numpy as np
|
||||
|
||||
from .metrics import luminance
|
||||
from .types import PhaseFieldSourceAnalysis, TileGraphSourceAnalysis
|
||||
from .types import PhaseFieldSourceAnalysis
|
||||
|
||||
|
||||
def _require_torch():
|
||||
|
|
@ -74,22 +74,3 @@ def analyze_phase_field_source(
|
|||
return PhaseFieldSourceAnalysis(
|
||||
edge_map=edge_map,
|
||||
)
|
||||
|
||||
|
||||
def analyze_tile_graph_source(
|
||||
rgba: np.ndarray,
|
||||
*,
|
||||
device: str | None = None,
|
||||
) -> TileGraphSourceAnalysis:
|
||||
if device is not None:
|
||||
torch = _require_torch()
|
||||
resolved_device = _resolve_device(torch, device)
|
||||
rgba_t = torch.from_numpy(rgba).to(device=resolved_device, dtype=torch.float32)
|
||||
edge_map_t = _compute_edge_map_torch(torch, rgba_t)
|
||||
return TileGraphSourceAnalysis(
|
||||
edge_map=edge_map_t.detach().cpu().numpy().astype(np.float32),
|
||||
)
|
||||
|
||||
return TileGraphSourceAnalysis(
|
||||
edge_map=_compute_edge_map(rgba),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -33,12 +33,6 @@ def build_parser() -> argparse.ArgumentParser:
|
|||
target.add_argument("--seed", type=int, default=7, help="Random seed")
|
||||
target.add_argument("--steps", type=int, default=200, help="Number of optimizer steps")
|
||||
target.add_argument("--device", default="auto", choices=("auto", "cpu", "cuda"), help="Torch device")
|
||||
target.add_argument(
|
||||
"--reconstruction-mode",
|
||||
default="phase-field",
|
||||
choices=("phase-field", "tile-graph"),
|
||||
help="Reconstruction engine to run after lattice estimation",
|
||||
)
|
||||
target.add_argument(
|
||||
"--strip-background",
|
||||
action="store_true",
|
||||
|
|
@ -144,7 +138,6 @@ def main(argv: list[str] | None = None) -> int:
|
|||
seed=args.seed,
|
||||
steps=args.steps,
|
||||
device=args.device,
|
||||
reconstruction_mode=args.reconstruction_mode,
|
||||
strip_background=args.strip_background,
|
||||
enable_phase_rerank=not args.skip_phase_rerank,
|
||||
)
|
||||
|
|
@ -197,7 +190,6 @@ def main(argv: list[str] | None = None) -> int:
|
|||
seed=args.seed,
|
||||
steps=args.steps,
|
||||
device=args.device,
|
||||
reconstruction_mode=args.reconstruction_mode,
|
||||
strip_background=args.strip_background,
|
||||
enable_phase_rerank=not args.skip_phase_rerank,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -70,7 +70,6 @@ def run_compare(
|
|||
steps: int = 200,
|
||||
device: str = "auto",
|
||||
strip_background: bool = False,
|
||||
reconstruction_mode: str = "phase-field",
|
||||
enable_phase_rerank: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
diagnostics_path = Path(diagnostics_dir) if diagnostics_dir else Path(output_path).with_suffix("")
|
||||
|
|
@ -90,7 +89,6 @@ def run_compare(
|
|||
steps=steps,
|
||||
device=device,
|
||||
strip_background=strip_background,
|
||||
reconstruction_mode=reconstruction_mode,
|
||||
enable_phase_rerank=enable_phase_rerank,
|
||||
)
|
||||
source = result.source_rgba
|
||||
|
|
|
|||
|
|
@ -28,19 +28,6 @@ class SolverHyperParams:
|
|||
phase_rerank_confidence_threshold: float = 0.12
|
||||
phase_rerank_max_size_delta_ratio: float = 0.40
|
||||
phase_rerank_margin: float = 0.004
|
||||
tile_graph_max_candidates: int = 768
|
||||
tile_graph_max_candidates_per_coord: int = 2
|
||||
tile_graph_edge_candidates_per_coord: int = 6
|
||||
tile_graph_component_color_threshold: float = 0.055
|
||||
tile_graph_component_alpha_threshold: float = 0.12
|
||||
tile_graph_source_region_min_area_ratio: float = 0.06
|
||||
tile_graph_source_region_window_coverage: float = 0.12
|
||||
tile_graph_area_weight: float = 0.03
|
||||
tile_graph_alpha_weight: float = 0.25
|
||||
tile_graph_coverage_weight: float = 0.05
|
||||
tile_graph_edge_peak_weight: float = 0.10
|
||||
tile_graph_adjacency_weight: float = 0.45
|
||||
tile_graph_iterations: int = 10
|
||||
|
||||
def to_dict(self) -> dict[str, float | int]:
|
||||
return asdict(self)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from typing import Any
|
|||
|
||||
import numpy as np
|
||||
|
||||
from .analysis import analyze_phase_field_source, analyze_tile_graph_source
|
||||
from .analysis import analyze_phase_field_source
|
||||
from .diagnostics import (
|
||||
summarize_run,
|
||||
write_alpha_preview,
|
||||
|
|
@ -29,8 +29,7 @@ from .params import SolverHyperParams
|
|||
from .phase_field import optimize_phase_field
|
||||
from .palette import load_palette, quantize_rgba, save_palette_report
|
||||
from .preprocess import strip_edge_background
|
||||
from .tile_graph import optimize_tile_graph
|
||||
from .types import InferenceResult, PhaseFieldSourceAnalysis, RunResult, TileGraphSourceAnalysis
|
||||
from .types import InferenceResult, PhaseFieldSourceAnalysis, RunResult
|
||||
|
||||
|
||||
def run_pipeline(
|
||||
|
|
@ -50,7 +49,6 @@ def run_pipeline(
|
|||
device: str = "auto",
|
||||
solver_params: SolverHyperParams | None = None,
|
||||
strip_background: bool = False,
|
||||
reconstruction_mode: str = "phase-field",
|
||||
enable_phase_rerank: bool = True,
|
||||
) -> RunResult:
|
||||
started = time.perf_counter()
|
||||
|
|
@ -84,7 +82,6 @@ def run_pipeline(
|
|||
source,
|
||||
seed=seed,
|
||||
device=device,
|
||||
reconstruction_mode=reconstruction_mode,
|
||||
)
|
||||
inference = _select_phase_candidate(
|
||||
source,
|
||||
|
|
@ -94,7 +91,6 @@ def run_pipeline(
|
|||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
reconstruction_mode=reconstruction_mode,
|
||||
enable_phase_rerank=enable_phase_rerank,
|
||||
)
|
||||
solver, reconstruction_diagnostics = _run_reconstruction(
|
||||
|
|
@ -105,7 +101,6 @@ def run_pipeline(
|
|||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
reconstruction_mode=reconstruction_mode,
|
||||
)
|
||||
cleanup = cleanup_pixels(solver.target_rgba, source_guidance=solver.guidance_strength)
|
||||
palette = load_palette(palette_path) if palette_path else None
|
||||
|
|
@ -172,7 +167,6 @@ def run_pipeline(
|
|||
"steps": steps,
|
||||
"device": device,
|
||||
"strip_background": strip_background,
|
||||
"reconstruction_mode": reconstruction_mode,
|
||||
"enable_phase_rerank": enable_phase_rerank,
|
||||
"inference_mode": inference_mode,
|
||||
"solver_params": solver_params.to_dict(),
|
||||
|
|
@ -190,10 +184,7 @@ def _prepare_analysis(
|
|||
*,
|
||||
seed: int,
|
||||
device: str,
|
||||
reconstruction_mode: str,
|
||||
) -> PhaseFieldSourceAnalysis | TileGraphSourceAnalysis:
|
||||
if reconstruction_mode == "tile-graph":
|
||||
return analyze_tile_graph_source(source, device=device)
|
||||
) -> PhaseFieldSourceAnalysis:
|
||||
return analyze_phase_field_source(source, seed=seed, device=device)
|
||||
|
||||
|
||||
|
|
@ -206,7 +197,6 @@ def _select_phase_candidate(
|
|||
seed: int,
|
||||
device: str,
|
||||
solver_params: SolverHyperParams | None = None,
|
||||
reconstruction_mode: str = "phase-field",
|
||||
enable_phase_rerank: bool = True,
|
||||
) -> InferenceResult:
|
||||
return _select_phase_candidate_with_reconstruction(
|
||||
|
|
@ -217,7 +207,6 @@ def _select_phase_candidate(
|
|||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
reconstruction_mode=reconstruction_mode,
|
||||
enable_phase_rerank=enable_phase_rerank,
|
||||
)
|
||||
|
||||
|
|
@ -231,14 +220,11 @@ def _select_phase_candidate_with_reconstruction(
|
|||
seed: int,
|
||||
device: str,
|
||||
solver_params: SolverHyperParams | None = None,
|
||||
reconstruction_mode: str = "phase-field",
|
||||
enable_phase_rerank: bool = True,
|
||||
) -> InferenceResult:
|
||||
solver_params = solver_params or SolverHyperParams()
|
||||
if not enable_phase_rerank:
|
||||
return inference
|
||||
if reconstruction_mode != "phase-field":
|
||||
return inference
|
||||
if len(inference.top_candidates) <= 1 or inference.confidence >= solver_params.phase_rerank_confidence_threshold:
|
||||
return inference
|
||||
|
||||
|
|
@ -262,7 +248,6 @@ def _select_phase_candidate_with_reconstruction(
|
|||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
reconstruction_mode=reconstruction_mode,
|
||||
)
|
||||
support = source_lattice_consistency_breakdown(
|
||||
source,
|
||||
|
|
@ -433,34 +418,21 @@ def _run_reconstruction(
|
|||
seed: int,
|
||||
device: str,
|
||||
solver_params: SolverHyperParams,
|
||||
reconstruction_mode: str,
|
||||
):
|
||||
if reconstruction_mode == "tile-graph":
|
||||
return optimize_tile_graph(
|
||||
source,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
)
|
||||
if reconstruction_mode == "phase-field":
|
||||
solver = optimize_phase_field(
|
||||
source,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
)
|
||||
phase_field_metrics = getattr(solver, "stage_diagnostics", {}).get("phase_field", {})
|
||||
return solver, {
|
||||
"mode": "phase-field",
|
||||
**{
|
||||
f"phase_field_{key}": value
|
||||
for key, value in phase_field_metrics.items()
|
||||
},
|
||||
}
|
||||
raise ValueError(f"Unknown reconstruction mode: {reconstruction_mode}")
|
||||
solver = optimize_phase_field(
|
||||
source,
|
||||
inference=inference,
|
||||
analysis=analysis,
|
||||
steps=steps,
|
||||
seed=seed,
|
||||
device=device,
|
||||
solver_params=solver_params,
|
||||
)
|
||||
phase_field_metrics = getattr(solver, "stage_diagnostics", {}).get("phase_field", {})
|
||||
return solver, {
|
||||
"mode": "phase-field",
|
||||
**{
|
||||
f"phase_field_{key}": value
|
||||
for key, value in phase_field_metrics.items()
|
||||
},
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -32,14 +32,6 @@ class PhaseFieldSourceAnalysis:
|
|||
edge_map: np.ndarray
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class TileGraphSourceAnalysis:
|
||||
edge_map: np.ndarray
|
||||
|
||||
|
||||
SourceAnalysis = PhaseFieldSourceAnalysis | TileGraphSourceAnalysis
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SourceLatticeReference:
|
||||
mean_rgba: np.ndarray
|
||||
|
|
@ -86,7 +78,7 @@ class RunResult:
|
|||
source_rgba: np.ndarray
|
||||
output_rgba: np.ndarray
|
||||
inference: InferenceResult
|
||||
analysis: PhaseFieldSourceAnalysis | TileGraphSourceAnalysis
|
||||
analysis: PhaseFieldSourceAnalysis
|
||||
solver: SolverArtifacts
|
||||
cleanup: CleanupArtifacts
|
||||
palette_result: PaletteResult | None
|
||||
|
|
|
|||
Loading…
Reference in a new issue