A Prototype-Based Neural Network for Image Anomaly Detection and
  Localization
Image anomaly detection and localization perform not only image-level anomaly
classification but also locate pixel-level anomaly regions. Recently, it has
received much research attention due to its wide application in various fields.
This paper proposes ProtoAD, a prototype-based neural network for image anomaly
detection and localization. First, the patch features of normal images are
extracted by a deep network pre-trained on nature images. Then, the prototypes
of the normal patch features are learned by non-parametric clustering. Finally,
we construct an image anomaly localization network (ProtoAD) by appending the
feature extraction network with $L2$ feature normalization, a $1\times1$
convolutional layer, a channel max-pooling, and a subtraction operation. We use
the prototypes as the kernels of the $1\times1$ convolutional layer; therefore,
our neural network does not need a training phase and can conduct anomaly
detection and localization in an end-to-end manner. Extensive experiments on
two challenging industrial anomaly detection datasets, MVTec AD and BTAD,
demonstrate that ProtoAD achieves competitive performance compared to the
state-of-the-art methods with a higher inference speed. The source code is
available at: https://github.com/98chao/ProtoAD.
Chao Huang, Zhao Kang, Hong Wu
# A Prototype-Based Neural Network for

###### Abstract

Image anomaly detection and localization perform not only image-level anomaly classification but also locate pixel-level anomaly regions. Recently, it has received much research attention due to its wide application in various fields. This paper proposes ProtoAD, a prototype-based neural network for image anomaly detection and localization. First, the patch features of normal images are extracted by a deep network pre-trained on nature images. Then, the prototypes of the normal patch features are learned by non-parametric clustering. Finally, we construct an image anomaly localization network (ProtoAD) by appending the feature extraction network with \(\mathbf{L2}\) feature normalization, a \(\mathbf{1\times 1}\) convolutional layer, a channel max-pooling, and a subtraction operation. We use the prototypes as the kernels of the \(\mathbf{1\times 1}\) convolutional layer; therefore, our neural network does not need a training phase and can conduct anomaly detection and localization in an end-to-end manner. Extensive experiments on two challenging industrial anomaly detection datasets, MVTec AD and BTAD, demonstrate that ProtoAD achieves competitive performance compared to the state-of-the-art methods with a higher inference speed. The source code is available at: [https://github.com/98chao/ProtoAD](https://github.com/98chao/ProtoAD).

**Keywords: Image Anomaly Detection, Image Anomaly Localizationf, Non-parametric Clustering, Prototype-Based Network**
## 1 Introduction

_Anomaly detection_ (AD) [1, 2] aims to detect anomalous samples that are deviated from a set of normal samples predefined during training. Traditional image anomaly detection adopts a semantic AD setting [3, 4, 5, 6], where anomaly samples are from unknown semantic classes different from the one normal samples belong to. Recently, detecting and localizing subtle image anomalies has become an important task in computer vision with various applications, such as anomaly or defect detection in industrial optical inspection [7, 8], anomaly detection and localization in video surveillance [9, 10, 11], or anomaly detection in medical images [12, 13]. In this setting, anomaly detection determines whether an image contains any anomaly, and anomaly localization, aka anomaly segmentation, localizes the anomalies at the pixel level. This paper focuses on the second setting, especially industrial anomaly detection and localization. Some examples from the MVTec AD dataset [8] along with predictions by our method are shown in Figure 1.

In the above applications, anomalous samples are scarce and hard to collect. Therefore, image anomaly detection and localization are often solved with only normal samples. In addition, anomalous regions within images are often subtle (see Figure 1), making image anomaly localization a more challenging task that has not been thoroughly studied compared to image anomaly detection. Recent anomaly localization methods can be roughly categorized into two classes: reconstruction-based methods and OOD-based (out-of-distribution based) methods.

Reconstruction-based methods are mainly based on the assumption that a model trained only on normal images can not reconstruct anomalous images accurately. They reconstruct image as a whole [14, 15, 16, 17, 18, 19, 20, 8, 21, 22, 12], or reconstruct in the feature space [23, 24, 22]. Then anomaly detection and localization can be

Figure 1: Examples from the MVTec benchmark datasets. From top to bottom: anomaly samples, anomaly mask, and anomaly score maps predicted by our method.
performed by measuring the difference between the reconstructed and original ones. This kind of method always needs cumbersome network training.

OOD-based methods evaluate the degree of abnormality for a patch feature by measuring its deviation from a set of normal patch features, which is intrinsically a patch-wise OOD detecting task. Some methods such as PatchSVDD [25] and CutPaste [26] learn feature representation by self-supervised learning. On the contrary, some other methods [27, 28, 29, 30] simply extract features by deep networks pre-trained on natural image datasets such as ImageNet[31], and achieve promising and even better performances. Since the number of training patches is much larger than that of training images, the inference time and storage increase remarkably. Different strategies have been proposed to tackle this problem. Napoletano et al. [27] used k-means to learn the dictionary/prototypes for normal patch features, but they evaluated each test patch independently, resulting in high inference time. SPADE [28] selects k-nearest normal images for patch-wise evaluation based on the global image features, limiting anomaly localization performance. PaDiM [29] models the normal patches at each position by a multidimensional Gaussian distribution and measures the anomaly by the Mahalanobis distance between a test patch feature and the Gaussian at the same position. However, both SPADE [28] and PaDiM [29] are reliant on image alignment. The current state-of-the-art method, PatchCore [30], uses greedy coreset subsampling to reduce the inference time and storage significantly.

This paper proposes ProtoAD, a prototype-based neural network for image anomaly detection and localization, to improve OOD-based methods' inference speed. We assume that all normal patch features can be grouped into some prototypes, and abnormal patch features cannot be properly assigned to any of them. Therefore, image anomaly localization can be performed by measuring the deviation of test patch features from the prototypes of normal patch features. First, the patch features of normal images are extracted by a deep network pre-trained on nature images and are \(L2\)-normalized. Then the prototypes of the normalized normal patch features are learned by a non-parametric clustering algorithm. The cosine similarity between two \(L2\)-normalized vectors is equivalent to the dot product between them. Therefore the cosine similarity between a normalized patch feature and a prototype can be implemented by a \(1\times 1\) convolution. Based on this equivalence, we construct an image anomaly localization network (ProtoAD) by appending the feature extraction network with the \(L2\) feature normalization, a \(1\times 1\) convolutional layer, a channel max-pooling, and a subtraction operation. We use the prototypes as the kernels of the \(1\times 1\) convolutional layer; therefore, our neural network does not need a training phase. Compared with previous OOD-based methods [27, 28, 29, 30], ProtoAD can perform the anomaly detection and localization in an end-to-end manner, which is more elegant and efficient. Extensive experiments on two challenging industrial anomaly detection datasets, MVTec AD [8] and BTAD [32], demonstrate that ProtoAD achieves competitive performance compared
to the state-of-the-art methods with a higher inference speed. This advantage of ProtoAD makes it better match the needs of real-world industrial applications.

## 2 Related Works

### Image Anomaly Localization

Anomaly detection is an image-level task to determine whether an image contains any anomaly. On the other hand, anomaly localization is more complex to locate anomalies at the pixel level. Here, we only introduce the methods that can be directly applied to image anomaly localization and roughly categorize current methods into two types: reconstruction-based and OOD-based.

Reconstruction-based methods are mainly based on the assumption that a model trained only on normal images can not reconstruct anomalous images accurately, and anomaly detection and localization can be performed by measuring the difference between the reconstructed and original images. Early reconstruction-based methods [8, 12, 14, 15, 17] reconstruct image by auto-encoders (AE), variational autoencoders (VAE) or generative adversarial networks (GAN). However, the neural networks have high generalization capacities and can reconstruct anomalies well. Later, different strategies have been proposed to tackle this problem. Different memory-based auto-encoders [16, 18, 20] have been proposed to reconstruct images with features from memory bank to limit the generalization ability. Student-teacher models [22, 23] have been used to reconstruct pre-trained deep features. RIAD [19] randomly removes partial image regions and reconstructs the image by image in-painting. Glance [24] trains a Global-Net to regress the deep features of cropped patches based on their context. DRAEM [21] combines a reconstructive sub-network and a discriminative network and trains them in an end-to-end manner on synthetically generated just-out-of-distribution images.

OOD-based methods evaluate the degree of abnormality for a patch feature by measuring its deviation from a set of normal patch features, which is intrinsically a patch-wise OOD detecting task. Some methods such as PatchSVDD [25] and CutPaste [26] learn feature representation by self-supervised learning. On the contrary, some other methods [27, 28, 29, 30] simply extract features by deep networks pre-trained on natural image datasets such as ImageNet [31], and achieve promising and even better performances. Since the number of training patches is much larger than that of training images, the inference time and storage increase remarkably. Different strategies such as clustering, density estimation, and sampling have been proposed to tackle this problem. Napoletano et al. [27] learned a dictionary of normal patches from the training set by k-means, and evaluated each patch of a test image by measuring its visual similarity with the k-nearest neighbors in the dictionary. SPADE [28] compares patch features of a test image with the patch features at the same position of k-nearest normal images selected based on global image features. However, this oversimplified pre-selection strategy will limit the localization
performance. PaDiM [29] models the normal patches at each position by a multidimensional Gaussian distribution and detect anomaly by the Mahalanobis distance between a test patch feature and the Gaussian at the same position. Both SPADE [28] and PaDiM [29] are reliant on image alignment. Recently, PatchCore [30] constructs the memory bank of locally aware patch features by greedy coreset subsampling, and localizes anomaly by measuring the distances of test patch features to their nearest normal patch features in the bank. As a result, PatchCore achieves a new state-of-the-art and significantly reduces the inference time and storage.

Our method is also an OOD-based method with pre-trained deep features but has several differences from the previous works. Our method uses non-parametric clustering instead of k-means in [27] to learn the prototypes for normal patch features. More importantly, our method can perform anomaly detection and localization by a network in an end-to-end manner, which is more elegant and efficient than the previous methods. Compared to reconstruction-based methods, our network do not need a cumbersome network training phase.

### Clustering Algorithms

Clustering is a type of unsupervised learning task of dividing a set of unlabeled data points into a number of groups such that the data points in the same groups are more similar to each other than they are to the data points in other groups. Clustering provides an abstraction from data points to the clusters, and each cluster can be characterized by a cluster prototype, such as the centroid of a cluster, for further analysis. Clustering algorithms can be roughly divided into four categories: Partition-based cluster, Density-based clustering, Spectral Clustering, and Hierarchical-based clustering.

Partition-based clustering algorithms divide the data into k groups, where k is the predefined number of cluster. The classical algorithms are k-means [33] and its variations. Although these algorithms are very fast, they need the number of clusters as a parameter and are sensitive to the selection of the initial k centroids.

Density-based clustering defines a cluster as the largest set of densely connected points and can find clusters of arbitrary shapes. DBSCAN [34] is the most representative algorithm of this class. It has two parameters, radius length \(\epsilon\) and a parameter \(MinPts\). If there are \(MinPts\) points in the radius of \(\epsilon\) of a point, it is regarded as a high-density point.

Spectral Clustering [35] has recently attracted much attention. Most spectral clustering algorithms need to compute the full similarity graph Laplacian matrix and have quadratic complexities, thus severely restricting their application to large data sets.

Hierarchical clustering [36] is of two types: bottom-up and top-down approaches. In the bottom-up approach (aka agglomerative clustering), each data point starts as a cluster, and the most similar cluster pairs are iteratively merged according to the chosen similarity measure until some stopping
criteria are met. In the top-down approach (aka divisive clustering), the clustering begins with a large cluster including all data and recursively breaks down into smaller clusters. Hierarchical clustering produces a clustering tree that provides meaningful ways to interpret data at different levels of granularity. Recently, Sarfraz et al. [37] proposed FINCH, a high-speed, scalable, and fully parameter-free hierarchical agglomerative clustering algorithm.

In [27], k-means is used to learn the prototypes from normal patch features. To avoid choosing the number of clusters ahead, we adopt FINCH to learn the prototypes for normal patch features.

## 3 Method

Our method consists of three steps: patch feature extraction, prototype learning, and anomaly detection and localization. An overview of our method is given in Figure. 2. We describe them sequentially in the following subsection.

### Patch Feature Extraction

Since the features extracted by pre-trained networks have shown their effectiveness for various visual applications including anomaly detection [22, 23, 27, 28, 29, 30], we also adopt deep networks pre-trained on ImageNet dataset [31] as the feature extractor, and choose the backbone of Wide-ResNet [38] as the feature extractor following the previous works [28, 29, 30].

ResNet-like deep networks [38, 39] include several convolutional stages. The features become more abstract when the stage goes deeper, but their resolution gets lower. Thus, the feature maps from different stages form a

Figure 2: An overview of the proposed method. First, the patch features of normal images are extracted by a deep network pre-trained on nature images. Then, the prototypes of the normal patch features are learned by FINCH clustering. For inference, an image anomaly localization network (ProtoAD) is constructed by appending the feature extraction network with the \(L2\) feature normalization, a \(1\times 1\) convolutional layer, a channel max-pooling (CMP), and a subtraction operation, and anomaly localization is performed in an end-to-end manner.
feature hierarchy for an input image. Each spatial position of a feature map has a receptive field and corresponds to a patch/region in an input image; therefore, the feature vector at a spatial position of feature maps can be considered as a feature representation for the corresponding image patch. If the feature maps of a stage have a resolution of \(H\times W\), they contains \(H\times W\) patch features. The deep and abstract features from the ImageNet pre-trained networks are biased towards the ImageNet classification task and are less relevant to the anomaly detection and localization task. Therefore, we adopt the low- and mid-level (stage 1-3) feature representations and combine them as the patch features. Concretely, the feature maps at the higher-level are bilinearly re-scaled to have the same resolution as the lowest level, then the feature maps at different levels are concatenated together for handling multi-scale anomalies. The extracted features are then \(L2\)-normalized where each feature vector is divided by its \(L2\) norm.

### Prototype Learning

After feature extraction, the prototypes of the \(L2\)-normalized patch features are learned by a clustering algorithm. Then, the prototypes are used in anomaly detection and localization instead of all the normal patch features to reduce the inference time and storage. There are mainly two concerns in choosing a clustering algorithm. First, the number of patch features is much larger than that of training images. For example, each category of MVTec AD dataset has several hundreds of images, while it has several hundreds of thousands of patch features in our implementation. Therefore, the clustering algorithm should be efficient and scalable to large-scale data. Second, most clustering algorithms have some parameters, e.g., the number of clusters or distance thresholds, which can not be well set without a priori knowledge of the data distribution. Thus, these algorithms demand a tedious parameter tuning process to achieve good performance. To meet the requirements of real applications, we adopt FINCH [37], a high-speed, scalable, and fully parameter-free hierarchical agglomerative clustering algorithm.

The core idea of FINCH is to use the nearest neighbor information of each data point for clustering, which does not need to specify any parameters and has a low computational overhead. Given the integer indices of the first neighbor of each data point, an adjacency matrix is defined according to the following rules:

\[A(i,j)=\begin{cases}1,&\text{if }j=\kappa_{i}^{1}\text{ or }\kappa_{j}^{1}=i \text{ or }\kappa_{i}^{1}=\kappa_{j}^{1}\\ 0,&otherwise\end{cases} \tag{1}\]

where \(\kappa_{i}^{1}\) symbolizes the first neighbor of data point \(i\). This sparse adjacency matrix specifies a graph where connected data points form clusters. It directly provides clusters without solving a graph segmentation problem. After computing the first partition, FINCH merges the clusters recursively by using cluster means to compute the first neighbor of each cluster until all data points are
included in a single cluster or until some stopping criteria is met. In this work, we define the stopping criteria as the number of cluster is less than a threshold and set the threshold to 10,000 to get good results in our experiments. We choose the last partition as the clustering result, and use the mean vectors of clusters as the prototypes of normal patch features.

When the features are \(L2\)-normalized (making the length of a vector to 1), cosine similarity and Euclidean distance between the normalized features are equivalent in the sense of nearest neighbor searching:

\[\frac{1}{2}L_{2}(\mathbf{x}_{a},\mathbf{x}_{b})^{2}=\frac{1}{2}(\mathbf{x}_{a }-\mathbf{x}_{b})\cdot(\mathbf{x}_{a}-\mathbf{x}_{b})=1-\mathbf{x}_{a}\cdot \mathbf{x}_{b}=1-\cos{(\mathbf{x}_{a},\mathbf{x}_{b})} \tag{2}\]

where \(L_{2}()\) is Euclidean distance, \(\mathbf{x}_{a}\) and \(\mathbf{x}_{b}\) are two \(L2\)-normalized feature vectors, and \(\cos\) is cosine similarity. Therefore, we use cosine similarity for clustering and measuring the deviation of test patch features from norm patch features in the next subsection.

### Neural Network for Anomaly Detection and Localization

When a test image passes through the feature extraction network, \(H\times W\) patch features have been extracted. The anomaly score of each patch feature can be computed by measuring its deviation from the prototypes of normal patch features. We compute the anomaly score of a test patch as one minus the cosine similarity between the normalized test patch feature and its nearest prototype. Formally, the anomaly score for the patch at position \((i,j)\) can be calculated as

\[s_{ij}=1-\max_{1\leqslant k\leqslant K}\cos{(\mathbf{x}_{ij},\mathbf{m}_{k})} \tag{3}\]

where \(\mathbf{x}_{ij}\) is the normalized patch feature at position \((i,j)\), \(\mathbf{m}_{k}\) is the \(k\)-th prototype, and \(\cos\) is cosine similarity. In addition, the image-level anomaly score for a test image can be simply computed by maximizing the anomaly scores of all its patch features.

\[S=\max_{1\leqslant i\leqslant H,1\leqslant j\leqslant W}s_{ij} \tag{4}\]

The cosine similarities between a normalized patch feature and a prototype can be computed by a \(1\times 1\) convolution (dot product) between them. Based on this equivalence, we construct a neural network (ProtoAD) for anomaly detection and localization. First, the \(L2\) feature normalization and a \(1\times 1\) convolutional layer are appended to the feature extraction network, and outputs feature maps of size \(H\times W\times K\), including the cosine similarities between the \(H\times W\) normalized patch features and all \(K\) prototypes. Then, channel max-pooling (CMP) is applied to the feature maps to get the normal score map of \(H\times W\), including the cosine similarities between the \(H\times W\) normalized patch features and their nearest prototypes. The anomaly score map can be
further obtained by computing one minus the normal score map. This process is illustrated by Figure 3. Since the spatial resolution of feature maps is lower than that of an input image, we resize the anomaly score map to the resolution of the input image and use a Gaussian filter to smooth it. Finally, anomaly localization can be achieved by thresholding the anomaly score map, and the anomaly score for the test image can be obtained by maximizing the anomaly score map.

We use the prototypes of normal patch features as the kernels of the \(1\times 1\) convolutional layer. Therefore the proposed neural network does not need a training phase. Compared to previous works [27, 28, 29, 30], our method can perform the anomaly detection and localization in an end-to-end manner, which is more elegant and efficient.

## 4 Experiments

### Datasets and Metrics

#### 4.1.1 Dataset

MVTec AD dataset [8] is a real-world industrial defect detection dataset which has become a standard benchmark for evaluating image anomaly detection and localization methods. It has 5354 high-resolution images belonging to 10 objects and 5 texture categories. The images of each category are split into a training and a testing set. Totally, the training set has 3629 normal images, and the test set has 1725 normal and abnormal images of various defects. The ground truth of the test set contains anomaly labels for image-level evaluation and anomaly masks for pixel-level evaluation.

BTAD (BeanTech Anomaly Detection dataset) is a real-world industrial dataset recently released by [32]. It contains a total of 2830 real-world images of 3 industrial products. The images of each category are split into a defect-free training set and a testing set, supporting evaluation of both anomaly detection and localization.

We follow the split of the two datasets for training and testing.

Figure 3: Anomaly detection and localization process of ProtoAD.
#### Evaluation Metrics

AUROC (Area Under the Receiver Operating Characteristic curve) is the most commonly used metric for anomaly detection, which is independent of the threshold. We use image-level AUROC for evaluating the performance of anomaly detection, pixel-level AUROC for anomaly localization. Since the pixel-level AUROC is biased in favor of large anomalies, we also use PRO-score (per-region-overlap) [22] to evaluate anomaly localization, which weights ground-truth regions of different sizes equally.

### Experimental Setup

We normalize the size of images from all categories of MVTec AD and BTAD dataset to \(256\times 256\), center crop images to \(224\times 224\), and do not apply any data augmentation. The backbone of Wide-ResNet50 pre-trained on ImageNet is employed as the feature extractor in our method as in [28, 29, 30]. We define the stopping criteria for FINCH clustering algorithm as the number of clusters is less than 10,000 and choose the last generated partition as the clustering result. For inference, we up-sample the anomaly score map to image size using bilinear interpolation and smooth it with the Gaussian filter with parameter \(\delta=4\) as in [29]. We implemented our models in Python 3.7 [40] and PyTorch [41], and run experiments on NVIDIA GeForce RTX 2080 Ti.

### Results on MVTec AD

#### Comparison with the State-of-the-art

We compare ProtoAD with the state-of-the-art methods including both the reconstruction and OOD-based methods. The compared reconstruction-based methods include Uninformed students (U-Student) [22], RIAD [19], MKD [23], Glance [24], DAAD [20] and DREAM [21]. And the compared OOD-based methods include SPADE [28], PatchSVDD (P-SVDD) [25], CutPaste [26], PaDiM [29], and PatchCore (P-Core) [30]. We directly use their evaluation results if they have been provided.

We report the evaluation results (pixel-level AUROC and PRO-score) for pixel-level anomaly localization on MVTec AD dataset in Table 1 and Table 2 respectively. From table 1, we can see that the OOD-based methods generally achieve better pixel-level AUROC than the reconstruct-based methods. Among the OOD-based methods, the methods using the pre-trained deep features achieve better pixel-level AUROC than the methods based on self-supervised learning. PatchCore achieves the best pixel-level AUROC, PaDiM the second, and the reconstruct-based method DREAM the third. The pixel-level AUROC of our method is very close to those of PaDiM and DREAM. We also notice that our method is more effective on the texture category and achieves the second best AUROC. Table 2 gives the PRO-score results for methods which have used this metric. Among them, Glance achieves the best result, our method is the second best and outperform other OOD-based methods. After all, our method
achieves competitive anomaly localization performance to the state-of-the-art methods.

Figure 4 gives qualitative anomaly localization results of our method on MVTec AD dataset. We can see that our method can give accurate pixel-level localization regardless of anomaly region size and type (see supplementary for more qualitative results).

We also report the image-level AUROC results for anomaly detection in Table 3. PatchCore achieves the best AUROC again, DREAM the second. Our method remains competitive and achieves the third-best AUROC, which is very close to that of DREAM.

#### 4.3.2 Inference Efficiency

Anomaly detection and localization algorithms need high precision and inference speed to match the requirements of real-world applications. Thus, we

\begin{table}
\begin{tabular}{l c c c c c c c c c c} \hline \hline \multirow{2}{*}{**Category**} & \multicolumn{4}{c}{**Reconstruction-based**} & \multicolumn{4}{c}{**OOD-based**} \\ \cline{2-10}  & **MKD** & **Clanco** & **RIAD** & **DRAEM** & **R-SVDD** & **CutPaste** & **SPADE** & **PaDiM** & **P-Core** & **ProtoAD** \\ \hline Carpet & 95.6 & 96.0 & 96.3 & 95.5 & 92.6 & 98.3 & 97.5 & 99.1 & 99.0 & 99.2 \\ Grid & 91.8 & 78.0 & 98.8 & 99.7 & 96.2 & 97.5 & 93.7 & 97.3 & 98.7 & 98.0 \\ Leather & 98.1 & 90.0 & 99.4 & 98.6 & 97.4 & 99.5 & 97.6 & 99.2 & 99.3 & 99.4 \\ Tile & 82.8 & 80.0 & 89.1 & 99.2 & 91.4 & 90.5 & 87.4 & 94.1 & 95.6 & 95.2 \\ Wood & 84.8 & 81.0 & 85.8 & 96.4 & 90.8 & 96.5 & 88.5 & 94.9 & 95.0 & 95.6 \\ \hline
**Texture** & 90.6 & 85.0 & 93.9 & 97.9 & 93.7 & 96.3 & 92.9 & 96.9 & 97.5 & 97.5 \\ \hline Bottie & 96.3 & 93.0 & 98.4 & 96.1 & 98.1 & 97.6 & 96.4 & 98.3 & 98.6 & 98.3 \\ Cable & 82.4 & 94.0 & 84.2 & 94.7 & 96.8 & 90.0 & 97.2 & 96.7 & 98.4 & 97.5 \\ Capsule & 95.9 & 90.0 & 92.8 & 94.3 & 96.8 & 97.4 & 99.0 & 98.5 & 98.8 & 98.2 \\ Hazult & 94.6 & 84.0 & 96.1 & 90.7 & 97.5 & 97.3 & 99.1 & 98.2 & 98.7 & 98.8 \\ Metal Nut & 86.4 & 91.0 & 92.5 & 99.5 & 98.0 & 93.1 & 98.1 & 97.2 & 98.4 & 98.8 \\ Pill & 88.6 & 93.0 & 95.7 & 97.6 & 96.1 & 95.7 & 96.5 & 95.7 & 97.4 & 94.2 \\ Serv & 96.0 & 96.0 & 96.8 & 97.6 & 96.7 & 96.7 & 98.9 & 98.5 & 99.4 & 98.9 \\ Toothbrush & 96.1 & 96.0 & 98.9 & 98.1 & 98.1 & 98.1 & 97.9 & 98.8 & 98.7 & 98.8 \\ Transistor & 76.5 & 100.7 & 87.0 & 99.7 & 93.0 & 94.1 & 97.5 & 96.3 & 92.5 \\ Zipper & 93.9 & 99.0 & 97.8 & 98.8 & 96.1 & 99.3 & 96.5 & 98.5 & 98.8 & 96.7 \\ \hline
**Object** & 90.8 & 90.6 & 94.3 & 97.0 & 96.7 & 95.8 & 97.6 & 97.8 & 98.4 & 97.1 \\ \hline
**All** & 90.7 & 90.7 & 94.2 & **97.3** & 96.7 & 96.0 & 96.0 & 97.5 & **98.1** & 97.2 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Anomaly localization performance on MVTec AD (Pixel-level AUROC). The best results of the two classes of methods are bold-faced respectively.

\begin{table}
\begin{tabular}{l c c c c c c} \hline \hline \multirow{2}{*}{**Category**} & \multicolumn{4}{c}{**Reconstruction-based**} & \multicolumn{4}{c}{**OOD-based**} \\ \cline{2-7}  & **U-Student** & **Glance** & **SPADE** & **PaDiM** & **P-Core** & **ProtoAD** \\ \hline Carpet & 87.9 & 97.7 & 54.7 & 96.2 & 96.6 & 97.0 \\ Grid & 95.2 & 93.2 & 86.7 & 94.6 & 96.0 & 93.9 \\ Leather & 94.5 & 90.9 & 97.2 & 97.8 & 98.9 & 98.1 \\ Tile & 94.6 & 88.3 & 75.6 & 86.0 & 87.3 & 87.2 \\ Wood & 91.1 & 94.1 & 87.4 & 91.1 & 89.4 & 93.2 \\ \hline
**Texture** & 92.7 & 92.8 & 88.3 & 93.1 & 93.6 & 93.9 \\ \hline Bottie & 93.1 & 96.8 & 95.5 & 94.8 & 96.2 & 95.8 \\ Cable & 81.8 & 98.0 & 90.9 & 88.8 & 92.5 & 93.8 \\ Capsule & 96.8 & 96.0 & 93.7 & 93.5 & 95.5 & 93.7 \\ Hazult & 96.5 & 96.2 & 95.4 & 92.6 & 93.8 & 95.3 \\ Metal Nut & 94.2 & 96.7 & 94.4 & 85.6 & 91.4 & 94.2 \\ Pill & 96.1 & 97.8 & 94.6 & 92.7 & 93.2 & 94.7 \\ Screw & 94.2 & 100 & 96.0 & 94.4 & 97.9 & 94.7 \\ Toothbrush & 93.3 & 96.1 & 93.5 & 93.0 & 91.5 & 91.2 \\ Transistor & 66.6 & 99.9 & 87.4 & 84.5 & 83.7 & 87.9 \\ Zipper & 95.1 & 99.2 & 92.6 & 95.9 & 97.1 & 93.3 \\
**Object** & 90.8 & 97.7 & 93.4 & 91.6 & 93.3 & 93.4 \\ \hline
**All** & 91.4 & **96.1** & 91.7 & 92.1 & 93.4 & **93.6** \\ \hline \hline \end{tabular}
\end{table}
Table 2: Anomaly localization performance on MVTec AD (PRO-score). The best results of the two classes of methods are bold-faced respectively.
also report the inference speed of our method and previous OOD-based methods using pre-trained deep features [28, 29, 30] in the table 4. In the experiments, all the methods adopt Wide-ResNet50 pre-trained on ImageNet as the feature extractor, center-cropped \(224\times 224\) image as input, and run on the same machine with a NVIDIA GeForce RTX 2080 Ti. For PatchCore, we use the implementation provided by the authors, which downsamples the normal patch features via greedy coreset subsampling (PatchCore-\(x\%\) denotes the percentage x of normal patch features are used in inference) and uses _faiss_[42] for nearest neighbor retrieval and distance computations. For PaDiM, we make

\begin{table}
\begin{tabular}{c c c} \hline Method & Scores & Inference Speed (FPS) \\ \hline SPADE & (85.5, 96.0, 91.7) & 7.58 \\ PatchCore(25\%) & (**99.1**, **98.1**, 93.4) & 22.30 \\ PatchCore(10\%) & (99.0, **98.1**, 93.5) & 24.45 \\ PatchCore(1\%) & (99.0, 98.0, 93.1) & 26.17 \\ PaDiM & (95.3, 97.5, 92.1) & 60.32 \\ ProtoAD & (97.7, 97.2, **93.6**) & **72.45** \\ \hline \end{tabular}
\end{table}
Table 4: Comparison of inference speed. Scores includes image-level AUROC, pixel-level AUROC, and PRO-score. The best results are bold-faced.

Figure 4: Qualitative anomaly localization results of our method. From top to bottom: abnormal images, ground-truth, and anomaly score maps produced by our method.

\begin{table}
\begin{tabular}{l c c c c c c c c c} \hline \multirow{2}{*}{**Category**} & \multicolumn{4}{c}{**Reconstruction-based**} & \multicolumn{4}{c}{**OOD-based**} \\ \cline{2-10}  & **MKD** & **DAAD** & **RIAD** & **DRAEM** & **SPADE** & **PaDiM** & **P-SVDD** & **CutPaste** & **P-Core** & **ProtoAD** \\ \hline Carpet & 79.3 & 86.6 & 84.2 & 97.0 & - & - & 92.6 & 98.3 & 98.7 & 99.5 \\ Grid & 78.0 & 95.7 & 99.6 & 99.9 & - & - & 96.2 & 97.5 & 98.2 & 93.7 \\ Leather & 95.1 & 86.2 & 100 & 100 & - & - & 97.4 & 99.5 & 100 & 100 \\ Tile & 91.6 & 88.2 & 98.7 & 99.6 & - & - & 91.4 & 90.5 & 98.7 & 99.2 \\ Wood & 94.3 & 98.2 & 93.0 & 99.1 & - & - & 90.8 & 95.5 & 99.2 & 99.1 \\ \hline
**Texture** & 87.6 & 91.0 & 95.1 & 99.1 & - & 98.8 & 93.7 & 96.3 & 90.0 & 98.3 \\ \hline Bottle & 93.4 & 97.6 & 99.9 & 99.2 & - & - & 98.1 & 97.6 & 100 & 99.9 \\ Cable & 89.2 & 84.4 & 81.9 & 91.8 & - & - & 96.8 & 90.0 & 99.5 & 98.3 \\ Capsule & 80.5 & 76.7 & 88.4 & 98.5 & - & - & 95.8 & 97.4 & 98.1 & 93.1 \\ Hazelnut & 98.4 & 92.1 & 83.3 & 100 & - & - & 97.5 & 97.3 & 100 & 100 \\ Metal Nut & 73.6 & 75.8 & 88.5 & 98.7 & - & - & 98.0 & 93.1 & 100 & 99.9 \\ Pill & 82.7 & 90.0 & 83.8 & 98.9 & - & - & 95.1 & 95.7 & 96.6 & 95.8 \\ Srew & 83.3 & 98.7 & 84.5 & 93.9 & - & - & 95.7 & 96.7 & 98.1 & 94.9 \\ Toothbrush & 92.2 & 99.2 & 100 & 100 & - & - & 98.1 & 98.1 & 100 & 99.7 \\ Transistor & 85.6 & 87.6 & 90.9 & 93.1 & - & - & 97.0 & 93.0 & 100 & 97.7 \\ Zipper & 93.2 & 85.9 & 98.1 & 100 & - & - & 95.1 & 99.3 & 90.4 & 94.6 \\ \hline \hline
**Object** & 87.8 & 88.8 & 89.9 & 97.4 & - & 93.6 & 96.7 & 95.8 & 99.2 & 97.4 \\ \hline
**All** & 87.7 & 89.5 & 91.7 & **98.0** & 85.5 & 95.3 & 95.7 & 96.0 & **99.1** & 97.7 \\ \hline \end{tabular}
\end{table}
Table 3: Anomaly detection performance on MVTec AD (Image-level AUROC). The best results of the two classes of methods are bold-faced respectively.
extensive optimization via GPU acceleration. Compared with the previous methods, our model achieves the highest speed, which is 1.2x, 2.7x, and 9.5x faster than PaDiM, PatchCore, and SPADE, respectively. The high inference speed is mainly because our model performs inference in an end-to-end manner, and the main computation added to the feature extraction network is the \(1\times 1\) convolutional layer. Compared to the reconstruct-based methods, our method does not need a cumbersome network training process.

### Ablation Study

We report ablations studies on the MVTec AD dataset to evaluate the impact of different components of our method on the performance.

#### 4.4.1 Feature Layer Selection

ResNet-like deep networks [38, 39] include several convolutional stages. The feature maps from different stages can compose a feature hierarchy for an image. Since the deepest feature maps in the hierarchy are biased towards the ImageNet classification task, we only adopt the features at the low and middle hierarchy levels (stage 1-3) for anomaly detection and localization. Table 5 gives the performance achieved with the features from different levels and their combination. It can be observed that the features from hierarchy level 2 can achieve the best performance among the first three levels, and a combination of the three levels can further improve the performance. Therefore, our method uses the combination of the first three feature levels as the patch feature.

\begin{table}
\begin{tabular}{c c c c} \hline Partition & Texture & Object & All \\ \hline P2 & (98.9, 97.7, 48132) & (98.2, 97.2, 92904) & (98.5, 97.3, 77980) \\ P3 & (98.5, 97.6, 7802) & (97.8, 97.3, 20616) & (98.0, 97.4, 16345) \\ P4 & (98.4, 97.4, 1166) & (97.4, 97.1, 3397) & (97.7, 97.2, 2653) \\ P5 & (98.1, 97.1, 234) & (95.2, 96.6, 902) & (96.1, 96.8, 679) \\ P6 & (95.8, 96.4, 56) & (92.2, 95.9, 190) & (93.4, 96.1, 146) \\ \hline Best & (99.0, 97.7, 17559) & (98.2, 97.2, 52844) & (98.5, 97.4, 41083) \\ \hline Ours & (98.3, 97.5, 1787) & (97.4, 97.1, 4626) & (97.7, 97.2, 3680) \\ \hline \end{tabular}
\end{table}
Table 6: Anomaly detection and localization performance of ProtoAD with different FINCH partitions. Each tuple shows image-level AUROC, pixel-level AUROC, and average cluster numbers.

\begin{table}
\begin{tabular}{c c c c} \hline Feature Level & Texture & Object & All \\ \hline level 1 & (96.2, 96.6) & (85.5, 92.7) & (89.0, 94.0) \\ level 2 & (97.9, 97.3) & (97.2, 95.5) & (97.4, 96.1) \\ level 3 & (98.0, 96.7) & (95.7, 96.0) & (96.5, 96.2) \\ level 2+3 & (97.9, 97.3) & (96.8, 96.9) & (97.1, 96.9) \\ level 1+2+3 & (98.3, 97.5) & (97.4, 97.1) & (97.7, 97.2) \\ \hline \end{tabular}
\end{table}
Table 5: Anomaly detection and localization performance of ProtoAD with features at different levels. Each tuple shows image-level AUROC and pixel-level AUROC.
#### Partition Selection from Clustering Hierarchy

FINCH is a hierarchical agglomerative clustering algorithm. It recursively merges clusters from the bottom up and provides a set of partitions in a hierarchical structure. Each successive partition is a super-set of its preceding partitions, and the number of clusters in it is smaller than those in the preceding partitions. Thus, we need select a partition from the clustering hierarchy as the clustering result.

We report the performance of our method with different partitions, from the second (P2) to the 6-th (P6) partition of FINCH, in table 6. We do not include the first partition because it has a huge number of clusters. The results in table 6 indicate the average performance decreases along with the merging process. This may be because, when the number of clusters gets smaller, clusters are less compact and unsuitable for anomaly detection. On the other hand, if the number of clusters is too large, there are too many prototypes, and the inference time and storage would increase rapidly. We also give the "Best" performance, which FINCH can achieve by selecting the best partition for each category respectively. This best performance is the upper bound that our method can achieve. However, selecting partition based on the average performance (from P2 to P6) or performance for each category (Best) is time-consuming and not suitable for real applications. In our method, we stop FINCH when the number of cluster is less than 10,000 and use the final partition as the clustering result, and give its results in the last line of table 6. Our partition selection rule can achieve performance very close to the best one with only a tenth of clusters. Therefore, our method can reach a good trade-off between effectiveness and efficiency.

#### FINCH vs. K-Means

We compare FINCH clustering algorithm with k-means for the prototype-based anomaly detection. In our method, we choose the partition generated so far by FINCH which having less than 10,000 clusters as the clustering result. For a fair comparison, we set k to 10,000 for k-means. The results in table 7 indicate that the method based on FINCH (the third column) achieves better performance than that based on k-means (the first column). Although it may achieve better performance for k-means by tuning k, it is time-consuming and not feasible for real applications.

\begin{table}
\begin{tabular}{c c c c} \hline  & K-Means & K-Means & FINCH \\ Category & (L2) & (Norm L2) & (Coisne) \\ \hline texture & (95.0, 95.3) & (97.2, 96.6) & (98.3, 97.5) \\ \hline object & (92.9, 95.9) & (95.4, 96.1) & (97.4, 97.1) \\ \hline all & (93.6, 95.7) & (96.0, 96.2) & (97.7, 97.2) \\ \hline \end{tabular}
\end{table}
Table 7: Anomaly detection and localization performance of ProtoAD with different clustering methods. Each tuple shows image-Level AUROC and pixel-level AUROC.
#### 4.4.4 Feature Normalization and Cosine Similarity

We also explore the importance of feature normalization for the prototype-based anomaly detection. As shown in Table 7, k-means with Euclidean distance on the \(L2\)-normalized features (Norm L2) outperforms k-means with Euclidean distance on the original features (L2) in both anomaly detection and anomaly localization and achieves greater improvements in anomaly detection.

When the features are \(L2\)-normalized, cosine similarity and Euclidean distance are equivalent in the sense of nearest neighbor searching. Therefore, we use cosine similarity for clustering and measuring the deviation of test patch features from norm patch features. We further implement cosine similarity with a \(1\times 1\) convolution and append it to the feature extraction network. Therefore inference can be performed in an end-to-end manner.

### Results on BTAD

In Table 8, we report the results of our method on the BTAD dataset and compare them with those of the SOTA OOD-based method (SPADE, PaDiM, and ProtoAD) and the approaches adopted in [32]. In [32], three reconstruction-based methods have been evaluated, auto-encoder (AE) with MSE loss, auto-encoder with MSE and SSIM loss, and Vision-Transformer-based image anomaly detection and localization (VT-ADL). We report the image-level and pixel-level AUROC for each category and their average for all categories. For anomaly detection, ProtoAD achieved the best image-level AUROC. For anomaly localization, ProtoAD achieved the second-best pixel-level AUROC (97.0), very close to the best one (97.4) achieved by PaDiM. These results show our method's potential to generalize to new anomalous scenarios.

## 5 Conclusion

We propose ProtoAD, a new OOD-based image anomaly detection and localization method. First, a pre-trained neural network is used to extract features for image patches. Then, a non-parametric clustering algorithm learns the prototypes for normal patch features. Finally, an image anomaly detection and localization network is constructed by appending the feature extraction network with the \(L2\) feature normalization, a \(1\times 1\) convolutional layer, a channel max-pooling, and a subtraction operation. As a result, ProtoAD does not need

\begin{table}
\begin{tabular}{c c c c c c c c} \hline \hline
**Category** & AE(MSE) & AE(MSE+SSIM) & VT-ADL & SPADE & PaDiM & PatchCore & ProtoAD \\ \hline
01 & ( -, 49.0) & ( -, 53.0) & ( -, 99.0) & (93.2, 90.2) & (100.0, 97.2) & (95.4, 96.2) & (97.0, 95.5) \\
02 & ( -, 92.0) & ( -, 96.0) & ( -, 94.0) & (74.8, 93.5) & (81.5, 95.4) & (85.1, 95.2) & (85.2, 96.5) \\
03 & ( -, 55.0) & ( -, 89.0) & ( -, 77.0) & (99.4, 96.3) & (98.6, 99.6) & (99.7, 99.5) & (98.9, 99.0) \\ \hline
**All** & ( -, 78.7) & ( -, 79.3) & ( -, 90.0) & (89.1, 93.3) & (93.4, **97.4**) & (93.4, 97.0) & (**94.0**, 97.0) \\ \hline \hline \end{tabular}
\end{table}
Table 8: Anomaly detection and localization performance on BTAD (Image-level and Pixel-level AUROC). The best results are bold-faced.
a network training process and can conduct anomaly detection and localization in an end-to-end manner. Experimental results on the MVTec AD dataset and the BTAD dataset show that ProtoAD can achieve competitive performance compared to state-of-the-art methods. Furthermore, compared to other OOD-based methods, ProtoAD is more elegant and efficient. And compared to the reconstruct-based methods, ProtoAD does not need a cumbersome network training process. Therefore, it can better meet the requirements of real applications.

## 6 Funding and Competing interests

This research was supported by the National Defense Basic Scientific Research Program of China under Grant JCKY2020903B002. The authors have no relevant financial or non-financial interests to disclose.
