3.The main idea of GANomaly

#1 Introduction
**1.semi-supervised anomaly detection**
Employ adversarial **autoencoder** within an encoder-decoder-encoder pipeline and using a **conditional generative adversarial network**, capturing the data distribution with both images and latent vector.

**2.efficacy**
Yielding both statistically and computationally better performance.

**3.reproducibility**
It can be reproduced via the code made publicly available.

#2 Related Work
- AnoGAN ：Training a generator and discriminator using only normal images and utilizing the pre-trained generator and discriminator by freezing the wights and remap to the latent vector by optimizing the GAN based on the z vector. However, the model has a computational complexity.

- BiGAN: Examining joint training to map from image space to latent space simultaneously, and vice-versa.

#3 Approach
**Basic knowledge**
**1.GAN**: The generator learns the distribution of input data from a latent space while the discriminator network has a classical classification architecture to determine input image validity
**2.AAE**: Being consisted of encoder and decoder which is beneficial in reconstruction and control latent space
**3.GAN with Inference**: Exploiting latent space variables which means uses an additional encoder mapping from image to latent space

##3.1 Ganomaly Pipeline
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/623932/910f61bb-39eb-332d-7165-8ce2496eb34c.png)

**1.The first sub-network**
- The generator G first reads the input image x and forward-passes to encoder network $G_E$, and the z has the smallest dimension containing the best representation of x :
$$
G_E=\;convolutional\;layers\;+\;batch\;norm\;+\;leaky\;ReLU()
$$

- The decoder $G_D$ upscales the vector z to reconstruct x
$$
G_D=\;convolutional\;layers\;+\;batch\;norm\;+\;leaky\;ReLU()\;+\;tanh layer
$$

**2.The second sub-network** ★

- The encoder compresses the image $\overset\wedge x$ reconstructed by the network G.This E explicitly learns to minimize the distance with its parametrization

**3.The third sub-network**

- Discriminator network D: To classify the input and the output as real or fake

##3.2 Loss function

- **Adversarial loss** 
Compute the distance between the feature representation of the original and the generated images：
$$
L_{adv}\;=\;E_{x\sim p_X}\;\parallel f(x)-\;E_{x\sim p_X}\;f(G(x)\parallel_2
$$

- **Contextual loss** 
Learning the contextual information about input data($L_1$ will have a better perfornmance than $L_2$)

$$
L_{con}\;=\;E_{x\sim p_X}\;\parallel x-\;G(x)\parallel_1
$$

- **Encoder loss** 
Minimize the distance between the bottleneck feature of the input and the encoded features of the generated image
$$
L_{enc}\;=\;E_{x\sim p_X}\;\parallel G_E(x)-\;E(\;f(G(x)\;)\parallel_2
$$

Overall, the objective function is:
$$
L\;=\;w_{adv}L_{adv}\;+\;w_{con}L_{con}\;+\;w_{enc}L_{enc}
$$


##3.3 Model Testing
For the test sample $\overset\wedge x$ the anomaly score is defined as:

$$
A(x)\;=\;\parallel G_E(x)-\;E(\;G(x)\;)\parallel_1\\
$$

To evaluate the overall anomaly performance, the anomaly score for the final evaluation of the set will be defined as:

$$
s_i^'\;=\;\frac{s_i-min(s)}{max(s)-min(s)}
$$

#4 Result

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/623932/4d2d8c24-1b24-64dd-9805-49edea20dcdd.png)

- The GANomaly model achieves higher AUC than EGBAN and AnoGAN
- The reason for getting relatively lower quantitative results for a selected abnormal category, there exists a normal class that is similar to the abnormal

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/623932/9502f09b-9158-09d0-5149-bc15d0357270.png)

- (a) show the size of the latent vector will affect the overall performance of the model and the best size is 100
- (b) show the best wight of the loss function is:$w_{bce}$ = 1, $w_{rec}$ = 50,$w_{enc}$ = 1


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/623932/adb952a9-3c38-e1d1-ecf3-a563425d0118.png)

- a shows the histogram of the scores for normal and abnormal test samples
- b shows features extracted from the last conv

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/623932/2a17396d-a8a5-171f-73ec-eb8224e31ce8.png)

- The image shows the model is unable to produce abnormal objects

# References
Samet Akcay. GANomaly: Semi-Supervised Anomaly
Detection via Adversarial Training