predict
Description
calculates the unnormalized anomaly scores predicted by an anomaly detector during inference
for a set of test images, scores
= predict(detector
,I
)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.
specifies options using one or more name-value arguments. For example,
scores
= predict(detector
,I
,Name=Value
)predict(detector,I,MiniBatchSize=32)
limits the batch size to
32.
Examples
Calculate Optimal Anomaly Threshold
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)
T = single
2.9632e-04
roc = rocmetrics with properties: Metrics: [4976x4 table]
Set the Threshold
property of the FCDD anomaly detector as the optimal threshold.
net.Threshold = T;
Input Arguments
detector
— Anomaly detector
efficientADAnomalyDetector
(since R2024b)
object | fcddAnomalyDetector
object | fastFlowAnomalyDetector
object | patchCoreAnomalyDetector
object
Anomaly detector, specified as an efficientADAnomalyDetector
object, an fcddAnomalyDetector
object, a fastFlowAnomalyDetector
object, or a patchCoreAnomalyDetector
object.
I
— Test image
numeric array | datastore | formatted dlarray
object
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 | EfficientAD, FCDD, FastFlow |
FCDD anomaly detectors also support grayscale test images, with one color channel instead of three.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=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.
MiniBatchSize
— Size of batches
128
(default) | positive integer
Size of batches for calculating predictions, specified as a positive integer. Larger batch sizes lead to faster processing but take up more memory.
ScoreFunction
— Function for computing scalar score from anomaly map
function handle
Function for computing a scalar score from an anomaly map when detector
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 |
|
EfficientAD |
|
FCDD |
|
PatchCore | Not supported |
ExecutionEnvironment
— Hardware resource
"auto"
(default) | "gpu"
| "cpu"
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
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
FCDD, FastFlow, PatchCore | EfficientAD |
---|---|
|
|
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Usage notes and limitations:
FCDD, FastFlow, PatchCore | EfficientAD |
---|---|
|
|
Version History
Introduced in R2022bR2024b: Support for EfficientAD anomaly detector
You can now specify the detector
argument as an efficientADAnomalyDetector
object.
R2024a: Option to specify custom anomaly score function for FastFlow and FCDD detectors
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.
R2023a: Support for FastFlow and PatchCore anomaly detectors
You can now specify the detector
argument as a fastFlowAnomalyDetector
object or a patchCoreAnomalyDetector
object.
R2023a: Support for C and GPU code generation
predict
now supports the
generation of C code (requires MATLAB®
Coder™) and optimized CUDA code (requires GPU Coder™).
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)