# Unsupervised Anomaly Detection and Segmentation on Dirty Datasets
## 3 Proposed Method
### Trimming Patch-Level Gaussian Distribution  
For all samples in the training set, we obtain the patch feature vectors \(x_{ij}\) at patch position \((i,j)\in[1,h^{\prime}]\times[1,w^{\prime}]\), and the set of feature vectors \(X_{i,j}=\{x_{ij}^{n}|n\in[1,N]\}\), \(N\) is the number of samples in the training set. We can estimate a Gaussian distribution with parameters \(\mu_{i,j}\) and \(\Sigma_{i,j}\) based on \(X_{i,j}\), where \(\mu_{i,j}\) is the sample mean in \(X_{i,j}\) and \(\Sigma_{i,j}\) is the covariance matrix shown in Equations (4) and (5). In order to ensure that the covariance matrix is invertible for the subsequent Mahalanobis distance calculation, we added the term \(\lambda I\). \(I\) is an identity matrix, \(\lambda=1\times 10^{-6}\). \[\mu_{i,j}=\frac{1}{N}\sum_{n=1}^{N}x_{i,j}^{n} \tag{4}\]  
\[\Sigma_{i,j}=\frac{1}{N-1}\sum_{n=1}^{N}(x_{i,j}^{n}-\mu_{i,j})(x_{i,j}^{n}- \mu_{i,j})^{T}+\lambda I \tag{5}\]  
After estimating a Gaussian distribution at each patch position\((i,j)\), we obtain a Gaussian map. As shown in Figure 1, in the patch-level distribution, the patches containing anomalies are in the low-density region. In robust statistics, trimming methods [6] remove part of the maximum-minimum samples to obtain a statistical model with better robustness. Inspired by [15], we use the Mahalanobis distance in Equation (6) to measure the distance of a patch feature \(x_{i,j}\) to the center of the Gaussian distribution as the anomaly score. A high anomaly score indicates that the patch is in a low-density region, and a low anomaly score indicates that the patch is in a high-density region. Inspired by the trimming method in robust statistics, we exclude the samples that contain patches in low-density regions. \[M(x_{i,j})=\sqrt{(x_{i,j}-\mu_{i,j})^{T}\Sigma_{i,j}^{-1}(x_{i,j}-\mu_{i,j})} \tag{6}\]  
\[M_{i,j}^{norm}=\frac{M_{i,j}-M_{i,j}^{min}}{M_{i,j}^{max}-M_{i,j}^{min}} \tag{7}\]  
As shown in Figure 4, sorting the obtained Mahalanobis distance from smallest to largest, a line segment with a sudden increase in slope can be found. We can calculate a distance threshold from this line segment. All patches with Mahalanobis distance greater than this threshold are considered anomaly patches. It is important to choose an appropriate threshold of the Mahalanobis distance to perform trimming operations. If the threshold is too low, too many normal samples will be excluded, and anomaly samples will not be effectively excluded if the threshold is too high. To make the distances on a similar scale, we use Min-Max scaling in Equation (7) for each group of Mahalanobis distances. Figure 4: At position \((i,j)\), a Gaussian distribution is fitted with the patch feature vector of all samples. The dashed box represents the sliding window and the red dashed box represents the target window. This paper uses the sliding-window method to implement an adaptive threshold. A window slides from left to right, and the slope of the line connecting the two endpoints of the window is considered the slope of the line segment inside the window. As the window slides to the right, we save the slope of each line segment in the window and calculate the mean value of the saved slope \(Slope_{mean}\). We define the window with a slope larger than \(v\times Slope_{mean}\) as the target window. The value of \(v\) will be described in Section 3.3.2. In order to find the threshold of the Mahalanobis distance more accurately, the target window occurs \(U\) times consecutively, and these target windows are saved as sequence \(SEQ\). We use the mean Mahalanobis distance of all samples within the first window of \(SEQ\) as the threshold. \[L=\alpha N \tag{8}\]  
\[S=\frac{L}{\beta} \tag{9}\]  
\[U=\frac{L}{S}+1 \tag{10}\]  
\[T_{i,j}=\frac{1}{L}\sum_{l=1}^{L}W_{i,j}^{l} \tag{11}\]  
As shown in Equations (8)-(11), \(L\) is the length of the sliding window, \(N\) is the number of samples in the dataset, \(S\) is the sliding window step.