This repo is a fork of open-mmlab mmengine. It adds support for training and evaluating Laplace-approximated Bayesian models for pre-trained openmmlab models.
Upstream repo README.
- Curvature objects: New object class
mmengine.curvature.KFACsupporting kronecker-factored Laplace posterior of a trained model - Gradient objects: The
KFACobjects supports kronecker-factored gradients and inner products - Loop objects: Loop objects
FisherLoopandTestUncLoopadded inmmengine/runner/loops.py, iterators for training KFAC-fisher and testing predictive uncertainty - New runner functions: added functionalities to
mmengine/runner/runner.py - Model-agnostic: works with any OpenMMlab model
KFAC code adapted from https://github.com/DLR-RM/curvature
pip install -U openmim
mim install mmcv
git clone https://github.com/romiebanerjee/mmengine-bayes
pip install -e mmengine-bayes/.import sys
sys.path.append('/path/to/mmengine-bayes')
import mmcv, mmengine- Input:
modela pre-trained OpenMMlab (Pytorch) model with weights dictweights - KFAC: Instantiate a
mmengine.curvature.KFACobject withmodelandweights - Iterator: The
FisherLoopiterator (similar to theTrainLoopiterator)- runs forward-pass on the model
modelloaded with weightsweights - through the training dataset (batch size = 1), without
optimizer.step()(unlike ) - At every batch iteration, the run
KFAC.update_fisher()to updateKFAC.statedictionary - Run one epoch
- runs forward-pass on the model
- method:
runner.fisher()
- Input:
model:model,weights:model_weights_dict,kfac:model_kfac,kfac.state:kf-fisher_dict,val_loader - Iterator: The
TestUncLoopwill iteratively calculate downstream GLM predictive distribution covariance, through the validation dataset, using the following methods:MC-GLM: A monte-carlo estimator for GLM predictive covariance1-GLM: Rank-One GLM predictive covariance, using single monte-carlo samplee-GLM: Rank-One GLM predictive, using a single fisher eigen-direction in weight space
- method:
runner.test_unc()