Main Content


Load FCDD anomaly detector model for code generation

Since R2023a



    net = vision.loadFCDDAnomalyDetector(filename) loads a pretrained fcddAnomalyDetector object saved in the filename MAT file. filename must be a compile-time constant.


    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.


    collapse all

    Load a pretrained fcddAnomalyDetector object from a MAT file. This detector was trained on grayscale images of size 28-by-28.


    Create an entry-point function myFCDD that accepts an image as input. The entry-point function performs these operations:

    • Define a persistent variable called myDetector. The persistent variable prevents reconstructing and reloading the network object during subsequent calls to the myFCDD function.

    • Load the detector in the file digit8AnomalyDetector.mat into the myDetector variable using the vision.loadFCDDAnomalyDetector function.

    • Perform operations, such as prediction and classification, on the input image using the detector.

    Show the contents of the entry-point function. This function is attached to the example as a supporting file.

    type myFCDD.m
    function [score,label] = myFCDD(in)
      persistent myDetector;
        if isempty(myDetector)
            myDetector = vision.loadFCDDAnomalyDetector("digit8AnomalyDetector.mat");
      score = predict(myDetector,in);
      if score < myDetector.Threshold
        label = "normal";
        label = "anomaly";

    Specify the size of the training images as the size of the input to the entry-point function.

    inputSize = [28 28];

    Create a coder.config configuration object for MEX code generation and set the target language to C. On the configuration object, set the DeepLearningConfig property with no target library. The codegen function must determine the size, class, and complexity of MATLAB function inputs. Use the -args option to specify the size of the input to the entry-point function. Use the -config option to pass the code configuration object.

    cfg = coder.config("mex");
    cfg.TargetLang = "C";
    cfg.DeepLearningConfig = coder.DeepLearningConfig;
    codegen -args {ones(inputSize,"single")} -config cfg myFCDD -report;
    Code generation successful: To view the report, open('codegen/mex/myFCDD/html/report.mldatx')

    The codegen command places all the generated files in the codegen folder. The folder contains the C code for the entry-point function myFCDD.c, header and source files containing the C class definitions for the convoluted neural network (CNN), weight, and bias files.

    Load the test data. The test images must be the same size as the training images.

    [XTest,YTest] = digitTest4DArrayData;

    Select a test image. Convert the image to data type single, as expected by the generated code.

    idxToTest = 1;
    imTest = XTest(:,:,:,idxToTest);
    imTest = im2single(imTest);

    Predict the anomaly score and label of the test image by calling the generated code.

    [predScore,predLabel] = myFCDD_mex(imTest)
    predScore = single
    predLabel = 

    Input Arguments

    collapse all

    Filename of the MAT file containing the pretrained fcddAnomalyDetector object, specified as a string scalar. The MAT file must exist on the MATLAB® path and contain only the network to be loaded.

    This input argument must be a compile-time constant.

    Data Types: string

    Output Arguments

    collapse all

    Network, returned as an fcddAnomalyDetector object.

    Extended Capabilities

    C/C++ Code Generation
    Generate C and C++ code using MATLAB® Coder™.

    GPU Code Generation
    Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

    Version History

    Introduced in R2023a

    See Also

    (MATLAB Coder) | (MATLAB Coder) | (MATLAB Coder)