Main Content

classify

Classify image as normal or anomalous

    Description

    example

    isAnomaly = classify(detector,I) predicts whether test image I is anomalous or normal, given an FCDD anomaly detector with an anomaly threshold.

    isAnomaly = classify(detector,I,Name,Value) specifies options using one or more name-value arguments. For example, classify(detector,I,MiniBatchSize=32) limits the batch size to 32.

    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™.

    Examples

    collapse all

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

    [Xtest,catLabels] = digitTest4DArrayData;
    dsTest = arrayDatastore(Xtest,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 positive labels as the set of digits between 0 and 9, excluding 8.

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

    Classify each test image as normal or anomalous.

    labelsPred = classify(detector,dsTest);

    Calculate metrics for the anomaly detection results.

    metrics = evaluateAnomalyDetection(labelsPred,catLabels,posLabels)
    Evaluating anomaly detection results
    ------------------------------------
    * Finalizing... Done.
    * Data set metrics:
    
        GlobalAccuracy    MeanAccuracy    Precision    Recall    Specificity    F1Score    FalsePositiveRate    FalseNegativeRate
        ______________    ____________    _________    ______    ___________    _______    _________________    _________________
    
            0.7662           0.839         0.98971     0.748        0.93        0.85204          0.07                 0.252      
    
    metrics = 
      anomalyDetectionMetrics with properties:
    
                  ConfusionMatrix: [2x2 table]
        NormalizedConfusionMatrix: [2x2 table]
                   DataSetMetrics: [1x8 table]
                     ClassMetrics: [2x2 table]
    
    

    Display the confusion matrix.

    cm = metrics.ConfusionMatrix
    cm=2×2 table
                   Normal    Anomaly
                   ______    _______
    
        Normal       465        35  
        Anomaly     1134      3366  
    
    

    Input Arguments

    collapse all

    FCDD anomaly detector, specified as a fcddAnomalyDetector object. You must set the Threshold property of the detector before calling this function.

    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. You can specify multiple 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: isAnomaly = classify(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.

    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

    Test image is anomaly, returned as an N-element logical vector, where N is the number of test images in I.

    Data Types: logical

    Version History

    Introduced in R2022b