Main Content


Load FastFlow anomaly detector model for code generation

Since R2023a



    net = vision.loadFastFlowAnomalyDetector(filename) loads a pretrained fastFlowAnomalyDetector 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

    Use the vision.loadFastFlowAnomalyDetector function to load a FastFlow anomaly detector and generate C code for this network. This example assumes you have a pretrained fastFlowAnomalyDetector object saved as a variable named detector in the file trainedFastFlow8Detector.mat. For an example of training a FastFlow anomaly detector, see trainFastFlowAnomalyDetector.

    Load the pretrained fastFlowAnomalyDetector object from the MAT file.


    Create an entry-point function myFastFlow.m 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 myFastFlow function.

    • Load the detector in the file trainedFastFlow8Detector.mat into the myDetector variable using the vision.loadFastFlowAnomalyDetector function.

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

    function [score,label] = myFastFlow(in)
      persistent myDetector;
        if isempty(myDetector)
            myDetector = vision.loadFastFlowAnomalyDetector("trainedFastFlow8Detector.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. The example assumes the network was trained on truecolor images of size 16-by-16 pixels.

    inputSize = [16 16 3];

    Create a coder.config (MATLAB Coder) configuration object for MEX code generation and set the target language to C. On the configuration object, set DeepLearningConfig with no target library. The codegen (MATLAB Coder) 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 myFastFlow -report;

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

    Read a test image from a graphics file. The image must be the same size as inputSize. Convert the image to data type single as expected by the generated code.

    dataDir = fullfile(toolboxdir("vision"),"visiondata","digits","handwritten","4");
    imTest = imread(dataDir+filesep+"digit_4_5.png");
    imTest = im2single(imTest);

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

    [predScore,predLabel] = myFastFlow_mex(imTest)
    predScore =
    predLabel = 

    Input Arguments

    collapse all

    Filename of the MAT file containing the pretrained fastFlowAnomalyDetector 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 a fastFlowAnomalyDetector 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)