-
Notifications
You must be signed in to change notification settings - Fork 0
/
VGG16alphabeta.py
264 lines (230 loc) · 11 KB
/
VGG16alphabeta.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# %% Imports
import torch
import torch.nn as nn
import torchvision
import torchvision.models as models
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import torch.optim as optim
import copy
import pandas as pd
import os
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:54.1"
# Set GPU device
print(torch.cuda.is_available())
device = torch.device("cuda:0")
# %% Load data
TRAIN_ROOT = "D:/research/xai-series-master/xai-series-master/data/brain_mri/training"
TEST_ROOT = "D:/research/xai-series-master/xai-series-master/data/brain_mri/testing"
train_dataset = torchvision.datasets.ImageFolder(root=TRAIN_ROOT)
test_dataset = torchvision.datasets.ImageFolder(root=TEST_ROOT)
# %% Building the model
class CNNModel(nn.Module):
def __init__(self):
super(CNNModel, self).__init__()
self.vgg16 = models.vgg16(pretrained=True) #This pretrained model is used as a feature extractor in the CNNModel class
# Replace output layer according to our problem
in_feats = self.vgg16.classifier[6].in_features
self.vgg16.classifier[6] = nn.Linear(in_feats, 4)
def forward(self, x):
x = self.vgg16(x)
return x
model = CNNModel()
model.to(device)
model
# %% Prepare data for pretrained model
train_dataset = torchvision.datasets.ImageFolder(
root=TRAIN_ROOT,
transform=transforms.Compose([
transforms.Resize((255,255)),
transforms.ToTensor()
])
)
test_dataset = torchvision.datasets.ImageFolder(
root=TEST_ROOT,
transform=transforms.Compose([
transforms.Resize((255,255)),
transforms.ToTensor()
])
)
#train_dataset[0][0].permute(1,2,0)
# %% Create data loaders
batch_size = 32
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True
)
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=True
)
# %% Train
cross_entropy_loss = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001)
epochs = 1 #epochs changed
# Iterate x epochs over the train data
for epoch in range(epochs):
for i, batch in enumerate(train_loader, 0):
inputs, labels = batch
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
# Labels are automatically one-hot-encoded
loss = cross_entropy_loss(outputs, labels)
loss.backward()
optimizer.step()
print("This is loss-->",loss)
# %% Inspect predictions for first batch
import pandas as pd
inputs, labels = next(iter(test_loader))
inputs = inputs.to(device)
labels = labels.numpy()
outputs = model(inputs).max(1).indices.detach().cpu().numpy()
comparison = pd.DataFrame()
print("Batch accuracy: ", (labels==outputs).sum()/len(labels))
comparison["labels"] = labels
comparison["outputs"] = outputs
comparison
# %% Layerwise relevance propagation for VGG16
# For other CNN architectures this code might become more complex
# Source: https://git.tu-berlin.de/gmontavon/lrp-tutorial
# http://iphome.hhi.de/samek/pdf/MonXAI19.pdf
def new_layer(layer, g):
"""Clone a layer and pass its parameters through the function g."""
layer = copy.deepcopy(layer)
try: layer.weight = torch.nn.Parameter(g(layer.weight))
except AttributeError: pass
try: layer.bias = torch.nn.Parameter(g(layer.bias))
except AttributeError: pass
return layer
def dense_to_conv(layers):
""" Converts a dense layer to a conv layer """
newlayers = []
for i,layer in enumerate(layers):
if isinstance(layer, nn.Linear):
newlayer = None
if i == 0:
m, n = 512, layer.weight.shape[0]
newlayer = nn.Conv2d(m,n,7)
newlayer.weight = nn.Parameter(layer.weight.reshape(n,m,7,7))
else:
m,n = layer.weight.shape[1],layer.weight.shape[0]
newlayer = nn.Conv2d(m,n,1)
newlayer.weight = nn.Parameter(layer.weight.reshape(n,m,1,1))
newlayer.bias = nn.Parameter(layer.bias)
newlayers += [newlayer]
else:
newlayers += [layer]
return newlayers
def get_linear_layer_indices(model):
offset = len(model.vgg16._modules['features']) + 1
indices = []
for i, layer in enumerate(model.vgg16._modules['classifier']):
if isinstance(layer, nn.Linear):
indices.append(i)
indices = [offset + val for val in indices]
return indices
def apply_lrp_on_vgg16(model, image):
image = torch.unsqueeze(image, 0)
# >>> Step 1: Extract layers
layers = list(model.vgg16._modules['features']) \
+ [model.vgg16._modules['avgpool']] \
+ dense_to_conv(list(model.vgg16._modules['classifier']))
linear_layer_indices = get_linear_layer_indices(model)
# >>> Step 2: Propagate image through layers and store activations
n_layers = len(layers)
activations = [image] + [None] * n_layers # list of activations
for layer in range(n_layers):
if layer in linear_layer_indices:
if layer == 32:
activations[layer] = activations[layer].reshape((1, 512, 7, 7))
activation = layers[layer].forward(activations[layer])
if isinstance(layers[layer], torch.nn.modules.pooling.AdaptiveAvgPool2d):
activation = torch.flatten(activation, start_dim=1)
activations[layer+1] = activation
# >>> Step 3: Replace last layer with one-hot-encoding
output_activation = activations[-1].detach().cpu().numpy()
max_activation = output_activation.max()
one_hot_output = [val if val == max_activation else 0
for val in output_activation[0]]
activations[-1] = torch.tensor(one_hot_output, device=device)
# >>> Step 4: Backpropagate relevance scores
relevances = [None] * n_layers + [activations[-1]]
# Iterate over the layers in reverse order
for layer in range(0, n_layers)[::-1]:
current = layers[layer] #layer is an integer val
# Treat max pooling layers as avg pooling
if isinstance(current, torch.nn.MaxPool2d):
layers[layer] = torch.nn.AvgPool2d(2)
current = layers[layer]
if isinstance(current, torch.nn.Conv2d) or \
isinstance(current, torch.nn.AvgPool2d) or\
isinstance(current, torch.nn.Linear):
activations[layer] = activations[layer].data.requires_grad_(True)
# Apply variants of LRP depending on the depth
# see: https://link.springer.com/chapter/10.1007%2F978-3-030-28954-6_10
# Lower layers, LRP-gamma >> Favor positive contributions (activations)
if layer <= 16:
rho = lambda p: p + 0.25*p.clamp(min=0);
incr = lambda z: z+1e-9
# LRP-alpha-gamma rule >> Favor positive contributions (activations) and amplify them by a factor of gamma
if 17 <= layer <= 30:
# LRP-epsilon-z-beta rule >> Remove some noise / Only most salient factors survive
alpha = 2; gamma = 0.5; z_beta = 1
rho = lambda p: p + alpha*(p.clamp(min=0)**gamma);
incr = lambda z: z+1e-9+z_beta*torch.sign(z)*torch.sqrt(torch.abs(z))
# Upper Layers, LRP-0 >> Basic rule
if layer >= 31:
rho = lambda p: p;
incr = lambda z: z+1e-9
# Transform weights of layer and execute forward pass
z = incr(new_layer(layers[layer],rho).forward(activations[layer]))
# Element-wise division between relevance of the next layer and z
s = (relevances[layer+1]/z).data
# Calculate the gradient and multiply it by the activation
(z * s).sum().backward();
#In this specific code snippet, the backward pass is
# being performed to compute the gradients of the
# output with respect to the layer activations.
# These gradients are then used to compute the relevance values for each layer,
# which represent the contribution of each neuron in that layer to the final output of the network.
c = activations[layer].grad
# Assign new relevance values
relevances[layer] = (activations[layer]*c).data #The code you provided specifically updates the relevance scores for each layer by multiplying the activations of that layer by the gradients of the same layer with respect to the output.
else:
relevances[layer] = relevances[layer+1] #If the current layer is not the output layer, the relevance values are instead assigned to the relevance values of the next layer, which has already been computed using the same method.
# >>> Potential Step 5: Apply different propagation rule for pixels
return relevances[0]
# %%
# Calculate relevances for first image in this test batch
image_id = 24
image_relevances = apply_lrp_on_vgg16(model, inputs[image_id])
image_relevances = image_relevances.permute(0,2,3,1).detach().cpu().numpy()[0]
image_relevances = np.interp(image_relevances, (image_relevances.min(),
image_relevances.max()),
(0, 1))
# Show relevances
pred_label = list(test_dataset.class_to_idx.keys())[ #pred_label is a variable that stores the predicted label for a given image based on the class index of the test dataset
list(test_dataset.class_to_idx.values())
.index(labels[image_id])]
print("length of image id ---> ",len(outputs))
print("length of lable ---> ",len(labels))
#outputs and labels are lists that store the model's predicted output and ground truth label for each image in the test dataset.
if outputs[image_id] == labels[image_id]: #image_id is an index that represents the current image being evaluated
print("Groundtruth for this image: ", pred_label)
# Plot images next to each other
plt.axis('off')
plt.subplot(1,2,1)
#If the model's prediction is correct, it prints the predicted label for the image, and then displays a plot of the image's relevance heatmap (image_relevances) and the image itself.
plt.imshow(image_relevances[:,:,0], cmap="seismic")
plt.subplot(1,2,2)
plt.imshow(inputs[image_id].permute(1,2,0).detach().cpu().numpy())
plt.show()
else:
print("This image is not classified correctly.")
# %%
#Output of the lrp is : The output of LRP is often referred to as a heatmap, which is a visual representation of the input image where the color intensity of each pixel corresponds to its relevance value. These heatmaps can help identify the regions of the input image that the model has focused on to make its decision, providing insights into the inner workings of the model and increasing the transparency and interpretability of the model.