• Stars
    star
    185
  • Rank 208,271 (Top 5 %)
  • Language
    Python
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Variational autoencoder for anomaly detection

PyPI PyPI - Python Version PyPI - License PyPI - Downloads

Pytorch/TF1 implementation of Variational AutoEncoder for anomaly detection following the paper Variational Autoencoder based Anomaly Detection using Reconstruction Probability by Jinwon An, Sungzoon Cho

How to install

  1. pip package containing the model and training_step only

     pip install vae-anomaly-detection
    
  2. Use repository

    a. Clone the repo

     git clone [email protected]:Michedev/VAE_anomaly_detection.git
    

    b. Install anaconda and install anaconda-project package if you use miniconda

     conda install anaconda-project
    

    c. Install the environment

     anaconda-project prepare
    

    d. Run the train

     anaconda-project run train
    

    To know all the train parameters run anaconda-project run train --help

This version contains the model and the training procedure

How To Train your Model

  • Define your dataset into dataset.py and overwrite the line train_set = rand_dataset() # set here your dataset in train.py
  • Subclass VAEAnomalyDetection and define the methods make_encoder and make_decoder. The output of make_encoder should be a flat vector while the output of `make_decoder should have the same shape of the input.

Make your model

Subclass VAEAnomalyDetection and define your encoder and decoder like in VaeAnomalyTabular

class VAEAnomalyTabular(VAEAnomalyDetection):

    def make_encoder(self, input_size, latent_size):
        """
        Simple encoder for tabular data.
        If you want to feed image to a VAE make another encoder function with Conv2d instead of Linear layers.
        :param input_size: number of input variables
        :param latent_size: number of output variables i.e. the size of the latent space since it's the encoder of a VAE
        :return: The untrained encoder model
        """
        return nn.Sequential(
            nn.Linear(input_size, 500),
            nn.ReLU(),
            nn.Linear(500, 200),
            nn.ReLU(),
            nn.Linear(200, latent_size * 2)
            # times 2 because this is the concatenated vector of latent mean and variance
        )

    def make_decoder(self, latent_size, output_size):
        """
        Simple decoder for tabular data.
        :param latent_size: size of input latent space
        :param output_size: number of output parameters. Must have the same value of input_size
        :return: the untrained decoder
        """
        return nn.Sequential(
            nn.Linear(latent_size, 200),
            nn.ReLU(),
            nn.Linear(200, 500),
            nn.ReLU(),
            nn.Linear(500, output_size * 2)  # times 2 because this is the concatenated vector of reconstructed mean and variance
        )

How to make predictions:

Once the model is trained (suppose for simplicity that it is under saved_models/{train-datetime}/ ) just load and predict with this code snippet:

import torch

#load X_test
model = VaeAnomalyTabular.load_checkpoint('saved_models/2022-01-06_15-12-23/last.ckpt')
# load saved parameters from a run
outliers = model.is_anomaly(X_test)

More Repositories

1

DDPMs-Pytorch

Implementation of various DDPM papers to understand how they work
Python
81
star
2

DecisionTreeNim

Decision tree and Random forest implementation in nim
Nim
23
star
3

Ant-Dracula-Blue

Ant-Dracula Gnome Theme where the pink components are replaced with blue
CSS
16
star
4

MONet-pytorch

Pytorch implementation of Multi-Object Network(MONet)
Python
9
star
5

sequtils2

nim package that provide additional functions for sequences
HTML
9
star
6

VLAE

Variational Ladder AutoEncoder implementation in tensorflow 2
Jupyter Notebook
4
star
7

snail

Pytorch implementation of SNAIL (Simple Neural Attentive Meta-Learner)
Python
4
star
8

slot-attention-pytorch

Python
3
star
9

Parameterization-Curves-NN

Parameterization for polynomial curves approximation via residual deep neural networks
Python
3
star
10

Prototypical-Networks-Few-Zero-Shot

Pytorch implementation and metric reproduction of the paper Prototypical Networks, including both few shot learning and zero shot experiments (includes dataset for zero shot as well)
Python
3
star
11

NOStream

Extension of classic Java data structures that avoid the use of Stream abstraction in favour of direct list transformation methods
Java
3
star
12

bitday-wallpaper-changer

Python script file that changes automatically the wallpaper of your Linux DE
Python
3
star
13

tensorguard

TensorGuard helps to guard against bad Tensor shapes in any tensor based library
Python
2
star
14

Kiss-cool-nvidia-elementary

Adaptation of Kiss-cool-nvidia theme for elementary OS
CSS
1
star
15

deep-learning-template

Deep Learning Template Project
Python
1
star
16

FactorVAE

PyTorch implementation of FactorVAE
Python
1
star
17

ATTSW-Project-2

Java
1
star
18

genesis

Python
1
star
19

VQ-VAE

Pytorch Implementation of "Neural Discrete Representation Learning (Van den Oord, 2017)"
Python
1
star