<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Data Load2&nbsp;&nbsp;Guided Backpropagation3&nbsp;&nbsp;MNIST4&nbsp;&nbsp;CIFAR105&nbsp;&nbsp;Save saliency maps5.1&nbsp;&nbsp;MNIST5.2&nbsp;&nbsp;CIFAR10<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

import sys
sys.path.append('../code')

from dataload import mnist_load, cifar10_load
from saliency.attribution_methods import GuidedBackprop
from saliency.ensembles import *
from utils import get_samples
from visualization import visualize_saliencys<jupyter_output><empty_output><jupyter_text>Data Load<jupyter_code>original_images_mnist, original_targets_mnist, pre_images_mnist, mnist_classes, mnist_model = get_samples('mnist')
original_images_cifar10, original_targets_cifar10, pre_images_cifar10, cifar10_classes, cifar10_model = get_samples('cifar10')<jupyter_output>Files already downloaded and verified
Files already downloaded and verified
Data Complete
Model Complete<jupyter_text>Guided Backpropagation![](https://github.com/TooTouch/WhiteBox-Part1/blob/master/images/models/GuidedBackprop.PNG?raw=true)<jupyter_code>GB_mnist = GuidedBackprop(mnist_model)
GB_cifar10 = GuidedBackprop(cifar10_model)<jupyter_output><empty_output><jupyter_text>MNIST<jupyter_code># Guided Backprop
outputs, probs, preds = GB_mnist.generate_image(pre_images_mnist, original_targets_mnist)

# ensemble
n = 50
sigma = 2
# Guided Backprop + smooth grad
outputs_SG, _, _ = generate_smooth_grad(pre_images_mnist, original_targets_mnist, n, sigma, GB_mnist)
# Guided Backprop + smooth square grad
outputs_SG_SQ, _, _ = generate_smooth_square_grad(pre_images_mnist, original_targets_mnist, n, sigma, GB_mnist)
# Guided Backprop + smooth var grad
outputs_SG_VAR, _, _ = generate_smooth_var_grad(pre_images_mnist, original_targets_mnist, n, sigma, GB_mnist)
names = ['Guided Backprop',
         'Guided Backprop\nSmoothGrad','Guided Backprop\nSmoothGrad-Square','Guided Backprop\nSmoothGrad-VAR'] # names
results = [outputs, outputs_SG, outputs_SG_SQ, outputs_SG_VAR]
target = 'mnist'

visualize_saliencys(original_images_mnist,
                    results,
                    probs,
                    preds,
                    mnist_classes,
                    names,
                    target,
                    col=5, row=10, size=(20,35), labelsize=20, fontsize=25)<jupyter_output><empty_output><jupyter_text>CIFAR10<jupyter_code># Guided Backprop
outputs, probs, preds = GB_cifar10.generate_image(pre_images_cifar10, original_targets_cifar10)

# ensemble
n = 50
sigma = 2
# Guided Backprop + smooth grad
outputs_SG, _, _ = generate_smooth_grad(pre_images_cifar10, original_targets_cifar10, n, sigma, GB_cifar10)
# Guided Backprop + smooth square grad
outputs_SG_SQ, _, _ = generate_smooth_square_grad(pre_images_cifar10, original_targets_cifar10, n, sigma, GB_cifar10)
# Guided Backprop + smooth var grad
outputs_SG_VAR, _, _ = generate_smooth_var_grad(pre_images_cifar10, original_targets_cifar10, n, sigma, GB_cifar10)
names = ['Guided Backprop',
         'Guided Backprop\nSmoothGrad','Guided Backprop\nSmoothGrad-Square','Guided Backprop\nSmoothGrad-VAR'] # names
results = [outputs, outputs_SG, outputs_SG_SQ, outputs_SG_VAR]
target = 'cifar10'

visualize_saliencys(original_images_cifar10,
                    results,
                    probs,
                    preds,
                    cifar10_classes,
                    names,
                    target,
                    col=5, row=10, size=(20,35), labelsize=20, fontsize=25)<jupyter_output><empty_output><jupyter_text>Save saliency maps  MNIST<jupyter_code>trainloader, validloader, testloader = mnist_load(shuffle=False)
GB_mnist.save(trainloader, '../saliency_maps/[mnist]GB_train.hdf5')
GB_mnist.save(validloader, '../saliency_maps/[mnist]GB_valid.hdf5')
GB_mnist.save(testloader, '../saliency_maps/[mnist]GB_test.hdf5')<jupyter_output>Vanilla Backprop: 100%|██████████████████████████████████████████████████████████████| 375/375 [03:00<00:00,  1.78it/s]<jupyter_text>CIFAR10<jupyter_code>trainloader, validloader, testloader = cifar10_load(shuffle=False, augmentation=False)
GB_cifar10.save(trainloader, '../saliency_maps/[cifar10]GB_train.hdf5')
GB_cifar10.save(validloader, '../saliency_maps/[cifar10]GB_valid.hdf5')
GB_cifar10.save(testloader, '../saliency_maps/[cifar10]GB_test.hdf5')<jupyter_output>Vanilla Backprop: 100%|██████████████████████████████████████████████████████████████| 313/313 [04:30<00:00,  1.14it/s]
{'fraction_non_alphanumeric': 0.12737430167597766, 'fraction_numerical': 0.03441340782122905, 'mean_word_length': 5.621301775147929, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '117934', 'license': 'MIT', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1588, 'n_words': 240, 'path': 'notebook/[Attribution] - Guided Backpropagation & Ensemble.ipynb', 'repo_name': 'bllfpc/WhiteBox', 'size': '4475'}