Repository navigation
Conversation
sohailshk
requested review from
ashwinvaidya17,
rajeshgangireddy and
samet-akcay
as code owners
November 13, 2026 06:39
sohailshk
added a commit
to sohailshk/anomalib
that referenced
this pull request
Nov 13, 2026
…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>
sohailshk
force-pushed
the
fix/pca-components-and-inplace
branch
from
November 13, 2026 16:39
1e74177 to
4d062f0
Compare
4 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
Four related problems in
anomalib.models.components.PCA, which DFM, FUVAS and DFKDE use:One component too few for a variance ratio. For a float
n_components,fitusedtorch.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, sofitkept one component too few, and none when the first component alone reaches the ratio. It now keeps index + 1, the ruleStreamingPCAinmh_patchcore/components.pyalready uses. Rank-3 data plus small noise (10 features), compared withsklearn.decomposition.PCA(n_components=ratio, svd_solver="full"):singular_vectorsshape (10, 0))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.Input changed in place.
fitandtransformdiddataset -= mean/features -= self.meanon the caller's tensor. They now leave it unchanged.inverse_transformdid not add the mean back, soinverse_transform(transform(x))was notx: 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 becausetransformhad already centered their features in place. With 2 and 3 together their FRE is unchanged.fit_transformwas a separate copy offit. It usedint(n_components), so a variance ratio gave 0 components. It also ran a full SVD (an N×N left singular matrix) and did not storesingular_values. It now callsfitand thentransform.fittreats only a float as a variance ratio, so an integer count such as DFKDE'sn_pca_components=1keeps one component (before,fitsentn_components=1through the ratio branch). On 6000×512 features,fit_transformto 16 components went from 0.67 s to 0.10 s.fitalso keeps the input dtype, sofit_transformstill accepts float64.Effect on trained models
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 resnet18layer3, 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.state_dictsaved on main and loaded with this change gives the same FRE scores (max relative difference 1.3e-7).score_type="nll":DFM.fitcallsfit(memory_bank)and thentransform(memory_bank), so the in-place subtraction centered the features for the Gaussian twice. That path currently fails earlier (SingleClassGaussian.fitpassesdevice=totorch.mean, which raises a TypeError), so no saved model is affected.PCA:fit/transformno longer modify their argument, andinverse_transformreturns data in the original space, likesklearn.decomposition.PCA.inverse_transform.Tests (
tests/unit/models/components/dimensionality_reduction/test_pca.py) cover:fit/transform/fit_transformfit_transformequal tofit+transformfor a variance ratioThese 7 fail on main. Two guards pass on main and with this PR:
n_components=1keeps one component, andfit_transformkeeps float64 input.✨ Changes
✅ Checklist
Checks run:
pytest tests/unit/models/components/dimensionality_reduction(9 passed; 7 fail on main),ruff check,ruff format --check(0.12.0) andmypy(1.16.1) on the touched files. Not run: the full suite, DFM/FUVAS/DFKDE integration tests (they download backbones), MVTec benchmarks, CUDA.