Main Content

trainFCDDAnomalyDetector

Train fully convolutional data description (FCDD) anomaly detection network

    Description

    example

    detector = trainFCDDAnomalyDetector(normalData,anomalyData,untrainedDetector,options) trains the input FCDD anomaly detection network object untrainedDetector. The training data consists of normal images in normalData and anomaly images in anomalyData. The options argument controls options for training.

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

    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 = trainFCDDAnomalyDetector(normalData,anomalyData,untrainedDetector,options,Name=Value) uses additional options specified by one or more name-value arguments.

    [detector,info] = trainFCDDAnomalyDetector(___) 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.

    dataDir = fullfile(toolboxdir("vision"),"visiondata","digits","synthetic");
    dsTrain = imageDatastore(dataDir,IncludeSubfolders=true, ...
        LabelSource="foldernames");

    Split the training data into normal and anomaly images. For instance, set the images of the digit 8 to be normal, and all other digits to be anomalous.

    trainingLabels = dsTrain.Labels;
    normalDataIdx = find(trainingLabels == "8");
    anomalyDataIdx = find(trainingLabels ~= "8");
    dsNormal = subset(dsTrain,normalDataIdx);
    dsAnomaly = subset(dsTrain,anomalyDataIdx);

    Create an fcddAnomalyDetector object with a ResNet-18 backbone.

    detector = fcddAnomalyDetector(pretrainedEncoderNetwork("resnet18",1));

    Specify training options for SGDM optimization.

    options = trainingOptions("sgdm", ...
        Plots="training-progress", ...
        InitialLearnRate = 0.01, ...
        BatchNormalizationStatistics="moving", ...
        MaxEpochs=40, ...
        MiniBatchSize=300, ...
        ResetInputNormalization=false, ...
        Shuffle="every-epoch");

    Train the anomaly detector.

    detector = trainFCDDAnomalyDetector(dsNormal,dsAnomaly,detector,options);
     
        Epoch    Iteration    TimeElapsed    LearnRate    TrainingFcddLoss
        _____    _________    ___________    _________    ________________
    

    Input Arguments

    collapse all

    FCDD network to train, specified as an fcddAnomalyDetector object.

    Labeled ground truth normal images, specified as a datastore.

    Labeled ground truth anomaly images, specified as a datastore.

    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" and the ResetInputNormalization property as false.

    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: detector = trainFCDDAnomalyDetector(normalData,anomalyData,untrainedDetector,options,FreezeBackbone=false) does not freeze the backbone of the FCDD network during training.

    Use mask data in the anomaly training data to form the pixelwise loss, specified as false or true. When true, the anomaly training data in anomalyData must be a datastore that returns pairs of images as a two-element cell array of the form {anomalyImage,anomalyMask}. The first element, anomalyImage, is a sample image of an anomaly. The second element, anomalyMask, is a logical image of the same size as anomalyImage and has true valued pixels at locations where anomalyImage has anomaly content.

    Data Types: logical

    Freeze the backbone of the FCDD network during training, specified as true or false. When true, the weight of layers in the backbone does not change during training.

    Data Types: logical

    Output Arguments

    collapse all

    Trained FCDD network, returned as an fcddAnomalyDetector object.

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

    • Epoch — Epoch

    • Iteration — Iteration

    • TimeElapsed — Total elapsed duration

    • LearnRate — Learning rate for each iteration

    • TrainingFcddLoss — FCDD loss at the end of each iteration

    • OutputNetworkIteration — Iteration number of returned network

    Version History

    Introduced in R2022b