Building Your Anomaly Detection Model

Train an EfficientAD model that flags defects using only examples of normal images.

Anomaly Detection projects let you train a model to spot defects, damage or anything out of the ordinary, without having to label every possible type of defect. Instead of learning each class, the model learns what a normal (Pristine) image looks like, and flags images that differ from it as an Anomaly.

This makes Anomaly Detection a good fit for use cases like manufacturing inspection, where defects are rare, varied and hard to collect in large numbers.

This guide walks you through training an anomaly detection model on Datature Nexus, from creating your project to deploying your trained model.

Recap

  1. Create a project and select Anomaly Detection as the project type. Your project comes with two fixed tags, Pristine and Anomaly.
  2. Upload your images through the Dataset tab. See Uploading Data for supported formats.
  3. Tag each image as Pristine or Anomaly in the Annotator by selecting the tag and pressing A, or import your annotations as a CSV. Focus on collecting plenty of varied Pristine images, since the model only trains on these.
  4. Create a new workflow and select Anomaly Detection as your AI Vision objective in the workflow recommendation. The Dataset module becomes the Anomaly Detection Dataset module, where you set how many Pristine images to hold out for validation. Then pick one of the EfficientAD models in the Model module.
  5. Run your training and monitor it. You'll see the EfficientAD losses alongside Precision, Recall and F1 Score, and the evaluation previews show the predicted anomaly region on your validation images.
  6. Export your model to PyTorch, ONNX, OpenVINO, TensorFlow, TFLite, CoreML or TensorRT.
  7. Download your model to run it locally, or deploy it as an API. See Anomaly Detection Predictions for what the model returns and how to set the anomaly threshold.
📘

The project type cannot be changed after the project is created. If you already have a classification project with good and defective images, create a new Anomaly Detection project and import your annotations into it (see below).

What tags does an Anomaly Detection project use?

Every Anomaly Detection project comes with exactly two tags, which are created for you:

TagMeaning
PristineThe image is normal and contains no defects.
AnomalyThe image contains a defect or something out of the ordinary.

These tags are fixed. You can change their colours, but you cannot rename, delete or add tags in an Anomaly Detection project.

How do I annotate my images?

Each image takes a single image-level tag, either Pristine or Anomaly. Open an asset in the Annotator, select Pristine or Anomaly under the Tag panel on the right, then press A to assign it. To remove a tag, press D.

You do not need to draw boxes or masks around defects. The model works out where the anomaly is on its own, and returns a heatmap of the anomalous region at prediction time.

How much data do I need?

The model is trained only on Pristine images. Anomaly images are used purely to evaluate how well the model separates normal images from defective ones. This means:

  • Most of your effort should go into collecting a good variety of Pristine images that cover the normal variation you expect to see (lighting, position, background, and so on).
  • You should still tag a set of Anomaly images, so that your evaluation metrics are meaningful.

Each of the training and validation sets must contain at least 5 × your batch size images. For example, with the default batch size of 32 on EfficientAD Small 256x256, you need at least 160 Pristine images for training and at least 160 images in validation. If there aren't enough, the training run will fail with a message asking you to add more annotated assets.

Can I import existing annotations?

Yes. Anomaly Detection projects accept the same annotation upload formats as other projects, with a few rules:

  • Only the tags Pristine and Anomaly are accepted (case-insensitive). Any other tag name will be rejected.
  • Each image ends up with a single tag. If an image has any Anomaly annotation, including bounding boxes or masks, the whole image is tagged Anomaly. Otherwise it is tagged Pristine.
  • Bounding boxes and masks can only be tagged Anomaly. A box or mask tagged Pristine will be rejected.
  • For COCO files, images listed under images with no annotations are imported as Pristine.

The simplest format is a classification CSV with filename and label columns:

filename,label
bottle_001.png,Pristine
bottle_002.png,Pristine
bottle_003.png,Anomaly

What does the Anomaly Detection Dataset module do?

The Anomaly Detection Dataset module replaces the usual Dataset module in your workflow. Because the model only learns from normal images, the split works a little differently:

  • All Anomaly images go to the validation set.
  • Pristine images are split between training and validation according to the Pristine Dataset Split Ratio.
OptionsInputDescription
Pristine Dataset Split Ratio0 to 1Fraction of Pristine images held out for validation. The rest are used for training. Default: 0.3.
Shuffle DatasetTrue / FalseShuffles your images before splitting. Default: True.
Random SeedIntegerSeed used for shuffling, so you can reproduce the same split. Default: 0.
Advanced FiltersTags, asset statusesRestrict which assets are used, and choose up to 9 images for evaluation previews.

Class mapping, sliding window and class balancing are not available for Anomaly Detection projects.

The recommended workflow for Anomaly Detection is made of three modules: Anomaly Detection Dataset, Augmentation and an EfficientAD model.



Did this page help you?