Repository for Dropout studies using the MNIST dataset.
The dependency of ths package is listed as folling:
- torch
- pyro
- torchvision
- cuda if you want to run with GPU
- numpy
- scipy
- matplotlib
- h5py
The study includes a comparison between Dropout and the Bayesian Neural Network, which requires Pyro. Unfortunately, it does not exist on conda, so you'll have to install via pip. Run: pip3 install pyro-ppl - note the ppl! Crossing conda and pip makes things tangle easily, which is a pain.
To make this a bit easier, an environment.yaml file is included for easy installation:
conda env create -f environment.yaml
The following following instructions outline how to train each network, then run it on the testing data. These are also drivers within the Makefile to make the procedure less involved. The general design flow is to train each network, run on testing data and produce one uniform data housing object EvaluationRecords, containing useful output quantities for either network.
Makefile approach:
make dropout_workflow
python3 train.py $(model_name) $(droupout_rate) $(epochs) $(n_images)
The model_name parameter must be defined in models.py, generally you'll want CNNWeak. This produces a file of format:
../output/trainModule/mnist_{model_name}_dr0p{dropout_rate*10}_ep{epochs}_ev{n_images}.pt
python3 calibrate.py $(model_name) $(training_string) $(dropout_rate) $(posterior_samples) $(start_image) $(end_image)
Where training_string is the core string of the output file mentioned above, something like: dr0p2_ep300_ev10000 for a dropout rate of 0.2, 300 epochs, and 10000 images.
This will produce the EvaluationRecords object, which will be saved at
../output/testResult/uncertainty/cali_{model_name}_{training_string}_test{dropout_rate*10}_image{range_min+1}_{range_max}.h5
Additionally, it runs all of the plotting functions within the file plotting/eval_plot_library.py, which are saved within ../output/testResult/uncertainty/ (this directory is a global in that script, may be switched).
Makefile approach:
make bnn_workflow
The Bayesian network is driven by a configuration file to set all the options. A sample can be found at example_opts.yaml.
For training, run:
python bnn.py options.yaml
This uses SVI to do the training and outputs a file with the Predictive object and state dictionary saved the name given in the configuration as model_file (bcnn.pyro in the example)
Next we want to use the model to make posterior predictions. The script takes the same options file:
python ppc.py options.yaml
This is the BNN equivalent of calibrate.py, and the output object is the same, the finalize method of EvaluationRecords.
A utility script to load out output of the EvaluationRecords object is found in plotting/dropout_evaluations.py.
Two primary analysis scripts live in this directory, analysis_tests.py is intended to perform analysis on a single model.
To compare two (todo: or more) models, use the model_comparison.py script.
python model_comparison.py bnn_output.h5 dropout_1.h5 dropout_2.h5 dropout_3.h5 ....
Given the output of calibrate.py (the dropout posterior) and of ppc.py (the bnn posterior), we want to make comparisons. The following script (WIP) is built to do the image-wise KS test:
- Create specific
EvaluationRecordsobjects for the BNN and dropout network independently, which inherit from a parent class. - Make testing suite more rigorous. Current coverage is quite small, would be nice to expand this.
General
analysis_tests.py- Analysis on single modelEvaluationRecords.py- Class for recording evaluation on testing datamodels.py- Defined NN modelsmutualinfo.py- Functions to calculate mutual information (KL divergence, Jensen-Shannon divergence)smoothing.py- Class to smooth 2D array along one axisutils.py- General utilities for loading data, rotating images, adding noise
Dropout Network
train.py- Training routine for dropout networkcalibrate.py- Evaluate on testing data
BNN
bnn.py- Training script for BNNppc.py- Evaluation script for running on testing data
example_opts.yaml- example options for BNN workflowMakefile- makefile that includes general workflowsrun.sh- Binbin's original run script, allows running by passing model as argtests.py- Testing suite for functions usingpytest
eval.py- (todo: deprecated?)probabilities.py- Calculate probabilities of dropout network, replaced by vectorized codesampleAcc.py- Calculate sample accuracy, replaced by vectorized code
- Dropout: a simple way to prevent neural networks from overfitting
- Dropout as a Bayesian Approximation: Representing Model Uncertainty in Deep Learning
- Well-calibrated Model Uncertainty with Temperature Scaling for Dropout Variational Inference
- Everything you always wanted to know about pulls
- Evidential Deep Learning to Quantify Classification Uncertianty
- Work in ALCF to use a Stochastic Spiking NN to approximate Dropout as a means for UQ
- Calibration of Model Uncertainty for Dropout Variational Inference
- Qualitative Analysis of Monte Carlo Dropout (unpublished but interesting)
- Uncertainty Quantification in Deep Learning
- Peculiar Velocity Estimation from Kinetic SZ Effect using Deep Neural Networks