Run pymatting foreground estimation on GPU via CuPy when available

The multi-level foreground colour estimation (pymatting estimate_foreground_ml)
is the pipeline's CPU bottleneck (~7 s/image). pymatting ships a CuPy backend
(estimate_foreground_ml_cupy) that runs the same multi-level solve on the GPU
~250x faster; verified visually identical to the float64 CPU result (opaque
colours bit-close, the float32 edge delta is alpha-weighted away).

Add ForegroundSettings.use_gpu (default True): with method "ml", try the CuPy
backend when cupy + a CUDA device are present, otherwise transparently fall back
to the CPU solver. pymatting only re-exports the cupy function when cupy is
importable, so CPU-only deployments (no cupy) are unaffected. Requires
cupy-cudaXXx matching the CUDA build on GPU boxes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 09:32:35 +00:00
parent ece9542978
commit 9094b5d9c0
2 changed files with 45 additions and 0 deletions
+5
View File
@@ -105,6 +105,11 @@ class CrossCheckSettings:
class ForegroundSettings:
enabled: bool = True
method: str = "ml" # "ml" (pymatting) or "unmix" (legacy heuristic)
# With method "ml", use pymatting's CuPy GPU backend when it is available
# (cupy installed + a CUDA device) — ~250x faster, float32 output visually
# identical to the float64 CPU solver. Transparently falls back to CPU when
# cupy/CUDA are absent, so it is safe to leave on for CPU-only deployments.
use_gpu: bool = True
fallback_to_unmix: bool = True
ml_regularization: float = 1e-5
# Legacy "unmix" method parameters (used only when method == "unmix").