Main Content

trainFastFlowAnomalyDetector

R2026b

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

    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 = trainFastFlowAnomalyDetector(normalData,detectorIn,options,ExperimentMonitor=experimentMonitor) also specifies whether to monitor the progress of the training experiment with Experiment Manager.

    [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:11        0.01        8.8293e+09 
          8          8         00:00:18        0.01        5.9454e+09 
         12         12         00:00:24        0.01        4.9817e+09 
         16         16         00:00:29        0.01        4.8341e+08 
         20         20         00:00:35        0.01        7.4841e+08 
         24         24         00:00:39        0.01        2.9788e+09 
         28         28         00:00:43        0.01        6.0622e+08 
         32         32         00:00:47        0.01        9.3466e+08 
         36         36         00:00:51        0.01        8.4266e+09 
         40         40         00:00:54        0.01        2.3599e+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
    
    -2.4520e-04
    
    roc = 
      rocmetrics with properties:
    
        Metrics: [100×4 table]
    
    Properties, Methods
    
    
    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".

    Note

    If you specify the OutputFcn function handle using the OutputFcn (Deep Learning Toolbox) name-value argument, it must use a per-epoch info structure with these fields:

    • Epoch

    • Iteration

    • TimeElapsed

    • LearnRate

    • TrainingLoss

    Monitor of detector training experiments, specified as an experiments.Monitor (Deep Learning Toolbox) object for use with the Experiment Manager (Deep Learning Toolbox) app. You can use this object to track the progress of training, update information fields in the training results table, record values of the metrics used by the training, and to produce training plots. For an example using this app, see Train Object Detectors in Experiment Manager.

    The app monitors this information during training:

    • Training loss at each iteration

    • Learning rate at each iteration

    • Validation loss at each iteration, when the options input contains validation data

    When the options input contains validation data, the app also monitors validation loss at each iteration.

    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

    expand all