class_docstr: This class computes the occlusion sensitivity for a model's prediction of a given image. By occlusion sensitivity,
  we mean how the probability of a given prediction changes as the occluded section of an image changes. This can be
  useful to understand why a network is making certain decisions.
  
  As important parts of the image are occluded, the probability of classifying the image correctly will decrease.
  Hence, more negative values imply the corresponding occluded volume was more important in the decision process.
  
  Two ``torch.Tensor`` will be returned by the ``__call__`` method: an occlusion map and an image of the most probable
  class. Both images will be cropped if a bounding box used, but voxel sizes will always match the input.
  
  The occlusion map shows the inference probabilities when the corresponding part of the image is occluded. Hence,
  more -ve values imply that region was important in the decision process. The map will have shape ``BCHW(D)N``,
  where ``N`` is the number of classes to be inferred by the network. Hence, the occlusion for class ``i`` can
  be seen with ``map[...,i]``.
  
  The most probable class is an image of the probable class when the corresponding part of the image is occluded
  (equivalent to ``occ_map.argmax(dim=-1)``).
  
  See: R. R. Selvaraju et al. Grad-CAM: Visual Explanations from Deep Networks via
  Gradient-based Localization. https://doi.org/10.1109/ICCV.2017.74.
  
  Examples:
  
  .. code-block:: python
  
      # densenet 2d
      from monai.networks.nets import DenseNet121
      from monai.visualize import OcclusionSensitivity
      import torch
  
      model_2d = DenseNet121(spatial_dims=2, in_channels=1, out_channels=3)
      occ_sens = OcclusionSensitivity(nn_module=model_2d)
      occ_map, most_probable_class = occ_sens(x=torch.rand((1, 1, 48, 64)), b_box=[2, 40, 1, 62])
  
      # densenet 3d
      from monai.networks.nets import DenseNet
      from monai.visualize import OcclusionSensitivity
  
      model_3d = DenseNet(spatial_dims=3, in_channels=1, out_channels=3, init_features=2, growth_rate=2, block_config=(6,))
      occ_sens = OcclusionSensitivity(nn_module=model_3d, n_batch=10)
      occ_map, most_probable_class = occ_sens(torch.rand(1, 1, 6, 6, 6), b_box=[1, 3, -1, -1, -1, -1])
  
  See Also:
  
      - :py:class:`monai.visualize.occlusion_sensitivity.OcclusionSensitivity.`
class_docstr_tok: ["This","class","computes","the","occlusion","sensitivity","for","a","model","'s","prediction","of","a","given","image",".","By","occlusion","sensitivity",",","we","mean","how","the","probability","of","a","given","prediction","changes","as","the","occluded","section","of","an","image","changes",".","This","can","be","useful","to","understand","why","a","network","is","making","certain","decisions",".","As","important","parts","of","the","image","are","occluded",",","the","probability","of","classifying","the","image","correctly","will","decrease",".","Hence",",","more","negative","values","imply","the","corresponding","occluded","volume","was","more","important","in","the","decision","process",".","Two","``","torch.Tensor","``","will","be","returned","by","the","``","__call__","``","method",":","an","occlusion","map","and","an","image","of","the","most","probable","class",".","Both","images","will","be","cropped","if","a","bounding","box","used",",","but","voxel","sizes","will","always","match","the","input",".","The","occlusion","map","shows","the","inference","probabilities","when","the","corresponding","part","of","the","image","is","occluded",".","Hence",",","more","-ve","values","imply","that","region","was","important","in","the","decision","process",".","The","map","will","have","shape","``","BCHW","(","D",")","N","``",",","where","``","N","``","is","the","number","of","classes","to","be","inferred","by","the","network",".","Hence",",","the","occlusion","for","class","``","i","``","can","be","seen","with","``","map","[","...",",","i","]","``",".","The","most","probable","class","is","an","image","of","the","probable","class","when","the","corresponding","part","of","the","image","is","occluded","(","equivalent","to","``","occ_map.argmax","(","dim=-1",")","``",")",".","See",":","R.","R.","Selvaraju","et","al",".","Grad-CAM",":","Visual","Explanations","from","Deep","Networks","via","Gradient-based","Localization",".","https",":","\/\/doi.org\/10.1109\/ICCV.2017.74",".","Examples",":","..","code-block",":",":","python","#","densenet","2d","from","monai.networks.nets","import","DenseNet121","from","monai.visualize","import","OcclusionSensitivity","import","torch","model_2d","=","DenseNet121","(","spatial_dims=2",",","in_channels=1",",","out_channels=3",")","occ_sens","=","OcclusionSensitivity","(","nn_module=model_2d",")","occ_map",",","most_probable_class","=","occ_sens","(","x=torch.rand","(","(","1",",","1",",","48",",","64",")",")",",","b_box=","[","2",",","40",",","1",",","62","]",")","#","densenet","3d","from","monai.networks.nets","import","DenseNet","from","monai.visualize","import","OcclusionSensitivity","model_3d","=","DenseNet","(","spatial_dims=3",",","in_channels=1",",","out_channels=3",",","init_features=2",",","growth_rate=2",",","block_config=","(","6",",",")",")","occ_sens","=","OcclusionSensitivity","(","nn_module=model_3d",",","n_batch=10",")","occ_map",",","most_probable_class","=","occ_sens","(","torch.rand","(","1",",","1",",","6",",","6",",","6",")",",","b_box=","[","1",",","3",",","-1",",","-1",",","-1",",","-1","]",")","See","Also",":","-",":","py",":","class",":","`","monai.visualize.occlusion_sensitivity.OcclusionSensitivity.","`"]
docstr: Occlusion sensitivity constructor.
  
  Args:
      nn_module: Classification model to use for inference
      mask_size: Size of box to be occluded, centred on the central voxel. If a single number
          is given, this is used for all dimensions. If a sequence is given, this is used for each dimension
          individually.
      n_batch: Number of images in a batch for inference.
      verbose: Use progress bar (if ``tqdm`` available).
      mode: what should the occluded region be replaced with? If a float is given, that value will be used
          throughout the occlusion. Else, ``gaussian``, ``mean_img`` and ``mean_patch`` can be supplied:
  
          * ``gaussian``: occluded region is multiplied by 1 - gaussian kernel. In this fashion, the occlusion
            will be 0 at the center and will be unchanged towards the edges, varying smoothly between. When
            gaussian is used, a weighted average will be used to combine overlapping regions. This will be
            done using the gaussian (not 1-gaussian) as occluded regions count more.
          * ``mean_patch``: occluded region will be replaced with the mean of occluded region.
          * ``mean_img``: occluded region will be replaced with the mean of the whole image.
  
      overlap: overlap between inferred regions. Should be in range 0<=x<1.
      activate: if ``True``, do softmax activation if num_channels > 1 else do ``sigmoid``. If ``False``, don't do any
          activation. If ``callable``, use callable on inferred outputs.
docstr_tok: ["Occlusion","sensitivity","constructor",".","Args",":","nn_module",":","Classification","model","to","use","for","inference","mask_size",":","Size","of","box","to","be","occluded",",","centred","on","the","central","voxel",".","If","a","single","number","is","given",",","this","is","used","for","all","dimensions",".","If","a","sequence","is","given",",","this","is","used","for","each","dimension","individually",".","n_batch",":","Number","of","images","in","a","batch","for","inference",".","verbose",":","Use","progress","bar","(","if","``","tqdm","``","available",")",".","mode",":","what","should","the","occluded","region","be","replaced","with","?","If","a","float","is","given",",","that","value","will","be","used","throughout","the","occlusion",".","Else",",","``","gaussian","``",",","``","mean_img","``","and","``","mean_patch","``","can","be","supplied",":","*","``","gaussian","``",":","occluded","region","is","multiplied","by","1","-","gaussian","kernel",".","In","this","fashion",",","the","occlusion","will","be","0","at","the","center","and","will","be","unchanged","towards","the","edges",",","varying","smoothly","between",".","When","gaussian","is","used",",","a","weighted","average","will","be","used","to","combine","overlapping","regions",".","This","will","be","done","using","the","gaussian","(","not","1-gaussian",")","as","occluded","regions","count","more",".","*","``","mean_patch","``",":","occluded","region","will","be","replaced","with","the","mean","of","occluded","region",".","*","``","mean_img","``",":","occluded","region","will","be","replaced","with","the","mean","of","the","whole","image",".","overlap",":","overlap","between","inferred","regions",".","Should","be","in","range","0","<","=x","<","1",".","activate",":","if","``","True","``",",","do","softmax","activation","if","num_channels",">","1","else","do","``","sigmoid","``",".","If","``","False","``",",","do","n't","do","any","activation",".","If","``","callable","``",",","use","callable","on","inferred","outputs","."]
code: def __init__(
      self,
      nn_module: nn.Module,
      mask_size: int | Sequence = 16,
      n_batch: int = 16,
      verbose: bool = True,
      mode: str | float | Callable = "gaussian",
      overlap: float = 0.25,
      activate: bool | Callable = True,
  ) -> None:
      """
      Occlusion sensitivity constructor.
  
      Args:
          nn_module: Classification model to use for inference
          mask_size: Size of box to be occluded, centred on the central voxel. If a single number
              is given, this is used for all dimensions. If a sequence is given, this is used for each dimension
              individually.
          n_batch: Number of images in a batch for inference.
          verbose: Use progress bar (if ``tqdm`` available).
          mode: what should the occluded region be replaced with? If a float is given, that value will be used
              throughout the occlusion. Else, ``gaussian``, ``mean_img`` and ``mean_patch`` can be supplied:
  
              * ``gaussian``: occluded region is multiplied by 1 - gaussian kernel. In this fashion, the occlusion
                will be 0 at the center and will be unchanged towards the edges, varying smoothly between. When
                gaussian is used, a weighted average will be used to combine overlapping regions. This will be
                done using the gaussian (not 1-gaussian) as occluded regions count more.
              * ``mean_patch``: occluded region will be replaced with the mean of occluded region.
              * ``mean_img``: occluded region will be replaced with the mean of the whole image.
  
          overlap: overlap between inferred regions. Should be in range 0<=x<1.
          activate: if ``True``, do softmax activation if num_channels > 1 else do ``sigmoid``. If ``False``, don't do any
              activation. If ``callable``, use callable on inferred outputs.
  
      """
      self.nn_module = nn_module
      self.mask_size = mask_size
      self.n_batch = n_batch
      self.verbose = verbose
      self.overlap = overlap
      self.activate = activate
      # mode
      if isinstance(mode, str) and mode not in (
          "gaussian",
          "mean_patch",
          "mean_img",
      ):
          raise NotImplementedError
      self.mode = mode
  
file_path: MONAI/monai/visualize/occlusion_sensitivity.py
imports: from __future__ import annotations
  from collections.abc import Callable, Mapping, Sequence
  from typing import Any
  import numpy
  import torch
  import torch.nn
  from monai.data.meta_tensor import MetaTensor
  from monai.networks.utils import eval_mode
  from monai.transforms import Compose, GaussianSmooth, Lambda, ScaleIntensity, SpatialCrop
  from monai.utils import ensure_tuple_rep
label_desc: Use image node_id 1 to create a new OcclusionSensitivity object  with example: obj = OcclusionSensitivity(nn_module, mask_size, n_batch, verbose, mode, overlap, activate)