Main Content


Train FastFlow anomaly detection network

Since R2023a



    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.


    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.


    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.


    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, ...
        BatchNormalizationStatistics="moving", ...
        MaxEpochs=40, ...
        VerboseFrequency=4, ...
        MiniBatchSize=250, ...
        Shuffle="every-epoch", ...

    Train the anomaly detector.

    detector = trainFastFlowAnomalyDetector(dsNormal,untrainedDetector,options);
    Computing Input Normalization Statistics.
        Epoch    Iteration    TimeElapsed    LearnRate    TrainingLoss
        _____    _________    ___________    _________    ____________
          4          4         00:00:08        0.01       -1.4359e+05 
          8          8         00:00:14        0.01       -1.4452e+05 
         12         12         00:00:18        0.01       -1.4566e+05 
         16         16         00:00:21        0.01       -1.4623e+05 
         20         20         00:00:25        0.01       -1.4705e+05 
         24         24         00:00:28        0.01       -1.4786e+05 
         28         28         00:00:32        0.01       -1.4855e+05 
         32         32         00:00:35        0.01       -1.4943e+05 
         36         36         00:00:38        0.01        -1.502e+05 
         40         40         00:00:42        0.01       -1.5086e+05 

    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
    roc = 
      rocmetrics with properties:
        Metrics: [98x4 table]
            AUC: 0.7940
    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

    • OutputNetworkIteration — Iteration number of returned network

    Version History

    Introduced in R2023a