Ejemplo n.º 1
0
 def test_evaluate_sg(self):
     paddle_model = mobilenet_v2(pretrained=True)
     img_path = 'imgs/catdog.jpg'
     sg = it.SmoothGradInterpreter(paddle_model, device='cpu')
     exp = sg.interpret(img_path,
                        n_samples=5,
                        noise_amount=0.1,
                        visual=False,
                        labels=None,
                        save_path=None)
     evaluator = it.DeletionInsertion(paddle_model, device='cpu')
     r = evaluator.evaluate(img_path, exp)
Ejemplo n.º 2
0
    def test_shape(self):
        paddle_model = mobilenet_v2(pretrained=True)
        img_path = 'imgs/catdog.jpg'
        algo = it.SmoothGradInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path,
                             n_samples=1,
                             resize_to=256,
                             crop_to=224,
                             visual=False)
        result = np.array([*exp.shape])

        assert_arrays_almost_equal(self, result, np.array([1, 3, 224, 224]))
Ejemplo n.º 3
0
    def test_algo(self):
        paddle_model = mobilenet_v2(pretrained=True)

        np.random.seed(42)
        img_path = np.random.randint(0,
                                     255,
                                     size=(1, 64, 64, 3),
                                     dtype=np.uint8)
        algo = it.SmoothGradInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, n_samples=5, visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array(
            [5.1930110e-06, 4.0496187e-03, -2.3638349e-02, 3.2775488e-02])

        assert_arrays_almost_equal(self, result, desired)
Ejemplo n.º 4
0
    def test_visual(self):
        import matplotlib
        matplotlib.use('agg')  # non-GUI, for skipping.

        paddle_model = mobilenet_v2(pretrained=True)
        img_path = 'imgs/catdog.jpg'
        algo = it.SmoothGradInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path,
                             n_samples=1,
                             resize_to=256,
                             crop_to=224,
                             visual=True,
                             save_path='tmp.jpg')
        result = np.array([*exp.shape])

        assert_arrays_almost_equal(self, result, np.array([1, 3, 224, 224]))
        os.remove('tmp.jpg')