Created
April 1, 2023 22:17
-
-
Save Dref360/08371d15196329478b0b2c1682436e0d to your computer and use it in GitHub Desktop.
Quick example to show how to activate and deactivate mcdropout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import torch | |
from torchvision.models import vgg16 | |
from baal.bayesian.dropout import MCDropoutModule | |
from baal.modelwrapper import ModelWrapper | |
model = vgg16() | |
wrapper = ModelWrapper(model, None) | |
input = torch.randn([2, 3, 64, 64]) | |
def is_deterministic(preds): | |
return all([np.allclose(preds[..., i], preds[..., 0], rtol=1e-3) for i in range(1, preds.shape[-1])]) | |
with MCDropoutModule(model): | |
model.eval() | |
# The model will not be deterministic | |
assert not is_deterministic(wrapper.predict_on_batch(input, iterations=10)) | |
# Is deterministic again. | |
model.eval() | |
assert is_deterministic(wrapper.predict_on_batch(input, iterations=10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment