vae_anomaly_detection


Namevae_anomaly_detection JSON
Version 2.0.1 PyPI version JSON
download
home_pageNone
SummaryPytorch/TF1 implementation of Variational AutoEncoder for anomaly detection following the paper "Variational Autoencoder based Anomaly Detection using Reconstruction Probability by Jinwon An, Sungzoon Cho"
upload_time2023-08-28 19:41:39
maintainerNone
docs_urlNone
authorNone
requires_python<3.12,>3.6
licenseMIT
keywords anomaly detection deep learning pytorch vae
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Variational autoencoder for anomaly detection

![PyPI](https://img.shields.io/pypi/v/vae-anomaly-detection?style=flat-square)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/vae-anomaly-detection?style=flat-square)
![PyPI - License](https://img.shields.io/pypi/l/vae-anomaly-detection?style=flat-square)
![PyPI - Downloads](https://img.shields.io/pypi/dm/vae-anomaly-detection?style=flat-square)

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](https://www.semanticscholar.org/paper/Variational-Autoencoder-based-Anomaly-Detection-An-Cho/061146b1d7938d7a8dae70e3531a00fceb3c78e8)
 <br>

## How to install

#### Python package way
 _pip_ package containing the model and training_step only 
   
        pip install vae-anomaly-detection


#### Hack this repository


   a. Clone the repo

    git clone git@github.com:Michedev/VAE_anomaly_detection.git

   b. Install hatch

    pip install hatch

   c. Make the environment with torch gpu support

    hatch env create
      
or with cpu support

    hatch env create cpu

   d. Run the train

    hatch run train

or in cpu
          
    hatch run cpu: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```

```python
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:
```python
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)
```


## train.py help

        usage: train.py [-h] --input-size INPUT_SIZE --latent-size LATENT_SIZE
                        [--num-resamples NUM_RESAMPLES] [--epochs EPOCHS] [--batch-size BATCH_SIZE]
                        [--device {cpu,gpu,tpu}] [--lr LR] [--no-progress-bar]
                        [--steps-log-loss STEPS_LOG_LOSS]
                        [--steps-log-norm-params STEPS_LOG_NORM_PARAMS]

        options:
        -h, --help            show this help message and exit
        --input-size INPUT_SIZE, -i INPUT_SIZE
                                Number of input features. In 1D case it is the vector length, in 2D
                                case it is the number of channels
        --latent-size LATENT_SIZE, -l LATENT_SIZE
                                Size of the latent space
        --num-resamples NUM_RESAMPLES, -L NUM_RESAMPLES
                                Number of resamples in the latent distribution during training
        --epochs EPOCHS, -e EPOCHS
                                Number of epochs to train for
        --batch-size BATCH_SIZE, -b BATCH_SIZE
        --device {cpu,gpu,tpu}, -d {cpu,gpu,tpu}, --accelerator {cpu,gpu,tpu}
                                Device to use for training. Can be cpu, gpu or tpu
        --lr LR               Learning rate
        --no-progress-bar
        --steps-log-loss STEPS_LOG_LOSS
                                Number of steps between each loss logging
        --steps-log-norm-params STEPS_LOG_NORM_PARAMS
                                Number of steps between each model parameters logging

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "vae_anomaly_detection",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.12,>3.6",
    "maintainer_email": null,
    "keywords": "anomaly detection,deep learning,pytorch,vae",
    "author": null,
    "author_email": "Michele De Vita <mik3dev@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/cf/1b/fa393ddca774665dcdd7da40fabe77acda1dff984e1aac43042a3e792f37/vae_anomaly_detection-2.0.1.tar.gz",
    "platform": null,
    "description": "# Variational autoencoder for anomaly detection\n\n![PyPI](https://img.shields.io/pypi/v/vae-anomaly-detection?style=flat-square)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/vae-anomaly-detection?style=flat-square)\n![PyPI - License](https://img.shields.io/pypi/l/vae-anomaly-detection?style=flat-square)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/vae-anomaly-detection?style=flat-square)\n\nPytorch/TF1 implementation of Variational AutoEncoder for anomaly detection following the paper\n [Variational Autoencoder based Anomaly Detection using Reconstruction Probability by Jinwon An, Sungzoon Cho](https://www.semanticscholar.org/paper/Variational-Autoencoder-based-Anomaly-Detection-An-Cho/061146b1d7938d7a8dae70e3531a00fceb3c78e8)\n <br>\n\n## How to install\n\n#### Python package way\n _pip_ package containing the model and training_step only \n   \n        pip install vae-anomaly-detection\n\n\n#### Hack this repository\n\n\n   a. Clone the repo\n\n    git clone git@github.com:Michedev/VAE_anomaly_detection.git\n\n   b. Install hatch\n\n    pip install hatch\n\n   c. Make the environment with torch gpu support\n\n    hatch env create\n      \nor with cpu support\n\n    hatch env create cpu\n\n   d. Run the train\n\n    hatch run train\n\nor in cpu\n          \n    hatch run cpu:train\n\n   To know all the train parameters run `anaconda-project run train --help`\n\n\n\n\nThis version contains the model and the training procedure\n\n## How To Train your Model\n\n- Define your dataset into dataset.py and overwrite the line `train_set = rand_dataset()  # set here your dataset` in `train.py`\n- 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.\n## Make your model\n\nSubclass ```VAEAnomalyDetection``` and define your encoder and decoder like in ```VaeAnomalyTabular```\n\n```python\nclass VAEAnomalyTabular(VAEAnomalyDetection):\n\n    def make_encoder(self, input_size, latent_size):\n        \"\"\"\n        Simple encoder for tabular data.\n        If you want to feed image to a VAE make another encoder function with Conv2d instead of Linear layers.\n        :param input_size: number of input variables\n        :param latent_size: number of output variables i.e. the size of the latent space since it's the encoder of a VAE\n        :return: The untrained encoder model\n        \"\"\"\n        return nn.Sequential(\n            nn.Linear(input_size, 500),\n            nn.ReLU(),\n            nn.Linear(500, 200),\n            nn.ReLU(),\n            nn.Linear(200, latent_size * 2)\n            # times 2 because this is the concatenated vector of latent mean and variance\n        )\n\n    def make_decoder(self, latent_size, output_size):\n        \"\"\"\n        Simple decoder for tabular data.\n        :param latent_size: size of input latent space\n        :param output_size: number of output parameters. Must have the same value of input_size\n        :return: the untrained decoder\n        \"\"\"\n        return nn.Sequential(\n            nn.Linear(latent_size, 200),\n            nn.ReLU(),\n            nn.Linear(200, 500),\n            nn.ReLU(),\n            nn.Linear(500, output_size * 2)  # times 2 because this is the concatenated vector of reconstructed mean and variance\n        )\n```\n\n## How to make predictions:\nOnce the model is trained (suppose for simplicity that it is under _saved_models/{train-datetime}/_ ) just load and predict with this code snippet:\n```python\nimport torch\n\n#load X_test\nmodel = VaeAnomalyTabular.load_checkpoint('saved_models/2022-01-06_15-12-23/last.ckpt')\n# load saved parameters from a run\noutliers = model.is_anomaly(X_test)\n```\n\n\n## train.py help\n\n        usage: train.py [-h] --input-size INPUT_SIZE --latent-size LATENT_SIZE\n                        [--num-resamples NUM_RESAMPLES] [--epochs EPOCHS] [--batch-size BATCH_SIZE]\n                        [--device {cpu,gpu,tpu}] [--lr LR] [--no-progress-bar]\n                        [--steps-log-loss STEPS_LOG_LOSS]\n                        [--steps-log-norm-params STEPS_LOG_NORM_PARAMS]\n\n        options:\n        -h, --help            show this help message and exit\n        --input-size INPUT_SIZE, -i INPUT_SIZE\n                                Number of input features. In 1D case it is the vector length, in 2D\n                                case it is the number of channels\n        --latent-size LATENT_SIZE, -l LATENT_SIZE\n                                Size of the latent space\n        --num-resamples NUM_RESAMPLES, -L NUM_RESAMPLES\n                                Number of resamples in the latent distribution during training\n        --epochs EPOCHS, -e EPOCHS\n                                Number of epochs to train for\n        --batch-size BATCH_SIZE, -b BATCH_SIZE\n        --device {cpu,gpu,tpu}, -d {cpu,gpu,tpu}, --accelerator {cpu,gpu,tpu}\n                                Device to use for training. Can be cpu, gpu or tpu\n        --lr LR               Learning rate\n        --no-progress-bar\n        --steps-log-loss STEPS_LOG_LOSS\n                                Number of steps between each loss logging\n        --steps-log-norm-params STEPS_LOG_NORM_PARAMS\n                                Number of steps between each model parameters logging\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "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\"",
    "version": "2.0.1",
    "project_urls": {
        "homepage": "https://github.com/Michedev/VAE_anomaly_detection",
        "repository": "https://github.com/Michedev/VAE_anomaly_detection"
    },
    "split_keywords": [
        "anomaly detection",
        "deep learning",
        "pytorch",
        "vae"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "70b6ba45ebaaecbd6029feae5ae18c4d88212edaaa9fb5e7957dbbed3bf54d87",
                "md5": "bb0cdfc50360cf73d75b23dc056eddfd",
                "sha256": "f6624f3a8cc3a75e3f423ba0d070041de38796045c1178b6560cebc013b692a0"
            },
            "downloads": -1,
            "filename": "vae_anomaly_detection-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bb0cdfc50360cf73d75b23dc056eddfd",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.12,>3.6",
            "size": 9563,
            "upload_time": "2023-08-28T19:41:40",
            "upload_time_iso_8601": "2023-08-28T19:41:40.601517Z",
            "url": "https://files.pythonhosted.org/packages/70/b6/ba45ebaaecbd6029feae5ae18c4d88212edaaa9fb5e7957dbbed3bf54d87/vae_anomaly_detection-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "cf1bfa393ddca774665dcdd7da40fabe77acda1dff984e1aac43042a3e792f37",
                "md5": "3b604854c6c3cd380fc8245a90084cac",
                "sha256": "f0cff4fb1749d74ef6d9d68c112a55faff1fe34184ccc26d2847eb81c5de9a7d"
            },
            "downloads": -1,
            "filename": "vae_anomaly_detection-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3b604854c6c3cd380fc8245a90084cac",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.12,>3.6",
            "size": 9404,
            "upload_time": "2023-08-28T19:41:39",
            "upload_time_iso_8601": "2023-08-28T19:41:39.705780Z",
            "url": "https://files.pythonhosted.org/packages/cf/1b/fa393ddca774665dcdd7da40fabe77acda1dff984e1aac43042a3e792f37/vae_anomaly_detection-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-28 19:41:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Michedev",
    "github_project": "VAE_anomaly_detection",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "vae_anomaly_detection"
}
        
Elapsed time: 0.39372s