Main Content

predict

Predict unnormalized anomaly scores

    Description

    example

    scores = predict(detector,I) calculates the unnormalized anomaly scores predicted by an FCDD anomaly detection network during inference for a set of test images, I. Use this function to get predictions from the output layers of the FCDD network during inference.

    Note

    This function requires 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. To run this function, you will require the Deep Learning Toolbox™.

    scores = predict(detector,I,Name=Value) 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,catLabels] = 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, convert the categorical labels to binary labels by setting the categorical label 8 as 0 (false) and all other labels as 1 (true).

    load("digit8AnomalyDetector.mat");
    binaryLabels = catLabels~="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 binary labels.

    [T,roc] = anomalyThreshold(binaryLabels,scores,true)
    T = single
        2.9632e-04
    
    roc = 
      rocmetrics with properties:
    
        Metrics: [4980x4 table]
            AUC: 0.9114
    
    
      Properties, Methods
    
    

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

    net.Threshold = T;

    Input Arguments

    collapse all

    FCDD anomaly detector, specified as a fcddAnomalyDetector object.

    Test image, specified in one of these formats:

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

    • M-by-N numeric matrix representing a grayscale image

    • Formatted dlarray (Deep Learning Toolbox) object with two spatial dimensions. The test data can contain one or more test images by including a batch dimension.

    • Datastore that reads and returns M-by-N-by-3 truecolor images or M-by-N grayscale images.

    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: MiniBatchSize=32

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

    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

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

    Version History

    Introduced in R2022b