Conversation
| 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.
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.
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.
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.
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.
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
WhiteBoxExplainerinterface, 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.