{"id":13737731,"url":"https://github.com/MrtnMndt/OpenVAE_ContinualLearning","last_synced_at":"2025-05-08T15:31:07.579Z","repository":{"id":41420035,"uuid":"189074327","full_name":"MrtnMndt/OpenVAE_ContinualLearning","owner":"MrtnMndt","description":"Open-source code for our paper: Unified Probabilistic Deep Continual Learning through Generative Replay and Open Set Recognition","archived":false,"fork":false,"pushed_at":"2022-04-01T12:53:56.000Z","size":1945,"stargazers_count":61,"open_issues_count":1,"forks_count":12,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-15T06:32:21.518Z","etag":null,"topics":["continual-learning","deep-learning","open-set-recognition","pytorch","variational-autoencoder"],"latest_commit_sha":null,"homepage":"https://doi.org/10.3390/jimaging8040093","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MrtnMndt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-28T17:35:13.000Z","updated_at":"2024-11-11T21:56:51.000Z","dependencies_parsed_at":"2022-08-27T16:41:39.383Z","dependency_job_id":null,"html_url":"https://github.com/MrtnMndt/OpenVAE_ContinualLearning","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrtnMndt%2FOpenVAE_ContinualLearning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrtnMndt%2FOpenVAE_ContinualLearning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrtnMndt%2FOpenVAE_ContinualLearning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrtnMndt%2FOpenVAE_ContinualLearning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MrtnMndt","download_url":"https://codeload.github.com/MrtnMndt/OpenVAE_ContinualLearning/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253095916,"owners_count":21853508,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["continual-learning","deep-learning","open-set-recognition","pytorch","variational-autoencoder"],"created_at":"2024-08-03T03:01:58.695Z","updated_at":"2025-05-08T15:31:07.246Z","avatar_url":"https://github.com/MrtnMndt.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Code for our paper Unified Probabilistic Deep Continual Learning through Generative Replay and Open Set Recognition\n\nThis repository contains PyTorch code for our [paper](https://www.mdpi.com/2313-433X/8/4/93\n):\n\n\u003e **Martin Mundt, Iuliia Pliushch, Sagnik Majumder, Yongwon Hong, and Visvanathan Ramesh:\n\u003e *\"Unified Probabilistic Deep Continual Learning through Generative Replay and Open Set Recognition\"*, Journal of Imaging. 2022; 8(4):93. \n\u003e https://doi.org/10.3390/jimaging8040093**\n\npublished as part of the special issue on Continual Learning in Computer Vision: Theory and Applications in Journal of Imaging. \n\nThe code implements our proposed approach to unify the prevention of catastrophic interference in continual learning with the recognition of unknown data instances (out-of-distribution detection or open set recognition) on the basis of the aggregate posterior in variational inference with deep variational auto-encoders. Respectively, we name our approach OpenVAE. \n\nIn addition to our own contributions, we provide code for our abstract dataset class, that converts any existing (PyTorch) classification dataset into a class incremental scenario. \n\nRecall that the key to our approach is the use of extreme value theory on the basis of the aggregate posterior in a deep VAE, which is optimized for the joint distribution p(x,y). With the mechanisms established in the paper, we can conveniently assign an outlier probability to a novel instance or analogously to randomly drawn samples used for generation. \n\n![](README_gfx/openset_examples.png)\n\n## Requirements\n\nWe list our main python packages that are required. In principle our code should work with many different versions. To allow for full reproducibility at later points in time, we also add a pip freeze `requirements.txt` for our machine's exact configuration at the point of writing the paper. Not all of these libraries are needed and the critical libraries that we use are:\n\n* Python 3 (3.5.2)\n* PyTorch 1.3.1 \u0026 torchvision 0.4.2 \n* Cython \u003e= 0.17 (for libmr) \u0026 libmr 0.1.9 (for open set recognition)\n* tqdm 4.17.1 (for progress bars)\n* scipy 1.2.1 \u0026 librosa 0.6.3 (for creation of AudioMNIST spectrograms)\n\t\nand for visualization:\n\n* Matplotlib 3.0.2\n* Seaborn 0.8.1\n* Tensorboard 2.0.1\n\n## Running experiments - command line parser\nWe have added a command line parser in `lib/cmdparser.py`. There are a variety of options that can be specified. The large majority is also set to a sensible default value and doesn't need to be touched to reproduce the results of our paper. All the options, together with a help dialogue can be printed with:\n\n\tpython3 main.py --help\n\nWe will describe the most important options that can be used for the various experimental settings below. \n\nIn the code we have added docstrings to each function and class, specifying what is being done, what the arguments are, what attributes a class has and what is returned. \n\n### Choice of datasets\nBy default the MNIST dataset is being loaded and a wide residual network (WRN) is trained for 120 epochs in an isolated fashion (i.e. on all data at once, without any continuous learning).\n\nThe dataset can be changed with the `--dataset` option (MNIST, AudioMNIST, FashionMNIST, KMNIST, CIFAR10, CIFAR100 and SVHN are implemented, derived from torchvision):\n\n\tpython3 main.py --dataset FashionMNIST\n\t\nThe flower dataset also has a dataloader, but the data needs to be first downloaded from the following website: https://www.robots.ox.ac.uk/~vgg/data/flowers/ . For our dataloader, it is assumed that the 5 class version, consisting of \"sunflower, daisy, iris, daffodil and pansy\" is copied to `datasets/flower_data/5flowers_class/`. \n\t\n### Continual training and prevention of catastrophic interference \nTo automatically generate continual learning scenarios, we have created an incremental data class, that inherits from any of the specified datasets.  \nWe can specify its use with the `--incremental-data True` option. This will split the given dataset into the individual classes and automatically add a new increment of classes at the end of the specified amount of epochs before continuing to train. The number of initial increments can be set with the `--num-base-tasks` argument and the number of class increments can be set via `--num-increment-tasks`. The order of the increments is sequential by default but can be randomized with `--randomize-task-order True` or loaded from an array with `--load-task-order path`. \nAs specified in the paper, there are 4 general scenarios to choose from by setting: \n\t\n* `--incremental-data True`: this can be seen as a continual learning lower bound, i.e. a simple fine-tuning, and the model will only be trained on the most recent increment's real data.\n* `--incremental-data True --train-incremental-upper-bound True`: this is the incremental upper bound, i.e. the maximum achievable performance of a model given the hyper-parameter setting, where each task increment adds real data and the existing previous real data is kept.\n* `--incremental-data True --generative-replay True`: This corresponds to a VAE model where old tasks are rehearsed using generative replay with conventional sampling from the prior. \n* `--incremental-data True --openset-generative-replay True`: Our proposed approach using open set recognition on the basis of the aggregate posterior (OpenVAE). Uses generative replay with statistical outlier rejection for improved rehearsal of previously seen data distributions. \n\nWe note that test sets always consist of real data. \n\t\nA different datasets can easily be added by following the conventions in `lib/Datasets/datasets.py` and creating a class for each new dataset. The continual learning incremental_dataset.py classes should take care of the rest automatically.\n\n### Neural network architecture\nThe default model is a wide residual network, as evaluated towards the end of our paper (with command line options for embedding size, layer width and architecture depth). In order to be able to use the code if less compute is available, and reproduce our MLP experiments, there is also the smaller `MLP` in `lib/Models/architectures.py`. The model can be selected with:\n\n\tpython3 main.py -a WRN\n\nOur default latent dimensionality is 60, with one sample z being drawn per data point during training (as is typical in the VAE literature). We have added the options to change this with:\n\n\tpython3 main.py --var-latent-dim 60 --var-samples 1\n\t\nOur generative models can be turned into their autoregressive counterparts with the boolean `--autoregression` option. There are further options for setting the amount of channels of the autoregressive decoder, number of layers and kernel sizes, should the user wish to modify this from what has been used in the paper.\n\n\tpython3 main.py --autoregression True --out-channels 60 --pixel-cnn-channels 60 --pixel-cnn-layers 3 --pixel-cnn-kernel-size 7\n\t\nSimilarly, the introspection version can be turned on with:\n\n\tpython3 main.py -introspection True \n\nWe emphasize that this does not induce any modification to the architecture (in contrast to autoregression), but simply reuses the encoder to compute an adversarial loss during training.\n\nWe point to our paper's appendix for a summary of autoregression and introspection.\n\t\n### Logging and visualization\nWe save visualizations, command line parameters, optimizers and models at every continual learning step to an automatically generated (time-stamped) folder on hard-drive.\n\nIn addition, we add this information to a respective TensorBoard instance. This TensorBoard log file also contains all history of losses and our more detailed continual learning metrics, as described in the paper's appendix. Each experiment generates a new folder that contains the major important command line options in its name and a time stamp to quickly distinguish experiments. A full specification of command line arguments is further saved to file and to the TensorBoard text tab.\n\nYou can visualize the TensorBoard log by pointing to the automatically generated runs/ folder:\n\n\ttensorboard --logdir runs/\n\t\nA screenshot of a Tensorboard example for the validation metrics of a continually trained MLP on MNIST can be found below: \n\t\n![](README_gfx/tensorboard_example.png)\n\t\n## Standalone script for open set evaluation with a trained model\nWe provide a separate standalone script to simply evaluate open set dataset outlier rejection rates on an already trained model by specifying the dataset it has been trained on and choosing unseen datasets to evaluate on.  \n\nThe script is called `eval_openset.py` and uses the same command line parser. The `--resume path/to/model.pth.tar` option needs to be specified to load an already trained model. It will print inlier percentages and outlier percentages for all specified datasets and methods in the paper, as well as produce corresponding figures.\n\nThe open set datasets can be specified in a comma separated string as shown in the following example:\n\n\tpython3 eval_openset.py --resume \u003cpath/to/model\u003e --openset-datasets 'FashionMNIST,AudioMNIST,CIFAR10,CIFAR100,SVHN'\n\nIn addition you can optionally specify number of samples to draw `--var-samples`.\n \nNote that for a large number of samples, our Weibull EVT approach (sitting directly on z) is very fast to compute. The predictive entropy of the classifier is similarly quick to compute as it consists of only additional matrix multiplication. However, to use reconstruction loss to distinguish datasets implies recalculating the entire decoder for every sample (batch). We have thus added an extra command line option named `--calc-reconstruction` that can be set to `True`. If this calculation is desired for reasons of comparison and reproducing the figures and tables in the paper. Here is an example, averaged over 100 samples:\n\n\tpython3 eval_openset.py --resume \u003cpath/to/model\u003e --openset-datasets 'FashionMNIST,AudioMNIST,CIFAR10,CIFAR100,SVHN' --var-samples 100 --calc-reconstruction True\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMrtnMndt%2FOpenVAE_ContinualLearning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMrtnMndt%2FOpenVAE_ContinualLearning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMrtnMndt%2FOpenVAE_ContinualLearning/lists"}