## 3 Proposed Model
)\) should yield minimal scores for normal samples used in training but higher scores for abnormal samples that the model is not trained with. A higher anomaly score is also used for normal samples. Figure 1: The proposed model architecture. The input image \(x\) size is \(64\times 160\times 3\) for both the encoder and the discriminator \(D(x,\hat{x})\). Encoder architecture has two sub-networks: a conventional CNN and a dilated convolutional CNN (DCN). Both CNN and DCN have \(3\times 3\) convolutional layers, with DCN having a dilation factor of two. At the end of both networks, the latent vectors are concatenated and become the input of the generator where the input image \(x\) is reconstructed as \(\hat{x}\) through the transpose convolution layers. In addition, a Softmax layer is added to this latent vector for the ablation test. When the parallel encoder is trained with the labeled data, classification is performed in this layer. Partial skip-connections from CNN and DCN into the generator model alleviate the vanishing gradient and the mode collapse phenomenons observed in deep learning models. Afterward, both the input image and the reconstructed images are sent through the discriminator. The images are classified as either real or fake in the sigmoid layer (with one node). The fully connected layer of the discriminator \(f(\cdot)\) is utilized for anomaly detection during the inference. Each input image, whether it belongs to a normal-designated class or an anomaly-designated class, results in a reconstruction score, based on Equation (6). score \(\mathcal{A}(x)\) would indicate the sample \(x\) is from an abnormal class with respect to the data distribution learned by \(f\) from \(D_{tr}\). Three loss functions have been implemented to train the proposed model. Each of the loss functions has its weighting in the training objective. 1. Contextual Loss: \(L_{1}\) normalization between the input image \(x\) and the corresponding reconstructed image \(\hat{x}\) is applied to learn the image distribution. This causes the model to generate contextually similar images from normal samples. The loss is defined as: \[\mathcal{L}_{context}=\mathbb{E}_{x\sim p_{x}}|x-\hat{x}|_{1}.\] (1)
2. Adversarial Loss: Wasserstein loss proposed in [40] is applied to improve the reconstruction performance for normal image \(x\) during training. This loss is helpful for the generator to reconstruct an image \(\hat{x}\) from the input image \(x\) as realistic as possible while helping the discriminator to classify real or fake (generated) samples. It is defined as: \[\mathcal{W}(q,p)=\min_{D}\max_{D\in\mathcal{D}}\mathbb{E}_{x\sim\mathbb{P}_{q }}[D(x)]-E_{\hat{x}\sim\mathbb{P}_{q}}[D(\hat{x})]\] (2) where \(D\) is the set of 1-Lipshitz functions and \(\mathbb{P}_{g}\) is the model distribution defined by \(\hat{x}=G(z),z\sim P(z)\). The discriminator in WGAN is sometimes called a critic since it is not explicitly trained to classify; it minimizes the value function with respect to the generator parameters, \(W(\mathbb{P}_{r},\mathbb{P}_{g})\). 3. Latent Loss: The \(\mathcal{L}_{2}\) loss is used to reconstruct the latent representations for the input \(x\) and the corresponding reconstruction \(\hat{x}\). This ensures the network is sufficiently trained to produce contextually meaningful latent representations for normal samples. The final feature vector layer of the discriminator is used to obtain the features of the input image \(z=f(x)\) and for the reconstructed image, \(\hat{z}=f(\hat{x})\). The loss becomes: \[\mathcal{L}_{latent}=E_{x\sim p_{x}}|f(x)-f(\hat{x})|_{2}.\] (3) The total loss for the model becomes the weighted sum of all the losses: \[\mathcal{L}_{total}=\lambda_{context}\mathcal{L}_{context}+\lambda_{adv} \mathcal{L}_{adv}+\lambda_{latent}\mathcal{L}_{latent}.\] (4) where \(\lambda\) is the weighting parameter to assign the importance of individual losses. The optimal weighting in this study is done via a grid search operation.