acra-backend / code /pipeline /reencoding_mild.py
Davionnic
fix(pipeline): prevent excessive green lightness shift and unneeded re-encoding
b13e551
Raw History Blame Contribute Delete
25.2 kB
"""
Milder dynamic re-encoding (default pipeline path).
Today's mixer stays in reencoding.py as reencode / enforce_rg_separation.
Set ACRA_LEGACY_REENCODING=1 (or run_full_pipeline(use_legacy_reencoding=True))
to use that path. This module only nudges L* enough to stop a CVD collapse.
"""
from typing import List, Optional, Tuple
import numpy as np
from .conflict import delta_e_cie76, delta_e_ciede2000
from .cvd_simulation import get_simulation_matrix
from .reencoding import (
ORTHOGONAL_DIRECTIONS,
_candidate_center,
_chroma,
_classify_pair,
_clipping_penalty,
_effective_target_gap,
_green_strength,
_is_green,
_is_neutral,
_is_red,
_red_strength,
_simulate_lab_center,
enforce_rg_separation,
reencode,
)
MILD_MAX_DELTA_L = 16.0
MILD_GREEN_UP_CAP = 5.0
MILD_RED_DOWN_CAP = 28.0
MILD_GAP_CAP = 14.0
MILD_POLARITY = 1.0
MILD_CHROMA_RETAIN = 0.45
MILD_RESCAN_PASSES = 2
# Chroma-first separation. The thesis caps chroma change at <= 10 (a*/b*), so
# the search pushes along the intact blue-yellow axis up to that budget before
# spending any lightness. This keeps red red and green green instead of the
# 24-30 L* crush a lightness-only fix needs to clear the red-green bar.
MILD_MAX_CHROMA_STEP = 10.0
MILD_SOFT_L_BUDGET = 6.0
def _is_any_green(c: np.ndarray) -> bool:
"""True for any green shade (a* < -2 and b* > 0)."""
return float(c[1]) < -2.0 and float(c[2]) > 0.0
def _delivered(centers: np.ndarray, original: np.ndarray) -> np.ndarray:
"""Centers as they actually ship: L* moves in full, a*/b* keep only 45%.
The reconstruction stage multiplies chroma shifts by MILD_CHROMA_RETAIN, so
a pair that looks separated on the raw mixed centers can collapse again on
the delivered ones. The search and the rescan must judge this state, not
the raw mix.
"""
return original + (centers - original) * np.array([1.0, MILD_CHROMA_RETAIN, MILD_CHROMA_RETAIN])
def _classify_pair_mild(centers: np.ndarray, ci: int, cj: int) -> Tuple[int, int, bool]:
"""Keep the already-lighter side lighter as soon as |dL| >= 1."""
c1_red, c1_green = _is_red(centers[ci]), _is_green(centers[ci])
c2_red, c2_green = _is_red(centers[cj]), _is_green(centers[cj])
is_rg_pair = (c1_red and c2_green) or (c1_green and c2_red)
l_diff = float(centers[ci][0]) - float(centers[cj][0])
if abs(l_diff) >= MILD_POLARITY:
if l_diff < 0:
return ci, cj, is_rg_pair
return cj, ci, is_rg_pair
# True tie: split later; still tag the pair type. Prefer existing L order.
if centers[ci][0] <= centers[cj][0]:
return ci, cj, is_rg_pair
return cj, ci, is_rg_pair
def reencode_mild(
centers: np.ndarray,
conflict_pairs: List[Tuple[int, int]],
severity: float,
cvd_type: str,
use_fast: bool = False,
target_de: float = 24.0,
target_rg_l_gap: float = 30.0,
allow_chroma_shift: bool = True,
max_delta_l: float = 38.0,
max_delta_chroma: float = 8.0,
refinement_passes: int = 2,
cluster_labels: Optional[List[str]] = None,
pinned_indices: Optional[set] = None,
original_centers: Optional[np.ndarray] = None,
) -> np.ndarray:
"""Same search as reencode, but polarity at 1 L* and a 12-point cap."""
modified_centers = centers.copy().astype(np.float64)
original_centers = (
original_centers.copy().astype(np.float64)
if original_centers is not None
else modified_centers.copy()
)
if not conflict_pairs:
return modified_centers
sim_matrix = get_simulation_matrix(severity, cvd_type)
delta_e_fn = delta_e_cie76 if use_fast else delta_e_ciede2000
l_cap = min(float(max_delta_l), MILD_MAX_DELTA_L)
protected = np.array([_is_neutral(c) for c in original_centers], dtype=bool)
if cluster_labels is not None:
for idx, lbl in enumerate(cluster_labels):
if idx < len(protected):
nl = lbl.lower()
if idx == 0 or "person" in nl or "exclude" in nl or "paper" in nl or "background" in nl or ("text" in nl and _is_neutral(original_centers[idx])):
protected[idx] = True
if pinned_indices is not None:
for idx in pinned_indices:
if idx < len(protected):
protected[idx] = True
effective_base_gap = min(
_effective_target_gap(severity, cvd_type, target_rg_l_gap),
MILD_GAP_CAP,
)
# Lightness budget depends on the pair. The detector clears a red-green
# pair on a simulated L* gap >= 22 (cheaper than the dE >= 35 exit), so rg
# pairs get up to +/-11 each; other pairs stay on the soft 6-point nudge.
dark_steps = np.array([0.0, -2.0, -4.0, -6.0])
light_steps = np.array([0.0, 2.0, 4.0, 6.0])
rg_dark_steps = np.array([0.0, -3.0, -6.0, -8.0, -10.0, -12.0, -15.0, -18.0, -21.0, -24.0, -28.0])
rg_light_steps = np.array([0.0, 3.0, 6.0, 8.0, 10.0, 12.0])
rg_green_light_steps = np.array([0.0, 2.0, 3.0, 4.0, 5.0])
single_rg_steps_dark = np.array([0.0, -4.0, -8.0, -12.0, -16.0, -20.0, -22.0])
single_rg_steps_light = np.array([0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 22.0])
RG_L_GAP_EXIT = 22.0
neutral_chroma = [1.0]
red_chroma = [1.0, 1.08] if allow_chroma_shift else neutral_chroma
green_chroma = [1.0, 1.06] if allow_chroma_shift else neutral_chroma
other_chroma = [1.0, 1.06] if allow_chroma_shift else neutral_chroma
ortho_dir = ORTHOGONAL_DIRECTIONS.get(cvd_type) if (allow_chroma_shift and cvd_type in ORTHOGONAL_DIRECTIONS) else None
# Sweep the full chroma budget in both directions. _candidate_center scales
# this by max_delta_chroma*0.5, so a step of 2.0 lands at the <=8 cap.
ortho_steps = (
np.array([0.0, 0.5, 1.0, 1.5, 2.0, -0.5, -1.0, -1.5, -2.0])
if ortho_dir is not None else np.array([0.0])
)
def _pair_is_flagged(ci: int, cj: int) -> bool:
if protected[ci] and protected[cj]:
return False
delivered = _delivered(modified_centers, original_centers)
sim_i = _simulate_lab_center(delivered[ci], sim_matrix)
sim_j = _simulate_lab_center(delivered[cj], sim_matrix)
de_sim = delta_e_fn(sim_i, sim_j)
_, _, is_rg = _classify_pair_mild(original_centers, ci, cj)
# Match the conflict detector's own thresholds so a pair the mixer calls
# done is a pair the metric also calls resolved.
pair_thresh = 35.0 if is_rg else 20.0
l_thresh = RG_L_GAP_EXIT if is_rg else 20.0
if de_sim >= pair_thresh or abs(float(sim_i[0]) - float(sim_j[0])) >= l_thresh:
return False
return True
for _pass in range(max(1, refinement_passes)):
changed = False
for ci, cj in conflict_pairs:
if not _pair_is_flagged(ci, cj):
continue
dark_idx, light_idx, is_rg_pair = _classify_pair_mild(original_centers, ci, cj)
if is_rg_pair and len(conflict_pairs) > 1:
l_diff_orig = abs(float(original_centers[ci][0]) - float(original_centers[cj][0]))
if l_diff_orig <= 10.0:
if _is_red(original_centers[ci]) and _is_green(original_centers[cj]):
dark_idx, light_idx = ci, cj
elif _is_red(original_centers[cj]) and _is_green(original_centers[ci]):
dark_idx, light_idx = cj, ci
if protected[dark_idx] and protected[light_idx]:
continue
orig_dark_l = float(original_centers[dark_idx][0])
orig_light_l = float(original_centers[light_idx][0])
dark_is_red = _is_red(original_centers[dark_idx])
dark_is_green = _is_green(original_centers[dark_idx])
light_is_green = _is_green(original_centers[light_idx])
if is_rg_pair:
red_c = original_centers[dark_idx] if dark_is_red else original_centers[light_idx]
green_c = original_centers[dark_idx] if dark_is_green else original_centers[light_idx]
# Aim at the detector's L*-gap exit (22), not the paper's 30 -
# gentler than the documented target and reachable with chroma.
rg_gap_target = RG_L_GAP_EXIT
pair_dark_steps = rg_dark_steps
pair_light_steps = rg_green_light_steps if light_is_green else rg_light_steps
dark_chroma_options = red_chroma if dark_is_red else green_chroma
light_chroma_options = green_chroma if light_is_green else red_chroma
else:
rg_gap_target = min(effective_base_gap, MILD_GAP_CAP)
pair_dark_steps = dark_steps
pair_light_steps = light_steps
dark_chroma_options = other_chroma
light_chroma_options = other_chroma
pair_l_cap = min(l_cap, 12.0) if is_rg_pair else MILD_SOFT_L_BUDGET
if is_rg_pair:
dark_l_budget = min(l_cap, MILD_RED_DOWN_CAP if dark_is_red else MILD_GREEN_UP_CAP)
light_l_budget = min(l_cap, MILD_GREEN_UP_CAP if light_is_green else MILD_RED_DOWN_CAP)
else:
dark_l_budget = light_l_budget = min(l_cap, pair_l_cap)
dark_base = modified_centers[dark_idx]
light_base = modified_centers[light_idx]
if protected[dark_idx]:
pair_dark_steps = np.array([0.0])
dark_chroma_options = neutral_chroma
dark_l_budget = 0.0
if is_rg_pair and not protected[light_idx]:
light_l_budget = min(float(max_delta_l), RG_L_GAP_EXIT)
pair_light_steps = single_rg_steps_light
elif not protected[light_idx]:
light_l_budget = min(float(max_delta_l), 12.0)
pair_light_steps = np.array([0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0])
if protected[light_idx]:
pair_light_steps = np.array([0.0])
light_chroma_options = neutral_chroma
light_l_budget = 0.0
if is_rg_pair and not protected[dark_idx]:
dark_l_budget = min(float(max_delta_l), RG_L_GAP_EXIT)
pair_dark_steps = single_rg_steps_dark
elif not protected[dark_idx]:
dark_l_budget = min(float(max_delta_l), 12.0)
pair_dark_steps = np.array([0.0, -2.0, -4.0, -6.0, -8.0, -10.0, -12.0])
conflict_set = set(conflict_pairs)
def _scope(idx: int) -> str:
if cluster_labels and idx < len(cluster_labels):
return cluster_labels[idx].lower().split(" #")[0].split(" (")[0].strip()
return str(idx)
dark_scope = _scope(dark_idx)
light_scope = _scope(light_idx)
unconflicted_dark = [
idx for idx in range(len(modified_centers))
if idx != dark_idx and idx != light_idx
and (min(dark_idx, idx), max(dark_idx, idx)) not in conflict_set
and not protected[idx]
and _scope(idx) != dark_scope
]
unconflicted_light = [
idx for idx in range(len(modified_centers))
if idx != dark_idx and idx != light_idx
and (min(light_idx, idx), max(light_idx, idx)) not in conflict_set
and not protected[idx]
and _scope(idx) != light_scope
]
del_current = _delivered(modified_centers, original_centers)
sim_current = _simulate_lab_center(del_current, sim_matrix)
best_score = -np.inf
best_dark = dark_base.copy()
best_light = light_base.copy()
for dl_dark in pair_dark_steps:
for dl_light in pair_light_steps:
for c_dark in dark_chroma_options:
for c_light in light_chroma_options:
for o_step in ortho_steps:
cand_dark = _candidate_center(
dark_base, dl_dark, c_dark, max_delta_chroma,
ortho_dir=ortho_dir, ortho_step=-o_step,
)
cand_light = _candidate_center(
light_base, dl_light, c_light, max_delta_chroma,
ortho_dir=ortho_dir, ortho_step=o_step,
)
if abs(float(cand_dark[0]) - orig_dark_l) > dark_l_budget:
continue
if abs(float(cand_light[0]) - orig_light_l) > light_l_budget:
continue
# Normal-vision naturalness: green must NEVER be pushed down b* into blue/cyan.
if _is_any_green(original_centers[dark_idx]) and (cand_dark[2] < original_centers[dark_idx][2] - 0.5 or cand_dark[2] < 6.0):
continue
if _is_any_green(original_centers[light_idx]) and (cand_light[2] < original_centers[light_idx][2] - 0.5 or cand_light[2] < 6.0):
continue
# Judge candidates at DELIVERED strength (chroma
# ships at 45%), or the search clears pairs that
# collapse again after reconstruction.
del_dark = original_centers[dark_idx] + (cand_dark - original_centers[dark_idx]) * np.array([1.0, MILD_CHROMA_RETAIN, MILD_CHROMA_RETAIN])
del_light = original_centers[light_idx] + (cand_light - original_centers[light_idx]) * np.array([1.0, MILD_CHROMA_RETAIN, MILD_CHROMA_RETAIN])
sim_dark = _simulate_lab_center(del_dark, sim_matrix)
sim_light = _simulate_lab_center(del_light, sim_matrix)
de_sim = delta_e_fn(sim_dark, sim_light)
l_gap = float(cand_light[0] - cand_dark[0])
l_move = 0.5 * (
abs(float(cand_dark[0]) - orig_dark_l)
+ abs(float(cand_light[0]) - orig_light_l)
)
mean_drift = 0.5 * (
delta_e_fn(original_centers[dark_idx], del_dark)
+ delta_e_fn(original_centers[light_idx], del_light)
)
# Chroma-first: reward separation, then penalise
# lightness movement so the search only spends L*
# when chroma alone cannot reach the bar. A pair
# is cleared by EITHER detector exit: dE >= bar,
# or (red-green) a simulated L* gap >= 22.
pair_bar = 35.0 if is_rg_pair else 24.0
sim_l_gap = abs(float(sim_dark[0]) - float(sim_light[0]))
cleared = (de_sim >= pair_bar) or (
is_rg_pair and sim_l_gap >= RG_L_GAP_EXIT
)
score = 0.0
score += min(de_sim, pair_bar + 8.0) * 20.0
if is_rg_pair and l_gap < 0.0:
score -= 400.0
if cleared:
score += 400.0
score -= max(0.0, l_gap - rg_gap_target) * 6.0
else:
# Distance to the nearest exit. For rg pairs
# the L* gap exit is usually closer.
de_short = max(0.0, pair_bar - de_sim)
if is_rg_pair:
l_short = max(0.0, RG_L_GAP_EXIT - sim_l_gap) * 1.6
score -= min(de_short, l_short) * 60.0
else:
score -= de_short * 60.0
score -= l_move * 30.0
score -= max(0.0, mean_drift - 12.0) * 8.0
score -= _clipping_penalty(cand_dark) * 8.0
score -= _clipping_penalty(cand_light) * 8.0
for nb_idx in unconflicted_dark:
de_nb = delta_e_fn(sim_dark, sim_current[nb_idx])
if de_nb < 14.0:
score -= (14.0 - de_nb) * 10.0
for nb_idx in unconflicted_light:
de_nb = delta_e_fn(sim_light, sim_current[nb_idx])
if de_nb < 14.0:
score -= (14.0 - de_nb) * 10.0
if score > best_score:
best_score = score
best_dark = cand_dark
best_light = cand_light
if (
delta_e_fn(modified_centers[dark_idx], best_dark) > 1e-6
or delta_e_fn(modified_centers[light_idx], best_light) > 1e-6
):
changed = True
modified_centers[dark_idx] = best_dark
modified_centers[light_idx] = best_light
if not changed:
break
return modified_centers
def enforce_rg_separation_mild(
centers: np.ndarray,
target_gap: Optional[float] = None,
l_min: float = 5.0,
l_max: float = 95.0,
conflict_pairs: Optional[List[Tuple[int, int]]] = None,
cluster_labels: Optional[List[str]] = None,
pinned_indices: Optional[set] = None,
severity: float = 1.0,
cvd_type: str = "deutan",
original_centers: Optional[np.ndarray] = None,
max_delta_l: Optional[float] = None,
) -> np.ndarray:
"""Safety net aimed at the detector's L*-gap exit.
The conflict detector stops flagging a red-green pair once the simulated
L* gap reaches 22, so the net targets that gap with up to +/-11 per side.
Gentler than the manuscript's own 30-point target, and chroma-first has
already done part of the work by the time this runs.
The clusters in red-green pairs are moved as two GROUPS (reds down, greens
up), not pair by pair. Per-pair moves cancel when a mid-lightness cluster
sits in several pairs -- one pair pushes it down, the next pushes it back.
"""
out = centers.copy().astype(np.float64)
if out.shape[0] == 0:
return out
if target_gap is None:
target_gap = _effective_target_gap(severity, cvd_type, base_target=30.0)
target_gap = min(float(target_gap), 22.0)
cap = 11.0 if max_delta_l is None else min(float(max_delta_l), 11.0)
orig = original_centers if original_centers is not None else centers
if conflict_pairs is None or len(conflict_pairs) == 0:
return out
# Collect the clusters that appear in red-green pairs, grouped by hue sign.
red_idx: List[int] = []
green_idx: List[int] = []
for ci, cj in conflict_pairs:
for a, b in ((ci, cj), (cj, ci)):
if _is_red(orig[a]) and _is_green(orig[b]):
if a not in red_idx:
red_idx.append(a)
if b not in green_idx:
green_idx.append(b)
if not red_idx or not green_idx:
return out
def _is_pinned(i: int) -> bool:
if pinned_indices and i in pinned_indices:
return True
if cluster_labels is not None and i < len(cluster_labels):
nl = cluster_labels[i].lower()
if "background" in nl or "paper" in nl or "person" in nl or "exclude" in nl:
return True
if "text" in nl and _is_neutral(orig[i]):
return True
return False
red_free = [i for i in red_idx if not _is_pinned(i)]
green_free = [i for i in green_idx if not _is_pinned(i)]
if not red_free and not green_free:
return out
red_l = float(np.median(out[red_idx, 0]))
green_l = float(np.median(out[green_idx, 0]))
gap = green_l - red_l
shortfall = target_gap - abs(gap)
if shortfall <= 0.0:
return out
# Preserve current polarity (green above red by convention when tied).
red_down = gap >= 0.0
def _shift_group(indices: List[int], amount: float, down: bool, current_cap: float = cap) -> float:
spent = 0.0
for i in indices:
lo = max(l_min, float(orig[i, 0]) - current_cap)
hi = min(l_max, float(orig[i, 0]) + current_cap)
new_l = float(out[i, 0]) - amount if down else float(out[i, 0]) + amount
new_l = float(np.clip(new_l, lo, hi))
spent += abs(new_l - float(out[i, 0]))
out[i, 0] = new_l
return spent
# Split the shortfall across whichever sides are free to move.
if red_free and green_free:
if red_down:
green_cap = min(cap, MILD_GREEN_UP_CAP)
red_cap = min(22.0 if max_delta_l is None else float(max_delta_l), MILD_RED_DOWN_CAP)
g_move = min(shortfall / 2.0, green_cap)
r_move = shortfall - g_move
_shift_group(green_free, g_move, down=False, current_cap=green_cap)
_shift_group(red_free, r_move, down=True, current_cap=red_cap)
else:
half = shortfall / 2.0
_shift_group(red_free, half, down=False, current_cap=cap)
_shift_group(green_free, half, down=True, current_cap=cap)
elif red_free:
single_cap = 22.0 if max_delta_l is None else min(float(max_delta_l), 22.0)
_shift_group(red_free, shortfall, down=red_down, current_cap=single_cap)
else:
single_cap = (
min(float(max_delta_l) if max_delta_l is not None else MILD_GREEN_UP_CAP, MILD_GREEN_UP_CAP)
if red_down
else (22.0 if max_delta_l is None else min(float(max_delta_l), 22.0))
)
_shift_group(green_free, shortfall, down=not red_down, current_cap=single_cap)
return out
def rescan_and_reencode_mild(
original_centers,
modified_centers,
severity,
cvd_type,
*,
use_fast=False,
cluster_labels=None,
pinned_indices=None,
cluster_weights=None,
conflict_max_clusters=None,
already_seen=None,
**reencode_kwargs,
):
"""Re-detect conflicts on the whole image after a mix; re-encode leftovers."""
import numpy as np
from .conflict import detect_conflicts
from .reencoding import _simulate_lab_center
from .cvd_simulation import get_simulation_matrix
sim_matrix = get_simulation_matrix(severity, cvd_type)
seen = set(tuple(sorted(p)) for p in (already_seen or []))
found_all = []
original_centers = np.asarray(original_centers, dtype=np.float64)
modified_centers = np.asarray(modified_centers, dtype=np.float64).copy()
for _ in range(MILD_RESCAN_PASSES):
# Detect on the delivered centers (chroma ships at 45%), not the raw mix.
delivered = _delivered(modified_centers, original_centers)
sim_after = _simulate_lab_center(delivered, sim_matrix)
found = detect_conflicts(
original_centers,
sim_after,
cvd_type,
use_fast=use_fast,
max_clusters=conflict_max_clusters,
cluster_weights=cluster_weights,
cluster_labels=cluster_labels,
pinned_indices=pinned_indices,
after_reencode=True,
)
if not found:
break
found_all.extend(found)
for p in found:
seen.add(tuple(sorted(p)))
modified_centers = reencode_mild(
modified_centers,
found,
severity,
cvd_type,
use_fast=use_fast,
cluster_labels=cluster_labels,
pinned_indices=pinned_indices,
original_centers=original_centers,
**reencode_kwargs,
)
modified_centers = enforce_rg_separation_mild(
modified_centers,
conflict_pairs=found,
cluster_labels=cluster_labels,
pinned_indices=pinned_indices,
original_centers=original_centers,
severity=severity,
cvd_type=cvd_type,
max_delta_l=reencode_kwargs.get("max_delta_l"),
)
return modified_centers, found_all
def reencode_legacy(*args, **kwargs):
"""Today's mixer. Kept so the team can A/B without git archaeology."""
return reencode(*args, **kwargs)