predict - Predict unnormalized anomaly scores - MATLAB (original) (raw)

Predict unnormalized anomaly scores

Since R2022b

Syntax

Description

[scores](#mw%5Fb7a4cd21-1647-4dd1-9a56-1023f7cff936) = predict([detector](#mw%5F0cc1df5f-fa72-42ff-96f8-fa3591b6588b%5Fsep%5Fmw%5Ff0686b83-345a-47b2-8ed0-7c3d0dde776c),[I](#mw%5F5fcdda5d-8814-409e-9023-b8e2fdc4a762)) calculates the unnormalized anomaly scores predicted by an anomaly detector during inference for a set of test images, I. Use this function to get predictions from the output layers of the detector during inference.

Note

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

example

[scores](#mw%5Fb7a4cd21-1647-4dd1-9a56-1023f7cff936) = predict([detector](#mw%5F0cc1df5f-fa72-42ff-96f8-fa3591b6588b%5Fsep%5Fmw%5Ff0686b83-345a-47b2-8ed0-7c3d0dde776c),[I](#mw%5F5fcdda5d-8814-409e-9023-b8e2fdc4a762),[Name=Value](#namevaluepairarguments)) specifies options using one or more name-value arguments. For example,predict(detector,I,MiniBatchSize=32) limits the batch size to 32.

Examples

collapse all

Load calibration images and corresponding labels, then create a datastore that reads the calibration data. The data set consists of grayscale images of handwritten digits 0–9.

[Xcal,gtLabels] = digitTest4DArrayData; dsCal = arrayDatastore(Xcal,IterationDimension=4);

Load a pretrained FCDD anomaly detector. This detector has been trained to classify the digit 8 as normal and all other digits as anomalies. Therefore, specify the set of anomaly labels as the set of digits between 0 and 9, excluding 8.

load("digit8AnomalyDetector.mat"); anomalyLabels = setdiff(string(0:9),"8");

Predict the anomaly score of each calibration image.

scores = predict(detector,dsCal);

Calculate the optimal threshold and corresponding ROC metrics from the anomaly scores and ground truth labels.

[T,roc] = anomalyThreshold(gtLabels,scores,anomalyLabels)

roc = rocmetrics with properties:

Metrics: [4976×4 table]

Properties, Methods

Set the Threshold property of the FCDD anomaly detector as the optimal threshold.

Input Arguments

collapse all

Test image, specified in one of these formats:

Format Supported Detectors
_M_-by-_N_-by-3 numeric array representing a truecolor image. EfficientAD, FCDD, FastFlow, PatchCore
_M_-by-_N_-by-3-by-B numeric array representing a batch of B truecolor images. EfficientAD, FCDD, FastFlow, PatchCore
Datastore that reads and returns truecolor images. The images must all have the same size. EfficientAD, 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. EfficientAD, FCDD, FastFlow

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

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: isAnomaly = predict(detector,I,MiniBatchSize=32) limits the batch size to 32.

Size of batches for calculating predictions, specified as a positive integer. Larger batch sizes lead to faster processing but take up more memory.

Function for computing a scalar score from an anomaly map whendetector is an efficientADAnomalyDetector object, an fcddAnomalyDetector object, or a fastFlowAnomalyDetector object, specified as a function handle. The function handle ScoreFunction must represent a function that accepts the input data I and returns an output in the form of a numeric scalar. I must be 2-D numeric image data, dlarray data with two spatial dimensions, or a datastore. The default value of ScoreFunction depends on the type of detector specified:

Detector Specified in detector Argument Default ScoreFunction Value
FastFlow @(I)max(I,[],[1 2])
EfficientAD @(I)max(I,[],[1 2 3])
FCDD @(I)mean(I,[1 2])
PatchCore Not supported

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, seeGPU Computing Requirements (Parallel Computing Toolbox).
"cpu" Use the CPU.

Output Arguments

collapse all

Predicted anomaly scores, returned as a numeric scalar, numeric vector, or formatteddlarray (Deep Learning Toolbox) object.scores contains one element for each image inI.

Extended Capabilities

expand all

Usage notes and limitations:

FCDD, FastFlow, PatchCore EfficientAD
The size of I must be fixed at code generation time.The only name-value argument supported for code generation isMiniBatchSize. The name-value argument must be a compile-time constant. The datastore data format of I is not supported.

Usage notes and limitations:

FCDD, FastFlow, PatchCore EfficientAD
The size of I must be fixed at code generation time.GPU code generation does not support gpuArray inputs.GPU code generation supports inputs that are defined as half-precision floating point data types. For more information, see half (GPU Coder).The only name-value argument supported for code generation isMiniBatchSize. The name-value argument must be a compile-time constant. The datastore data format of I is not supported.

Version History

Introduced in R2022b

expand all

Specify a custom anomaly score function, which the predict function uses to compute a scalar score from the anomaly map, by using the ScoreFunction name-value argument.

predict now supports the generation of C code (requires MATLAB® Coder™) and optimized CUDA code (requires GPU Coder™).