Feat/clustering models#19
Merged
Merged
Conversation
Introduce pyvisim/clustering with KMeans, GaussianMixtureModel and PCA, models that own the underlying scikit-learn estimator and expose the attributes the encoders need (cluster_centers, weights, means, covariances, n_components, n_features_in, ...) through typed getters. The models take the scikit-learn constructor parameters directly and are created unfitted; this prepares for removing scikit-learn objects from the encoder constructors. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…earn objects Breaking change: VLADEncoder and FisherVectorEncoder no longer accept scikit-learn estimators (kmeans_model/gmm_model/pca) in their constructors. VLAD always uses K-Means and Fisher Vectors always use a GMM, so the encoders now build the matching pyvisim.clustering models themselves from the parameters passed at initialization: n_clusters/n_components plus the optional kmeans_params/gmm_params and pca_params dictionaries, whose entries are forwarded verbatim to the underlying scikit-learn estimators. - learn() no longer takes n_clusters/kwargs; it fits the models that were configured at initialization. A configured PCA is now applied (and fitted first if necessary) before fitting the clustering model; previously it was silently reset with a warning. - All scikit-learn attribute access (cluster_centers_, weights_, means_, covariances_, n_features_in_, ...) goes through the clustering and PCA model getters. - Dimension validation is skipped for unfitted models and applies once the models are fitted. - The default RootSIFT feature extractor moved into ImageEncoderBase. - Loading pretrained KMeansWeights/GMMWeights still works; the loaded estimators are adopted by the corresponding pyvisim models. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Encoders can now persist their learned state to a versioned .encoder file (fitted clustering model, PCA model and normalization hyperparameters) and be restored from it via the load_from_disk classmethod. The feature extractor and similarity function are not serialized and are provided again at load time; dimension validation runs on restore. This is the designated replacement for loading pretrained models via the KMeansWeights/GMMWeights enums. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Passing the weights enums to the encoder constructors now emits a DeprecationWarning; the enums and the loading path will be removed in a future release in favor of save_to_disk()/load_from_disk() with .encoder files. The enum docstrings carry the same notice. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Quickstart now configures the encoder from parameters, calls learn() and shows save_to_disk/load_from_disk with .encoder files. Document the kmeans_params/gmm_params/pca_params dictionaries in the encoders README and mark KMeansWeights/GMMWeights loading as deprecated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new pyvisim.clustering subpackage (typed wrappers around scikit-learn estimators) and refactors VLAD/Fisher encoders to configure clustering/PCA at initialization rather than accepting raw sklearn objects, while also adding .encoder persistence and deprecating enum-based pretrained weight loading.
Changes:
- Added
pyvisim.clusteringwrappers (KMeans,GaussianMixtureModel,PCA) with a shared fitted-state interface. - Refactored
VLADEncoder/FisherVectorEncoderto build and fit clustering/PCA models configured vian_clusters/n_components+*_paramsdicts. - Added
save_to_disk()/load_from_disk()encoder persistence and updated docs with deprecation guidance forKMeansWeights/GMMWeights.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Documents .encoder save/load and warns that enum-based pretrained weights are deprecated. |
| pyvisim/init.py | Exposes the new clustering subpackage in the public package surface. |
| pyvisim/encoders/_base_encoder.py | Central refactor: default RootSIFT, new clustering/PCA handling, persistence API, deprecation warning path. |
| pyvisim/encoders/vlad.py | Updates VLAD to configure internal KMeans/PCA wrappers at init and use wrapper getters. |
| pyvisim/encoders/fisher_vector.py | Updates Fisher Vector to configure internal GMM/PCA wrappers at init and use wrapper getters. |
| pyvisim/encoders/README.md | Documents the new params-at-init encoder API and persistence workflow. |
| pyvisim/clustering/init.py | Defines clustering public exports (KMeans, GaussianMixtureModel, PCA, base type). |
| pyvisim/clustering/_base_clustering.py | Adds shared sklearn-backed base with is_fitted, fit, and adoption from sklearn estimators. |
| pyvisim/clustering/kmeans.py | Adds KMeans wrapper exposing n_clusters, cluster_centers, predict. |
| pyvisim/clustering/gmm.py | Adds GMM wrapper exposing weights/means/covariances/predict_proba with diag-covariance constraint. |
| pyvisim/clustering/pca.py | Adds PCA wrapper exposing fitted n_components and transform. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…to be greater than 0, and an integer)
…es use different format
…MM (instead of warning and mutating like currently done)
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.
Related Issues
Proposed Changes
This PR introduces a
pyvisim.clusteringsub-package and refactors the encoder API around it. It contains one breaking change.New:
pyvisim/clustering/module (feat(clustering))KMeans,GaussianMixtureModel, andPCAclustering models, each owning the underlying scikit-learn estimator and exposing typed getters for the attributes the encoders need (cluster_centers,weights,means,covariances,n_components,n_features_in, etc.).Breaking change: encoder params-at-init API (
refactor(encoders)!)VLADEncoderandFisherVectorEncoderno longer accept scikit-learn estimators (kmeans_model,gmm_model,pca) in their constructors.pyvisim.clusteringmodels themselves fromn_clusters/n_componentsand optionalkmeans_params/gmm_params/pca_paramsdicts forwarded verbatim to the underlying sklearn estimators.learn()no longer takesn_clusters/kwargs; it fits models configured at initialization. PCA is now applied (and fitted first if needed) before fitting the clustering model.RootSIFTfeature extractor moved intoImageEncoderBase.KMeansWeights/GMMWeightsstill works; the loaded estimators are adopted by the corresponding pyvisim models.New: encoder persistence (
feat(encoders))save_to_disk()/load_from_disk()serialize the fitted clustering model, PCA model, and normalization hyperparameters to a versioned.encoderfile.KMeansWeights/GMMWeightsloading.Deprecation:
KMeansWeights/GMMWeightsloading (feat(encoders))DeprecationWarning. The enums and the loading path will be removed in a future release.Docs (
docs)README.mdand addedpyvisim/encoders/README.mddocumenting the new params-at-init API,kmeans_params/gmm_params/pca_paramsdicts, andsave_to_disk/load_from_diskwith.encoderfiles.KMeansWeights/GMMWeightsas deprecated in docstrings and README.How did you test it?
make test-typesandmake fmtpass on the branch.learn(),save_to_disk(), andload_from_disk()exercised via the existing test suite and manually against thegetting_startednotebook.Notes for the reviewer
KMeansWeights/GMMWeightsloading is still functional but deprecated — callers should migrate tosave_to_disk/load_from_disk.pyvisim/clustering/_base_clustering.pyabstract base holds the shared interface; reviewers may want to verify the getter contracts match what_base_encoder.pyconsumes.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.