# Industrial Image Anomaly Detection via Self-Supervised Learning with Feature Enhancement Assistance
## 3 Materials and Methods
### Calculate Anomaly Score Map  
We used multivariate Gaussian distribution \(\mathcal{N}(\mu,\Sigma)\) to model the samples and utilized the Mahalanobis distance between the normal and abnormal samples at position \((i,j)\) as the anomaly score. The Mahalanobis distance is a commonly used measure of the difference between two samples, which takes into account the characteristics of the sample distribution, correlation between features, and scale invariance. It effectively measures differences between samples, thereby identifying anomalous data more efficiently. The details are as follows:  
Firstly, the multi-scale feature map \(f_{agg}(x^{k})\) of the normal samples in the training set is extracted by the model obtained from self-supervised learning, where \(k\in[1,2,\ldots,N]\) and \(N\) represents the number of normal samples. Each feature vector of the feature map is treated as a patch, and the patch-level multivariate Gaussian distribution model \(\mathcal{N}(\mu_{(i,j)},\Sigma_{(i,j)})\) is obtained by calculating its mean and covariance matrices at each patch \((i,j)\). The mean and covariance matrices of the samples are calculated as follows:  
\[\mu_{(i,j)}=\frac{1}{N}\sum_{k=1}^{N}f_{agg}(x^{k})_{(i,j)}, \tag{9}\]  
\[\Sigma_{(i,j)}=\frac{1}{N-1}\sum_{k=1}^{N}\left(f_{agg}(x^{k})_{(i,j)}-\mu_{( i,j)}\right)\left(f_{agg}(x^{k})_{(i,j)}-\mu_{(i,j)}\right)^{\text{T}}+\alpha E. \tag{10}\]  
The \(\mu_{(i,j)}\) and \(\Sigma_{(i,j)}\) in Equations (9) and (10) represent the mean and covariance at each patch, respectively, where \(i\in[1,W],j\in[1,H]\), \(W\times H\) denote the resolution of the multi-scale feature map. \(\alpha\) is a very small number, typically taken as 0.01, and \(E\) is the unit matrix. \(\alpha E\) is used as a regularization constraint term to ensure that the covariance matrix is full rank and invertible, allowing it to be used to compute the Mahalanobis distance.  
During the inference process, the same model is used to extract the multi-scale feature maps \(f_{agg}(x_{t})\) of the test samples in the test set. An anomaly score \(M(f_{agg}(x_{t})_{(i,j)})\) is  
\begin{table}
\begin{tabular}{c c c c} \hline \hline
**Structure** & **Input** & **Output** & **Channel** \\ \hline Conv\_block1 & \(56\times 56\) & \(56\times 56\) & 128 \\ Avgpool1 & \(56\times 56\) & \(28\times 28\) & 128 \\ Conv\_block2 & \(28\times 28\) & \(28\times 28\) & 256 \\ Avgpool2 & \(28\times 28\) & \(14\times 14\) & 256 \\ Conv\_block3 & \(14\times 14\) & \(14\times 14\) & 512 \\ Avgpool3 & \(14\times 14\) & \(7\times 7\) & 512 \\ AdaptiveAvgPool & \(7\times 7\) & \(1\times 1\) & 512 \\ MLP & & [512; 512; 128] & \\ FC & & 3 & \\ \hline \hline \end{tabular}
\end{table}
Table 1: The structure of encoder and classifier.  
assigned to each patch by calculating the Mahalanobis distance of the feature maps from the set of normal samples at each patch \((i,j)\). The Mahalanobis distances are calculated as follows:  
\[M(f_{agg}(x_{t})_{(i,j)})=\sqrt{(f_{agg}(x_{t})_{(i,j)}-\mu_{(i,j)})^{T}\Sigma_{ (i,j)}^{-1}(f_{agg}(x_{t})_{(i,j)}-\mu_{(i,j)})}. \tag{11}\]  
In Equation (11), \(\Sigma_{(i,j)}^{-1}\) denotes the inverse of the covariance matrix, and we end up with a Mahalanobis distance matrix serving as the anomaly score map. It contains the anomaly scores of all feature vectors in the feature map. A low score means that the region is closer to the normal sample, hence considered normal. On the contrary, a high score indicates that the region is further from the normal samples, indicating abnormality.