Skip to content

feat(model): add UniADet for zero- and few-shot anomaly detection - #3947

Draft
rajeshgangireddy wants to merge 3 commits into
open-edge-platform:mainfrom
rajeshgangireddy:feature/add_uniadet
Draft

rajeshgangireddy wants to merge 3 commits into
open-edge-platform:mainfrom
rajeshgangireddy:feature/add_uniadet

Conversation

@rajeshgangireddy

@rajeshgangireddy rajeshgangireddy commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

📝 Description

  • Adds UniADet to the Anomalib Engine for zero- and few-shot anomaly detection with CLIP, DINOv2-R, and DINOv3. Few-shot adds normal reference images without retraining.
  • Uses the authors' pretrained heads, so nothing is trained on the target data. Anomalib matches all 96 published MVTec AD and VisA averages within 0.07 percentage points.
  • UniADet code and heads are Apache-2.0. The heads are gated on Hugging Face and not bundled; backbone licenses are separate.
  • UniADet keeps raw scores. Two shared fixes: PostProcessor now applies calibrated thresholds when normalization is disabled (previously a fixed 0.5), and INT8_ACQ export ranks rollback with the configured validation metric.

✨ 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.

Validation

Paper / Anomalib (%) with the authors' pretrained heads and evaluation protocol. AP is area under the precision–recall curve.

Dataset Backbone Shots Image AUROC Pixel AUROC Image AP Pixel AP
MVTec AD CLIP 0 92.4 / 92.40 91.8 / 91.83 96.1 / 96.08 42.8 / 42.81
MVTec AD CLIP 4 97.7 / 97.75 96.3 / 96.30 98.6 / 98.58 58.8 / 58.85
MVTec AD DINOv2-R 0 93.5 / 93.50 92.4 / 92.44 97.2 / 97.23 50.9 / 50.85
MVTec AD DINOv2-R 4 98.7 / 98.73 97.7 / 97.65 99.2 / 99.22 65.4 / 65.44
MVTec AD DINOv3 0 94.0 / 93.98 93.2 / 93.20 97.1 / 97.14 52.7 / 52.69
MVTec AD DINOv3 4 98.2 / 98.20 97.9 / 97.90 98.8 / 98.80 69.0 / 68.99
VisA CLIP 0 88.0 / 87.96 95.8 / 95.76 89.1 / 89.11 28.0 / 27.98
VisA CLIP 4 93.3 / 93.26 98.0 / 97.96 93.8 / 93.78 36.7 / 36.67
VisA DINOv2-R 0 91.3 / 91.27 95.9 / 95.87 91.0 / 90.99 32.7 / 32.73
VisA DINOv2-R 4 96.9 / 96.92 98.6 / 98.58 96.8 / 96.80 45.2 / 45.25
VisA DINOv3 0 91.9 / 91.94 96.3 / 96.34 92.8 / 92.80 32.5 / 32.54
VisA DINOv3 4 97.1 / 97.03 98.4 / 98.38 96.6 / 96.57 45.5 / 45.47
1- and 2-shot results
Dataset Backbone Shots Image AUROC Pixel AUROC Image AP Pixel AP
MVTec AD CLIP 1 96.3 / 96.32 95.6 / 95.60 98.1 / 98.08 55.7 / 55.74
MVTec AD CLIP 2 97.0 / 96.94 95.9 / 95.91 98.3 / 98.31 57.0 / 56.98
MVTec AD DINOv2-R 1 97.6 / 97.54 97.1 / 97.11 98.6 / 98.62 63.1 / 63.07
MVTec AD DINOv2-R 2 98.0 / 97.96 97.4 / 97.40 98.9 / 98.94 64.1 / 64.14
MVTec AD DINOv3 1 97.2 / 97.16 97.4 / 97.39 98.1 / 98.11 66.5 / 66.51
MVTec AD DINOv3 2 97.6 / 97.65 97.7 / 97.64 98.6 / 98.57 67.5 / 67.51
VisA CLIP 1 89.3 / 89.34 97.5 / 97.51 90.6 / 90.56 32.8 / 32.82
VisA CLIP 2 92.3 / 92.33 97.8 / 97.81 93.0 / 92.99 35.8 / 35.84
VisA DINOv2-R 1 95.2 / 95.24 98.2 / 98.18 95.6 / 95.56 42.1 / 42.14
VisA DINOv2-R 2 96.1 / 96.15 98.4 / 98.44 96.3 / 96.27 44.2 / 44.25
VisA DINOv3 1 95.8 / 95.82 98.2 / 98.17 95.5 / 95.55 44.0 / 43.94
VisA DINOv3 2 96.8 / 96.79 98.3 / 98.32 96.3 / 96.31 45.3 / 45.25

Engine.test on MVTec AD matches the author-protocol replay within 0.044 percentage points of mean AUROC.

MVTec AD: author-protocol replay / Engine AUROC (%) at seed 10
Backbone Shots Image AUROC Pixel AUROC
CLIP 0 92.390 / 92.431 91.829 / 91.847
CLIP 4 98.000 / 97.956 96.293 / 96.298
DINOv2-R 0 93.499 / 93.487 92.443 / 92.460
DINOv2-R 4 98.730 / 98.727 97.627 / 97.640
DINOv3 0 93.979 / 93.982 93.200 / 93.205
DINOv3 4 98.285 / 98.294 97.895 / 97.905

OpenVINO weight-only INT8 and PTQ reached maximum map errors of 0.066 and 0.39, respectively (limit 0.05).
ACQ reached 0.38 and flipped 2 of 18 labels with either batch size.

Sample anomaly maps

VisA, DINOv2-R at four shots with the authors' MVTec AD head. Left to right: input, ground truth, anomaly map, predicted mask.

VisA chewing gum: input, ground truth, anomaly map, and predicted mask

VisA circuit board: input, ground truth, anomaly map, and predicted mask

VisA capsules: input, ground truth, anomaly map, and predicted mask

Adapted from VisA, Zou et al. (2022), CC BY 4.0.

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

Copilot AI balanced review requested due to automatic review settings November 23, 2026 22:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The report can accept CSV results from a mismatched run, verification claims conflict with final-source hashes, and DINOv3 initialization and CLIP feature projection lack targeted regression coverage.

4 open issues
What changed in this PR

Adds UniADet to Anomalib’s model registry for zero- and few-shot anomaly detection using frozen CLIP, DINOv2-R, and DINOv3 encoders with the existing Engine.

Changes:

  • Adds encoder adapters, prototype heads, auxiliary loss, and example configuration.
  • Extends quantization support and adds offline inference, export, data, and reporting tests.
  • Adds documentation, licensing notices, and measured training, reproduction, and deployment artifacts, distinguishing author-head evaluation from independent training.
File Description
uv.lock Aligns the minimum timm requirement.
tools/​uniadet/​report.py Aggregates validated benchmark sweeps and paper comparisons.
tools/​uniadet/​paper_results.csv Records published comparison values.
tools/​uniadet/​author.py Downloads and converts authorized author heads.
tools/​uniadet/​__init__.py Defines the reproduction-tools package.
third-party-programs.txt Attributes VisA sample imagery.
tests/​unit/​models/​image/​uniadet/​test_report.py Tests aggregation and invalid-result rejection.
tests/​unit/​models/​image/​uniadet/​test_quantization.py Tests compression and accuracy-control behavior.
tests/​unit/​models/​image/​uniadet/​test_inference.py Tests Engine, checkpoint, and export agreement.
tests/​unit/​models/​image/​uniadet/​test_diagnosis.py Tests scoring diagnostics and protocol validation.
tests/​unit/​models/​image/​uniadet/​test_data.py Tests augmentation, calibration, and reference handling.
tests/​unit/​models/​image/​uniadet/​conftest.py Supplies a deterministic offline encoder.
tests/​integration/​model/​test_models.py Integrates UniADet into model lifecycle tests.
src/​anomalib/​models/​image/​uniadet/​loss.py Implements auxiliary classification and segmentation losses.
src/​anomalib/​models/​image/​uniadet/​head.py Implements multi-scale cosine prototype heads.
src/​anomalib/​models/​image/​uniadet/​backbones.py Adapts frozen encoders and author feature conventions.
src/​anomalib/​models/​image/​uniadet/​__init__.py Exports UniADet from its package.
src/​anomalib/​models/​image/​__init__.py Registers UniADet among image models.
src/​anomalib/​models/​components/​base/​export_mixin/​mixin.py Extends quantization options and validation outputs.
src/​anomalib/​models/​__init__.py Exposes UniADet through the public model API.
results/​UniADet/​training/​dinov3_visa/​training.json Records DINOv3 training on VisA.
results/​UniADet/​training/​dinov3_mvtec/​training.json Records DINOv3 training on MVTec AD.
results/​UniADet/​training/​dinov2_visa/​training.json Records DINOv2 training on VisA.
results/​UniADet/​training/​dinov2_mvtec/​training.json Records DINOv2 training on MVTec AD.
results/​UniADet/​training/​clip_visa/​training.json Records CLIP training on VisA.
results/​UniADet/​training/​clip_mvtec/​training.json Records CLIP training on MVTec AD.
results/​UniADet/​training_summary.csv Consolidates training timings and provenance.
results/​UniADet/​reproduction/​verification.json Records reproduction verification claims and limitations.
results/​UniADet/​reproduction/​summary.json Summarizes author-head evaluation and deployment.
results/​UniADet/​reproduction/​quantization/​weight_only_runtime_probe.json Records weight-compression runtime probes.
results/​UniADet/​reproduction/​quantization/​author_weights_probe.json Compares author-graph weight-compression variants.
results/​UniADet/​reproduction/​quantization/​author_smoothquant_probe.json Records SmoothQuant parameter probes.
results/​UniADet/​reproduction/​quantization/​author_sigmoid_probe.json Measures error amplification through sigmoid scoring.
results/​UniADet/​reproduction/​quantization/​activation_group_probe.json Isolates activation-quantization error sources.
results/​UniADet/​reproduction/​protocol.json Documents evaluation protocol and limitations.
results/​UniADet/​reproduction/​frozen_backbones.json Records backbone snapshots and checksums.
results/​UniADet/​reproduction/​clip_precision_deltas.csv Compares CLIP precision policies.
results/​UniADet/​initial_recipe/​training/​dinov2_visa/​training.json Preserves initial DINOv2 VisA training provenance.
results/​UniADet/​initial_recipe/​training/​dinov2_mvtec/​training.json Preserves initial DINOv2 MVTec training provenance.
results/​UniADet/​initial_recipe/​training/​clip_visa/​training.json Preserves initial CLIP VisA training provenance.
results/​UniADet/​initial_recipe/​training/​clip_mvtec/​training.json Preserves initial CLIP MVTec training provenance.
results/​UniADet/​initial_recipe/​smoke/​quantization_dinov2/​parity.json Records initial quantization smoke results.
results/​UniADet/​initial_recipe/​smoke/​dinov3_visa/​training.json Records a DINOv3 training smoke run.
results/​UniADet/​initial_recipe/​smoke/​dinov2_visa/​training.json Records a DINOv2 training smoke run.
results/​UniADet/​initial_recipe/​smoke/​clip_visa/​training.json Records a CLIP training smoke run.
results/​UniADet/​initial_recipe/​smoke/​benchmark_dinov2/​evaluation.json Records benchmark smoke-run configuration.
results/​UniADet/​initial_recipe/​smoke/​benchmark_dinov2/​categories.csv Preserves benchmark smoke measurements.
results/​UniADet/​initial_recipe/​sample_images.json Preserves initial sample-selection provenance.
results/​UniADet/​initial_recipe/​provenance.json Explains superseded recipe differences.
results/​UniADet/​initial_recipe/​diagnostics/​metric_cashew/​evaluation.json Records the initial cashew evaluation setup.
results/​UniADet/​initial_recipe/​diagnostics/​metric_cashew/​categories.csv Preserves initial cashew metrics.
results/​UniADet/​initial_recipe/​deployment/​dinov2_mvtec_0_quantized_legacy/​parity.json Preserves legacy deployment measurements.
results/​UniADet/​diagnosis/​training_resize_alignment.json Measures training interpolation differences.
results/​UniADet/​diagnosis/​training_convergence.json Records auxiliary training loss curves.
results/​UniADet/​diagnosis/​preprocessing.json Measures image-resizing differences.
results/​UniADet/​diagnosis/​paired_feature_normalization/​summary.json Summarizes paired feature-normalization experiments.
results/​UniADet/​diagnosis/​paired_feature_normalization/​categories.csv Records per-category normalization comparisons.
results/​UniADet/​diagnosis/​metric_harness.json Compares metric implementations on fixed cases.
results/​UniADet/​diagnosis/​dinov2_scoring/​summary.json Summarizes scoring-component experiments.
results/​UniADet/​diagnosis/​dinov2_scoring/​categories.csv Records per-category scoring variants.
results/​UniADet/​diagnosis/​dinov2_reference_sampling/​summary.json Summarizes reference-sampling experiments.
results/​UniADet/​diagnosis/​dinov2_reference_sampling/​categories.csv Records reference-sampling comparison metrics.
results/​UniADet/​diagnosis/​caa_alignment.json Records augmentation supervision checks.
results/​UniADet/​diagnosis/​augmentation_distribution.json Records augmentation frequencies and recipe differences.
results/​UniADet/​deployment/​dinov3_mvtec_4_activation/​parity.json Records DINOv3 four-shot activation quantization.
results/​UniADet/​deployment/​dinov3_mvtec_0_activation/​parity.json Records DINOv3 zero-shot activation quantization.
results/​UniADet/​deployment/​dinov2_mvtec_4_activation/​ptq_diagnosis.json Investigates DINOv2 quantization error sources.
results/​UniADet/​deployment/​dinov2_mvtec_4_activation/​parity.json Records DINOv2 four-shot activation quantization.
results/​UniADet/​deployment/​dinov2_mvtec_0_activation/​ptq.json Records DINOv2 zero-shot post-training quantization.
results/​UniADet/​deployment/​clip_mvtec_4_activation/​parity.json Records CLIP four-shot activation quantization.
results/​UniADet/​deployment/​clip_mvtec_0_activation/​parity.json Records CLIP zero-shot activation quantization.
results/​UniADet/​activation_accuracy_control.json Records auxiliary-domain accuracy-control measurements.
pyproject.toml Requires timm 1.0.20 or later.
examples/​configs/​model/​uniadet.yaml Adds a UniADet configuration template.
docs/​source/​markdown/​guides/​reference/​models/​image/​uniadet.md Documents usage, benchmarks, deployment, and licensing.
docs/​source/​markdown/​guides/​reference/​models/​image/​index.md Adds UniADet to model documentation navigation.
docs/​source/​images/​uniadet/​results/​LICENSE Documents sample-panel attribution and licensing.
docs/​source/​images/​uniadet/​architecture.svg Illustrates the detector architecture.
CHANGELOG.md Announces UniADet under Unreleased.

Review effort: Balanced

Comment thread tools/uniadet/report.py Resolved
Copilot AI balanced review requested due to automatic review settings November 23, 2026 23:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unchecked CSV identifiers can misattribute measurements to the wrong run, while INT8_ACQ output-based ranking can produce undefined rollback scores for matching all-zero predictions and labels.

5 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 24, 2026 08:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The shared exporter forces ACQ into output-based ranking that can divide by zero on normal predictions, producing NaN/Inf and undefined rollback ordering across models.

7 open issues

Review effort: Balanced

Comment thread tools/uniadet/data.py Resolved
Comment thread tools/uniadet/validate.py Resolved
Copilot AI balanced review requested due to automatic review settings November 30, 2026 22:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 09:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 10:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integrate the authors' pretrained detection heads with CLIP, DINOv2-R,
and DINOv3, including few-shot reference memory and deployment support.
Keep raw model scores and support metric-based OpenVINO quantization
rollback. Add offline tests, configuration, and documentation.

Signed-off-by: Rajesh Gangireddy <rajesh.gangireddy@intel.com>
Signed-off-by: Rajesh Gangireddy <rajesh.gangireddy@intel.com>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 11:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new public methods lack required object annotations on *args and **kwargs, leaving implicit Any types in the public API.

1 open issue
1 resolved since last review

Review effort: Balanced

"""Collect few-shot references before prediction."""
self._initialize_references()

def forward(self, batch: torch.Tensor, *args, **kwargs) -> InferenceBatch:
Copilot AI balanced review requested due to automatic review settings December 1, 2026 11:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Two new public methods leave *args and **kwargs untyped, introducing implicit Any into their API signatures.

1 open issue

Review effort: Balanced

This branch has not been deployed

No deployments
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.

2 participants