Contenido principal

trainStudentTeacherAnomalyDetector

R2026b

Train Student-Teacher anomaly detection network

Since R2026b

    Description

    detector = trainStudentTeacherAnomalyDetector(normalData,detectorIn,options) trains the input Student-Teacher anomaly detection network detectorIn. The training data consists of the specified normal images normalData.

    Note

    This functionality requires Deep Learning Toolbox™.

    Note

    To improve training results, you can use a Parallel Computing Toolbox™ license with a CUDA® enabled NVIDIA® GPU. For information about the supported compute capabilities, see GPU Computing Requirements (Parallel Computing Toolbox).

    example

    detector = trainStudentTeacherAnomalyDetector(normalData,detectorIn,Name=Value) specifies training parameters, in addition to the input arguments from the previous syntax.

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

    untrainedDetector = studentTeacherAnomalyDetector;

    Specify training options for Adam optimization.

    options = trainingOptions("adam", ...
        InitialLearnRate=4e-4, ...
        L2Regularization=4e-5, ...
        MiniBatchSize=4, ...
        MaxEpochs=8, ...
        VerboseFrequency=4, ...
        ResetInputNormalization=false, ...
        Shuffle="every-epoch", ...
        Verbose=true);

    Train the anomaly detector.

    detector = trainStudentTeacherAnomalyDetector(dsNormal,untrainedDetector,options);
    Computing Teacher Output Normalization Statistics.
     
        Epoch    Iteration    TimeElapsed    LearnRate    TrainingLoss
        _____    _________    ___________    _________    ____________
          1          4         00:00:00       0.0004         130.78   
          1          8         00:00:01       0.0004         32.376   
          1         12         00:00:02       0.0004         17.778   
          1         16         00:00:02       0.0004         12.597   
          1         20         00:00:03       0.0004         9.2391   
          2         24         00:00:04       0.0004         8.4801   
          2         28         00:00:04       0.0004         11.895   
          2         32         00:00:05       0.0004         6.7937   
          2         36         00:00:05       0.0004         6.0606   
          2         40         00:00:06       0.0004         5.5692   
          2         44         00:00:07       0.0004         5.8852   
          3         48         00:00:07       0.0004         9.9935   
          3         52         00:00:08       0.0004         5.1907   
          3         56         00:00:08       0.0004         4.5969   
          3         60         00:00:09       0.0004         4.8934   
          3         64         00:00:10       0.0004         4.2091   
          4         68         00:00:10       0.0004         4.3455   
          4         72         00:00:11       0.0004         4.7955   
          4         76         00:00:11       0.0004         4.3453   
          4         80         00:00:11       0.0004         4.0068   
          4         84         00:00:12       0.0004         3.7975   
          4         88         00:00:12       0.0004         3.6742   
          5         92         00:00:13       0.0004         3.4984   
          5         96         00:00:14       0.0004         3.8344   
          5         100        00:00:14       0.0004         3.3941   
          5         104        00:00:15       0.0004         3.8335   
          5         108        00:00:16       0.0004         3.2247   
          6         112        00:00:16       0.0004         2.9846   
          6         116        00:00:17       0.0004         3.5693   
          6         120        00:00:17       0.0004         3.1082   
          6         124        00:00:18       0.0004         3.1043   
          6         128        00:00:18       0.0004         3.423    
          6         132        00:00:19       0.0004         3.5435   
          7         136        00:00:19       0.0004         2.7376   
          7         140        00:00:20       0.0004         3.2085   
          7         144        00:00:21       0.0004         3.0236   
          7         148        00:00:21       0.0004         3.0238   
          7         152        00:00:22       0.0004         3.0504   
          8         156        00:00:22       0.0004         3.3594   
          8         160        00:00:23       0.0004         3.3002   
          8         164        00:00:23       0.0004         3.4047   
          8         168        00:00:24       0.0004         2.3948   
          8         172        00:00:25       0.0004         2.7388   
          8         176        00:00:25       0.0004         2.6304   
    Computing Student Anomaly Map Percentile Statistics.
    
    Computing anomaly map percentile statistics for 11 input images.
    .
    

    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);
    detector.Threshold = T
    detector = 
      studentTeacherAnomalyDetector with properties:
    
        Threshold: 1.9495
        ImageSize: [16 16 3]
          Network: "small"
    
    

    Input Arguments

    collapse all

    Student-Teacher anomaly detector to train, specified as a studentTeacherAnomalyDetector 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.

    Tip

    When you perform tile-based training and GPU memory is limited, you can reduce the memory usage by decreasing the mini-batch size, using the MiniBatchSize (Deep Learning Toolbox) argument of the trainingOptions function, at the expense of slower computation speed.

    Name-Value Arguments

    collapse all

    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 = trainStudentTeacherAnomalyDetector(normalData,untrainedDetector,options,NormalizationDataRatio=0.2) specifies that the detector must use 20% of the training data for anomaly map normalization.

    Proportion of training data to use for anomaly map normalization and, optionally, optimization of the anomaly score for logical anomalies, specified as a numeric scalar in the range [0, 1). For example, a data ratio of 0.1 specifies that the detector must use only 10% of the training data for anomaly map normalization. If you specify this value as 0, the detector does not perform anomaly map normalization. Use a higher percentage of training data for normalization when your training data size is large.

    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 Student-Teacher anomaly detector, returned as a studentTeacherAnomalyDetector object.

    Version History

    Introduced in R2026b