Exemplo n.º 1
0
    def _cam_image(
        self, norm_image: torch.Tensor, class_idx: Optional[int] = None
    ) -> Dict[str, Union[np.ndarray, Dict[str, np.ndarray]]]:
        """Get CAM for an image.

        Args:
            norm_image (torch.Tensor): Normalized image.
            class_idx (:obj:`int`, optional): Class index for calculating GradCAM.
                If not specified, the class index that makes the highest model
                prediction score will be used.

        Returns:
            Dictionary containing unnormalized image, heatmap and CAM result.
        """
        image = unnormalize(norm_image, self.mean, self.std)  # Unnormalized image
        norm_image_cuda = norm_image.clone().unsqueeze_(0).to(self.device)
        heatmap, result = {}, {}
        for layer, gc in self.gradcam.items():
            mask, _ = gc(norm_image_cuda, class_idx=class_idx)
            cam_heatmap, cam_result = visualize_cam(
                mask,
                image.clone().unsqueeze_(0).to(self.device)
            )
            heatmap[layer], result[layer] = to_numpy(cam_heatmap), to_numpy(cam_result)
        return {
            'image': to_numpy(image),
            'heatmap': heatmap,
            'result': result
        }
Exemplo n.º 2
0
    def unnormalize(self, image, transpose=False):
        """Un-normalize a given image.

        Args:
            image (numpy.ndarray or torch.Tensor): A ndarray
                or tensor. If tensor, it should be in CPU.
            transpose (bool, optional): If True, transposed output will
                be returned. This param is effective only when image is
                a tensor. If tensor, the output will have channel number
                as the last dim. (default: False)
        """
        return unnormalize(image, self.mean, self.std, transpose)
    def unnormalize(self, image, transpose=False, data_type=None):
        """Un-normalize a given image.

        Args:
            image (numpy.ndarray or torch.Tensor): A ndarray
                or tensor. If tensor, it should be in CPU.
            transpose (bool, optional): If True, transposed output will
                be returned. This param is effective only when image is
                a tensor. If tensor, the output will have channel number
                as the last dim. (default: False)
            data_type (str, optional): Type of image. Required only when
                dataset has multiple types of images. (default: None)
        """
        mean = self.mean if data_type is None else self.mean[data_type]
        std = self.std if data_type is None else self.std[data_type]
        return unnormalize(image, mean, std, transpose)
Exemplo n.º 4
0
    def _cam_image(self, norm_image, class_idx=None):
        """Get CAM for an image.

        Args:
            norm_image: Normalized image. Should be of type
                torch.Tensor
        
        Returns:
            Dictionary containing unnormalized image, heatmap and CAM result.
        """
        image = unnormalize(norm_image, self.mean,
                            self.std)  # Unnormalized image
        norm_image_cuda = norm_image.clone().unsqueeze_(0).to(self.device)
        heatmap, result = {}, {}
        for layer, gc in self.gradcam.items():
            mask, _ = gc(norm_image_cuda, class_idx=class_idx)
            cam_heatmap, cam_result = visualize_cam(
                mask,
                image.clone().unsqueeze_(0).to(self.device))
            heatmap[layer], result[layer] = to_numpy(cam_heatmap), to_numpy(
                cam_result)
        return {'image': to_numpy(image), 'heatmap': heatmap, 'result': result}