-
Notifications
You must be signed in to change notification settings - Fork 770
π 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
17
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.
Open
Changes from 8 commits
Commits
Show all changes
17 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 261ff80
Remove else branch
ashwinvaidya17 908c841
Merge branch 'main' into feat/model/uninet
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
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 .de_resnet 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,33 @@ | ||
"""UniNet Model for anomaly detection. | ||
|
||
This module implements anomaly detection using the UniNet model. It is a model designed for diverse domains and is | ||
suited for both supervised and unsupervised anomaly detection. It also focuses on multi-class anomaly detection. | ||
|
||
Example: | ||
>>> from anomalib.models import UniNet | ||
>>> from anomalib.data import MVTecAD | ||
>>> from anomalib.engine import Engine | ||
|
||
>>> # Initialize model and data | ||
>>> datamodule = MVTecAD() | ||
>>> model = UniNet() | ||
|
||
>>> # Train the model | ||
>>> engine = Engine() | ||
>>> engine.train(model=model, datamodule=datamodule) | ||
|
||
>>> # Get predictions | ||
>>> engine.predict(model=model, datamodule=datamodule) | ||
|
||
See Also: | ||
- :class:`UniNet`: Main model class for UniNet-based anomaly detection | ||
- :class:`UniNetModel`: PyTorch implementation of the UniNet model | ||
""" | ||
|
||
# Copyright (C) 2025 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .lightning_model import UniNet | ||
from .torch_model import UniNetModel | ||
|
||
__all__ = ["UniNet", "UniNetModel"] |
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,74 @@ | ||
"""Utilities for computing anomaly maps.""" | ||
|
||
# Original Code | ||
# Copyright (c) 2025 Shun Wei | ||
# https://github.com/pangdatangtt/UniNet | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Modified | ||
# Copyright (C) 2025 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import einops | ||
import torch | ||
from kornia.filters import gaussian_blur2d | ||
from torch.nn import functional as F # noqa: N812 | ||
|
||
|
||
def weighted_decision_mechanism( | ||
batch_size: int, | ||
output_list: list[list[torch.Tensor]], | ||
alpha: float, | ||
beta: float, | ||
output_size: tuple[int, int] = (256, 256), | ||
) -> tuple[torch.Tensor, torch.Tensor]: | ||
"""Compute anomaly maps using weighted decision mechanism. | ||
|
||
Args: | ||
batch_size (int): Batch size. | ||
output_list (list[list[torch.Tensor]]): List of output tensors. | ||
alpha (float): Alpha parameter. Used for controlling the upper limit | ||
beta (float): Beta parameter. Used for controlling the lower limit | ||
output_size (tuple[int, int]): Output size. | ||
|
||
Returns: | ||
tuple[torch.Tensor, torch.Tensor]: Anomaly score and anomaly map. | ||
""" | ||
total_weights_list = [] | ||
for i in range(batch_size): | ||
low_similarity_list = [torch.max(output_list[j][i]) for j in range(len(output_list))] | ||
probs = F.softmax(torch.tensor(low_similarity_list), dim=0) | ||
weight_list = [] # set P consists of L high probability values, where L ranges from n-1 to n+1 | ||
for idx, prob in enumerate(probs): | ||
weight_list.append(low_similarity_list[idx]) if prob > torch.mean(probs) else None | ||
weight = torch.max(torch.tensor([torch.mean(torch.tensor(weight_list)) * alpha, beta])) | ||
total_weights_list.append(weight) | ||
|
||
assert len(total_weights_list) == batch_size, "The number of weights do not match the number of samples" | ||
|
||
anomaly_maps: list[list[torch.Tensor]] = [[] for _ in output_list] | ||
for idx, output in enumerate(output_list): | ||
anomaly_map_ = torch.unsqueeze(output, dim=1) # Bx1xhxw | ||
# Bx256x256 | ||
anomaly_maps[idx] = F.interpolate(anomaly_map_, output_size, mode="bilinear", align_corners=True)[:, 0, :, :] | ||
|
||
processed_anomaly_maps: torch.Tensor = sum(anomaly_maps) | ||
|
||
anomaly_score = [] | ||
for idx in range(batch_size): | ||
top_k = int(output_size[0] * output_size[1] * total_weights_list[idx]) | ||
assert top_k >= 1 / (output_size[0] * output_size[1]), "weight can not be smaller than 1 / (H * W)!" | ||
|
||
single_anomaly_score_exp = processed_anomaly_maps[idx] | ||
single_anomaly_score_exp = gaussian_blur2d( | ||
einops.rearrange(single_anomaly_score_exp, "h w -> 1 1 h w"), # kornia expects 4D tensor | ||
kernel_size=(5, 5), | ||
sigma=(4, 4), | ||
).squeeze() | ||
assert single_anomaly_score_exp.reshape(1, -1).shape[-1] == output_size[0] * output_size[1], ( | ||
"something wrong with the last dimension of reshaped map!" | ||
) | ||
single_map = single_anomaly_score_exp.reshape(1, -1) | ||
single_anomaly_score = single_map.topk(top_k).values[0][0] | ||
anomaly_score.append(single_anomaly_score.detach()) | ||
return torch.vstack(anomaly_score), processed_anomaly_maps.detach() |
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,13 @@ | ||
"""PyTorch modules for the UniNet model implementation.""" | ||
|
||
# Copyright (C) 2025 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
from .attention_bottleneck import AttentionBottleneck, BottleneckLayer | ||
from .dfs import DomainRelatedFeatureSelection | ||
|
||
__all__ = [ | ||
"DomainRelatedFeatureSelection", | ||
"AttentionBottleneck", | ||
"BottleneckLayer", | ||
] | ||
ashwinvaidya17 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
copyright goes to the top?
Same for other files as well.