def visualize(self, input_, target_class, guided=False, use_gpu=False, figsize=(16, 4), cmap='viridis', alpha=.5, return_output=False): """Calculates gradients and visualizes the output. A method that combines the backprop operation and visualization. It also returns the gradients, if specified with `return_output=True`. Args: input_ (torch.Tensor): With shape :math:`(N, C, H, W)`. target_class (int, optional, default=None) take_max (bool, optional, default=False): If True, take the maximum gradients across colour channels for each pixel. guided (bool, optional, default=Fakse): If True, perform guided backpropagation. See `Striving for Simplicity: The All Convolutional Net <https://arxiv.org/pdf/1412.6806.pdf>`_. use_gpu (bool, optional, default=False): Use GPU if set to True and `torch.cuda.is_available()`. figsize (tuple, optional, default=(16, 4)): The size of the plot. cmap (str, optional, default='viridis): The color map of the gradients plots. See avaialable color maps `here <https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html>`_. alpha (float, optional, default=.5): The alpha value of the max gradients to be jaxaposed on top of the input image. return_output (bool, optional, default=False): Returns the output(s) of optimization if set to True. Returns: gradients (torch.Tensor): With shape :math:`(C, H, W)`. """ # Calculate gradients gradients = self.calculate_gradients(input_, target_class, guided=guided) max_gradients = self.calculate_gradients(input_, target_class, guided=guided, take_max=True) # Setup subplots subplots = [ # (title, [(image1, cmap, alpha), (image2, cmap, alpha)]) ('Input image', [(format_for_plotting(denormalize(input_)), None, None)]), ('Gradients across RGB channels', [(format_for_plotting(standardize_and_clip(gradients)), None, None)]), ('Max gradients', [(format_for_plotting(standardize_and_clip(max_gradients)), cmap, None)]), ('Overlay', [(format_for_plotting(denormalize(input_)), None, None), (format_for_plotting(standardize_and_clip(max_gradients)), cmap, alpha)]) ] fig = plt.figure(figsize=figsize) for i, (title, images) in enumerate(subplots): ax = fig.add_subplot(1, len(subplots), i + 1) ax.set_axis_off() ax.set_title(title) for image, cmap, alpha in images: ax.imshow(image, cmap=cmap, alpha=alpha) if return_output: return gradients, max_gradients
def test_denormalize_tensor(image): transformed = apply_transforms(image) denormalized = denormalize(transformed) assert denormalized.shape == transformed.shape assert denormalized.min() >= 0.0 and denormalized.max() <= 1.0
model.load_state_dict(torch.load(r'C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Resultater\CNN_b\model1_l5_b.pt')) model.eval() def get_image(image,num): img1 = image[num, :, :, :].unsqueeze(0).requires_grad_(requires_grad=True) return img1 img1 = get_image(images,0) img2 = get_image(images,1) img3 = get_image(images,2) img4 = get_image(images,3) img5 = get_image(images,4) img6 = get_image(images,5) img7 = get_image(images,6) img8 = get_image(images,7) plt.imshow(format_for_plotting(denormalize(img2))) plt.show() def visualize_saliency(model, tensor, k=1, guide=True): backprop = Backprop(model) backprop.visualize(tensor, k, guided=guide) plt.show() def get_image(image,num): img1 = image[num, :, :, :].unsqueeze(0).requires_grad_(requires_grad=True) return img1 start = 0 def multiple_saliency_maps(model, tensors, start, num, k=0, guide = True): backprop = Backprop(model) for i in range(start,start+num): cur_image = tensors[i,:,:,:].unsqueeze(0).requires_grad_(requires_grad=True) backprop.visualize(cur_image, k, guided=guide)