Main Content


Detect anomalies using PatchCore network

Since R2023a


    The patchCoreAnomalyDetector object detects images of anomalies using a PatchCore anomaly detector network. Train the detector using the trainPatchCoreAnomalyDetector function. To detect anomalous images, pass the trained detector to the classify function.


    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.



    detector = patchCoreAnomalyDetector creates a PatchCore anomaly detector from a ResNet-18 backbone network.


    detector = patchCoreAnomalyDetector(Backbone=backbone) specifies the base feature extraction backbone network from which to create the PatchCore anomaly detector.

    Input Arguments

    expand all

    Name of the pretrained backbone network that serves as the base feature extraction network, specified as a dlnetwork (Deep Learning Toolbox) object or one of these values:

    • "resnet18" — ResNet-18 convolutional neural network. See resnet18 (Deep Learning Toolbox) for more information.

    • "resnet50" — ResNet-50 convolutional neural network. See resnet50 (Deep Learning Toolbox) for more information.

    You must install the associated add-on for the selected pretrained network.

    If you specify the network as a dlnetwork (Deep Learning Toolbox) object, it must accept a single input and return one or more outputs.

    Data Types: char | string | dlnetwork


    expand all

    Anomaly threshold, specified as a numeric scalar. You must set the value of the threshold before calling the classify function. You can calculate a threshold using the anomalyThreshold function.

    This property is read-only.

    Image size of images used for training and inference, stored as a three-element numeric row vector. The trainPatchCoreAnomalyDetector function sets the ImageSize property during training, which indicates the size of the training images. The image size can be different from the input layer size of backbone.


    When using the detector, the image size at inference must match the image size used during training.

    Object Functions

    predictPredict unnormalized anomaly scores
    classifyClassify image as normal or anomalous
    anomalyMapPredict per-pixel anomaly score map


    collapse all

    Create a patchCoreAnomalyDetector object from a ResNet-50 backbone network.

    detector = patchCoreAnomalyDetector(Backbone="resnet50")
    detector = 
      patchCoreAnomalyDetector with properties:
        Threshold: []
        ImageSize: []


    [1] Roth, Karsten, Latha Pemula, Joaquin Zepeda, Bernhard Schölkopf, Thomas Brox, and Peter Gehler. “Towards Total Recall in Industrial Anomaly Detection.” arXiv, May 5, 2022.

    Extended Capabilities

    Version History

    Introduced in R2023a

    expand all