Main Content

vision.loadPatchCoreAnomalyDetector

Load PatchCore anomaly detector model for code generation

Since R2023a

    Description

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

    Note

    This functionality requires Deep Learning Toolbox™ and the Automated Visual Inspection Library for Computer Vision Toolbox™. You can install the Automated Visual Inspection Library for Computer Vision Toolbox from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.

    example

    Examples

    collapse all

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

    Load the pretrained patchCoreAnomalyDetector object from the MAT file.

    load("trainedPatchCore8Detector.mat");

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

    • Load the detector in the file trainedPatchCore8Detector.mat into the myDetector variable using the vision.loadPatchCoreAnomalyDetector function.

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

    function [score,label] = myPatchCore(in)
      persistent myDetector;
        if isempty(myDetector)
            myDetector = vision.loadPatchCoreAnomalyDetector("trainedPatchCore8Detector.mat");
        end
      score = predict(myDetector,in);
      if score < myDetector.Threshold
        label = "normal";
      else
        label = "anomaly";
      end
    end

    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 myPatchCore -report;

    The codegen command places all the generated files in the codegen folder. The folder contains the C code for the entry-point function myPatchCore.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] = myPatchCore_mex(imTest)
    predScore =
    
      single
    
        2.6989
    
    
    predLabel = 
    
        "anomaly"

    Input Arguments

    collapse all

    Filename of the MAT file containing the pretrained patchCoreAnomalyDetector 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 patchCoreAnomalyDetector object.

    Version History

    Introduced in R2023a

    See Also

    (MATLAB Coder) | (MATLAB Coder)