-
Notifications
You must be signed in to change notification settings - Fork 846
feat(anomalydino): Introduces anomalydino with semgrep "fixes" #3193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
* init commit Signed-off-by: Alfie Roddan <[email protected]> * add cdist instead of euclidean distance, divide by 2 for cosine and improve comments Signed-off-by: Alfie Roddan <[email protected]> * remove redundant euclidean distance Signed-off-by: Alfie Roddan <[email protected]> * Add AnomalyDINO to model list. Also alphabetically re-order some models Signed-off-by: Alfie Roddan <[email protected]> * add precision modifier Signed-off-by: Alfie Roddan <[email protected]> * remove fit comments. small typo of shape dimensions Signed-off-by: Alfie Roddan <[email protected]> * update docs for anomaly dino Signed-off-by: Alfie Roddan <[email protected]> * cleanup docstrings Signed-off-by: Alfie Roddan <[email protected]> * add unit tests for anomalydino. change distance computation from cdist to matmul, work with half tensors Signed-off-by: Alfie Roddan <[email protected]> * add vit/dino implementation (no xformers). implement factory class for generating dinov2. update anomaly_dino to use factory method Signed-off-by: Alfie Roddan <[email protected]> * change dinov2loader to factory method, remove duplicated components from dinomaly Signed-off-by: Alfie Roddan <[email protected]> * add from_name back Signed-off-by: Alfie Roddan <[email protected]> * add tests for vit and dinov2loader Signed-off-by: Alfie Roddan <[email protected]> * update docstrings Signed-off-by: Alfie Roddan <[email protected]> * fix(accelerator): Adding name method in XPUAccelerator (#3108) * Update xpu.py regarind PR #3092 Added the name method to fix an issue related to a newly added feature in lightning 2.5.6 Signed-off-by: Niclas <[email protected]> * Update xpu.py Signed-off-by: Niclas <[email protected]> * Update xpu.py Signed-off-by: Niclas <[email protected]> * Update xpu.py with docstring Signed-off-by: Niclas <[email protected]> * Update xpu.py with correct docstring Signed-off-by: Niclas <[email protected]> * added name method for XPUAccelerator Signed-off-by: waschsalz <[email protected]> --------- Signed-off-by: Niclas <[email protected]> Signed-off-by: waschsalz <[email protected]> Signed-off-by: Alfie Roddan <[email protected]> * change licesning with meta. Tensor is torch.Tensor. remove __future__. Signed-off-by: Alfie Roddan <[email protected]> * Update src/anomalib/models/components/dinov2/layers/block.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/attention.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/dino_head.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/drop_path.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/layer_scale.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/mlp.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/patch_embed.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/layers/swiglu_ffn.py Signed-off-by: Samet Akcay <[email protected]> * Update src/anomalib/models/components/dinov2/vision_transformer.py Signed-off-by: Samet Akcay <[email protected]> --------- Signed-off-by: Alfie Roddan <[email protected]> Signed-off-by: Niclas <[email protected]> Signed-off-by: waschsalz <[email protected]> Signed-off-by: Samet Akcay <[email protected]> Co-authored-by: Niclas <[email protected]> Co-authored-by: Samet Akcay <[email protected]> Co-authored-by: Samet Akcay <[email protected]> Co-authored-by: Rajesh Gangireddy <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces the AnomalyDINO model for anomaly detection using DINOv2 vision transformers. The implementation refactors shared DINOv2 components into a common anomalib.models.components.dinov2 module that can be used by both AnomalyDINO and Dinomaly models. Semgrep exceptions are applied to allow safe model weight loading with weights_only=True.
Key Changes:
- Introduces AnomalyDINO model with memory-bank-based anomaly detection
- Refactors DINOv2 vision transformer and loader components into shared components module
- Migrates Dinomaly to use the new shared DINOv2 components
Reviewed changes
Copilot reviewed 31 out of 31 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
src/anomalib/models/image/anomaly_dino/torch_model.py |
Core PyTorch implementation of AnomalyDINO with feature extraction and kNN-based scoring |
src/anomalib/models/image/anomaly_dino/lightning_model.py |
Lightning wrapper for AnomalyDINO training and inference |
src/anomalib/models/components/dinov2/vision_transformer.py |
Shared DINOv2 vision transformer implementation |
src/anomalib/models/components/dinov2/dinov2_loader.py |
Shared loader for DINOv2 pretrained weights |
src/anomalib/models/components/dinov2/layers/ |
Shared DINOv2 layer components (attention, blocks, MLP, etc.) |
src/anomalib/models/image/dinomaly/torch_model.py |
Updated to use shared DINOv2 loader |
src/anomalib/models/image/dinomaly/components/ |
Removed duplicated DINOv2 loader and layers, now imports from shared components |
tests/unit/models/image/anomaly_dino/test_torch_model.py |
Unit tests for AnomalyDINO torch model |
tests/unit/models/components/dinov2/ |
Unit tests for shared DINOv2 components |
examples/configs/model/anomaly_dino.yaml |
Configuration example for AnomalyDINO |
docs/source/markdown/guides/reference/models/image/anomaly_dino.md |
Documentation for AnomalyDINO model |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 31 out of 31 changed files in this pull request and generated 5 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "Dinomaly", | ||
| "Dfm", |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import order in __all__ should maintain alphabetical sorting. Dinomaly appears after Dfm but should come after Dfkde alphabetically. Move Dinomaly to line 79 (between Dfkde and Dfm) to maintain consistency.
| "Dinomaly", | |
| "Dfm", | |
| "Dfm", | |
| "Dinomaly", |
| "EfficientAd", | ||
| "Fastflow", | ||
| "Fre", | ||
| "Fuvas", |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The __all__ list should maintain alphabetical order. Fuvas appears after Fre but Ganomaly follows immediately after. The ordering is inconsistent with the alphabetical pattern. Move Fuvas after Fre but ensure all entries maintain strict alphabetical order.
| w0 = w // self.patch_size | ||
| h0 = h // self.patch_size | ||
| m = int(math.sqrt(n_pos)) | ||
| assert n_pos == m * m |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion lacks an error message. If the assertion fails, users will see only 'AssertionError' without context. Add a descriptive message: assert n_pos == m * m, f'Expected {m * m} positional embeddings but got {n_pos}'
| assert n_pos == m * m | |
| assert n_pos == m * m, f'Expected {m * m} positional embeddings but got {n_pos}' |
| A tuple ``(h, w)``. | ||
| """ | ||
| if isinstance(x, tuple): | ||
| assert len(x) == 2 |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion lacks an error message explaining why the tuple must have length 2. Add a descriptive message: assert len(x) == 2, f'Expected tuple of length 2, got {len(x)}'
| assert len(x) == 2 | |
| assert len(x) == 2, f'Expected tuple of length 2, got {len(x)}' |
| if nlayers == 1: | ||
| return nn.Linear(in_dim, bottleneck_dim, bias=bias) | ||
|
|
||
| assert hidden_dim is not None, "hidden_dim must be provided when nlayers > 1" |
Copilot
AI
Dec 4, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message could be more helpful by indicating the actual value of nlayers. Change to: assert hidden_dim is not None, f'hidden_dim must be provided when nlayers ({nlayers}) > 1'
| assert hidden_dim is not None, "hidden_dim must be provided when nlayers > 1" | |
| assert hidden_dim is not None, f"hidden_dim must be provided when nlayers ({nlayers}) > 1" |
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Signed-off-by: Barabanov, Alexander <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
Signed-off-by: Barabanov, Alexander <[email protected]>
Signed-off-by: Barabanov, Alexander <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
📝 Description
AnomalyDino implemented by @alfieroddanintel at : #3105
However, semgrep was failing even after following the safe way of loading models.
Therefore, we apply semgrep exception on top.
✨ Changes
Select what type of change your PR is:
✅ Checklist
Before you submit your pull request, please make sure you have completed the following steps:
For more information about code review checklists, see the Code Review Checklist.