Skip to content

Conversation

@rajeshgangireddy
Copy link
Contributor

📝 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:

  • 🚀 New feature (non-breaking change which adds functionality)
  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • 🔄 Refactor (non-breaking change which refactors the code base)
  • ⚡ Performance improvements
  • 🎨 Style changes (code style/formatting)
  • 🧪 Tests (adding/modifying tests)
  • 📚 Documentation update
  • 📦 Build system changes
  • 🚧 CI/CD configuration
  • 🔧 Chore (general maintenance)
  • 🔒 Security update
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)

✅ Checklist

Before you submit your pull request, please make sure you have completed the following steps:

  • 📚 I have made the necessary updates to the documentation (if 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.

For more information about code review checklists, see the Code Review Checklist.

alfieroddanintel and others added 2 commits December 4, 2025 16:50
* 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]>
Copilot AI review requested due to automatic review settings December 4, 2025 16:44
Copy link
Contributor

Copilot AI left a 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.

Copilot AI review requested due to automatic review settings December 4, 2025 16:59
Copy link
Contributor

Copilot AI left a 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.

Comment on lines 79 to 80
"Dinomaly",
"Dfm",
Copy link

Copilot AI Dec 4, 2025

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.

Suggested change
"Dinomaly",
"Dfm",
"Dfm",
"Dinomaly",

Copilot uses AI. Check for mistakes.
"EfficientAd",
"Fastflow",
"Fre",
"Fuvas",
Copy link

Copilot AI Dec 4, 2025

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.

Copilot uses AI. Check for mistakes.
w0 = w // self.patch_size
h0 = h // self.patch_size
m = int(math.sqrt(n_pos))
assert n_pos == m * m
Copy link

Copilot AI Dec 4, 2025

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}'

Suggested change
assert n_pos == m * m
assert n_pos == m * m, f'Expected {m * m} positional embeddings but got {n_pos}'

Copilot uses AI. Check for mistakes.
A tuple ``(h, w)``.
"""
if isinstance(x, tuple):
assert len(x) == 2
Copy link

Copilot AI Dec 4, 2025

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)}'

Suggested change
assert len(x) == 2
assert len(x) == 2, f'Expected tuple of length 2, got {len(x)}'

Copilot uses AI. Check for mistakes.
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"
Copy link

Copilot AI Dec 4, 2025

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'

Suggested change
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"

Copilot uses AI. Check for mistakes.
Signed-off-by: Barabanov, Alexander <[email protected]>
Copilot AI review requested due to automatic review settings December 5, 2025 22:40
Copy link
Contributor

Copilot AI left a 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]>
Copilot AI review requested due to automatic review settings December 5, 2025 22:56
Copy link
Contributor

Copilot AI left a 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.

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.

5 participants