Main Content

anomalyThreshold

Optimal anomaly threshold for set of anomaly scores and corresponding labels

Since R2022b

    Description

    t = anomalyThreshold(gtLabels,scores,anomalyLabels) calculates the optimal anomaly threshold given per-image anomaly scores and corresponding ground truth labels. anomalyLabels indicates which class labels in gtLabels belong to the anomaly (positive) class. When performing anomaly detection, images with scores below the calculated threshold are considered normal images and images with scores above the threshold are considered anomalous images.

    Note

    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.

    t = anomalyThreshold(gtLabels,scores,anomalyLabels,optimMethod) also specifies the optimization method.

    t = anomalyThreshold(gtLabels,scores,anomalyLabels,MaxFalsePositiveRate=maxFPR) also specifies the maximum false positive rate.

    t = anomalyThreshold(gtLabels,scores,anomalyLabels,MaxFalseNegativeRate=maxFNR) also specifies the maximum false negative rate.

    example

    [t,anomalyROC] = anomalyThreshold(___) also returns the receiver operating characteristic (ROC) curve and performance metrics.

    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)
    T = single
        2.9632e-04
    
    roc = 
      rocmetrics with properties:
    
        Metrics: [4976x4 table]
            AUC: 0.9114
    
    
    
    

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

    net.Threshold = T;

    Input Arguments

    collapse all

    Ground truth labels for each image, specified as a numeric vector, logical vector, or categorical vector. The anomalyThreshold function converts the labels into a logical vector according to the set of anomaly labels in anomalyLabels.

    Predicted anomaly score for each image, specified as a numeric vector of the same length as gtLabels.

    Anomaly labels, specified as a vector of the same data type as gtLabels. When gtLabels is categorical, anomalyLabels can be of data type string whose values correspond to categories in gtLabels.

    The anomalyThreshold function converts all ground truth labels in gtLabels that belong to the set of anomaly labels to a logical true, indicating an anomaly (positive detection). The function converts all other ground truth labels to a logical false, indicating normality (negative detection).

    Optimization method, specified as one of the values in the table.

    Optimization MethodDescription
    "YoudensIndexROC"

    The optimal threshold is the point on the ROC curve with the maximum difference between the true positive rate (TPR) and false positive rate (FPR). The difference between TPR and FPR is also known as Youden's index or Youden's J statistic.

    "NearestZeroOneROC"

    The optimal threshold is the point on the ROC curve nearest to the coordinate (0, 1), where the FPR is 0 and the TPR is 1. This point minimizes the Euclidean distance between the ROC curve and the (0,1) point.

    "MaxF1Score"

    The optimal threshold is the point on the precision-recall (PR) curve that maximizes the F1 score. This value is the harmonic mean of the precision and recall values.

    Data Types: char | string

    Maximum false positive rate (FPR), specified as a number in the range [0, 1]. The optimal threshold is the point on the ROC curve where the FPR is less than or equal to maxFPR.

    Maximum false negative rate (FNR), specified as a number in the range [0, 1]. The optimal threshold is the point on the ROC curve where the FNR is less than or equal to maxFNR.

    Output Arguments

    collapse all

    Optimal threshold, returned as a numeric scalar of the same data type as scores.

    ROC curve and performance metrics, returned as a rocmetrics (Deep Learning Toolbox) object.

    Tips

    • You can plot ROC and PR curves returned by anomalyROC using the plot (Deep Learning Toolbox) function.

    Version History

    Introduced in R2022b