MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / SaliencyInferer

Class SaliencyInferer

monai/inferers/inferer.py:697–741  ·  view source on GitHub ↗

SaliencyInferer is inference with activation maps. Args: cam_name: expected CAM method name, should be: "CAM", "GradCAM" or "GradCAMpp". target_layers: name of the model layer to generate the feature map. class_idx: index of the class to be visualized. if None, defa

Source from the content-addressed store, hash-verified

695
696
697class SaliencyInferer(Inferer):
698 """
699 SaliencyInferer is inference with activation maps.
700
701 Args:
702 cam_name: expected CAM method name, should be: "CAM", "GradCAM" or "GradCAMpp".
703 target_layers: name of the model layer to generate the feature map.
704 class_idx: index of the class to be visualized. if None, default to argmax(logits).
705 args: other optional args to be passed to the `__init__` of cam.
706 kwargs: other optional keyword args to be passed to `__init__` of cam.
707
708 """
709
710 def __init__(
711 self, cam_name: str, target_layers: str, class_idx: int | None = None, *args: Any, **kwargs: Any
712 ) -> None:
713 Inferer.__init__(self)
714 if cam_name.lower() not in ("cam", "gradcam", "gradcampp"):
715 raise ValueError("cam_name should be: 'CAM', 'GradCAM' or 'GradCAMpp'.")
716 self.cam_name = cam_name.lower()
717 self.target_layers = target_layers
718 self.class_idx = class_idx
719 self.args = args
720 self.kwargs = kwargs
721
722 def __call__(self, inputs: torch.Tensor, network: nn.Module, *args: Any, **kwargs: Any): # type: ignore
723 """Unified callable function API of Inferers.
724
725 Args:
726 inputs: model input data for inference.
727 network: target model to execute inference.
728 supports callables such as ``lambda x: my_torch_model(x, additional_config)``
729 args: other optional args to be passed to the `__call__` of cam.
730 kwargs: other optional keyword args to be passed to `__call__` of cam.
731
732 """
733 cam: CAM | GradCAM | GradCAMpp
734 if self.cam_name == "cam":
735 cam = CAM(network, self.target_layers, *self.args, **self.kwargs)
736 elif self.cam_name == "gradcam":
737 cam = GradCAM(network, self.target_layers, *self.args, **self.kwargs)
738 else:
739 cam = GradCAMpp(network, self.target_layers, *self.args, **self.kwargs)
740
741 return cam(inputs, self.class_idx, *args, **kwargs)
742
743
744class SliceInferer(SlidingWindowInferer):

Callers 1

test_shapeMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…