# Deep Morphological Anomaly Detection Based on Angular Margin Loss
## 4 Proposed Method
### Deep Morphological Anomaly Detection via Angular Margin Loss
After the creation of \(\mathcal{D}^{\prime}\), the proposed DAD model \(F\) is trained to predict the transformation information \([M_{i},W_{j},H_{o},R_{p}]\) on the input-perturbed instance \(\mathbf{A}^{\prime}_{[M_{i},W_{j},H_{o},R_{p}]}\); Notably, the label \([M_{i},W_{j},H_{o},R_{p}]\) is unknown to \(F\). The proposed DAD model \(F\) consists of a single feature extractor and four classifiers, similar to a hard-parameter-sharing-based multi-task model. We denote the classifiers in \(F\) as \(F_{M}\), \(F_{W}\), \(F_{H}\), and \(F_{R}\); they are designed to predict the class of morphological transformation \(M_{i}\), the class of kernel width \(W_{j}\), the class of kernel height \(H_{o}\), and the class of geometric rotation \(R_{p}\), respectively. All objective functions of classifiers are defined following AML fashion to enhance the discriminative power. Therefore, the proposed objective function is defined as follows:  
\[L_{proposed}(\mathbf{A}^{\prime}_{[M_{i},W_{j},H_{o},R_{p}]})= \tag{9}\] \[L_{\mathcal{M}}(\mathbf{A}^{\prime}_{[M_{i},W_{j},H_{o},R_{p}]}| \theta_{\mathcal{M}})+L_{\mathcal{W}}(\mathbf{A}^{\prime}_{[M_{i},W_{j},H_{o},R_{p}]}|\theta_{\mathcal{W}})\] \[+L_{\mathcal{H}}(\mathbf{A}^{\prime}_{[M_{i},W_{j},H_{o},R_{p}]}| \theta_{\mathcal{H}})+L_{\mathcal{R}}(\mathbf{A}^{\prime}_{[M_{i},W_{j},H_{o},R_{p}]}|\theta_{\mathcal{R}}),\]  
where \(\theta_{\mathcal{M}}=\{\theta_{M_{1}},\dots,\theta_{M_{|\mathcal{M}|}}\}\), \(\theta_{\mathcal{W}}=\{\theta_{W_{1}},\dots,\theta_{W_{|\mathcal{W}|}}\}\), \(\theta_{\mathcal{H}}=\{\theta_{H_{1}},\dots,\theta_{H_{|\mathcal{H}|}}\}\), and \(\theta_{\mathcal{R}}=\{\theta_{R_{1}},\dots,\theta_{R_{|\mathcal{R}|}}\}\) are the sets of angles between the embedded features of \(F\) and given classes in \(\mathcal{M}\), \(\mathcal{W}\), \(\mathcal{H}\), and \(\mathcal{R}\), respectively. AMLs for \(\mathcal{M}\), \(\mathcal{W}\), \(\mathcal{H}\), and \(\mathcal{R}\) are defined according to the AML definition in Section 3. We report these four objective functions in Appendix A. Through the objective function, the proposed DAD model learns dominant morphological features of \"normal\" images. In addition, because the proposed loss function is based on self-supervised learning and AML, the DAD model can easily train \"normal\" instances in an improved discriminative manner. At the inference time, given an unseen image \(\mathbf{A}\), we decide whether it belongs to the \"normal\" class by first applying each transformation on it and then applying the classifier on each of the \(|\mathcal{M}||\mathcal{W}||\mathcal{H}||\mathcal{R}|\) transformed images. Each such application results in an AML response vector of size \(|\mathcal{M}||\mathcal{W}||\mathcal{H}||\mathcal{R}|\). The final normality score is defined using the combined log-likelihood of these vectors under an estimated distribution of \"normal\" AML output vectors. Subsequently, we define the normality score function \(g(\mathbf{A})\)--fix a set of morphological transformations \(T=\{T_{0},\ldots,T_{|\mathcal{M}||\mathcal{W}||\mathcal{H}||\mathcal{R}|-1}\}\) and define \(F(T_{i}(\mathbf{A}))\), which is the vector of AML response of the proposed model \(F\) applied on \(i\)th transformed image \(T_{i}(\mathbf{A})\). To construct the normality score, we define  
\[g(\mathbf{A})=\sum_{i=0}^{|\mathcal{M}||\mathcal{W}||\mathcal{H}||\mathcal{R}|- 1}\log\mathrm{P}(F(T_{i}(\mathbf{A}))|T_{i}), \tag{10}\]  
which is the combined log-likelihood of a transformed image conditioned on each of the applied transformations in \(T\). By following [7], we approximate each conditional to be \(F(T_{i}(\mathbf{A}))|T_{i}\sim\text{Dir}(\alpha_{i})\), where \(\alpha_{i}\in\mathbb{R}_{+}^{|\mathcal{M}||\mathcal{W}||\mathcal{H}||\mathcal{ R}|-1}\) is the Dirichlet parameter, \(\mathbf{A}\sim\mathrm{P}_{\text{real}}(\mathbf{A})\), \(i\sim\text{Uni}(0,|\mathcal{M}||\mathcal{W}||\mathcal{H}||\mathcal{R}|-1)\), and \(\mathrm{P}_{\text{real}}(\mathbf{A})\) is the real data probability distribution of \"normal\" images.