Ejemplo n.º 1
0
    def generate(self, orig_image, input_image, target_class):
        """
            Generates and returns multiple saliency maps, based on guided backpropagation.

            Args:
                orig_image: Original resized image.
                input_image: Preprocessed input image.
                target_class: Expected category.
            Returns:
                Colored and grayscale gradients for the guided backpropagation.
                Positive and negative saliency maps.
                Grayscale gradients multiplied with the image itself.
        """
        guided_grads = self.generate_gradients(input_image, target_class)

        color_guided_grads = normalize_gradient(guided_grads)
        grayscale_guided_grads = normalize_gradient(convert_to_grayscale(guided_grads))

        pos_sal, neg_sal = get_positive_negative_saliency(guided_grads)
        pos_sal_grads = normalize_gradient(pos_sal)
        neg_sal_grads = normalize_gradient(neg_sal)

        grad_times_image = guided_grads[0] * input_image.detach().numpy()[0]
        grad_times_image = convert_to_grayscale(grad_times_image)
        grad_times_image = normalize_gradient(grad_times_image)

        return {
            "guided_grads_colored": color_guided_grads,
            "guided_grads_grayscale": grayscale_guided_grads,
            "guided_grads_grayscale_grad_times_image": grad_times_image,
            "saliency_maps_positive": pos_sal_grads,
            "saliency_maps_negative": neg_sal_grads,
        }
Ejemplo n.º 2
0
    def generate(self, orig_image, input_image, target_class):
        """
            Generates and returns multiple saliency maps, based on vanilla backpropagation.

            Args:
                orig_image: Original resized image.
                input_image: Preprocessed input image.
                target_class: Expected category.
            Returns:
                Colored and grayscale gradients for the vanilla backpropagation.
                Grayscale gradients multiplied with the image itself.
        """
        vanilla_grads = self.generate_gradients(input_image, target_class)

        color_vanilla_bp = normalize_gradient(vanilla_grads)

        grayscale_vanilla_bp = convert_to_grayscale(vanilla_grads)
        grayscale_vanilla_bp = normalize_gradient(grayscale_vanilla_bp)

        grad_times_image = vanilla_grads[0] * input_image.detach().numpy()[0]
        grad_times_image = convert_to_grayscale(grad_times_image)
        grad_times_image = normalize_gradient(grad_times_image)

        return {
            "vanilla_colored_backpropagation": color_vanilla_bp,
            "vanilla_grayscale_backpropagation": grayscale_vanilla_bp,
            "vanilla_grayscale_grad_times_image": grad_times_image,
        }
    def generate(self, orig_image, input_image, target_class):
        """
            Generates heatmaps using the integrated gradient method.

            Args:
                orig_image: The original image.
                input_image: Preprocessed input image.
                target_class: Expected category.
            Returns:
                The heatmaps.
        """
        integrated_grads = self.generate_integrated_gradients(
            input_image, target_class, 5)
        grayscale_integrated_grads = normalize_gradient(
            convert_to_grayscale(integrated_grads))

        grad_times_image = integrated_grads[0] * input_image.detach().numpy(
        )[0]
        grad_times_image = convert_to_grayscale(grad_times_image)
        grad_times_image = normalize_gradient(grad_times_image)

        return {
            "integrated_gradients": grayscale_integrated_grads,
            "integrated_gradients_times_image": grad_times_image,
        }
Ejemplo n.º 4
0
    def generate(self, orig_image, input_image, target_class):
        """
            Guided gradcam is just pointwise multiplication of the cam mask and
            the guided backprop mask.

            Args:
                orig_image: Original resized image.
                input_image: Preprocessed input image.
                target_class: Expected category.
            Returns:
                Colored and grayscale gradients for the guided Grad-CAM.
        """
        cam = self.gradcam.generate_cam(input_image, target_class)
        guided_grads = self.gbp.generate_gradients(input_image, target_class)

        cam_gb = np.multiply(cam, guided_grads)

        guided_gradcam = normalize_gradient(cam_gb)
        grayscale_cam_gb = convert_to_grayscale(cam_gb)
        guided_gradcam_grayscale = normalize_gradient(grayscale_cam_gb)

        return {
            "guided_gradcam": guided_gradcam,
            "guided_gradcam_grayscale": guided_gradcam_grayscale,
        }
Ejemplo n.º 5
0
    def generate(self, orig_image, input_image, target_class):
        """
            Generates and returns multiple sensitivy heatmaps, based on SmoothGrad technique.
            Args:
                orig_image: Original resized image.
                input_image: Preprocessed input image.
                target_class: Expected category.
            Returns:
                Colored and grayscale gradients for the SmoothGrad backpropagation.
        """
        param_n = 5
        param_sigma_multiplier = 4
        smooth_grad = self.generate_smooth_grad(
            input_image, target_class, param_n, param_sigma_multiplier
        )

        color_smooth_grad = normalize_gradient(smooth_grad)

        grayscale_smooth_grad = convert_to_grayscale(smooth_grad)
        grayscale_smooth_grad = normalize_gradient(grayscale_smooth_grad)

        return {
            "smooth_grad_colored": color_smooth_grad,
            "smooth_grad_grayscale": grayscale_smooth_grad,
        }