> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/models/supported-tasks/anomaly-detection.md).

# Anomaly Detection

Flag defects you have few or no pictures of by training on normal images, then deploy your own anomaly detection model on Roboflow.

## What is anomaly detection?

Anomaly detection learns what a normal image looks like from normal images only, then flags images that differ. For each image, the model returns a "normal" or "anomalous" verdict, an anomaly score, and a heatmap of where the image differs. You do not need pictures of every defect, so it fits problems where defects are rare or vary a lot.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-fe2da5b5e5420fdf0e904cacb5f71171b5fbdd14%2Fimg-task-anomaly-detection-wide.webp?alt=media" alt="A blemished orange outlined in red and tagged Anomaly, with a heatmap over the blemish"><figcaption></figcaption></figure>

## Supported anomaly detection models

Roboflow supports these anomaly detection models. See the [Anomaly Detection models](/models/supported-models.md#anomaly-detection) table for export and deployment support per model.

<table data-search="false"><thead><tr><th>Model</th><th>License</th><th data-type="checkbox">Trainable on Roboflow</th><th>Latest release</th></tr></thead><tbody><tr><td><a href="/models/supported-models/patchcore.md">PatchCore</a></td><td><a href="/models/model-licenses.md">Apache 2.0</a> (free for commercial use)</td><td>true</td><td>2021</td></tr><tr><td><a href="/models/supported-models/foundad.md">FoundAD</a></td><td><a href="/models/model-licenses.md#other-models">Upstream license</a></td><td>true</td><td>2025</td></tr></tbody></table>

## Choosing an anomaly detection model

FoundAD is more accurate, especially when you only have a few normal images. More normal images help both models, and PatchCore the most.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-cede5a786ee20a3c831b3ddab964182dac7b742d%2Fanomaly-detection-accuracy-by-training-images.webp?alt=media" alt="Image AUROC of PatchCore and FoundAD on four VisA categories, trained on 4 normal images and on all of them"><figcaption><p>Results on four categories of the <a href="https://github.com/amazon-science/spot-diff">VisA dataset</a> (CC BY 4.0).</p></figcaption></figure>

FoundAD takes the same time per image at any training set size. PatchCore slows down as you add normal images, but at its default 224×224 it stays the faster of the two. See [PatchCore inference speed](/models/supported-models/patchcore.md#inference-speed) for how image size changes this.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-e602269820cbd79d45f6cc939344ccc2d08119d5%2Fanomaly-detection-accuracy-vs-inference-time.webp?alt=media" alt="Average AUROC against inference time per image for PatchCore and FoundAD on a Jetson AGX Orin, a Jetson Orin NX, and a MacBook Pro CPU"><figcaption></figcaption></figure>

PatchCore trains in minutes. FoundAD takes longer, up to hours on large datasets. Try both on your data when you can.

## Fine-tuning an anomaly detection model

The [PatchCore](/models/supported-models/patchcore.md#train-patchcore) and [FoundAD](/models/supported-models/foundad.md#train-foundad) pages cover the split and preprocessing rules for this task.

{% stepper %}
{% step %}

### Create an anomaly detection project

Create a [Project](https://docs.roboflow.com/datasets/create-and-upload/create-a-project) and pick "Anomaly Detection" as the project type.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-e5fc378cd1e0d2d941b90739fd5dd8d7ae6ad898%2Fcreate-project-anomaly-detection.webp?alt=media" alt="Create project form with Anomaly Detection selected as the project type and a preview of an anomaly detection example"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Upload and label images

[Upload images](https://docs.roboflow.com/datasets/create-and-upload/adding-data), then label each image "normal" or "anomalous" in the [annotation editor](https://docs.roboflow.com/datasets/annotate/annotate/annotation-tools). Most of your images should be normal. The model trains on normal images only. Anomalous images set the decision threshold and measure accuracy, so they go to the valid and test splits only.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-b684088eff1e6aca44ee895f5dae24c290904598%2Fannotate-anomaly-detection.webp?alt=media" alt="Annotation editor on an egg image with the class picker open and normal selected over anomalous"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Generate a dataset version

[Generate a dataset version](https://docs.roboflow.com/datasets/versions/dataset-versions/create-a-dataset-version) with only the auto-orient and "Stretch to" resize preprocessing steps, and no augmentation.
{% endstep %}

{% step %}

### Train an anomaly detection model

Click "Train" and pick PatchCore or FoundAD. See [Train a Model](/models/train/train-a-model.md) for every option.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-1fe669ed568dac9ed343aad1414ef317a2c0b14a%2Ftrain-architectures-anomaly-detection.webp?alt=media" alt="Select Architecture step on the Train page listing PatchCore and FoundAD"><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## Deploy an anomaly detection model

After training, deploy your anomaly detection model with the [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), a [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments), or on your own hardware with [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted). See [Choosing a Deployment](https://docs.roboflow.com/deployment/choosing-a-deployment) to compare the options.
