Main Content

trainFastFlowAnomalyDetector

Train FastFlow anomaly detection network

Since R2023a

    Description

    example

    detector = trainFastFlowAnomalyDetector(normalData,detectorIn,options) trains the input FastFlow anomaly detection network detectorIn. The training data consists of normal images in normalData. The options argument controls options for training.

    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.

    Note

    It is recommended that you also have Parallel Computing Toolbox™ to use with a CUDA®-enabled NVIDIA® GPU. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).

    [detector,info] = trainFastFlowAnomalyDetector(normalData,detectorIn,options) also returns information on the training progress, such as the training accuracy and learning rate for each iteration.

    Examples

    collapse all

    Load a data set that consists of images of digits from 0 to 9. Consider images of the digit 8 to be normal, and all other digits to be anomalous.

    trainDir = fullfile(toolboxdir("vision"),"visiondata","digits","synthetic");
    dsNormal = imageDatastore(fullfile(trainDir,"8"));

    Create a fastFlowAnomalyDetector object.

    untrainedDetector = fastFlowAnomalyDetector;

    Specify training options for Adam optimization.

    options = trainingOptions("adam", ...
       InitialLearnRate = 1e-2, ...
        MaxEpochs=40, ...
        VerboseFrequency=4, ...
        MiniBatchSize=250, ...
        Shuffle="every-epoch", ...
        Plots="none");

    Train the anomaly detector.

    detector = trainFastFlowAnomalyDetector(dsNormal,untrainedDetector,options);
    Computing Input Normalization Statistics.
     
        Epoch    Iteration    TimeElapsed    LearnRate    TrainingLoss
        _____    _________    ___________    _________    ____________
          4          4         00:00:13        0.01        5.2056e+08 
          8          8         00:00:23        0.01        6.0259e+08 
         12         12         00:00:30        0.01        7.0335e+06 
         16         16         00:00:36        0.01        6.7484e+07 
         20         20         00:00:42        0.01        2.0865e+08 
         24         24         00:00:48        0.01        1.3667e+09 
         28         28         00:00:52        0.01        1.3337e+09 
         32         32         00:00:56        0.01        2.4014e+08 
         36         36         00:01:00        0.01        3.8122e+07 
         40         40         00:01:04        0.01        1.9174e+07 
    

    Set the anomaly threshold of the detector using a calibration data set.

    calDir = fullfile(toolboxdir("vision"),"visiondata","digits","handwritten");
    dsCal = imageDatastore(calDir,IncludeSubfolders=true,LabelSource="foldernames");
    gtLabels = dsCal.Labels;
    anomalyLabels = setdiff(string(0:9),"8");
    scores = predict(detector,dsCal);
    [T,roc] = anomalyThreshold(gtLabels,scores,anomalyLabels)
    T = single
        -2.9593e-15
    
    roc = 
      rocmetrics with properties:
    
        Metrics: [120x4 table]
            AUC: 0.4738
    
    
    
    
    detector.Threshold = T;

    Input Arguments

    collapse all

    FastFlow anomaly detector to train, specified as a fastFlowAnomalyDetector object.

    Training data, specified as a datastore. The training data consists of samples of normal images. Do not include anomaly images in the training data.

    Training options, specified as a TrainingOptionsSGDM, TrainingOptionsRMSProp, or TrainingOptionsADAM object returned by the trainingOptions (Deep Learning Toolbox) function. To specify the solver name and other options for network training, use the trainingOptions function. You must set the BatchNormalizationStatistics property of the object as "moving".

    Output Arguments

    collapse all

    Trained FastFlow anomaly detector, returned as a fastFlowAnomalyDetector object.

    Training progress information, returned as a structure array with these fields.

    • Epoch — Epoch

    • Iteration — Iteration

    • TimeElapsed — Total elapsed duration

    • LearnRate — Learning rate for each iteration

    • TrainingLoss — Loss at the end of each iteration

    • ValidationLoss — Loss on the validation data

    If you specify AUC as a metric to track using the Metrics property of the trainingOptions (Deep Learning Toolbox) function, then the structure array will additionally include this field:

    • ValidationAUC — Area under the ROC curve (AUC) metric values for the validation data

    Note

    If you specify the ValidationData (Deep Learning Toolbox) training option to return ValidationLoss and ValidationAUC, you must specify your data in one of these formats:

    • A two-element cell array {dsNormal, dsDefect}, where dsNormal is a datastore of normal images, and dsDefect is a datastore of anomalous images.

    • A single datastore. You must organize the datastore so that calling the read and readall functions on it returns a table or two-element cell array of the form {I, Labels}, where I is the image data and Labels is the corresponding label data. In the label data, each element is a logical 1 (true) for anomaly images, or a logical 0 (false) for normal images.

    Version History

    Introduced in R2023a