# Fv-AD: F-AnoGAN Based Anomaly Detection in Chromatic Process for Smart Manufacturing
## 3 F-And: F-AnoGAN Based Anomaly Detection
### Calculating Anomaly Score  
GAN learning generates the generator G(z) = \(z\to x\), which maps to the latent space \(z\to x\), but not the inverse mapping to the manifold x -> latent space z, which is required for outlier detection. To solve this problem, AnoGAN maps to the latent space, but F-AnoGAN learns the encoder, enabling reverse mapping to E(x) = \(x\to z\). F-AnoGAN with encoder proposes three methods, but this study uses the last encoder model of \(izi_{f}\) method using feature space loss for the \(image\to z\to image\). In the learning process, the process of mapping from a real image to the latent space z is encoded and consists of an encoder model. When query image x arrives, it allows the quick creation of G(E(x)) such as x. The \(izi_{f}\) architecture has the same structure as the AutoEncoder and CAE [30], in which the encoder follows the decoder. During learning, the encoder trains mapping from a real image to z and learns with G, which maps z to the image space. This architecture is similar to image-to-image AE, and the learning proceeds in a manner that minimizes the MSE of lossmsx and E(G(x)) used when learning with the \(izi_{f}\) architecture. The \(izi\) Training objective implements similarities in the image space. The loss function of \(izi\) an encoder training architecture compared is as follows. It has a structure with a generator behind the encoder and minimizes MSE loss. \[\mathcal{L}_{izi}(\mathbf{x})=\frac{1}{n}\|\mathbf{x}-G(E(\mathbf{x}))\|^{2}, \tag{3}\]  
Minimizing the pixel-by-pixel difference is not an actual example of a normal image; an image with a small residual may be output even in the case of an abnormal image. The image space residual is not a reliable position. This leads to the creation of architecture by further calculating the statistics from real and reconstructed images. The statistics of the input are calculated using the discriminator feature \(f()\) of the intermediate layer, and the characteristic expression in the middle indicates the dimension [5]. The discriminator feature in F-AnoGAN is inspired by the feature mapping technique proposed in [31] and it is related to the loss value used in the initial outlier detection task for mapping repeated \(z\)-values. However, \(izi\)'s goal is to limit the generation of images, but \(izi\)'s shortcomings are that mapping to the latent space is unknown, and the exact result can not be predicted by calculating the residual loss. This adds statistics to the actual and generated images to construct the architecture. \(izi_{f}\)'s loss function is as follows:  
\[\mathcal{L}_{izi_{f}}(\mathbf{x})=\frac{1}{n}\cdot\|\mathbf{x}-G(E(\mathbf{x} ))\|^{2}+\frac{\kappa}{n_{d}}\cdot\|f(\mathbf{x})-f(G(E(\mathbf{x})))\|^{2}, \tag{4}\]From the formula, it can be seen that the discriminator features are related to the loss used in AnoGAN and the discriminator feature utilizes the parameters obtained when learning the GAN. It provides a good mapping of the image and latent spaces using this loss. The parameters learned during [5] WGAN training are fixed during encoder training, and the Figure 6 architecture guides encoder training in both the image and latent space at the same time, selecting the proposed encoder training architecture from the F-AnoGAN model. The discriminator parameters are learned during WGAN training and fixed during encoder training. After learning the GAN and encoder, the anomaly score is calculated by entering the query image X. Calculating the anomaly score is the same as the equation [5]. \[A(x)=A_{R}(x)+K*A_{D}(x) \tag{5}\]  
An anomaly score represents the deviation of the query and reconstruction images during outlier detection. By observing the formula in [5], it can be said that it is the same as the loss \(iz_{f}\) formula and usually results in a high score on an abnormal image and a low score on a normal image. The residual loss equation used for learning is employed to calculate the average score.