-
Notifications
You must be signed in to change notification settings - Fork 50
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
Opti-CAM Attribution Method #164
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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. |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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()).
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.