""" AdaptFormer inference for satellite change detection. Downloads a pre-trained AdaptFormer model from HuggingFace Hub and runs tile-based inference on arbitrary-size image pairs, producing a binary change mask compatible with the rest of the detection pipeline. Falls back gracefully when torch/transformers are not installed. """ import logging import os from pathlib import Path import cv2 import numpy as np logger = logging.getLogger(__name__) _ROOT = Path(__file__).resolve().parent.parent _MODEL = None _PROCESSOR = None _DEVICE = None _MODEL_ID = "deepang/adaptformer-LEVIR-CD" _TILE_SIZE = 256 # LEVIR-CD native patch size _AVAILABLE = None _LOAD_FAILED = False _LOAD_ERROR: str | None = None _LOADED_FROM: str | None = None _CALIBRATED_THRESHOLD: float | None = None _LOGITS_SHAPE_LOGGED = False # Prefer frozen v3 Delhi fine-tune over older exports / hub pretrained. _LOCAL_WEIGHT_CANDIDATES = ( "models/adaptformer_delhi/v3_frozen", "models/adaptformer_delhi/best_v3", "models/adaptformer_delhi/best", ) def _try_import(): try: import torch from transformers import AutoImageProcessor, AutoModel return torch, AutoImageProcessor, AutoModel except ImportError: return None, None, None def _looks_like_weights_dir(path: Path) -> bool: if not path.is_dir(): return False return ( (path / "config.json").is_file() or (path / "model.safetensors").is_file() or (path / "pytorch_model.bin").is_file() ) def _as_weights_path(raw: str) -> Path | None: """Resolve env/relative weight paths against the repo root (not CWD).""" text = (raw or "").strip() if not text: return None path = Path(text) if not path.is_absolute(): path = (_ROOT / path).resolve() return path if _looks_like_weights_dir(path) else None def _resolve_weights_source() -> str: """Prefer local Delhi fine-tune (v3_frozen) over HuggingFace pretrained.""" env = os.environ.get("ADAPTFORMER_WEIGHTS", "").strip() if env: resolved = _as_weights_path(env) if resolved is not None: return str(resolved) logger.warning( "ADAPTFORMER_WEIGHTS=%r is not a usable weights dir; " "trying conventional local exports then hub fallback", env, ) for rel in _LOCAL_WEIGHT_CANDIDATES: resolved = _as_weights_path(rel) if resolved is not None: return str(resolved) return _MODEL_ID def _env_threshold() -> float | None: """Explicit threshold override: ADAPTFORMER_THRESHOLD or DETECTION_DL_THRESHOLD.""" for key in ("ADAPTFORMER_THRESHOLD", "DETECTION_DL_THRESHOLD"): raw = os.environ.get(key, "").strip() if not raw: continue try: return float(raw) except ValueError: logger.warning("Ignoring invalid %s=%r", key, raw) return None def _load_threshold_sidecar(source: str) -> float | None: """Load val-calibrated threshold.json next to local AdaptFormer weights.""" env_thr = _env_threshold() if env_thr is not None: return env_thr if not source or source == _MODEL_ID: return None path = os.path.join(source, "threshold.json") if not os.path.isfile(path): return None try: import json data = json.loads(open(path, encoding="utf-8").read()) thr = float(data.get("threshold")) logger.info("Loaded AdaptFormer calibrated threshold=%s from %s", thr, path) return thr except Exception as exc: logger.warning("Could not read %s: %s", path, exc) return None def get_calibrated_threshold(default: float = 0.5) -> float: """Return val-calibrated DL threshold (loads model sidecar on first use).""" global _CALIBRATED_THRESHOLD if _CALIBRATED_THRESHOLD is not None: return _CALIBRATED_THRESHOLD env_thr = _env_threshold() if env_thr is not None: _CALIBRATED_THRESHOLD = env_thr return _CALIBRATED_THRESHOLD # Ensure weights (and sidecar) are resolved even if predict hasn't run yet try: _load_model() except Exception: pass if _CALIBRATED_THRESHOLD is not None: return _CALIBRATED_THRESHOLD return default def get_loaded_weights_source() -> str | None: """Path/id actually loaded into memory (None until first successful load).""" return _LOADED_FROM def _load_model(): global _MODEL, _PROCESSOR, _DEVICE, _AVAILABLE, _LOAD_FAILED, _LOAD_ERROR global _LOADED_FROM, _CALIBRATED_THRESHOLD if _MODEL is not None: return _MODEL, _PROCESSOR if _LOAD_FAILED: raise RuntimeError("AdaptFormer load previously failed") torch, AutoImageProcessor, AutoModel = _try_import() if torch is None: raise RuntimeError("PyTorch/transformers not installed") _DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") cache_dir = os.environ.get("HF_HOME", None) source = _resolve_weights_source() logger.info("Loading AdaptFormer from %s ...", source) try: _PROCESSOR = AutoImageProcessor.from_pretrained( source, cache_dir=cache_dir, trust_remote_code=True) _MODEL = AutoModel.from_pretrained( source, cache_dir=cache_dir, trust_remote_code=True) _MODEL.to(_DEVICE) _MODEL.eval() _AVAILABLE = True _LOAD_ERROR = None _LOADED_FROM = source _CALIBRATED_THRESHOLD = _load_threshold_sidecar(source) logger.info("AdaptFormer loaded on %s from %s", _DEVICE, source) except Exception as exc: # Fall back to HF hub if local fine-tune dir fails if source != _MODEL_ID: logger.warning("Local weights failed (%s); falling back to %s", exc, _MODEL_ID) try: _PROCESSOR = AutoImageProcessor.from_pretrained( _MODEL_ID, cache_dir=cache_dir, trust_remote_code=True) _MODEL = AutoModel.from_pretrained( _MODEL_ID, cache_dir=cache_dir, trust_remote_code=True) _MODEL.to(_DEVICE) _MODEL.eval() _AVAILABLE = True _LOAD_ERROR = None _LOADED_FROM = _MODEL_ID _CALIBRATED_THRESHOLD = _load_threshold_sidecar(_MODEL_ID) logger.info("AdaptFormer loaded on %s from hub fallback", _DEVICE) return _MODEL, _PROCESSOR except Exception as exc2: exc = exc2 _LOAD_FAILED = True _AVAILABLE = False _LOAD_ERROR = str(exc) logger.error("AdaptFormer load failed: %s", exc) raise return _MODEL, _PROCESSOR def is_model_available(): """True only if PyTorch is installed and the model loads successfully.""" global _AVAILABLE if _AVAILABLE is not None: return _AVAILABLE if _LOAD_FAILED: return False try: _load_model() return True except Exception: return False def preload_model(): """Warm-load AdaptFormer at app startup (best-effort).""" global _LOAD_ERROR try: _load_model() logger.info("AdaptFormer preload complete") return True except Exception as exc: _LOAD_ERROR = str(exc) logger.warning("AdaptFormer preload skipped: %s", exc) return False def get_model_status() -> dict: """Status for /health — shows whether AI detection or classical fallback is active.""" try: from .detection_config import get_fusion_mode fusion = get_fusion_mode() except Exception: fusion = "dl_only" if _AVAILABLE is True: mode = f"adaptformer_{fusion}" available = True elif _LOAD_FAILED: mode = "classical_fallback" available = False else: available = is_model_available() mode = f"adaptformer_{fusion}" if available else "classical_fallback" loaded = _LOADED_FROM or _resolve_weights_source() status = { "modelId": loaded, "hubModelId": _MODEL_ID, "loadedFrom": _LOADED_FROM, "available": available, "detectionMode": mode, "fusion": fusion, "device": str(_DEVICE) if _DEVICE is not None else None, "calibratedThreshold": get_calibrated_threshold(0.2) if available else None, "tta": [op or "identity" for op in _resolve_tta_ops()], "error": _LOAD_ERROR, } try: from .detection_config import get_ensemble_mode from .cd_models.bit_cd import bit_status status["ensemble"] = {"enabled": get_ensemble_mode(), **bit_status()} except Exception: pass return status def _logits_to_change_prob(logits, torch): """Robustly convert model logits to a single-channel change probability tile. Handles 1-channel (sigmoid), 2+-channel (softmax, change=last channel) and already-2D outputs so a change in the upstream model head does not silently break inference. Validated for ``deepang/adaptformer-LEVIR-CD``: logits are ``(N, 2, H, W)`` with channel 0 = no-change and channel 1 = change. Using softmax last channel is correct (see ``scripts/finetune_adaptformer.py`` output probe). """ t = logits if t.dim() == 4: # (N, C, H, W) c = t.shape[1] if c == 1: return torch.sigmoid(t)[0, 0] return torch.softmax(t, dim=1)[0, -1] if t.dim() == 3: # (C, H, W) or (N, H, W) — ambiguous without metadata if t.shape[0] == 1: return torch.sigmoid(t)[0] if t.shape[0] == 2: # Prefer (C,H,W) two-class head (AdaptFormer); change = channel 1 return torch.softmax(t, dim=0)[1] # ambiguous: treat as (N,H,W) single map logger.warning( "Ambiguous 3D logits shape %s — treating as (N,H,W) with sigmoid; " "expected AdaptFormer layout is (N,2,H,W)", tuple(t.shape), ) return torch.sigmoid(t[0]) return torch.sigmoid(t) def _log_logits_shape_once(logits) -> None: """Log / assert expected AdaptFormer logits layout on first inference.""" global _LOGITS_SHAPE_LOGGED if _LOGITS_SHAPE_LOGGED: return _LOGITS_SHAPE_LOGGED = True shape = tuple(logits.shape) logger.info("AdaptFormer outputs.logits.shape=%s (source=%s)", shape, _LOADED_FROM) # Expected for deepang/adaptformer-LEVIR-CD and Delhi fine-tunes: (N, 2, H, W) if logits.dim() == 4 and logits.shape[1] == 2: return if logits.dim() == 4 and logits.shape[1] == 1: logger.warning("AdaptFormer logits are 1-channel %s — using sigmoid path", shape) return logger.warning( "Unexpected AdaptFormer logits shape %s — verify _logits_to_change_prob mapping", shape, ) def _resolve_tta_ops(): """Choose test-time augmentation flips. Env DETECTION_TTA: off|0 | hflip | full | auto.""" mode = os.environ.get("DETECTION_TTA", "auto").strip().lower() if mode in ("0", "off", "none", "false"): return [None] if mode in ("h", "hflip"): return [None, "h"] if mode in ("full", "all"): return [None, "h", "v"] # auto: lighter on CPU (hflip only), fuller on GPU on_cuda = _DEVICE is not None and str(_DEVICE).startswith("cuda") return [None, "h", "v"] if on_cuda else [None, "h"] def _apply_flip(arr, op): if op == "h": return arr[:, ::-1] if op == "v": return arr[::-1, :] return arr def _unflip_map(score, op): if op == "h": return score[:, ::-1] if op == "v": return score[::-1, :] return score def _infer_score_map(img1, img2, tile_stats=None): """Single-pass tiled inference returning a float32 change-probability map at (h, w). The model always sees its native 256px patches; the shared tiler handles padding, sliding windows and cosine blending so stitching stays consistent across every deep model in the project. Tiles that are near-identical between img1/img2 skip the model call entirely (see ``detection_config.get_skip_unchanged_threshold``); pass ``tile_stats`` to accumulate ``{"totalTiles", "skippedTiles"}`` for observability. """ torch, _, _ = _try_import() model, processor = _load_model() from PIL import Image as PILImage from .cd_models.model_utils import tiled_score_map from .detection_config import get_tile_batch, get_skip_unchanged_threshold def _score_tile(t1, t2): pil1 = PILImage.fromarray(t1) pil2 = PILImage.fromarray(t2) inputs = processor(images=(pil1, pil2), return_tensors="pt") inputs = {k: v.to(_DEVICE) for k, v in inputs.items()} outputs = model(**inputs) logits = outputs.logits _log_logits_shape_once(logits) return _logits_to_change_prob(logits, torch).cpu().numpy() def _score_batch(pairs): # Stack each pair's processed tensors along the batch dim for one forward # pass. Only used when the model's output batch dim matches the number of # input pairs; otherwise fall back to correct per-tile scoring. n = len(pairs) try: per_pair = [ processor(images=(PILImage.fromarray(t1), PILImage.fromarray(t2)), return_tensors="pt") for t1, t2 in pairs ] keys = per_pair[0].keys() per_pair_bs = per_pair[0][next(iter(keys))].shape[0] stacked = {k: torch.cat([p[k] for p in per_pair], dim=0).to(_DEVICE) for k in keys} outputs = model(**stacked) logits = outputs.logits _log_logits_shape_once(logits) if logits.shape[0] != n * per_pair_bs: raise RuntimeError("unexpected batched logits layout") group = per_pair_bs return [ _logits_to_change_prob(logits[i * group:(i + 1) * group], torch).cpu().numpy() for i in range(n) ] except Exception as exc: logger.warning("Batched tile inference failed (%s); using per-tile", exc) return [_score_tile(t1, t2) for t1, t2 in pairs] batch = get_tile_batch() try: from .detection_config import get_tile_overlap overlap_frac = get_tile_overlap() except Exception: overlap_frac = 0.35 overlap_px = max(1, int(round(_TILE_SIZE * overlap_frac))) with torch.no_grad(): return tiled_score_map(_score_tile, img1, img2, tile_size=_TILE_SIZE, overlap=overlap_px, score_batch_fn=_score_batch if batch > 1 else None, batch=batch, skip_unchanged_threshold=get_skip_unchanged_threshold(), stats=tile_stats) def _count_windows(full_h, full_w, tile_size, overlap): step = max(1, int(round(tile_size * (1.0 - overlap)))) ny = len(range(0, max(1, full_h - tile_size + 1), step)) + (1 if full_h > tile_size else 0) nx = len(range(0, max(1, full_w - tile_size + 1), step)) + (1 if full_w > tile_size else 0) return max(1, ny) * max(1, nx) def predict_change_score_windowed(path_a, path_b, out_h, out_w, tile_size=512, overlap=0.25, on_progress=None): """Build a change-probability map from two large GeoTIFFs via disk windows. Reads paired native-resolution windows (so the model sees full detail), scores each with AdaptFormer, then accumulates into a bounded ``(out_h, out_w)`` canvas with cosine blending. Peak memory stays at one native window plus the small output canvas, so 10k+ rasters never OOM. ``on_progress(frac)`` (0..1) is called as windows complete. Returns a float32 score map in [0, 1] at (out_h, out_w). """ from .dda.geotiff_io import iter_geotiff_window_pairs, read_native_size _load_model() score_sum = np.zeros((out_h, out_w), dtype=np.float32) count = np.zeros((out_h, out_w), dtype=np.float32) scale_y = scale_x = None total = None native = read_native_size(__import__("pathlib").Path(path_a)) if native: total = _count_windows(native[1], native[0], tile_size, overlap) done = 0 for tile_a, tile_b, y0, x0, full_h, full_w in iter_geotiff_window_pairs( path_a, path_b, tile_size=tile_size, overlap=overlap): if scale_y is None: scale_y = out_h / float(full_h) scale_x = out_w / float(full_w) if total is None: total = _count_windows(full_h, full_w, tile_size, overlap) wh, ww = tile_a.shape[:2] score = _infer_score_map(tile_a, tile_b) dy0 = int(round(y0 * scale_y)) dx0 = int(round(x0 * scale_x)) dh = max(1, int(round(wh * scale_y))) dw = max(1, int(round(ww * scale_x))) dy1 = min(out_h, dy0 + dh) dx1 = min(out_w, dx0 + dw) dh, dw = dy1 - dy0, dx1 - dx0 if dh <= 0 or dw <= 0: continue score_ds = cv2.resize(score, (dw, dh), interpolation=cv2.INTER_AREA) wy = np.hanning(dh + 2)[1:-1] if dh > 2 else np.ones(dh) wx = np.hanning(dw + 2)[1:-1] if dw > 2 else np.ones(dw) weight = np.maximum(np.outer(wy, wx).astype(np.float32), 1e-3) score_sum[dy0:dy1, dx0:dx1] += score_ds * weight count[dy0:dy1, dx0:dx1] += weight done += 1 if on_progress and total: try: on_progress(min(1.0, done / float(total))) except Exception: pass count = np.maximum(count, 1e-6) return score_sum / count def _predict_score_tta(img1, img2, tile_stats=None): """TTA-averaged change score for one (already same-size) image pair.""" h, w = img1.shape[:2] ops = _resolve_tta_ops() acc = np.zeros((h, w), dtype=np.float32) n = 0 for op in ops: a1 = np.ascontiguousarray(_apply_flip(img1, op)) a2 = np.ascontiguousarray(_apply_flip(img2, op)) try: s = _infer_score_map(a1, a2, tile_stats=tile_stats) except Exception as exc: logger.warning("TTA pass %s failed: %s", op, exc) continue acc += _unflip_map(s, op) n += 1 if n == 0: raise RuntimeError("AdaptFormer inference produced no predictions") return acc / float(n) def predict_change_mask(img1, img2, threshold=None, tile_stats=None): """ Run AdaptFormer inference on two RGB numpy arrays (H, W, 3). Averages predictions over test-time augmentation flips (DETECTION_TTA) and, when DETECTION_MULTISCALE is set, fuses scores across scales (max) so both small and large changes are captured. Returns (uint8 mask [0 or 255], float32 score map [0-1]). Use threshold > 1.0 to obtain score map only (empty mask). When ``threshold`` is None, uses val-calibrated sidecar / ``ADAPTFORMER_THRESHOLD`` / ``DETECTION_DL_THRESHOLD`` (v3 Delhi = 0.2). Pass ``tile_stats`` (a dict) to accumulate tile pre-filter counters (``totalTiles``/``skippedTiles``) across every TTA/scale pass. """ _load_model() if threshold is None: threshold = get_calibrated_threshold(0.2) if img1.shape != img2.shape: img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0])) h, w = img1.shape[:2] from .detection_config import get_multiscale_scales scales = get_multiscale_scales() if not scales: avg_score = _predict_score_tta(img1, img2, tile_stats=tile_stats) else: fused = None for scale in scales: if scale == 1.0: s1, s2 = img1, img2 else: nh = max(64, int(round(h * scale))) nw = max(64, int(round(w * scale))) interp = cv2.INTER_AREA if scale < 1.0 else cv2.INTER_CUBIC s1 = cv2.resize(img1, (nw, nh), interpolation=interp) s2 = cv2.resize(img2, (nw, nh), interpolation=interp) score = _predict_score_tta(s1, s2, tile_stats=tile_stats) if score.shape != (h, w): score = cv2.resize(score, (w, h), interpolation=cv2.INTER_LINEAR) # Max fusion favors recall on small structures that only one scale sees fused = score if fused is None else np.maximum(fused, score) avg_score = fused avg_score = _maybe_ensemble_bit(img1, img2, avg_score) mask = (avg_score >= threshold).astype(np.uint8) * 255 return mask, avg_score def _maybe_ensemble_bit(img1, img2, adaptformer_score): """Average with BIT_CD when DETECTION_ENSEMBLE=on and its weights exist.""" try: from .detection_config import get_ensemble_mode if not get_ensemble_mode(): return adaptformer_score from .cd_models.bit_cd import bit_score_map, weights_available if not weights_available(): return adaptformer_score bit_score = bit_score_map(img1, img2) if bit_score is None: return adaptformer_score if bit_score.shape != adaptformer_score.shape: bit_score = cv2.resize( bit_score, (adaptformer_score.shape[1], adaptformer_score.shape[0]), interpolation=cv2.INTER_LINEAR, ) logger.info("Ensembling AdaptFormer + BIT_CD score maps") return 0.5 * adaptformer_score + 0.5 * bit_score except Exception as exc: logger.warning("BIT_CD ensemble skipped: %s", exc) return adaptformer_score