# Semantic Decomposition and Anomaly Detection of Tympan Membrane Endoscopic Images
## 4 VAE  
For use in real-world clinical circumstances, AI systems that leverage medical data should be able to discover and categorize a greater variety of instances rather than being prejudiced to certain instances. Several studies have been conducted on medical AI, however, most suffer from insolubility and deflection outside the training region, owing to the nature of supervised learning [11]. To overcome such challenges, we utilize a VAE for retrieving the abnormality scores. This section describes how abnormality scores can be extracted from the VAE.  
The VAE assumes that a latent variable z influences data x and aims to identify z to develop a fresh data sample comparable to training data x like Figure 8. The autoencoder (AE) is extracted to a single-value latent variable for encoder learning. Moreover, the primary purpose of the VAE is to generate a decoder, which is more useful than AE in understanding the distribution of training data because latent variable x appears as a distribution that optimizes the likelihood of data x based on a Gaussian probability distribution.  
Figure 8: Architecture of VAE.  
Figure 7: Images according to the derivative direction of Sobel edge detection.  
Initially, the VAE learns only from the normal tympan membrane endoscopic images. Adam was used as the optimizer and the learning rate was learned by 100 epochs at \(1\times 10^{-3}\). The abnormality scores are calculated using the VAE that learned only the normal class using test data containing classes, such as perforation, retraction, and cholesteatoma like Figure 9. The abnormality scores are calculated using the reconstruction error and Kullback-Leibler diversity, which are calculated by factors resulting from differences in the distributions between the normal class and test data learned by the model. In other words, abnormality scores indicate the distance between the normal class and test data distributions in this study.  
The VAE loss is determined as the sum of the abnormality scores: the reconstruction error and Kullback-Leibler diversity. The equations show the formula for computing the loss value of a VAE. The first item on the right is the Kullback-Leibler diversity and the second item is the reconstruction error.  
\[\mathcal{L}^{\text{B}}\Big{(}\theta,\phi;x^{(i)}\Big{)}=-D_{KL}\Big{(}q_{\phi} (z\big{|}x^{(i)})\parallel p_{\theta}(z)\Big{)}+\frac{1}{L}\sum_{l=1}^{L}\Big{(} \log p_{\theta(x^{(i)}|z^{(i)})}\Big{)}\]  
The Kullback-Leibler diversity is a function used to determine the difference between two probability distributions statistically. The VAE estimates the informative distance of \(q\phi(z|x^{(i)})\), which is the probability distribution of \(x\) that can be derived from \(p\theta(z)\) and \(z\). In other words, when the training data are distributed, the difference in entropy that can occur while sampling the input distribution of the test data is computed. The reconstruction error is the rate at which errors occur when the test data are reconstructed using a model. For instance, if data, such as perforation, retraction, and cholesteatoma, are fed into a model that learns only the normal class, the error rate increases during the reconstruction phase because a different distribution must be reconstructed from the learned data. The error rate that occurs during this process is known as the reconstruction error. We collected the reconstruction error and Kullback-Leibler diversity for the Section 3 dataset and applied them for detection.