Skip to content

Learning Blocks

Visual Anomaly Detection (Image)

Learn what normal images look like and flag images, and the spots in them, that don't fit.

Try it in Studio
On this page

What is this learning block?

Visual Anomaly Detection learns from images of things that are normal and flags any image that looks different, for example a scratched part, a missing screw or a stained surface. You do not need photos of every possible defect. It can work with no anomalous images at all.

You add it in the impulse designer with Add a learning block, where it is listed as Visual Anomaly Detection next to Object Detection. An Image impulse has one learning block. You then choose a scoring function on the training page:

  • Gaussian Mixture Model (GMM): a compact statistical model of normal image patches.
  • PatchCore: a memory of real normal patches.

What does it do?

For each image it gives:

  • an anomaly score for the whole image, and
  • an anomaly map: a small grid (for example 12 × 12 cells) with one score per cell, so you can see where in the image something looks wrong.

The image is flagged as an anomaly when its score is at or above the threshold. The score and every cell of the map use the same scale as the threshold.

How does it learn?

Both scoring functions share the same first steps:

  1. Studio reads each normal image from the training set (the same features the Image block generates).
  2. A frozen MobileNetV2 network, pre-trained on ImageNet, turns the image into a grid of patch features, one small description per cell.
  3. The scoring function learns what normal patches look like:
    • GMM fits a few Gaussian "modes" to the normal patches. A patch is unusual when it is unlikely under those modes.
    • PatchCore keeps a reduced memory bank of real normal patches. A patch is unusual when it is far from every patch in the bank. There is no gradient training: training means extracting features and choosing which patches to keep.
  4. Studio sets aside a held-out slice of the normal training images (15% by default, never used for fitting) and uses it to calibrate the scores. A score of 1.0 then means "about as unusual as the top 1% of normal images". The default threshold is 1.0.

Normal and anomalous data

  • Training set: normal images only. Choose which labels count as normal with the Normal classes picker. By default every label in the training set is pre-selected except names that look like defects (such as anomaly, defect, fault, ng or bad), and you confirm the choice.
  • Testing set: normal images and, if you have them, anomalous images (for example a scratch label). Anomalous images are used only to measure accuracy and to suggest a threshold. They are never used to train.

Both scoring functions need a fixed camera position and consistent lighting. See Collecting image data for how to upload images and choose the training or testing split.

What model architecture does it use?

  1. 1Input imageOutput of the Image block
  2. 2BackboneMobileNetV2 (frozen, ImageNet)
  3. 3Patch featuresOne feature vector per grid cell
  4. 4Scoring headGMM or PatchCore
  5. 5CalibrationScores scaled so the threshold is 1.0
  6. 6OutputAnomaly map + image score
Visual anomaly model

The whole path runs as a single model. A grayscale image is repeated to three channels inside the model, and the rescaling the backbone expects is also done inside it, so the input stays a single-channel image. The image score is the highest cell score on the map (or, as an advanced option, the mean of the top 1% of cells).

The backbone is MobileNetV2 at width 0.35 (the default) or 1.0. No other backbone is available.

Which scoring function should I choose?

Start with GMM. It trains in seconds, is small and is easy to retrain. Switch to PatchCore if the evaluation shows missed defects on complex images.

GMMPatchCore
Model of "normal"A few Gaussian modesA memory of real normal patches
Simple, uniform imagesGoodVery good
Complex, multi-part objectsWeaker: modes get averagedStrong
Small or subtle defectsModerateStrong
Training timeSeconds to a few minutesMinutes
Model sizeAbout 0.1 to 1 MBAbout 1 to 20 MB, depending on the bank size
Speed on the deviceFasterSlower
INT8Possible, if it passes the accuracy checkFloat32 only
Normal images neededAt least 30 recommendedWorks with few (10 or more); accuracy grows with variety
Best forUniform surfaces and textures (fabric, wood, metal sheet), fixed camera, steady lighting, tight speed or size limitsAssembled parts, PCBs, packaging, multi-part objects, high-resolution inspection, when accuracy matters more than size

The ImageNet backbone may transfer poorly to unusual image domains such as X-ray, thermal or microscopy images. Studio shows a note when grayscale input is selected, and PatchCore usually copes better there.

What settings can I configure?

SettingDefaultWhat it does
Scoring functionGMMGMM or PatchCore. Opened from Change on the training page. Switching needs retraining, and your current model stays available until the new one finishes.
Normal classesAll training labels except defect-like namesThe labels that count as normal.
Capacity (GMM)MediumLow, medium or high: how many Gaussian modes the model may use (2, 5 or 10).
Advanced settingsSee belowBackbone, how the image score is taken, the size of the calibration hold-out, and the scoring function's own parameters.

The advanced settings and their defaults:

  • Backbone: MobileNetV2 at 0.35 (default) or 1.0.
  • Score aggregation: the maximum cell (default) or the mean of the top 1% of cells.
  • Calibration hold-out: 15% of the normal training images (10% to 30%).
  • Treat background as normal: off.
  • GMM: covariance type (full by default, diagonal for very small datasets), PCA size (64 by default) and the number of patches used to fit (100,000 by default).
  • PatchCore: the share of patches kept (10% by default), the memory-bank cap (8,192 by default), the number of neighbours (1 by default) and the number of candidate patches (200,000 by default).

Studio checks your data before you train:

  • Fewer than 10 normal training images: training is blocked.
  • Fewer than 30: a warning.
  • No anomalous test images: a warning that AUROC is unavailable.
  • Image sides that are not a multiple of 16: training is blocked, with a one-click fix.
  • Image size: 96 to 224 pixels is the recommended range. Above 224 Studio warns, and PatchCore is capped at 320.

Select Start training to begin. Training uses the same job queue as the other Image models, and you can stop it at any time.

What problems can it solve?

  • Spotting scratches, dents, stains or missing parts on products that normally all look the same.
  • Checking assembled parts, PCBs or packaging against what a good one looks like.
  • Any case where you have many images of normal items but few or no images of faults.

Simple example

You inspect machined metal plates with a fixed camera.

  1. Upload at least 30 photos of good plates to the training set, labelled good.
  2. Optionally upload a few photos of scratched plates, labelled scratch, to the testing set.
  3. Add the Visual Anomaly Detection learning block, generate features, and open the training page. Keep GMM and leave good ticked in Normal classes.
  4. Select Start training. When it finishes, the Anomaly explorer shows the normal images on the left of the threshold and, if you uploaded scratches, those images further right.
  5. Open Live classification and show the camera a good plate, then a scratched one. The scratched plate gets a high score and the cells over the scratch light up.

How do I understand the training results?

  • AUROC is the headline number when the testing set has both normal and anomalous images. It shows how well the scores separate the two groups, independent of the threshold. 1.0 is a perfect separation, 0.5 is no better than chance.
  • Anomaly explorer shows the score distribution (normal and anomalous images as two histograms, with a line at the threshold), the threshold slider and a list of samples sorted by score. A sample that the current threshold gets wrong is marked with an icon as well as a colour.
  • Normal flagged and Anomalies caught update while you drag the threshold, so you see the trade-off before you commit.
  • With no anomalous images in the testing set, AUROC is unavailable. Add anomalous images to the test set to measure accuracy.

Threshold and the uncertain band

  • The default threshold is 1.00, the calibrated 99th percentile of the held-out normal images.
  • You can change it without retraining, and Reset to calibrated puts it back.
  • When the testing set has anomalous images, Studio also suggests a Max F1 threshold and a Youden J threshold. Select one to move the slider there.
  • Scores within 10% of the threshold (|score − threshold| ≤ 0.1 × threshold) are uncertain. Model testing and Live classification show them as Uncertain, not as a clear pass or fail.

Evaluating the model

The Evaluation page for a Visual Anomaly model shows:

  • AUROC (the headline) and AUPRC (average precision) as threshold-free measures,
  • a ROC curve,
  • a 2 × 2 confusion matrix at the current threshold, with precision, recall, F1, accuracy and the false-alarm rate on normal images,
  • the threshold suggestions (Calibrated, Max F1, Youden J).

When anomalous test images carry bounding boxes, Studio can also report a region hit rate: the share of anomalous images whose highest-scoring cell falls inside a box you drew. When the testing set has no anomalous images, the page tells you to add some to measure accuracy.

See Evaluating an Image model for the Evaluation, Model testing and Live classification pages in general.

Model testing and Live classification

  • Model testing classifies the images in your testing set. Each result shows the score against the threshold, a status of pass, fail or uncertain, and the anomaly map.
  • Live classification scores a new image: an uploaded file, a test sample or a webcam frame. The result card says Anomaly or Normal (or Uncertain), with an icon as well as a colour, and shows the score against the threshold. The picture gets a heatmap overlay, and you can switch between Image, Heatmap and Overlay views and set the overlay opacity. Cells at or above the threshold are outlined.
  • With the Device source, Live classification shows the result streamed from a connected device that runs the deployed package. See The result on the device.

Precisions

PrecisionAvailability
Float32Always, for both GMM and PatchCore.
INT8GMM only, and only when the INT8 model passes an accuracy check: the AUROC of the INT8 model must be within 0.02 of the float32 model (`

When INT8 does not pass, Studio lists it as unavailable and gives the reason. PatchCore stays Float32. See Choosing Float32 or INT8 for the general trade-off.

Deployment targets

TargetGMMPatchCore
TFLite (generic .pe package)SupportedSupported
Raspberry PiSupportedSupported
UNO Q (.pe package)SupportedSupported (the memory bank is capped to bound latency)
PXESupportedSupported
Arduino, ESP32, C++ libraryNot supportedNot supported

Why not Arduino, ESP32 and C++? Those generators produce C++ code that has a single flat classification output. A Visual Anomaly model produces an anomaly map that needs extra post-processing they do not have. PatchCore's memory bank also does not fit the RAM or flash of a microcontroller. Studio shows these targets as disabled on the Deployment page and gives the reason from the server, and a build request for them is rejected before anything is built.

The Deployment page's model summary reads, for example, Visual anomaly · GMM, Output: anomaly map 12×12 + score and Threshold: 1.00. The threshold is written into the package when you build it. See Deploying an Image model for the build steps and Supported devices for the hardware.

The result on the device

A package for a Visual Anomaly model reports model_type: "visual_anomaly" and an anomaly_map output. Each classified frame returns the verdict, the image score and the cells that reached the threshold:

{
  "model_type": "visual_anomaly",
  "label": "anomaly",
  "confidence": 1.0,
  "predictions": [
    { "label": "no anomaly", "confidence": 0.0 },
    { "label": "anomaly", "confidence": 1.0 }
  ],
  "is_anomaly": true,
  "anomaly": 1.84,
  "visual_anomaly_max": 1.84,
  "visual_anomaly_mean": 0.31,
  "visual_anomaly_threshold": 1.0,
  "visual_anomaly_input": { "width": 96, "height": 96, "cell_px": 8 },
  "visual_anomaly_grid": [
    { "label": "anomaly", "value": 1.84, "x": 40, "y": 16, "width": 8, "height": 8 }
  ],
  "timing": { "dsp": 2, "classification": 0, "anomaly": 38 }
}
FieldMeaning
label, is_anomalyThe decision: anomaly or no anomaly.
confidence, predictionsThe same decision as a two-entry classification, so tools that only know classification still read it.
anomalyThe image score, on the same scale as the threshold.
visual_anomaly_max, visual_anomaly_meanThe highest and the average cell score on the map.
visual_anomaly_thresholdThe threshold the device used.
visual_anomaly_inputThe size of the model's input frame in pixels and the size of one map cell (cell_px).
visual_anomaly_gridOnly the cells at or above the threshold, each with its score and its position and size in input-pixel coordinates. An empty list means no cell was flagged.
timingMilliseconds spent in preprocessing (dsp), the model (classification) and the anomaly map (anomaly).

Studio's Live classification shows the verdict, the score against the threshold and the timings, and draws the flagged cells on a grid of the input frame.

If a device runs an older daemon that sends only the decision, Studio shows Anomaly or No anomaly without a score or a grid.

Limitations

  • No deployment to the Arduino, ESP32 or C++ generators (see Deployment targets).
  • One learning block per Image impulse: you cannot combine object detection and anomaly detection in the same impulse.
  • Localization is evaluated only with bounding boxes. There are no pixel-accurate segmentation labels or metrics.
  • The memory bank is not updated incrementally: adding images means retraining.
  • MobileNetV2 (width 0.35 or 1.0) is the only backbone.
  • It needs a fixed camera position and consistent lighting.