Anomaly Detection Predictions

What an anomaly detection model returns, and how to choose an anomaly threshold.

An anomaly detection model doesn't predict classes directly. For every image, it produces:

  • an anomaly score between 0 and 1, where higher means more likely to be anomalous, and
  • an anomaly heatmap, which shows how anomalous each pixel is.

To turn these into a Pristine or Anomaly prediction, you compare the anomaly score against a threshold. This page explains how that works for exported models and for API deployments.

How do I run an exported model locally?

Anomaly detection models can be exported to PyTorch, ONNX, OpenVINO, TensorFlow, TFLite, CoreML and TensorRT (see Export Formats). Apart from TensorRT, each export includes the model file, a predict.py script, a requirements.txt and a README.md with setup instructions.

Install the requirements, then run:

python3 predict.py -i input_folder_path -o output_folder_path -m model_path

For each image, the script prints the anomaly score and saves two files to your output folder:

FileDescription
<name>Your original image with the anomaly heatmap overlaid on it.
heatmap_<name>The anomaly heatmap on its own.

The script does not apply a threshold. Compare the printed score against the threshold that suits your use case. An image is Anomaly if its score is above the threshold, and Pristine otherwise.

The export also includes a label map for reference. The model itself does not predict classes.

What does my API deployment return?

When you deploy an anomaly detection model as an API, the threshold is applied for you. Each prediction contains:

  1. An image-level classification, tagged Pristine or Anomaly.
  2. If the image is Anomaly, a mask covering the pixels whose anomaly value is above the threshold.
PredictionTagConfidenceBound
Image-levelPristine1 - anomaly scoreNone (boundType: "classification")
Image-levelAnomalyAnomaly scoreNone (boundType: "classification")
Region maskAnomalyAverage anomaly value inside the maskBounding box plus bitmask (boundType: "masks")

Here's an example response for an anomalous image:

[
  {
    "annotationId": 0,
    "confidence": 0.83,
    "tag": { "id": 1, "name": "Anomaly" },
    "boundType": "classification",
    "contourType": null,
    "bound": []
  },
  {
    "annotationId": 1,
    "confidence": 0.71,
    "tag": { "id": 1, "name": "Anomaly" },
    "boundType": "masks",
    "contourType": "bitmask",
    "bound": [[0.42, 0.18], [0.42, 0.35], [0.61, 0.35], [0.61, 0.18]],
    "bitmask": { "encoding": "CocoRle", "height": 256, "width": 256, "counts": "..." }
  }
]

The raw anomaly score and heatmap are not returned by the API. If you need them, run the exported model locally instead.

How do I change the anomaly threshold?

The threshold defaults to 0.5. You can set your own threshold on each request with the FilterConfidence routine, which applies to both the image-level prediction and the anomaly mask:

{
  "image_type": "base64",
  "data": "<base64 image>",
  "routines": [
    { "name": "FilterConfidence", "arguments": { "confidence": 0.65 } }
  ]
}
📘

confidence must be written as a decimal number, such as 1.0 rather than 1.

How do you choose a threshold? A lower threshold catches more defects but flags more normal images by mistake. A higher threshold flags fewer normal images, but may miss subtle defects. We recommend starting with the default of 0.5. If you see too many false alarms or missed defects, run your validation images through the model and adjust the threshold until you get the balance of precision and recall you need.

Does active learning work with anomaly detection?

Yes. Active Learning uses the image-level Pristine / Anomaly prediction to decide which images to send back to your project. Anomaly masks are not uploaded.



Did this page help you?