Skip to content

🐞 fix(model): fix PCA's component count for a variance ratio and stop it modifying its input - #3920

Open
sohailshk wants to merge 2 commits into
open-edge-platform:mainfrom
sohailshk:fix/pca-components-and-inplace
Open

sohailshk wants to merge 2 commits into
open-edge-platform:mainfrom
sohailshk:fix/pca-components-and-inplace

Conversation

@sohailshk

Copy link
Copy Markdown

📝 Description

Four related problems in anomalib.models.components.PCA, which DFM, FUVAS and DFKDE use:

  1. One component too few for a variance ratio. For a float n_components, fit used torch.nonzero(variance_ratios >= n_components)[0] as the number of components. That is the zero-based index of the first component that reaches the ratio, so fit kept one component too few, and none when the first component alone reaches the ratio. It now keeps index + 1, the rule StreamingPCA in mh_patchcore/components.py already uses. Rank-3 data plus small noise (10 features), compared with sklearn.decomposition.PCA(n_components=ratio, svd_solver="full"):

    ratio sklearn main this PR
    0.9 2 1 2
    0.99 3 2 3
    0.999 3 2 3
    0.9 on rank-1 data 1 0 (singular_vectors shape (10, 0)) 1

    On 300 random datasets × 8 ratios the count now equals sklearn's in all 2,400 cases (main: in none). sklearn uses a strict >, so the two can only differ when a cumulative ratio equals the threshold exactly.

  2. Input changed in place. fit and transform did dataset -= mean / features -= self.mean on the caller's tensor. They now leave it unchanged.

  3. inverse_transform did not add the mean back, so inverse_transform(transform(x)) was not x: max error 3.19 on the test data (50 samples, 6 features around 3, all components kept), 4.8e-7 with this PR. DFM and FUVAS got the right reconstruction error only because transform had already centered their features in place. With 2 and 3 together their FRE is unchanged.

  4. fit_transform was a separate copy of fit. It used int(n_components), so a variance ratio gave 0 components. It also ran a full SVD (an N×N left singular matrix) and did not store singular_values. It now calls fit and then transform. fit treats only a float as a variance ratio, so an integer count such as DFKDE's n_pca_components=1 keeps one component (before, fit sent n_components=1 through the ratio branch). On 6000×512 features, fit_transform to 16 components went from 0.67 s to 0.10 s. fit also keeps the input dtype, so fit_transform still accepts float64.

Effect on trained models

  • DFM (pca_level=0.97) and FUVAS (pca_level=0.98) models trained after this change keep one more principal component, so their scores change. In a small DFM check (random-initialised resnet18 layer3, 40 synthetic 128×128 training images), the count went from 35 to 36 and test-image scores moved by 0.3% (median) and 3% (max). The MVTec benchmarks were not re-run.
  • Saved models are not affected: the component count, singular vectors and mean are checkpoint buffers. A DFM state_dict saved on main and loaded with this change gives the same FRE scores (max relative difference 1.3e-7).
  • DFKDE scores are unchanged up to float rounding (max predicted-probability difference 1.8e-7 on synthetic embeddings).
  • DFM with score_type="nll": DFM.fit calls fit(memory_bank) and then transform(memory_bank), so the in-place subtraction centered the features for the Gaussian twice. That path currently fails earlier (SingleClassGaussian.fit passes device= to torch.mean, which raises a TypeError), so no saved model is affected.
  • Direct users of PCA: fit/transform no longer modify their argument, and inverse_transform returns data in the original space, like sklearn.decomposition.PCA.inverse_transform.

Tests (tests/unit/models/components/dimensionality_reduction/test_pca.py) cover:

  • the component count for a variance ratio on rank-1/2/3 data
  • inputs unchanged after fit/transform/fit_transform
  • zero-mean projection of the training data
  • round trip with all components
  • fit_transform equal to fit + transform for a variance ratio

These 7 fail on main. Two guards pass on main and with this PR: n_components=1 keeps one component, and fit_transform keeps float64 input.

  • 🛠️ Fixes: no open issue

✨ Changes

  • 🐞 Bug fix (non-breaking change which fixes an issue)

✅ Checklist

  • 📚 I have made the necessary updates to the documentation (if applicable). (not applicable)
  • 🧪 I have written tests that support my changes and prove that my fix is effective or my feature works (if applicable).
  • 🏷️ My PR title follows conventional commit format.

Checks run: pytest tests/unit/models/components/dimensionality_reduction (9 passed; 7 fail on main), ruff check, ruff format --check (0.12.0) and mypy (1.16.1) on the touched files. Not run: the full suite, DFM/FUVAS/DFKDE integration tests (they download backbones), MVTec benchmarks, CUDA.

…t modifying its input

A variance ratio kept one component too few, fit and transform centered the caller's tensor in place, inverse_transform did not add the mean back and fit_transform ignored a variance ratio.

Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>

This branch has not been deployed

No deployments
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