Comments (14)
Hi everyone! The problem comes from the facts that th enumber of channels in the map can be > 3, which is unhandy to plot. Reducing to 1 channel only aims at representing it more easily.
In the next tf-explain release, I'll make sure to split the generation of the 4D tensor from the generation of the visualization, so that anyone can use attribution map the way they want.
from tf-explain.
@matheushent Oh okay... now it is clear...Thank you :)
from tf-explain.
It happened to me and I fixed changing transform_to_normalized_grayscale function.
change:
grayscale_tensor = tf.reduce_sum(tensor, axis=-1)
to:
grayscale_tensor = tf.math.reduce_sum(tensor, axis=-1, keepdims=True)
from tf-explain.
Hi, thanks for your reply @matheushent .
Still no luck. I changed it and I still get the black image...
from tf-explain.
Hi, thanks for your reply @matheushent .
Still no luck. I changed it and I still get the black image...
Try to plot grayscale_integrated_gradients inside explain function. If it works so you found the problem is in grid_display function. Remember grayscale_integrated_gradients is a 4D array (batch_size, height, width, channels).
from tf-explain.
from tf-explain.
Will it work if I remove that function? Because I don’t see any point of it, other transforming it into grayscale.
…
On Mon, 6 Apr 2020 at 4:55 PM, Matheus Tosta @.***> wrote: Hi, thanks for your reply @matheushent https://github.com/matheushent . Still no luck. I changed it and I still get the black image... Try to plot grayscale_integrated_gradients inside explain function. If it works so you found the problem is in grid_display function. Remember grayscale_integrated_gradients is a 4D array (batch_size, height, width, channels). — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <#125 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJLQTTZMOXI6L7QZH57DTTTRLG3Y3ANCNFSM4MA6ZMAQ .
Indeed I don't use that function, so it will. I just return grayscale_integrated_gradients and work with it knowing it is a 4D array. Furthermore, doing that, you can get all pictures without using the grid, so you can work separately on each image.
from tf-explain.
from tf-explain.
When I use the transform_to_normalized_grayscale
function, I get a tensor with all values 0(with shape (1,400,400,1)), but when I just return integrated_gradients
in the get_integrated_gradients
function, I get the gradients(with shape (1,400,400,3)).
I think there's something wrong while changing my image to grayscale. Is there any alternate way to reduce the no of channels?
from tf-explain.
Hi, thanks for your reply @matheushent .
Still no luck. I changed it and I still get the black image...Try to plot grayscale_integrated_gradients inside explain function. If it works so you found the problem is in grid_display function. Remember grayscale_integrated_gradients is a 4D array (batch_size, height, width, channels).
@matheushent What is the use of grid_display function? I went through the code and I get the use of everything except for the grid_display function. I was wondering what is the significance of that function? What if we do not use that function?
from tf-explain.
Hi, thanks for your reply @matheushent .
Still no luck. I changed it and I still get the black image...Try to plot grayscale_integrated_gradients inside explain function. If it works so you found the problem is in grid_display function. Remember grayscale_integrated_gradients is a 4D array (batch_size, height, width, channels).
@matheushent What is the use of grid_display function? I went through the code and I get the use of everything except for the grid_display function. I was wondering what is the significance of that function? What if we do not use that function?
In my understanding, grid_display function only concatenates all images into one. When disabling it you need to change the way things happen on callback. For example, here is the example of what I did in the core:
def explain(self, validation_data, model, class_index, n_steps=10, _grid=True):
"""
Compute Integrated Gradients for a specific class index
Args:
validation_data (Tuple[np.ndarray, Optional[np.ndarray]]): Validation data
to perform the method on. Tuple containing (x, y).
model (tf.keras.Model): tf.keras model to inspect
class_index (int): Index of targeted class
n_steps (int): Number of steps in the path
_grid (bool): Whether display images on grid or separately.
Returns:
np.ndarray: Grid of all the integrated gradients or 4D array (batch_size, height, width, channels)
"""
images, _ = validation_data
interpolated_images = IntegratedGradients.generate_interpolations(
images, n_steps
)
integrated_gradients = IntegratedGradients.get_integrated_gradients(
interpolated_images, model, class_index, n_steps
)
grayscale_integrated_gradients = transform_to_normalized_grayscale(
tf.abs(integrated_gradients)
).numpy()
if _grid:
grid = grid_display(grayscale_integrated_gradients)
return grid
else:
return grayscale_integrated_gradients
Note that doing it and setting _grid as false, grayscale_integrated_gradients will return a 4D array of shape (batch_size, H, W, N), so a error will be raised since here it is:
with self.file_writer.as_default():
tf.summary.image(
"IntegratedGradients", np.expand_dims([grid], axis=-1), step=epoch
)
Note np.expand_dims([grid], axis=-1)
will be a 5D array. So, since you're not using grid_display, you'll need to write:
def on_epoch_end(self, epoch, logs=None):
"""
Draw Integrated Gradients outputs at each epoch end to Tensorboard.
Args:
epoch (int): Epoch index
logs (dict): Additional information on epoch
"""
explainer = IntegratedGradients()
images = explainer.explain(
self.validation_data, self.model, self.class_index, self.n_steps
)
# Using the file writer, log the reshaped image.
with self.file_writer.as_default():
tf.summary.image(
"IntegratedGradients", images, step=epoch
)
Also, I recommend you to change max_outputs parameter of tf.summary.image according your needs since the default is 3.
from tf-explain.
@matheushent So, basically grid_display is required only
- if we use callbacks instead of core i.e. IntegratedGradientsCallback(Callback) instead of IntegratedGradients
grid_display function only concatenates all images into one
- if we give a list of images test_data = ([img1, img2....etc], None)
Is that correct?
from tf-explain.
@matheushent So, basically grid_display is required only
- if we use callbacks instead of core i.e. IntegratedGradientsCallback(Callback) instead of IntegratedGradients
grid_display function only concatenates all images into one
- if we give a list of images test_data = ([img1, img2....etc], None)
Is that correct?
@rao208 Yes, but note you need to pass a list [img1, img2, etc...]
using specifically integrated gradients since here the code stacks the images. Using GradCAM you'll need to stack by yourself for example.
from tf-explain.
This also happened to me for other Gradient based Saliency Map methods.
For my purpose i could fix it by returning the gradients directly without applying the transform_to_normalized_grayscale
function
from tf-explain.
Related Issues (20)
- GradCAM varying on multiple calls HOT 3
- TF-Explain support for multi input model which take image and text as input for classification task HOT 1
- Requesting support for TF2.3.x HOT 4
- Normalization issue gives blank heatmaps HOT 2
- VanillaGradients computes gradients with regards to the likelihood, not the score HOT 5
- possible support for 1D models? HOT 4
- Value Error: Graph disconnected HOT 1
- Images are not padded HOT 1
- add threshold in GRAD-CAM HOT 3
- 5D input for video data
- saved files are .v2 files HOT 4
- Does tf-explain support applying multiple input mode on pretrained tensorflow keras model
- Explain tree data
- can tf-expain explain tf-lite models? HOT 1
- GradCAM opposite visualization HOT 9
- Separate attribution maps generation from visualizations
- Example or any Support for Tensorflow_datasets HOT 1
- Unsupported model architecture for VanillaGradients HOT 1
- Different shapes from running methods of tf.xplain.core HOT 1
- slice index 2 of dimension 1 out of bounds.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tf-explain.