Exemple #1
0
# As in other notebooks, we use a pretrained alexnet.

# In[2]:


from torchvision import models
import data_utils
import midnite
from plot_utils import show, show_normalized, show_heatmap


alexnet = models.alexnet(pretrained=True)
alexnet.eval().to(midnite.get_device());

example_img = data_utils.get_example_from_path(
    "../data/imagenet_example_283.jpg",
    data_utils.DataConfig.ALEX_NET
)

show_normalized(example_img)


# ## Neuron Selection and Split
# In the following examples, we measure the effect on the neuron for the target output class (283).
# This yields us a heatmap of the influence on the correct prediction.
# 
# The input image is split spatially (`SpatialSplit`), i.e. we _stride_ through the the spatial positions, doing a measurement at every step. The _chunk size_ controls which parts of the image are occluded. 
# 
# ## Paramters: Chunk Size and Stride
# To be generic for any split, _chunk size_ and _stride_ are three-dimensional tuples for (depth, height, width). In our example, the depth dimension is irrelevant, since our split is spatial.
# 
# A small _chunk size_ creates a nosiy, fine-grained heatmap, as only small parts of the image features are occluded. On the other hand, a larger chunk size rather shows which _areas_ are important. 
Exemple #2
0
def img():
    file = Path(__file__).parents[2].joinpath("data/imagenet_example_283.jpg")
    return data_utils.get_example_from_path(file, DataConfig.ALEX_NET)
Exemple #3
0
alexnet_ensemble.stochastic_eval();


# ### Step 3: Load Data
# Since we want to compare uncertainties, we load a few different images:
#  - one image (cat) in a class for which the model was trained (in-distribution example)
#  - one image outside ImageNet classes (out-of-distribution example)
#  - random noise

# In[4]:


import data_utils
from plot_utils import show_normalized

id_example = data_utils.get_example_from_path(
    "../data/imagenet_example_283.jpg", data_utils.DataConfig.ALEX_NET)

ood_example = data_utils.get_example_from_path(
    "../data/ood_example.jpg", data_utils.DataConfig.ALEX_NET)

random_example = data_utils.get_random_example(
    data_utils.DataConfig.ALEX_NET)

show_normalized(id_example)
show_normalized(ood_example)
show_normalized(random_example)


# ### Step 4: Calculate Uncertainties
# 
# Correct label for in-distribution image: 283