Skip to content

Opti-CAM Attribution Method#164

Open
Agustin-Picard wants to merge 5 commits intomasterfrom
opticam
Open

Opti-CAM Attribution Method#164
Agustin-Picard wants to merge 5 commits intomasterfrom
opticam

Conversation

@Agustin-Picard
Copy link
Member

Opti-CAM Attribution Method

This PR contains the code implementing the Opti-CAM attribution method

The class that implements it follows the WhiteBoxExplainer interface, produces a CAM explanation, and optimizes the weights for each filter.

Some relevant tests are introduced to make sure that the losses, normalization, parameters, and shapes make sense.

@Agustin-Picard Agustin-Picard self-assigned this Sep 10, 2024
@Agustin-Picard Agustin-Picard added the feature-attribution New feature or issue concerning Attribution methods label Sep 10, 2024
@AntoninPoche AntoninPoche self-requested a review September 25, 2024 09:39
Copy link
Collaborator

@AntoninPoche AntoninPoche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job Agustin! The code looks good to me. But there is no documentation, and I left some comments.

Did you make a qualitative test?

If an int is provided it will be interpreted as a layer index.
If a string is provided it will look for the layer name.
n_iters
Number of iterations to optimize the CAM.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some parameters do not appear in the docstring. (loss_type, normalization).

self.loss_fn = _loss_dict[loss_type]

# find the layer to apply opti-cam
if conv_layer is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that this triggered a pylint warning. You add a line comment in the beginning of the __init__ method with: # pylint: disable=duplicate-code. The code was deemed similar to GradCAM.

Images with the explanation overlayed.
"""
alpha = tf.nn.softmax(weights, axis=-1)
weighted_explanation = tf.reshape(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a clearer way to write this. Maybe with tf.einsum or tf.matvec.

), trainable=True, dtype=tf.float32)
optimizer = tf.keras.optimizers.Adam(0.05)
for _ in range(self.n_iters):
with tf.GradientTape(watch_accessed_variables=False) as tape:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explain function lacks comments.

method = OptiCAM(model, -2)
outputs = method.explain(samples, labels)

assert samples.shape[:3] == outputs.shape[:3]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also check that the outputs shape finish by a 1. There is a test for all methods where you should add OptiCAM. (tests/attributions/test_commons.py/test_data_types_shapes()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature-attribution New feature or issue concerning Attribution methods

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants