示例#1
0
    def test_one_feature(self):
        batch_size = 2
        classes = _get_classes()
        dataset = list(
            _labelled_img_data(num_labels=len(classes),
                               num_samples=batch_size))

        # NOTE: using DataLoader to batch the inputs
        # since AttributionVisualizer requires the input to be of size `B x ...`
        data_loader = torch.utils.data.DataLoader(list(dataset),
                                                  batch_size=batch_size,
                                                  shuffle=False,
                                                  num_workers=0)

        visualizer = AttributionVisualizer(
            models=[_get_cnn()],
            classes=classes,
            features=[
                ImageFeature(
                    "Photo",
                    input_transforms=[lambda x: x],
                    baseline_transforms=[lambda x: x * 0],
                )
            ],
            dataset=to_iter(data_loader),
            score_func=None,
        )
        visualizer._config = FilterConfig(attribution_arguments={"n_steps": 2})

        outputs = visualizer.visualize()

        for output in outputs:
            total_contrib = sum(
                abs(f.contribution) for f in output.feature_outputs)
            self.assertAlmostEqual(total_contrib, 1.0, places=6)
示例#2
0
    def test_multi_features(self):
        batch_size = 2
        classes = _get_classes()
        img_dataset = list(
            _labelled_img_data(num_labels=len(classes),
                               num_samples=batch_size))

        misc_feature_size = 2
        dataset = _multi_modal_data(img_dataset=img_dataset,
                                    feature_size=misc_feature_size)
        # NOTE: using DataLoader to batch the inputs since
        # AttributionVisualizer requires the input to be of size `N x ...`
        data_loader = torch.utils.data.DataLoader(list(dataset),
                                                  batch_size=batch_size,
                                                  shuffle=False,
                                                  num_workers=0)

        visualizer = AttributionVisualizer(
            models=[_get_multimodal(input_size=misc_feature_size)],
            classes=classes,
            features=[
                ImageFeature(
                    "Photo",
                    input_transforms=[lambda x: x],
                    baseline_transforms=[lambda x: x * 0],
                ),
                RealFeature(
                    "Random",
                    input_transforms=[lambda x: x],
                    baseline_transforms=[lambda x: x * 0],
                ),
            ],
            dataset=to_iter(data_loader),
            score_func=None,
        )
        visualizer._config = FilterConfig(steps=2)

        outputs = visualizer.visualize()

        for output in outputs:
            total_contrib = sum(
                abs(f.contribution) for f in output.feature_outputs)
            self.assertAlmostEqual(total_contrib, 1.0, places=6)