-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgradcam.py
More file actions
241 lines (197 loc) · 9.75 KB
/
gradcam.py
File metadata and controls
241 lines (197 loc) · 9.75 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from model import CNN1D , CNNMFCC,initialize_mobilenet,initialize_mobilenetV3
from audiodataloader import AudioDataLoader, AudioSegment, split_list_after_speaker
from Dataloader_gradcam import GradcamDataset , plot_mfcc,TrainSegment
from sklearn.model_selection import train_test_split
import datetime
import os
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from collections import defaultdict
import librosa
from torch.nn.functional import interpolate
import pickle
def normalize_image(image):
"""
Normalize an image so that its values are between 0 and 1.
Parameters:
- image: The input image as a 2D or 3D numpy array.
Returns:
- Normalized image with values in the range [0, 1].
"""
min_val = np.min(image)
max_val = np.max(image)
# Avoid division by zero if the image is constant
if max_val - min_val == 0:
return np.zeros_like(image)
normalized_image = (image - min_val) / (max_val - min_val)
return normalized_image
def grad_cam_heatmap(model, input_tensor, target_class):
"""
Generates a Grad-CAM heatmap for the given input and target class.
"""
# Ensure input tensor has the right dimensions
if input_tensor.dim() == 3:
input_tensor = input_tensor.unsqueeze(0) # Add batch dimension
input_tensor = input_tensor.to(next(model.parameters()).device) # Move to the correct device
model.eval()
# Identify the last convolutional layer for Grad-CAM
target_layer = model.features[-1] # Use the last layer of features
gradients = []
activations = []
# Register hooks to capture gradients and activations
def backward_hook(module, grad_input, grad_output):
gradients.append(grad_output[0])
def forward_hook(module, input, output):
activations.append(output)
target_layer.register_forward_hook(forward_hook)
target_layer.register_backward_hook(backward_hook)
# Forward pass
output = model(input_tensor)
_, predicted_class = torch.max(output, dim=1)
loss = output[:, target_class].sum() # Target class activation
print("Label: ",target_class," Prediction: ",predicted_class.numpy())
# Backward pass to compute gradients
model.zero_grad()
loss.backward()
# Extract the gradients and activations
gradients = gradients[0].detach().numpy()
activations = activations[0].detach().numpy()
# Compute the weights for Grad-CAM
weights = np.mean(gradients, axis=(2, 3)) # Global average pooling
cam = np.sum(weights[:, :, np.newaxis, np.newaxis] * activations, axis=1) # Weighted sum of activations
# ReLU on the heatmap to remove negative values
cam = np.maximum(cam, 0)
# Normalize the heatmap to [0, 1]
cam = cam[0] # Remove batch dimension
cam = cam / np.max(cam)
# Resize the heatmap to match the input size
cam_resized = interpolate(torch.tensor(cam).unsqueeze(0).unsqueeze(0), # Add batch and channel dims
size=(input_tensor.shape[2], input_tensor.shape[3]), # Match input height and width
mode='bilinear',
align_corners=False).squeeze().numpy()
return cam_resized,target_class,predicted_class.numpy()
def overlay_heatmap_with_input(input_tensor, heatmap, word_spoken, label, predicted, alpha=0.6):
"""
Overlay the Grad-CAM heatmap onto the input tensor (e.g., mel-spectrogram)
with improved visibility by using distinct colormaps.
"""
if isinstance(input_tensor, torch.Tensor):
input_tensor = input_tensor.cpu().numpy()
heatmap = heatmap.squeeze()
input_tensor = input_tensor.squeeze()
# Normalize the input tensor and heatmap to [0, 1]
input_tensor = normalize_image(input_tensor)
heatmap = normalize_image(heatmap)
# Create the overlay by first plotting the input in grayscale
# and then overlaying the heatmap with a vibrant colormap (e.g., 'jet').
overlay = alpha * heatmap + (1 - alpha) * input_tensor
fig, axs = plt.subplots(2, 2, figsize=(16, 12))
# Plot Input Tensor (use grayscale to provide contrast)
im0 = axs[0, 0].imshow(input_tensor, aspect='auto', origin='lower', cmap='inferno')
axs[0, 0].set_title(f"Input Tensor for word {word_spoken}")
axs[0, 0].set_xlabel("Time Frames")
axs[0, 0].set_ylabel("Mel Coefficients")
plt.colorbar(im0, ax=axs[0, 0])
# Plot Overlay (input in grayscale, heatmap in 'jet')
axs[0, 1].imshow(input_tensor, aspect='auto', origin='lower', cmap='gray')
im1 = axs[0, 1].imshow(heatmap, aspect='auto', origin='lower', cmap='inferno', alpha=alpha)
axs[0, 1].set_title(f"Overlay of Heatmap and Input Tensor for word {word_spoken}")
axs[0, 1].set_xlabel("Time Frames")
axs[0, 1].set_ylabel("Mel Coefficients")
plt.colorbar(im1, ax=axs[0, 1])
# Plot Heatmap alone
im2 = axs[1, 0].imshow(heatmap, aspect='auto', origin='lower', cmap='inferno')
axs[1, 0].set_title("Heatmap")
axs[1, 0].set_xlabel("Time Frames")
axs[1, 0].set_ylabel("Mel Coefficients")
plt.colorbar(im2, ax=axs[1, 0])
# Optionally, plot the combined overlay again in a separate plot
im3 = axs[1, 1].imshow(overlay, aspect='auto', origin='lower', cmap='inferno')
axs[1, 1].set_title(f"Alpha Blended Overlay Label: {label} Prediction: {predicted}")
axs[1, 1].set_xlabel("Time Frames")
axs[1, 1].set_ylabel("Mel Coefficients")
plt.colorbar(im3, ax=axs[1, 1])
plt.tight_layout()
plt.show()
def overlay_heatmap_with_input_2channel(input_tensor, heatmap, word_spoken, label, predicted, alpha=0.6):
"""
Overlay the Grad-CAM heatmap onto the input tensor (e.g., mel-spectrogram)
with improved visibility by using distinct colormaps.
"""
if isinstance(input_tensor, torch.Tensor):
input_tensor = input_tensor.cpu().numpy()
heatmap = heatmap.squeeze()
input_tensor = input_tensor.squeeze()
mel = normalize_image(input_tensor[0])
att = normalize_image(input_tensor[1])
# Normalize the input tensor and heatmap to [0, 1]
heatmap = normalize_image(heatmap)
# Create the overlay by first plotting the input in grayscale
# and then overlaying the heatmap with a vibrant colormap (e.g., 'jet').
overlay = alpha * heatmap + (1 - alpha) * input_tensor
fig, axs = plt.subplots(2, 2, figsize=(16, 12))
# Plot Input Tensor (use grayscale to provide contrast)
im0 = axs[0, 0].imshow(mel, aspect='auto', origin='lower', cmap='inferno')
axs[0, 0].set_title(f"Input Tensor for word {word_spoken}")
axs[0, 0].set_xlabel("Time Frames")
axs[0, 0].set_ylabel("Mel Coefficients")
plt.colorbar(im0, ax=axs[0, 0])
# Plot Overlay (input in grayscale, heatmap in 'jet')
axs[0, 1].imshow(mel, aspect='auto', origin='lower', cmap='gray')
im1 = axs[0, 1].imshow(heatmap, aspect='auto', origin='lower', cmap='inferno', alpha=alpha)
axs[0, 1].set_title(f"Overlay of Heatmap and Input Tensor for word {word_spoken}")
axs[0, 1].set_xlabel("Time Frames")
axs[0, 1].set_ylabel("Mel Coefficients")
plt.colorbar(im1, ax=axs[0, 1])
# Plot Heatmap alone
im2 = axs[1, 0].imshow(att, aspect='auto', origin='lower', cmap='inferno')
axs[1, 0].set_title("Heatmap")
axs[1, 0].set_xlabel("Time Frames")
axs[1, 0].set_ylabel("Mel Coefficients")
plt.colorbar(im2, ax=axs[1, 0])
# Optionally, plot the combined overlay again in a separate plot
axs[1, 1].imshow(att, aspect='auto', origin='lower', cmap='gray')
im11 = axs[1, 1].imshow(heatmap, aspect='auto', origin='lower', cmap='inferno', alpha=alpha)
axs[1, 1].set_title(f"Alpha Blended Overlay Label: {label} Prediction: {predicted}")
axs[1, 1].set_xlabel("Time Frames")
axs[1, 1].set_ylabel("Mel Coefficients")
plt.colorbar(im11, ax=axs[1, 1])
plt.tight_layout()
plt.show()
def explain_model(dataset):
#path = "models/Mobilenet_20241129-170942.pth"
#'path to best model'
path = "models\MEL+ATT_ohneschedluer_Train+val20250210-190753.pth"
model = initialize_mobilenetV3(num_classes=2, dropout = 0.3, input_channels=2)
model.load_state_dict(torch.load(path, weights_only=True,map_location=torch.device('cpu')))
#model.to(device)
#summary(model, input_size=(1, 224, 224))
input_tensor, target_class, word_spoken = find_spezific_word(dataset,"Satz",0)
heatmap,target_class,predicted_class = grad_cam_heatmap(model, input_tensor, target_class)
overlay_heatmap_with_input_2channel(input_tensor,heatmap, word_spoken,target_class,predicted_class)
#input_tensor , target_class,word_spoken = dataset[232]#select
#heatmap,target_class,predicted_class = grad_cam_heatmap(model, input_tensor, target_class)
#overlay_heatmap_with_input(input_tensor,heatmap, word_spoken,target_class,predicted_class)
def find_spezific_word(dataset, word, label):
for i in range(len(dataset)):
input_tensor, target_class, word_spoken = dataset[i+100]#adjust the 100 because it finds only the next index
if(word_spoken == word and label == target_class):
print(i)
return input_tensor, target_class, word_spoken
print("No word Found.....................................!")
return None
if __name__ == "__main__":
with open("data_lists\mother_list_augment.pkl", "rb") as f:
data = pickle.load(f)
segments_train, segments_val, segments_test= split_list_after_speaker(data)
print(f"Number of word segments in train: {len(segments_train)}, test: {len(segments_test)}")
# Create dataset
segments_test = GradcamDataset(segments_test)
segments_train = GradcamDataset(segments_train)
#Use test set to see how good model looks
explain_model(segments_test)