G2D: Generate to Detect Anomaly

Masoud p
Analytics Vidhya
Published in
5 min readNov 16, 2020
Generated instances (blue) by a GAN trained on normal (target class) samples (red) during training in different epochs
Generated instances (blue) by a GAN trained on normal (target class) samples (red) during training in different epochs. We use T-SNE to represent samples in two dimensions. As can be seen in earlier epochs, the generator of GAN produces samples which are completely different from normal ones. But as the learning process continues by the increase of epochs, both types of samples are getting close to each other gradually.

One-Class Classification (OCC) is the task of detecting samples which are unseen or out-of-target distribution. In other words, an OCC method looks for anomalies, i.e., unexpected behaviors or events which do not (or rarely) occur in the training data. Inspired by the successes of deep learning solutions, especially Generative Adversarial Networks (GANs), some methods have been presented for outlier detection. In a nutshell, such methods adversarially learn two deep neural networks, one for generating fake data and another for discriminating normal samples from outliers. Although they have achieved promising results, as same as all other GAN-based methods, learning to find the optimum parameters is a bothersome procedure. Also, for the OCC task, finding the appropriate time for stopping the learning process to achieve the best performance without any validation samples of outlier class is very challenging and needs trial and error.

To address the mentioned challenges, we propose a very simple yet efficient method for detecting outlier samples. Similar to the prior approaches, we take advantage of the adversarial learning, but use it in a completely different manner. Previously proposed methods have focused to adversarially learn the distribution of normal data and consider all of the instances with a major diversion from the reference model as outlier. In contrary to such solutions, we suggest a simple and straight-forward way for generating irregular samples which do not follow the distribution of normal data. Generated irregularities alongside of available normal instances, simply can be used for training a binary classifiers.

Irregularity detection is the task of finding samples with no or low likelihood of occurrence. Abnormal or Irregular samples can be any deviation from an expected and usual behavior or anything which are dissimilar to the target class data. Hence, they are very diverse and researchers prefer to learn the shared concept among the training data, i.e., regular instances. In this case, samples with a different concept are considered as irregularities. In this paper, we follow up a new approach for irregularity detection. Although modeling the outlier samples is difficult, generating them is simple. Due to the high diversity of irregularities, a randomly generated sample can be considered as an outlier instance regarding the target class, with high probability. We Generate to Detect (G2D) anomalies using a binary classification. Our method, i.e., G2D, is composed of three main modules: (1) Irregularity generator network (I), (2) Critic network, and (3) Detector network (D).

I acts as an irregularity generator, while it is totally unaware of such samples. The key idea is to train a GAN on normal samples and utilize its generator before the complete convergence. In fact, generated irregular data should have some deviation from normal instances. Generated irregularities by I alongside the available normal samples, form an informative training set for optimizing the parameters of a Convolutional Neural Network (CNN) like D to distinguish between normal and anomalous samples. A sketch of our proposed method is shown below. The detailed information of I, C and D is described in the following sections.

g2d_structure
The outline of our proposed method. (Left:) Irregularity generator, (Right:) Irregularity Detector. I + C networks are jointly and adversarially trained on normal class data. In training duration, several models (I with different weights) are considered as the irregularity detector. The D network as a binary classifier is trained on all of the available normal samples and generated irregular samples by I 1:k . After the training process, D acts as an irregularity detector.

I is tailored for generating samples from the the unseen class data. This network receives a random noise vector sampled from Gaussian distribution,i.e.,Z∈ N(μ1,σ1),as input, and then maps it to an image. Such networks can easily generate samples which do not follow the target class distribution even with no need for the training procedure and only based on randomly initialized parameters. I network gradually learns the distribution of the target class. I network gradually learns the distribution of the target class.

D network is a classifier for distinguishing the normal samples from the simulated anomalies. After the training process, there are sufficiently available data to train a binary classifier.

Training: I+C and D

To avoid that defects of the conventional GANs negatively affect our work, Wasserstein GAN (WGAN) is utilized in-stead. The discriminator of WGANs are broadly known as Critic. C network of WGAN is similar to G network of GAN, but the sigmoid function is eliminated. In WGAN, C network outputs a scalar score rather than a probability. This score can be interpreted as how real the input images are. In other words, it measures how good a state (the input) is. During training, I models have been saved for each epoch. Accordingly, we have n different models after n epochs.Selecting k networks of n saved models as irregularly generators can be done in two ways: (1) using validation set including both normal and anomalous data, and (2) by analyzing I network loss. In reality, there are not any samples from outlier class. Hence, using validation samples is not feasible,i.e., this approach is flatly contradict our primary assumption. To follow the second solution, the irregularity networks are selected as follows

The relation between the loss function values and behavior of I network.

We aim to proposed an end-to-end neural network to detect irregular samples. Accordingly, as explained previously,merely D network plays the role of anomaly detector in videos and outlier detector in images. The binary classification problem, which is the converted version of the OCC task in our case, can be simply formulated as:

Where α is a predetermined threshold (in our case α is equal to 0.5).

Results on Caltech-256 dataset. Inliers are randomly sampled of one, three or five selected categories. Furthermore,irregularities are randomly chosen from category 257-clutter.Two first rows:Inliers are from one category of images, with50%portion of outliers;Two second rows:Inliers are from three categories of images, with50%portion of outliers;Twolast rows:Inliers come from five categories of images, while outliers form50%of the samples

For more information you can find our paper here. G2D is one of accepted paper at #WACV2021. You can find source code at Github. Feel free to ask question about paper and codes.

--

--