<jupyter_start><jupyter_text>Interpreting ResNet Model With Smooth Gradients This notebook loads the pretrained ResNet model given by [PaddlePaddle Models](https://github.com/PaddlePaddle/models/tree/release/1.7) and performs image classification on selected images. Interpretations of the predictions are generated and visualized using Smooth Gradients algorithm, specifically the `SmoothGradInterpreter` class. If you have't done so, please first download the pretrained ResNet50 model from [this link](http://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_pretrained.tar). More pretrained models can be found [here](https://github.com/PaddlePaddle/models/tree/release/1.7/PaddleCV/image_classification).<jupyter_code>import paddle.fluid as fluid
import paddle
import numpy as np
import sys
from PIL import Image

sys.path.append('..')
import interpretdl as it
from interpretdl.data_processor.readers import preprocess_image, read_image
from interpretdl.data_processor.visualizer import visualize_overlay, visualize_grayscale
from assets.resnet import ResNet50<jupyter_output><empty_output><jupyter_text>Define the `paddle_model` function that takes in images and return prediction probabilities. `ResNet50` is borrowed from PaddlePaddle image  classification [models](https://github.com/PaddlePaddle/models/tree/release/1.7/PaddleCV/image_classification/models).<jupyter_code>def paddle_model(data):
    class_num = 1000
    model = ResNet50()
    logits = model.net(input=data, class_dim=class_num)
    probs = fluid.layers.softmax(logits, axis=-1)
    return probs<jupyter_output><empty_output><jupyter_text>Initialize the `SmoothGradInterpreter`.<jupyter_code>MODEL_PATH = "../../ResNet50_pretrained"

sg = it.SmoothGradInterpreter(paddle_model, MODEL_PATH, True)<jupyter_output><empty_output><jupyter_text>Before interpreting the image, we first take a look at the original image.<jupyter_code>img_path = 'assets/deer.png'
x = Image.fromarray(read_image(img_path)[0])
x<jupyter_output><empty_output><jupyter_text>Then, let Smooth Gradients method help us `interpret` the image with respect to the predicted label. We choose noise amount to be 0.1, which means the added Gaussian noise has standard deviation of 0.1 * (x_max - x_min), where x_max and x_min are the maximum and minimum values of the processed image. Gradients are overlayed with the original image.<jupyter_code>gradients = sg.interpret(
        img_path, 
        noise_amount=0.1,
        visual=True, 
        labels=None, 
        save_path=None)<jupyter_output><empty_output><jupyter_text>Another choice for visualization is in grayscale:<jupyter_code>visualize_grayscale(gradients[0])<jupyter_output><empty_output><jupyter_text>Let's visualize the effect of noise levels on the smooth gradients method.<jupyter_code>for noise in [0, 0.05, 0.1, 0.3, 0.5]:
    gradients = sg.interpret(
        img_path, 
        noise_amount=0.1,
        visual=False, 
        labels=None, 
        save_path=None)
    visualize_grayscale(gradients[0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08792650918635171, 'fraction_numerical': 0.014435695538057743, 'mean_word_length': 5.5853131749460045, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '565576', 'license': 'Apache-2.0', 'n_tokens_mistral': 993, 'n_tokens_neox': 934, 'n_words': 245, 'path': 'tutorials/smooth_grad_tutorial_cv.ipynb', 'repo_name': 'Tyihou/InterpretDL', 'size': '3048'}