# Deep Morphological Anomaly Detection Based on Angular Margin Loss
## 4 Proposed Method
### Deep Morphological Anomaly Detection via Angular Margin Loss  
The proposed algorithm was developed to detect \"abnormal\" instances based on morphological criteria in the anomaly detection process. To achieve this goal, the model adopts self-supervised learning, which easily makes the DAD model learn the features of interest, and the CNN architecture, which exhibits high performance in various computer vision tasks. In self-supervised learning, because the learned features are chosen, we  
Figure 3: Morphologically transformed “abnormal” images in the “cracked tile” class of MVTec [5]: (first column) the original “abnormal” image; (second column) dilated “abnormal” images; (third column) eroded “abnormal” images; (fourth column) morphological gradient of “abnormal” images; (first row except the top left image) “normal” images morphologically transformed with [13; 13] kernel; (second row) images transformed with [13; 1] kernel; (third row) images transformed with [13; 1] kernel. \([S,T]\) is an \(S\times T\) kernel, where \(S\) and \(T\) denote the width and height of the filter, respectively. propose a novel objective function that enforces the DAD model to represent the dominant morphological features of \"normal\" instances. The problem of anomaly detection in images can be defined as follows:  
\[f_{DAD}(\mathbf{A})= \begin{cases}1&g(\mathbf{A})\geq\lambda\\ 0&g(\mathbf{A})<\lambda^{\prime}\end{cases}  \tag{8}\]  
where \(\mathbf{A}\) denotes an input image, \(f_{DAD}\) denotes an anomaly detection function that returns 1 if the input instance \(\mathbf{A}\) belongs to \"normal\" class, \(g(\mathbf{A})\) is a scoring function that quantifies the normality of the input, and \(\lambda\) is the threshold parameter that controls the recall and precision of the DAD function \(f_{DAD}\). Similar to the previous algorithm [8; 9], the proposed algorithm aims to estimate the scoring function \(g\) in the aforementioned equation. Self-Supervised Learning Using Morphological Transformations  
The proposed algorithm estimates the optimal scoring function using self-supervised learning. Therefore, we constructed a self-labeled dataset of images from an initial \"normal\" training set \(\mathcal{D}=\{\mathbf{A}_{1},\dots,\mathbf{A}_{|\mathcal{D}|}\}\), using both a set of morphological transformations \(\mathcal{M}=\{M_{1},\dots,M_{|\mathcal{M}|}\}\) (where \(M_{i}\) denotes the \(i\)th class of morphological transformation set \(\mathcal{M}\), and \(|\mathcal{M}|\) is the number of classes of \(\mathcal{M}\)) and a set of geometric rotation \(R=\{R_{1},\dots,R_{|\mathcal{R}|}\}\) (where \(R_{i}\) is the \(i\)th class of \(\mathcal{R}\) and the \(|\mathcal{R}|\) denotes the number of classes of \(\mathcal{R}\)). As delineated in Figures 1 and 2, because the morphological transformation is affected by the kernel size, we define several classes for kernel width \(\mathcal{W}=\{W_{1},\dots,W_{|\mathcal{W}|}\}\) and kernel height \(\mathcal{H}=\{H_{1},\dots,H_{|\mathcal{H}|}\}\), respectively. Therefore, the self-labeled dataset \(\mathcal{D}^{\prime}=\{\mathbf{A}^{\prime}_{1,|M_{1},W_{1},H_{1},R_{1}|}, \dots,\mathbf{A}^{\prime}_{|\mathcal{D}|,|M_{|\mathcal{M}|},W_{|\mathcal{W}| },H_{|\mathcal{H}|},R_{|\mathcal{R}|}}\}\) is produced by applying each morphological transformation \(\mathcal{M}\), each kernel width \(\mathcal{W}\), each kernel height \(\mathcal{H}\), and each geometric rotation \(\mathcal{R}\) on all \"normal\" instances in \(\mathcal{D}\). Subsequently, we label each transformed image with indices of the transformation that was applied on it, i.e., \(\mathbf{A}^{\prime}_{k,|M_{i},W_{j},H_{\mathcal{R}},R_{p}|}\) denotes morphological transformed \(k\)th \"normal\" image in \(\mathcal{D}\), to which the transformation label \([M_{i},W_{j},H_{o},R_{p}]\) is applied.