-
Notifications
You must be signed in to change notification settings - Fork 767
π feat(model): add UniNet #2797
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
Open
ashwinvaidya17
wants to merge
15
commits into
open-edge-platform:main
Choose a base branch
from
ashwinvaidya17:feat/model/uninet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,239
β32
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c495a84
stash changes
ashwinvaidya17 4fe02e4
Add initial code
ashwinvaidya17 2a17f28
Merge branch 'main' into feat/model/uninet
ashwinvaidya17 51fa496
Working model
ashwinvaidya17 dc7174a
Working model
ashwinvaidya17 aee930d
Fix pre-commit
ashwinvaidya17 b1d6121
Merge branch 'main' into feat/model/uninet
ashwinvaidya17 6f40e31
Minor refactor
ashwinvaidya17 9e50776
Merge branch 'main' into feat/model/uninet
ashwinvaidya17 55c6bad
Refactor components
ashwinvaidya17 38041b4
single line
ashwinvaidya17 7ff16ff
de_resnet->resnet_decoder
ashwinvaidya17 9e481f8
bug fixes
ashwinvaidya17 2745861
openvino export + pr comments
ashwinvaidya17 348b7ee
Add documentation
ashwinvaidya17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
docs/source/markdown/guides/reference/models/image/uninet.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# UniNet | ||
|
||
```{eval-rst} | ||
.. automodule:: anomalib.models.image.uninet.lightning_model | ||
:members: | ||
:show-inheritance: | ||
``` | ||
|
||
```{eval-rst} | ||
.. automodule:: anomalib.models.image.uninet.torch_model | ||
:members: | ||
:show-inheritance: | ||
``` | ||
|
||
```{eval-rst} | ||
.. automodule:: anomalib.models.image.uninet.components.loss | ||
:members: | ||
:show-inheritance: | ||
``` | ||
|
||
```{eval-rst} | ||
.. automodule:: anomalib.models.image.uninet.components.anomaly_map | ||
:members: | ||
:show-inheritance: | ||
``` | ||
|
||
```{eval-rst} | ||
.. automodule:: anomalib.models.image.uninet.components.attention_bottleneck | ||
:members: | ||
:show-inheritance: | ||
``` | ||
|
||
```{eval-rst} | ||
.. automodule:: anomalib.models.image.uninet.components.dfs | ||
:members: | ||
:show-inheritance: | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
model: | ||
class_path: anomalib.models.UniNet | ||
init_args: | ||
student_backbone: wide_resnet50_2 | ||
teacher_backbone: wide_resnet50_2 | ||
temperature: 0.1 | ||
|
||
trainer: | ||
max_epochs: 100 | ||
callbacks: | ||
- class_path: lightning.pytorch.callbacks.EarlyStopping | ||
init_args: | ||
patience: 20 | ||
monitor: image_AUROC | ||
mode: max |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Common backbone models. | ||
|
||
Example: | ||
>>> from anomalib.models.components.backbone import get_decoder | ||
>>> decoder = get_decoder() | ||
|
||
See Also: | ||
- :func:`anomalib.models.components.backbone.de_resnet`: | ||
Decoder network implementation | ||
""" | ||
|
||
# Copyright (C) 2025 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .resnet_decoder import get_decoder | ||
|
||
__all__ = ["get_decoder"] |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Copyright (c) 2025 Intel Corporation | ||
SPDX-License-Identifier: Apache-2.0 | ||
|
||
Some files in this folder are based on the original UniNet implementation by Shun Wei, Jielin Jiang, and Xiaolong Xu. | ||
|
||
Original license: | ||
----------------- | ||
|
||
MIT License | ||
|
||
Copyright (c) 2025 Shun Wei | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# UniNet: Unified Contrastive Learning Framework for Anomaly Detection | ||
|
||
This is the implementation of the UniNet model, a unified contrastive learning framework for anomaly detection presented in CVPR 2025. | ||
|
||
Model Type: Classification and Segmentation | ||
|
||
## Description | ||
|
||
UniNet is a contrastive learning-based anomaly detection model that uses a teacher-student architecture with attention bottleneck mechanisms. The model is designed for diverse domains and supports both supervised and unsupervised anomaly detection scenarios. It focuses on multi-class anomaly detection and leverages domain-related feature selection to improve performance across different categories. | ||
|
||
The model consists of: | ||
|
||
- **Teacher Networks**: Pre-trained backbone networks that provide reference features for normal samples | ||
- **Student Network**: A decoder network that learns to reconstruct normal patterns | ||
- **Attention Bottleneck**: Mechanisms that help focus on relevant features | ||
- **Domain-Related Feature Selection**: Adaptive feature selection for different domains | ||
- **Contrastive Loss**: Temperature-controlled similarity computation between student and teacher features | ||
|
||
During training, the student network learns to match teacher features for normal samples while being trained to distinguish anomalous patterns through contrastive learning. The model uses a weighted decision mechanism during inference to combine multi-scale features for final anomaly scoring. | ||
|
||
## Architecture | ||
|
||
The UniNet architecture leverages contrastive learning with teacher-student networks, incorporating attention bottlenecks and domain-specific feature selection for robust anomaly detection across diverse domains. | ||
|
||
## Usage | ||
|
||
`anomalib train --model UniNet --data MVTecAD --data.category <category>` | ||
|
||
## Benchmark | ||
|
||
All results gathered with seed `42`. | ||
|
||
## [MVTecAD Dataset](https://www.mvtec.com/company/research/datasets/mvtec-ad) | ||
|
||
### Image-Level AUC | ||
|
||
| Avg | Bottle | Cable | Capsule | Carpet | Grid | Hazelnut | Leather | Metal Nut | Pill | Screw | Tile | Toothbrush | Transistor | Wood | Zipper | | ||
| :---: | :----: | :---: | :-----: | :----: | :---: | :------: | :-----: | :-------: | :---: | :---: | :---: | :--------: | :--------: | :---: | :----: | | ||
| 0.956 | 0.999 | 0.982 | 0.939 | 0.896 | 0.996 | 0.999 | 1.000 | 1.000 | 0.816 | 0.919 | 0.970 | 1.000 | 0.984 | 0.993 | 0.945 | | ||
|
||
### Pixel-Level AUC | ||
|
||
| Avg | Bottle | Cable | Capsule | Carpet | Grid | Hazelnut | Leather | Metal Nut | Pill | Screw | Tile | Toothbrush | Transistor | Wood | Zipper | | ||
| :---: | :----: | :---: | :-----: | :----: | :---: | :------: | :-----: | :-------: | :---: | :---: | :---: | :--------: | :--------: | :---: | :----: | | ||
| 0.976 | 0.989 | 0.983 | 0.985 | 0.973 | 0.992 | 0.987 | 0.993 | 0.984 | 0.964 | 0.992 | 0.965 | 0.992 | 0.923 | 0.961 | 0.984 | | ||
|
||
### Image F1 Score | ||
|
||
| Avg | Bottle | Cable | Capsule | Carpet | Grid | Hazelnut | Leather | Metal Nut | Pill | Screw | Tile | Toothbrush | Transistor | Wood | Zipper | | ||
| :---: | :----: | :---: | :-----: | :----: | :---: | :------: | :-----: | :-------: | :---: | :---: | :---: | :--------: | :--------: | :---: | :----: | | ||
| 0.957 | 0.984 | 0.944 | 0.964 | 0.883 | 0.973 | 0.986 | 0.995 | 0.989 | 0.921 | 0.905 | 0.946 | 0.983 | 0.961 | 0.974 | 0.959 | | ||
|
||
## Model Features | ||
|
||
- **Contrastive Learning**: Uses temperature-controlled contrastive loss for effective anomaly detection | ||
- **Multi-Domain Support**: Designed to work across diverse domains with domain-related feature selection | ||
- **Flexible Training**: Supports both supervised and unsupervised training modes | ||
- **Attention Mechanisms**: Incorporates attention bottlenecks for focused feature learning | ||
- **Multi-Scale Features**: Leverages multi-scale feature matching for robust detection | ||
|
||
## Parameters | ||
|
||
- `student_backbone`: Backbone model for student network (default: "wide_resnet50_2") | ||
- `teacher_backbone`: Backbone model for teacher network (default: "wide_resnet50_2") | ||
- `temperature`: Temperature parameter for contrastive loss (default: 0.1) |
Oops, something went wrong.
Oops, something went wrong.
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.
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 return type annotation mentions 'ResNet' in the docstring but should be 'ResNetDecoder' to match the actual return type annotation.
Copilot uses AI. Check for mistakes.