コード例 #1
0
ファイル: test_occlusion.py プロジェクト: evazhang612/captum
    def test_improper_stride(self) -> None:
        net = BasicModel_ConvNet_One_Conv()
        inp = torch.arange(16, dtype=torch.float).view(1, 1, 4, 4)
        occ = Occlusion(net)
        # Check error when too few stride dimensions
        with self.assertRaises(AssertionError):
            _ = occ.attribute(inp,
                              sliding_window_shapes=(1, 2, 2),
                              strides=(1, 2),
                              target=0)

        # Check error when too many stride dimensions
        with self.assertRaises(AssertionError):
            _ = occ.attribute(
                (inp, inp),
                sliding_window_shapes=((1, 1, 2), (1, 2, 2)),
                strides=((1, 1, 2), (2, 1, 2, 2)),
                target=0,
            )

        # Check error when too many stride tuples
        with self.assertRaises(AssertionError):
            _ = occ.attribute(
                (inp, inp),
                sliding_window_shapes=((1, 1, 2), (1, 2, 2)),
                strides=((1, 1, 2), (1, 2, 2), (1, 2, 2)),
                target=0,
            )
コード例 #2
0
ファイル: test_occlusion.py プロジェクト: evazhang612/captum
 def _occlusion_test_assert(
     self,
     model: Callable,
     test_input: TensorOrTupleOfTensorsGeneric,
     expected_ablation: Union[float, List[float], List[List[float]],
                              Tuple[Union[Tensor, List[float],
                                          List[List[float]]], ...], ],
     sliding_window_shapes: Union[Tuple[int, ...], Tuple[Tuple[int, ...],
                                                         ...]],
     target: TargetType = 0,
     additional_input: Any = None,
     perturbations_per_eval: Tuple[int, ...] = (1, ),
     baselines: BaselineType = None,
     strides: Union[None, int, Tuple[Union[int, Tuple[int, ...]],
                                     ...]] = None,
 ) -> None:
     for batch_size in perturbations_per_eval:
         ablation = Occlusion(model)
         attributions = ablation.attribute(
             test_input,
             sliding_window_shapes=sliding_window_shapes,
             target=target,
             additional_forward_args=additional_input,
             baselines=baselines,
             perturbations_per_eval=batch_size,
             strides=strides,
         )
         if isinstance(expected_ablation, tuple):
             for i in range(len(expected_ablation)):
                 assertTensorAlmostEqual(self, attributions[i],
                                         expected_ablation[i])
         else:
             assertTensorAlmostEqual(self, attributions, expected_ablation)
コード例 #3
0
ファイル: test_occlusion.py プロジェクト: zhangkehua/captum
 def _occlusion_test_assert(
         self,
         model,
         test_input,
         expected_ablation,
         sliding_window_shapes=None,
         target=0,
         additional_input=None,
         ablations_per_eval=(1, ),
         baselines=None,
         strides=None,
 ):
     for batch_size in ablations_per_eval:
         ablation = Occlusion(model)
         attributions = ablation.attribute(
             test_input,
             sliding_window_shapes=sliding_window_shapes,
             target=target,
             additional_forward_args=additional_input,
             baselines=baselines,
             ablations_per_eval=batch_size,
             strides=strides,
         )
         if isinstance(expected_ablation, tuple):
             for i in range(len(expected_ablation)):
                 assertTensorAlmostEqual(self, attributions[i],
                                         expected_ablation[i])
         else:
             assertTensorAlmostEqual(self, attributions, expected_ablation)
コード例 #4
0
 def test_too_large_stride(self) -> None:
     net = BasicModel_ConvNet_One_Conv()
     inp = torch.arange(16, dtype=torch.float).view(1, 1, 4, 4)
     occ = Occlusion(net)
     with self.assertRaises(AssertionError):
         _ = occ.attribute(
             inp, sliding_window_shapes=((1, 1, 2),), strides=2, target=0
         )
     with self.assertRaises(AssertionError):
         _ = occ.attribute(
             (inp, inp),
             sliding_window_shapes=((1, 1, 2), (1, 4, 2)),
             strides=(2, (1, 2, 3)),
             target=0,
         )
     with self.assertRaises(AssertionError):
         _ = occ.attribute(
             inp, sliding_window_shapes=((2, 1, 2),), strides=2, target=0
         )
コード例 #5
0
ファイル: test_occlusion.py プロジェクト: zhangkehua/captum
    def test_improper_window_shape(self):
        net = BasicModel_ConvNet_One_Conv()
        inp = torch.arange(16).view(1, 1, 4, 4).type(torch.FloatTensor)
        occ = Occlusion(net)
        # Check error when too few sliding window dimensions
        with self.assertRaises(AssertionError):
            _ = occ.attribute(inp, sliding_window_shapes=((1, 2), ), target=0)

        # Check error when too many sliding window dimensions
        with self.assertRaises(AssertionError):
            _ = occ.attribute((inp, inp),
                              sliding_window_shapes=((1, 1, 2), (1, 1, 1, 2)),
                              target=0)

        # Check error when too many sliding window tuples
        with self.assertRaises(AssertionError):
            _ = occ.attribute(
                (inp, inp),
                sliding_window_shapes=((1, 1, 2), (1, 1, 2), (1, 1, 2)),
                target=0,
            )