I’m encountering error
AttributeError (AttributeError: 'Sequential' object has no attribute 'rule')
when attempting to apply the LRP method to a VGG-16 model for score calculation using Captum library.
Here are the steps I’m following:
I’ve loaded the CIFAR-10 dataset along with the VGG-16 model.
import torch
from torchvision import models, transforms, datasets
from captum.attr import LayerLRP
import numpy as np
# Load CIFAR-10 dataset
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
# Load the pre-trained VGG-16 model
model = models.vgg16(pretrained=True)
model.eval()
I’m attempting to compute the LRP scores using the following approach:
# Initialize Captum's LayerLRP for VGG-16
layer_lrp = LayerLRP(model, model.features)
# Compute LRP scores for the test data
def get_lrp_scores(data_loader):
lrp_scores = []
for inputs, labels in data_loader:
inputs = inputs.requires_grad_()
attribution = layer_lrp.attribute(inputs, target=labels)
lrp_scores.append(attribution.detach().numpy())
return np.concatenate(lrp_scores, axis=0)
lrp_scores = get_lrp_scores(testloader)
print("Shape of LRP scores:", lrp_scores.shape)
The problem:
<ipython-input-33-fbab3259063c> in get_lrp_scores(data_loader)
25 for inputs, labels in data_loader:
26 inputs = inputs.requires_grad_()
---> 27 attribution = layer_lrp.attribute(inputs, target=labels)
28 lrp_scores.append(attribution.detach().numpy())
29 return np.concatenate(lrp_scores, axis=0)
/usr/local/lib/python3.10/dist-packages/captum/attr/_core/layer/layer_lrp.py in attribute(self, inputs, target, additional_forward_args, return_convergence_delta, attribute_to_layer_input, verbose)
231 self._forward_fn_wrapper, inputs, target, additional_forward_args
232 )
--> 233 relevances = self._get_output_relevance(output)
234 finally:
235 self._restore_model()
/usr/local/lib/python3.10/dist-packages/captum/attr/_core/layer/layer_lrp.py in _get_output_relevance(self, output)
280 return relevances
281 else:
--> 282 return self._get_single_output_relevance(self.layer, output)
283
284 @staticmethod
/usr/local/lib/python3.10/dist-packages/captum/attr/_core/layer/layer_lrp.py in _get_single_output_relevance(self, layer, output)
256 normalized_relevances = layer.rule.relevance_input
257 else:
--> 258 normalized_relevances = layer.rule.relevance_output
259 key_list = _sort_key_list(list(normalized_relevances.keys()), self.device_ids)
260 normalized_relevances = _reduce_list(
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
1707 if name in modules:
1708 return modules[name]
-> 1709 raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
1710
1711 def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:
AttributeError: 'Sequential' object has no attribute 'rule'
Could you please provide guidance on resolving this issue?
New contributor
Dev Adr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.