Skip to content

Unify inference preprocessing and consolidate repository helpers - #7

Open
yxteo2 wants to merge 2 commits into
mainfrom
fix/inference-preprocessing
Open

yxteo2 wants to merge 2 commits into
mainfrom
fix/inference-preprocessing

Conversation

@yxteo2

@yxteo2 yxteo2 commented Sep 13, 2026 •

Copy link
Copy Markdown
Owner

Problem

Legacy FastFlow/SSN inference used floor division for center-crop mask/overlay offsets, while torchvision rounds crop anchors (including round-to-even ties). For example, resizing to 37×55 before a 32×48 crop requires offsets (2,4), not (2,3). This could shift masks and overlays by one pixel.

Changes

  • Centralize RGB resize, center crop, ImageNet normalization and mask geometry in anomaly_detection/preprocessing.py. Training's deterministic evaluation transform, shared inference and legacy scripts use the same implementation.
  • Shared AnomalyInferenceEngine supports all five YAML-built detectors and loads once for repeated images. Architecture, input size and crop scale come from checkpoint metadata; weights reload without downloads.
  • Preserve each detector's raw scoring contract, including FastFlow's top-1%-pixel mean and SSN's image head. Do not clamp statistical distance maps to probabilities.
  • Add optional source-size display maps, valid-region masks and crop geometry. Cropped-away borders are NaN/unobserved, not normal. Pixel metrics remain in crop space.
  • Match dataset mask kernels exactly; mismatched original mask/image dimensions fail explicitly.
  • Add legacy --crop_scale fallback; saved checkpoint metadata takes precedence. Requested unavailable CUDA now fails rather than silently switching to CPU.
  • Document all-model inference, legacy behavior, coordinate spaces, calibration and DINO padding (applied once inside the model).

Verification

180 CPU regression tests passed; the focused 27-test preprocessing suite was rerun after adding actual legacy/shared prediction comparisons and passed. Tests cover all five DINO-backed models, crop scales 0.875 and 1.0, incorrect fallback metadata, odd margins, rectangular/416/512/704 geometry, masks and source-size valid regions. Compileall, pyflakes and diff checks passed. No physical CUDA GPU was available; this does not certify GPU performance or accuracy.

Main remains unchanged pending merge.

Repository cleanup (supersedes #5)

Consolidates 11 duplicated metrics/calibration/file helpers into anomaly_detection/inference_utils.py, removing approximately 650 copied lines from the legacy scripts while preserving their import names and commands. All five detector classes are exposed through the modeling package; builder registries and trainer headers are clarified. Adds docs/repository_layout.md documenting necessary files, entry points and generated-file conventions. No tracked datasets, model weights, checkpoints or output artifacts were found to remove.

Updated verification: 187 CPU tests passed, including analytic AUPRO cases, calibration lookup and real YAML checkpoint inference metrics; compileall, pyflakes and diff checks passed. The earlier cleanup PR #5 is superseded by this compatible consolidation.

@yxteo2 yxteo2 changed the title Fix center-crop alignment and unify inference preprocessing for all detectors Unify inference preprocessing and consolidate repository helpers Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant