Git Product home page Git Product logo

Comments (14)

RaphaelMeudec avatar RaphaelMeudec commented on May 17, 2024 3

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.

rao208 avatar rao208 commented on May 17, 2024 2

@matheushent Oh okay... now it is clear...Thank you :)

from tf-explain.

matheushent avatar matheushent commented on May 17, 2024

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.

r0cketr1kky avatar r0cketr1kky commented on May 17, 2024

Hi, thanks for your reply @matheushent .
Still no luck. I changed it and I still get the black image...

from tf-explain.

matheushent avatar matheushent commented on May 17, 2024

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.

r0cketr1kky avatar r0cketr1kky commented on May 17, 2024

from tf-explain.

matheushent avatar matheushent commented on May 17, 2024

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.

r0cketr1kky avatar r0cketr1kky commented on May 17, 2024

from tf-explain.

r0cketr1kky avatar r0cketr1kky commented on May 17, 2024

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.

rao208 avatar rao208 commented on May 17, 2024

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.

matheushent avatar matheushent commented on May 17, 2024

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.

rao208 avatar rao208 commented on May 17, 2024

@matheushent So, basically grid_display is required only

  1. if we use callbacks instead of core i.e. IntegratedGradientsCallback(Callback) instead of IntegratedGradients

grid_display function only concatenates all images into one

  1. if we give a list of images test_data = ([img1, img2....etc], None)

Is that correct?

from tf-explain.

matheushent avatar matheushent commented on May 17, 2024

@matheushent So, basically grid_display is required only

  1. if we use callbacks instead of core i.e. IntegratedGradientsCallback(Callback) instead of IntegratedGradients

grid_display function only concatenates all images into one

  1. 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.

VictorW96 avatar VictorW96 commented on May 17, 2024

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
image

from tf-explain.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.