Main Content


Predict per-pixel anomaly score map

Since R2022b



    map = anomalyMap(detector,I) creates an anomaly score map consisting of per-pixel anomaly scores predicted for test image I.


    This functionality requires Deep Learning Toolbox™ and the Computer Vision Toolbox™ Automated Visual Inspection Library. You can install the Computer Vision Toolbox Automated Visual Inspection Library from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.

    map = anomalyMap(detector,I,ExecutionEnvironment=executionEnvironment) also specifies the hardware resource on which to run the detector.


    collapse all

    Load a pretrained FCDD anomaly detector. This detector has been trained to classify the digit 8 as normal and all other digits as anomalies.


    Load test images and corresponding labels, then convert the test images to a formatted dlarray with the format label "SSCB" for spatial-spatial-channel-batch. The data set consists of grayscale images of handwritten digits 0–9.

    [Xtest,gtLabels] = digitTest4DArrayData;

    Select a test image of the digit 9.

    digitToTest = "9";
    digitIdx = find(gtLabels==digitToTest,1);
    imTest = Xtest(:,:,:,digitIdx);

    Predict the per-pixel anomaly scores for the test image.

    map = anomalyMap(detector,imTest);

    Display the anomaly map over the test image.

    overlay = anomalyMapOverlay(imTest,map);
    axis image off

    Input Arguments

    collapse all

    Anomaly detector, specified as an fcddAnomalyDetector object, a fastFlowAnomalyDetector object, or a patchCoreAnomalyDetector object.

    Test image, specified in one of these formats:

    FormatSupported Detectors

    M-by-N-by-3 numeric array representing a truecolor image.

    FCDD, FastFlow, PatchCore

    M-by-N-by-3-by-B numeric array representing a batch of B truecolor images.

    FCDD, FastFlow, PatchCore

    Formatted dlarray (Deep Learning Toolbox) object with two spatial dimensions and one channel dimension. You can specify multiple test images by including a batch dimension.

    FCDD, FastFlow

    FCDD anomaly detectors also support grayscale test images, with one color channel instead of three.

    Hardware resource on which to run the detector, specified as "auto", "gpu", or "cpu". The table shows the valid hardware resource values.

    Resource Action
    "auto"Use a GPU if it is available. Otherwise, use the CPU.
    "gpu"Use the GPU. To use a GPU, you must have Parallel Computing Toolbox™ and a CUDA® enabled NVIDIA® GPU. If a suitable GPU is not available, the function returns an error. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).
    "cpu"Use the CPU.

    Output Arguments

    collapse all

    Anomaly score map, returned in one of these formats:

    • M-by-N numeric matrix, when the test image I is a 2-D grayscale or truecolor image.

    • M-by-N-by-1-by-B numeric array representing a batch of images, when I is numeric array representing a batch of images.

    • Formatted dlarray (Deep Learning Toolbox) object, when I is a formatted dlarray object.

    The values in the map are unnormalized. Larger values indicate a greater chance of a pixel being an anomaly.

    Data Types: single

    Extended Capabilities

    Version History

    Introduced in R2022b

    expand all