LeeDoYup / AnoGAN-tf

Unofficial Tensorflow Implementation of AnoGAN (Anomaly GAN)
MIT License
273 stars 90 forks source link
anogan anomaly-detection anomalydetection dcgan gan generative-adversarial-network tensorflow

AnoGAN in tensorflow

Tensorflow implementation of Anomaly GAN (AnoGAN).

This model detect anomaly part in images, after training DCGAN with normal dataset.

(In Korean, H. Kim's detail explanation is here)

Basic model is DCGAN (Deep Convolutional Generative Adversarial Networks).

Model Description

After learn DCGAN model with normal dataset (not contains anomalies),

Model Structure

When unseen data comes, the model tries to find latent variable z that generates input image using backpropagation. (similar with style transfer)

Anomaly Score is based on residual and discrimination losses.

Res_Loss

Discrimination Loss

Total Loss for finding latent variable z is weighted sum of the two. (defualt lambda = 0.1)

Total Loss

File Descriptions

Prerequisites (my environments)

Usage

First, you "must" have trained DCGAN model with normal dataset.

If you have checkpoint file, the model tries to use it.

Model Preparation

(If you want to download and train the model) First, download dataset with:

$ python download.py mnist celebA

To train a model with downloaded dataset:

$ python main.py --dataset mnist --input_height=28 --output_height=28 --train
$ python main.py --dataset celebA --input_height=108 --train --crop

Or, you can use your own dataset (without central crop) by:

$ mkdir data/DATASET_NAME
... add images to data/DATASET_NAME ...
$ python main.py --dataset DATASET_NAME --train
$ python main.py --dataset DATASET_NAME
$ # example
$ python main.py --dataset=eyes --input_fname_pattern="*_cropped.png" --train

Anomaly Detection

After having trained DCGAN model, you have to prepare test images for anomaly detection.

$ mkdir ./test_data
... add test images to ./test_data ...

$ python main.py --dataset DATASET_NAME --input_height=108 --crop --anomaly_test

Results

To valid the model implementation, simple test was proceeded.

Initial generated image by DCGAN in training is conisdered as anomaly.

After learns DCGAN model, compared final and initial images on certain latent varaible z.

Then, anomaly score of initial images was calculated.

Eyes, mouth, and distorted parts in image were detected.

result

Related works

To Do

You can always request pull requests with feeling free.

Acknowledgement