# Back to the Metrics: Exploration of Distance Metrics in Anomaly Detection
## 2 Methodology
### The Structure of Memory Bank-Based Methods (MBBM)  
#### 2.2.1 Anomaly Score Metrics  
Traditional \(k\)NN algorithm is a classification algorithm based on similarity metrics. It determines the category of a new sample by selecting \(k\)NN to vote, where \(k\) is usually chosen as an odd number to avoid ties during the voting process [47]. In semi-supervised anomaly detection, anomalous samples do not appear in the training set. In contrast, the MBBM considers the influence of multiple different normal samples in the anomaly scoring function through techniques like re-weighting. DN2 is the first model to introduce a \(k\)NN-based method in industrial image anomaly detection, scoring image anomalies using Equation (5). \[s=d(x)=\frac{1}{k}\sum_{f\in\mathcal{N}_{k}^{*}(f_{x})}\lVert f-f_{x}\rVert_{2}. \tag{5}\]  
Figure 1: **(a)** The Architecture of back to the metrics (BTM). (**b**) Process of feature extraction and fusion with fast point feature histograms (FPFH) and pre-trained networks. The symbol \(\mathcal{N}_{k}(f_{x})\) represents the set of \(k\)-nearest feature vectors to \(f_{x}\) within the training set \(F_{\text{train}}\). The anomaly score \(s\) of the test image is denoted by the distance function \(d(x)\), which is the average sum of squared Euclidean distances between the feature vector \(f_{x}\) and the feature vectors \(f\) in its \(k\)NN \(\mathcal{N}_{k}(f_{x})\). The rule with \(k=1\) is commonly referred to as the nearest neighbor rule [47]. Work such as SPADE further extends DN2 to AS tasks. For an image sample that is divided into \(I\) rows and \(J\) columns, \(f_{i,j}\) represents the feature extracted from the patch at the \(i\)th row and \(j\)th column, and  
\[F=\{f_{i,j}\mid 0\leq i<I,0\leq j<I\}\]  
represents the set of features for the entire image. Let \(s_{i,j}\) represent the anomaly score of the patch at the \(i\)th row and \(j\)th column. Let \(\mathcal{M}\) represent the coreset of features obtained by applying a greedy algorithm for subsampling on the set of patch features from all images in the training set. Let \(\mathcal{N}_{k}(f_{y})\) represent the set of \(k\) nearest features to \(f_{y}\) in \(\mathcal{M}\). \[s_{i,j}=\frac{1}{k}\sum_{f\in\mathcal{N}_{k}(f_{i,j})}\|f-f_{i,j}\|_{2}. \tag{6}\]  
For each image, PatchCore's official code uses Equation (6) for AS anomaly score calculation, where \(k=1\) in the paper, which can be represented by Equation (7). \[s_{i,j}=\min_{m\in\mathcal{M}}\|m-f_{i,j}\|_{2}. \tag{7}\]  
PatchCore defines the maximum distance score \(s^{*}\) of a test image, which can be expressed as Equation (8) or more closely to the original text as Appendix A (less rigorous). Here, \(f_{i^{*},j^{*}}\) is the feature in the test image with the highest anomaly score, and \(m^{*}\) is the feature in the core set \(\mathcal{M}\) that is nearest to \(f_{i^{*},j^{*}}\). \[s^{*}=\max_{0\leq i<I,0\leq j<I}s_{i  
methods such as reweighting and one-class support vector machines for anomaly score re-calculation. \[ \begin{split}\alpha_{1}&=\frac{\exp(\|f_{i^{*},j^{*}} -m^{*}\|_{2})}{\sum_{m\in\mathcal{N}_{k}(m^{*})}\exp(\|f_{i^{*},j^{*}}-m\|_{2})}, \\ \alpha_{2}&=\sum_{m\in\mathcal{N}_{k}(m^{*})}\exp(\| f_{i^{*},j^{*}}-m\|_{2}),\\ &=\sum_{m\in\mathcal{N}_{k}(m^{*})}\exp(\sqrt{f_{i^{*},j^{*}}^{ \mathsf{T}}f_{i^{*},j^{*}}-2f_{i^{*},j^{*}}^{\mathsf{T}}m+m^{\mathsf{T}}m}), \\ &=\sum_{m\in\mathcal{N}_{k}(m^{*})}\exp(\sqrt{\|f_{i^{*},j^{*}}\|_ {2}^{2}-2f_{i^{*},j^{*}}^{\mathsf{T}}m+\|m\|_{2}^{2}}).\end{split}  \tag{12}\]  
To facilitate further analysis, we define partial component factors (or terms) \(\alpha_{1}\) and \(\alpha_{2}\) of the variable \(w\), as shown in Equation (12). The exponential function \(\exp(\cdot)\) and the square root function \(\sqrt{\cdot}\) are monotonically increasing and, therefore, not specifically considered. In the analysis of \(\alpha_{2}\), the terms \(\|f_{i^{*},j^{*}}\|_{2}^{2}\), \(\|m\|_{2}^{2}\), and \(f_{i^{*},j^{*}}^{\mathsf{T}}m\) can have adverse effects on the contour plots of the anomaly scores.