Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add split_channels parameter to LayerGradCam.attribute #1086

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions captum/attr/_core/layer/grad_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def attribute(
additional_forward_args: Any = None,
attribute_to_layer_input: bool = False,
relu_attributions: bool = False,
attr_dim_summation: bool = True,
) -> Union[Tensor, Tuple[Tensor, ...]]:
r"""
Args:
Expand Down Expand Up @@ -149,6 +150,10 @@ def attribute(
otherwise, by default, both positive and negative
attributions are returned.
Default: False
attr_dim_summation (bool, optional): Indicates whether to
sum attributions along dimension 1 (usually channel).
The default (True) means to sum along dimension 1.
Default: True

Returns:
*Tensor* or *tuple[Tensor, ...]* of **attributions**:
Expand Down Expand Up @@ -208,10 +213,17 @@ def attribute(
for layer_grad in layer_gradients
)

scaled_acts = tuple(
torch.sum(summed_grad * layer_eval, dim=1, keepdim=True)
for summed_grad, layer_eval in zip(summed_grads, layer_evals)
)
if attr_dim_summation:
scaled_acts = tuple(
torch.sum(summed_grad * layer_eval, dim=1, keepdim=True)
for summed_grad, layer_eval in zip(summed_grads, layer_evals)
)
else:
scaled_acts = tuple(
summed_grad * layer_eval
for summed_grad, layer_eval in zip(summed_grads, layer_evals)
)

if relu_attributions:
scaled_acts = tuple(F.relu(scaled_act) for scaled_act in scaled_acts)
return _format_output(len(scaled_acts) > 1, scaled_acts)
19 changes: 19 additions & 0 deletions tests/attr/layer/test_grad_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def test_simple_input_conv(self) -> None:
net, net.conv1, inp, [[[[11.25, 13.5], [20.25, 22.5]]]]
)

def test_simple_input_conv_split_channels(self) -> None:
net = BasicModel_ConvNet_One_Conv()
inp = torch.arange(16).view(1, 1, 4, 4).float()
expected_result = [
[
[[-3.7500, 3.0000], [23.2500, 30.0000]],
[[15.0000, 10.5000], [-3.0000, -7.5000]],
]
]
self._grad_cam_test_assert(
net,
net.conv1,
inp,
expected_activation=expected_result,
attr_dim_summation=False,
)

def test_simple_input_conv_no_grad(self) -> None:
net = BasicModel_ConvNet_One_Conv()

Expand Down Expand Up @@ -100,6 +117,7 @@ def _grad_cam_test_assert(
additional_input: Any = None,
attribute_to_layer_input: bool = False,
relu_attributions: bool = False,
attr_dim_summation: bool = True,
):
layer_gc = LayerGradCam(model, target_layer)
self.assertFalse(layer_gc.multiplies_by_inputs)
Expand All @@ -109,6 +127,7 @@ def _grad_cam_test_assert(
additional_forward_args=additional_input,
attribute_to_layer_input=attribute_to_layer_input,
relu_attributions=relu_attributions,
attr_dim_summation=attr_dim_summation,
)
assertTensorTuplesAlmostEqual(
self, attributions, expected_activation, delta=0.01
Expand Down