<jupyter_start><jupyter_text>Plot Saliency Map of the Inputs for Pretrained Models<jupyter_code>from google.colab import drive

drive.mount('/content/drive')
import os

os.chdir('drive/My Drive/Pytorch/Dataset/Saliency Map')<jupyter_output><empty_output><jupyter_text>**import necessary packages**<jupyter_code>import torch
import torchvision
from torchsummary import summary
import torchvision.transforms as T

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

import requests<jupyter_output><empty_output><jupyter_text>**load pretrained model**<jupyter_code>device= 'cuda:0' if torch.cuda.is_available() else 'cpu'

model = torchvision.models.vgg19(pretrained= True)
model.to(device)
def get(url, fname):

  response = requests.get(url)
  with open(fname, 'wb') as f :
    f.write(response.content)
get("https://specials-images.forbesimg.com/imageserve/5db4c7b464b49a0007e9dfac/960x0.jpg?fit=scale", 'dog.jpg')
get("https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2019/12/03202400/Yellow-Labrador-Retriever.jpg", 'dog_big.jpg')
Image.open('dog.jpg')
Image.open('dogs_and_cats.jpeg')<jupyter_output><empty_output><jupyter_text>**preprocess the input image**<jupyter_code>def preprocess(image, size= 224):
  transform = T.Compose([
              T.Resize((size, size)),
              T.ToTensor(),
              T.Normalize(mean = [0.485, 0.456, 0.406]
                          , std= [0.229, 0.225, 0.224])])
  return transform(image)

def deprocess(tensor):
  transform = T.Compose([
              T.Normalize(mean= [0, 0, 0]
                          , std = [4.3668, 4.4444, 4.4642]) , 
              T.Normalize(mean= [-0.485, -0.456, -0.406]
                          , std = [1, 1, 1]),
              T.ToPILImage()])          
  return transform(tensor)

def show_image(image_pil, cmap):
  plt.imshow(np.array(image_pil), cmap= cmap)  
  plt.axis('off')

img = Image.open('dog.jpg')
t = preprocess(img)
img = Image.open('dog-cat-1.jpg')
t = preprocess(img)
d = deprocess(t)
plt.imshow(np.array(d))<jupyter_output><empty_output><jupyter_text>**Get the Saliency Map....**<jupyter_code>X = preprocess(img)
X = X.unsqueeze(0)

model.eval()

X.requires_grad_()

scores = model(X.to(device))

#max_index = torch.argmax(scores)
#max_value = scores[0, max_index]

#max_value.backward()

max_value, max_index = scores.topk(2, largest= False)

max_value[0][1].backward()

val, idx = torch.max(X.grad.abs(), dim= 1)

val.size()
fig, axs = plt.subplots(nrows= 1, ncols= 2, figsize= (10,10))

axs[0].imshow(img.resize(size= (224, 224)))
axs[1].imshow(np.array(val[0]), cmap= plt.cm.hot)  
fig, axs = plt.subplots(nrows= 1, ncols= 2, figsize= (10,10))

axs[0].imshow(img.resize(size= (224, 224)))
axs[1].imshow(np.array(val[0]), cmap= plt.cm.hot)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16417910447761194, 'fraction_numerical': 0.046908315565031986, 'mean_word_length': 4.108892921960073, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '488388', 'license': 'MIT', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1100, 'n_words': 191, 'path': 'Saliency_Map.ipynb', 'repo_name': 'hassancpu/Deep-Learning-Course-Sharif-University-of-Technology-', 'size': '2814'}