Main Content

trainFastFlowAnomalyDetector

Train FastFlow anomaly detection network

Since R2023a

    Description

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

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

    example

    [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:10        0.01        2.1573e+10 
          8          8         00:00:17        0.01        1.1753e+09 
         12         12         00:00:21        0.01        4.1158e+10 
         16         16         00:00:25        0.01        1.6565e+08 
         20         20         00:00:30        0.01        2.5284e+08 
         24         24         00:00:35        0.01        8.9291e+08 
         28         28         00:00:39        0.01        4.8024e+08 
         32         32         00:00:43        0.01        1.3084e+08 
         36         36         00:00:47        0.01        8.1442e+08 
         40         40         00:00:52        0.01        5.0234e+11 
    

    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
    
    -0.0035
    
    roc = 
      rocmetrics with properties:
    
        Metrics: [119x4 table]
    
    
    
    
    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