Home

Awesome

TimeVQVAE

This is an official Github repository for the PyTorch implementation of TimeVQVAE from our paper "Vector Quantized Time Series Generation with a Bidirectional Prior Model", AISTATS 2023.

TimeVQVAE is a robust time series generation model that utilizes vector quantization for data compression into the discrete latent space (stage1) and a bidirectional transformer for the prior learning (stage2).

<p align="center"> <img src=".fig/stage1.jpg" alt="" width=100% height=100%> </p> <p align="center"> <img src=".fig/stage2.jpg" alt="" width=50% height=50%> </p> <p align="center"> <img src=".fig/iterative_decoding_process.jpg" alt="" width=100% height=100%> </p> <!-- <p align="center"> <img src=".fig/example_of_iterative_decoding.jpg" alt="" width=60% height=60%> </p> <p align="center"> <img src=".fig/visual_examples_generated_samples.jpg" alt="" width=100% height=100%> </p> -->

Notes

The implementation has been modified for better performance and smaller memory consumption. Therefore, the resulting evaluation metrics are probably somewhat different from the repoted scores in the paper. We've done so to benefit the community for their practical use. For details, see the Update Notes section below.

Install / Environment setup

You should first create a virtual environment, and activate the environment. Then you can install the necessary libraries by running the following command.

pip install -r requirements.txt

Dataset and Dataset Download

The UCR archive datasets are automatically downloaded if you run any of the training command below such as python stage1.py. If you just want to download the datasets only without running the training, run

python preprocessing/preprocess_ucr.py

[update note on July 8, 2024] We now use a larger training set by using the following re-arranged dataset: We reorganized the original datasets from the UCR archive by 1) merging the existing training and test sets, 2) resplitting it using StratifiedShuffleSplit (from sklearn) into 80% and 20% for a training set and test set, respectively. We did so becaused the original datasets have two primary issues to be used to train a time series generative model. Firstly, a majority of the datasets have a larger test set compared to a training set. Secondly, there is clear difference in patterns between training and test sets for some of the datasets. The data format remains the same.

Usage

Configuration

Training: Stage1 and Stage2

python stage1.py --dataset_names Wafer --gpu_device_ind 0
python stage2.py --dataset_names Wafer --gpu_device_ind 0

The trained model is saved in saved_models/. The details of the logged metrics are documented in evaluation/README.md.

<!-- :rocket: If you want to run stage 1 and stage 2 at the same time, use the following command. You can specify dataset(s) and a GPU device in the command line for `stages12_all_ucr.py`. ```commandline python stage12_all_ucr.py --dataset_names CBF BME --gpu_device_idx 0 ``` -->

Evaluation

FID, IS, visual inspection between $p(X)$ and $p_\theta(\hat{X})$ with the corresponding comparison in an evaluation latent space.

python evaluate.py --dataset_names Wafer --gpu_device_idx 0

Run CAS (Classification Accuracy Score)

python run_CAS.py  --dataset_names Wafer --gpu_device_idx 0

Minimal Code for Sampling

Refer to simple_sampling.ipynb.

Train it on a Custom Dataset

  1. a template class, DatasetImporterCustom, is given in preprocessing/preprocess_ucr.py.
    • no need to modify any other code except DatasetImporterCustom to train TimeVQVAE on your dataset.
  2. write a data loading code for your dataset in __init__ within DatasetImporterCustom.
  3. run the following codes - stage1,2.
python stage1.py --use_custom_dataset True --dataset_names custom --gpu_device_ind 0
python stage2.py --use_custom_dataset True --dataset_names custom --gpu_device_ind 0
python evaluate.py --use_custom_dataset True --dataset_names custom --gpu_device_idx 0

Also, you can sample synthetic time series with custom_dataset_sampling.ipynb.

Google Colab

Google Colab (NB! make sure to change your notebook setting to GPU.)

A Google Colab notebook is available for time series generation with the pretrained VQVAE. The usage is simple:

  1. User Settings: specify dataset_name and n_samples_to_generate.
  2. Sampling: Run the unconditional sampling and class-conditional sampling.
<!-- Note that the pretrained models are automatically downloaded within the notebook. In case you're interested, the pretrained models are stored in [here](https://figshare.com/articles/software/Pretrained_models_of_TimeVQVAE/22048034). --> <!-- ## Remarks * The full result tables for FID, IS, and CAS are available in `results/`. -->

Update Notes

Implementation Modifications

<!-- ### Enhanced Sampling Scheme (ESS) [2] We have published a [follow-up paper](https://arxiv.org/abs/2309.07945) [2] that enhances the sampling process by resolving its existing limitations, which in turn results in considerably higher fidelity. To be more precise, we first sample a token set with a naive iterative decoding (existing sampling process) and remove the less-likely tokens, and resample the tokens with a better realism approach for tokens. The figure below illustrates the overview of [2]. <p align="center"> <img src=".fig/proposed_sampling_strategy_overview.png" alt="" width=100% height=100%> </p> You can use it by setting `MaskGIT.ESS.use = True` in `configs/config.yaml`. -->

Fidelity Enhancer for Vector Quantized Time Series Generator (FE-VQTSG) [3] (not published yet)

<!-- We have published a follow-up paper that proposes, _FE-VQTSG_. -->

It is a U-Net-based mapping model that transforms a synthetic time series generated by a VQ-based TSG method to be more realistic while retaining the original context.

<p align="center"> <img src=".fig/inference_process_tsfe.png" alt="" width=100% height=100%> </p>

The model training is availble after finishing the stage1 and stage2 trainings. To train FE-VQTSG, run

python stage_fid_enhancer.py  --dataset_names Wafer --gpu_device_ind 0

During the evaluation, FE-VQTSG can be employed by setting --use_fidelity_enhancer True.

python evaluate.py --dataset_names Wafer --gpu_device_idx 0 --use_fidelity_enhancer True

TimeVQVAE for Anomaly Detection (TimeVQVAE-AD) [4]

TimeVQVAE learns a prior, and we can utilize the learned prior to measure the likelihood of a segment of time series, in which a high likelihood indicates a normal state while a low likelihood indicates an abnormal state (i.e., anomaly). With that principal, we have developed TimeVQVAE-AD. It not only achieves a state-of-the-art anomaly detection accuracy on the UCR Anomaly archive, but also provides a high level of explainability, covering counterfactual sampling (i.e., to answer the following question, "how is the time series supposed look if there was no anomaly?"). If AD is your interest, please check out the paper. Its open-source code is available here.

Citation

[1] Lee, Daesoo, Sara Malacarne, and Erlend Aune. "Vector Quantized Time Series Generation with a Bidirectional Prior Model." International Conference on Artificial Intelligence and Statistics. PMLR, 2023.

<!-- [2] Lee, Daesoo, Erlend Aune, and Sara Malacarne. "Masked Generative Modeling with Enhanced Sampling Scheme." arXiv preprint arXiv:2309.07945 (2023). -->

[3]

[4] Lee, Daesoo, Sara Malacarne, and Erlend Aune. "Explainable time series anomaly detection using masked latent generative modeling." Pattern Recognition (2024): 110826.

[5] Dempster, Angus, François Petitjean, and Geoffrey I. Webb. "ROCKET: exceptionally fast and accurate time series classification using random convolutional kernels." Data Mining and Knowledge Discovery 34.5 (2020): 1454-1495.

[6] Ziyin, Liu, Tilman Hartwig, and Masahito Ueda. "Neural networks fail to learn periodic functions and how to fix it." Advances in Neural Information Processing Systems 33 (2020): 1583-1594.