Ejemplo n.º 1
0
    def explain(self,
                validation_data,
                model,
                class_index,
                num_samples=5,
                noise=1.0):
        """
        Compute SmoothGrad 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
            num_samples (int): Number of noisy samples to generate for each input image
            noise (float): Standard deviation for noise normal distribution

        Returns:
            np.ndarray: Grid of all the smoothed gradients
        """
        images, _ = validation_data

        noisy_images = SmoothGrad.generate_noisy_images(
            images, num_samples, noise)

        smoothed_gradients = SmoothGrad.get_averaged_gradients(
            noisy_images, model, class_index, num_samples)

        grayscale_gradients = transform_to_normalized_grayscale(
            tf.abs(smoothed_gradients)).numpy()

        grid = grid_display(grayscale_gradients)

        return grid
Ejemplo n.º 2
0
def test_should_transform_gradients_to_grayscale():
    gradients = tf.random.uniform((4, 28, 28, 3))

    grayscale_gradients = transform_to_normalized_grayscale(gradients)
    expected_output_shape = (4, 28, 28)

    assert grayscale_gradients.shape == expected_output_shape
Ejemplo n.º 3
0
    def explain(self, validation_data, model, class_index, n_steps=10):
        """
        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

        Returns:
            np.ndarray: Grid of all the integrated gradients
        """
        images, _ = validation_data

        interpolated_images = IntegratedGradients.generate_interpolations(
            np.array(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()

        grid = grid_display(grayscale_integrated_gradients)

        return grid
Ejemplo n.º 4
0
    def explain_score_model(self, validation_data, score_model, class_index):
        """
        Perform gradients backpropagation for a given input

        Args:
            validation_data (Tuple[np.ndarray, Optional[np.ndarray]]): Validation data
                to perform the method on. Tuple containing (x, y).
            score_model (tf.keras.Model): tf.keras model to inspect. The last layer
            should not have any activation function.
            class_index (int): Index of targeted class

        Returns:
            numpy.ndarray: Grid of all the gradients
        """
        images, _ = validation_data

        gradients = self.compute_gradients(images, score_model, class_index)

        grayscale_gradients = transform_to_normalized_grayscale(
            tf.abs(gradients)).numpy()

        grid = grid_display(grayscale_gradients)

        return grid