-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAM.py
45 lines (34 loc) · 1.31 KB
/
CAM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import streamlit as st
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
from torchvision import models
import torch.nn as nn
import torch.nn.functional as F
import cv2
# Function to get CAM
def get_cam(model, img_tensor, target_layer_name):
model.eval()
def forward_hook(module, input, output):
activation[0] = output
activation = {}
layer = dict([*model.named_modules()]).get(target_layer_name, None)
if layer is None:
raise ValueError(f"Layer {target_layer_name} not found in the model")
hook = layer.register_forward_hook(forward_hook)
with torch.no_grad():
output = model(img_tensor)
predicted_class = torch.argmax(output, dim=1).item()
hook.remove()
weight_softmax_params = list(model.parameters())[-2].detach().numpy()
weight_softmax = np.squeeze(weight_softmax_params)
activation = activation[0].squeeze().cpu().data.numpy()
cam = np.zeros(activation.shape[1:], dtype=np.float32)
for i, w in enumerate(weight_softmax[predicted_class]):
cam += w * activation[i, :, :]
cam = np.maximum(cam, 0)
cam = cv2.resize(cam, (img_tensor.shape[-1], img_tensor.shape[-2]))
cam = cam - np.min(cam)
cam = cam / np.max(cam)
return cam