Giter Club home page Giter Club logo

anomalybert's Introduction

AnomalyBERT: Transformer-based Anomaly Detector

This is the code for Self-supervised Transformer for Time Series Anomaly Detection using Data Degradation Scheme.

Installation

Please clone our repository at path/to/repository/ and install the packages in requirements.txt. Before installing the packages, we recommend installing Python 3.8 and Pytorch 1.9 with CUDA.

git clone https://github.com/Jhryu30/AnomalyBERT.git path/to/repository/

conda create --name your_env_name python=3.8
conda activate your_env_name

pip install torch==1.9.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html  # example CUDA setting
pip install -r requirements.txt

We use five public datasets, SMAP, MSL, SMD, SWaT, and WADI. Following the instruction in here, you can download and preprocess the datasets. After preprocessing, you need to edit your dataset directory in utils/config.py.

DATASET_DIR = 'path/to/dataset/processed/'

Demo

We release our trained models on SWaT/WADI/SMAP/MSL. You can download the files from here, and we recommend placing it in logs/best_checkpoints/ folder. Now you can run our demo code in demo.ipynb and see how AnomalyBERT works.

Training

We provide the training code for our model. For example, to train a model of 6-layer Transformer body on SMAP dataset, run:

python3 train.py --dataset=SMAP --n_layer=6

To train a model on MSL dataset with patch size of 2 and customized outlier synthesis probability, run:

python3 train.py --dataset=MSL --patch_size=2 --soft_replacing=0.5 --uniform_replacing=0.1 --peak_noising=0.1 \
--length_adjusting=0.1

You can use the default option for training each dataset, as we did in our paper.

python3 train.py --default_options=SMAP # or any dataset name in MSL/SMD/SWaT/WADI and subset of SMD; SMD0 ~ SMD27

If you want to customize the model and training settings, please check the options in train.py.

Anomaly score estimation and metric computation

To estimate anomaly scores of test data with the trained model, run the estimate.py code. For example, you can estimate anomaly scores of SMAP test set divided by channel with window sliding of 16.

python3 estimate.py --dataset=SMAP --model=logs/YYMMDDhhmmss_SMAP/model.pt --state_dict=logs/YYMMDDhhmmss_SMAP/state_dict.pt \
--window_sliding=16

Now you will obtain results (npy) file that contains the estimated anomaly scores. With the results file, you can compute F1-score with and without the point adjustment by running:

python3 compute_metrics.py --dataset=SMAP --result=logs/YYMMDDhhmmss_SMAP/state_dict_results.npy

If you want to customize the estimation or computation settings, please check the options in estimate.py and compute_metrics.py.

anomalybert's People

Contributors

coffeetumbler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

anomalybert's Issues

It doesn't work on the gpu

I have installed Python 3.8 and Pytorch 1.9 with CUDA. And "cuda available" shows True, "device=cuda:0". But it still doesn't work on the GPU.
I try another Pytorch version, Pytorch 2.1 with CUDA, and no changes.
It looks like something is wrong with some part of the code, but many “to(device)” have been written.
Has anyone experienced this problem

question

Hello author, I would like to ask if my current graphics card configuration with a 4070ti and 12GB VRAM can run this model? If so, could you provide me with some basic settings to get started?

for non-categorical anomaly data

we encoutered error when training the model with data that has no categorical anomalies.

so we removed following code in train.py
# replacing_dim_categorical = replacing_dim_categorical
# - np.maximum(replacing_dim_categorical.min(axis=1, keepdims=True), 0.3) <= 0.001

Code questions

Hello, thank you for your excellent work.

Are the following two lines missing after line 258 in train.py?

x_anomaly[j, tar:tar+leng] = 1
x[j][tar:tar+leng] = _x.transpose(0, 1)

Thanks again.

About accuracy calculation

Hi there! thanks for sharing the code. Can you please explain once how the evaluation in terms of precision, F1 , accuracy is done? I am not getting the logic behind how you have done the accuracy calculation. Some details will be helpful. Thanks!

Issues about figures.

Thanks for your wonderful work!

This work focuses on multivariate time series data such SWaT, WADI and SMAP. But the curves of 'Abnormal Component' in Figure 1, 4 and 5 only show the univariate time series. How do you select the dimension? Or do you do conduct some operations to unify all the dimensions?

Unable to reproduce SMD

Thanks for your wonderful work!

As shown in the figure below, I tried many times but couldn't reproduce SMD, was it my command or some other problem。

python3 train.py --dataset=SMD --default_options=SMD

image

image

negative sample

Hi, Could you please share the code generated by the negative sample?Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.