示例#1
0
    def test_restore_image(self):
        f_img = np.array(
            [[[[-2.117904, -2.0665295, -2.0151553, -1.9637811],
               [-1.9124068, -1.8610326, -1.8096583, -1.7582841],
               [-1.7069098, -1.6555356, -1.6041613, -1.5527871],
               [-1.5014127, -1.4500386, -1.3986642, -1.34729],
               [-1.2959157, -1.2445415, -1.1931672, -1.1417929],
               [-1.0904187, -1.0390444, -0.9876701, -0.93629587]],
              [[-2.0182073, -1.9656863, -1.9131653, -1.8606442],
               [-1.8081232, -1.7556022, -1.7030813, -1.6505603],
               [-1.5980393, -1.5455183, -1.4929973, -1.4404761],
               [-1.3879551, -1.3354341, -1.2829131, -1.2303921],
               [-1.1778711, -1.1253501, -1.0728291, -1.0203081],
               [-0.9677871, -0.91526604, -0.86274505, -0.81022406]],
              [[-1.769586, -1.7172984, -1.6650108, -1.6127232],
               [-1.5604357, -1.5081481, -1.4558605, -1.4035729],
               [-1.3512853, -1.2989978, -1.2467102, -1.1944226],
               [-1.142135, -1.0898474, -1.0375599, -0.98527235],
               [-0.93298477, -0.8806972, -0.8284096, -0.77612203],
               [-0.72383446, -0.6715468, -0.61925924, -0.56697166]]]])
        result = restore_image(f_img)
        desired = np.array(range(1 * 6 * 4 * 3), dtype=np.uint8).reshape(
            (1, 6, 4, 3))

        assert_arrays_almost_equal(self, result, desired)
示例#2
0
 def test_shape(self):
     paddle_model = self.set_paddle_model()
     img_path = 'tutorials/assets/catdog.png'
     algo = it.RolloutInterpreter(paddle_model, device='cpu')
     exp = algo.interpret(img_path, resize_to=256, crop_to=224, visual=False)
     result = np.array([*exp.shape])
     assert_arrays_almost_equal(self, result, np.array([1, 14, 14]))
示例#3
0
    def test_crop_image(self):
        img = np.array(range(6 * 4 * 3), dtype=np.float32).reshape((6, 4, 3))
        result = crop_image(img, 2)
        desired = np.array([[[27., 28., 29.], [30., 31., 32.]],
                            [[39., 40., 41.], [42., 43., 44.]]])

        assert_arrays_almost_equal(self, result, desired)
    def test_shape(self):
        paddle_model = mobilenet_v2(pretrained=True)
        img_path = 'imgs/catdog.jpg'
        algo = it.GradShapCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, 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]))    
示例#5
0
 def test_sp_weights_to_image_explanation(self):
     np.random.seed(42)
     img = np.random.randint(0, 255, size=(64, 64, 3), dtype=np.uint8)
     sp_weights = {0: [(0, 1.0)]}
     exp = sp_weights_to_image_explanation(img, sp_weights)
     result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
     desired = np.array([0., 0., 0., 0.])
     assert_arrays_almost_equal(self, result, desired)
示例#6
0
    def test_shape(self):
        paddle_model = resnet50(pretrained=True)
        img_path = 'imgs/catdog.jpg'
        algo = it.LRPCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, resize_to=64, crop_to=64, visual=False)
        result = np.array([*exp.shape])

        assert_arrays_almost_equal(self, result, np.array([1, 1, 64, 64]))
示例#7
0
    def test_resize_image(self):
        img = np.array(range(6 * 4 * 3), dtype=np.float32).reshape((6, 4, 3))
        result = resize_image(img, 2)
        desired = np.array([[[7.5, 8.5, 9.5], [13.5, 14.5, 15.5]],
                            [[31.5, 32.5, 33.5], [37.5, 38.5, 39.5]],
                            [[55.5, 56.5, 57.5], [61.5, 62.5, 63.5]]])

        assert_arrays_almost_equal(self, result, desired)
示例#8
0
    def test_cv_multiple_inputs(self):
        paddle_model = mobilenet_v2(pretrained=True)
        img_path = ['imgs/catdog.jpg', 'imgs/catdog.jpg']
        algo = it.IntGradCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, steps=3, num_random_trials=2, resize_to=256, crop_to=224, visual=False)
        result = np.array([*exp.shape])

        assert_arrays_almost_equal(self, result, np.array([2, 3, 224, 224]))
示例#9
0
    def test_shape_and_algo(self):

        from interpretdl.common.file_utils import download_and_decompress
        download_and_decompress(
            "https://github.com/PaddlePaddle/InterpretDL/files/8837286/glime_avg_normlime_global_weights.zip"
        )

        paddle_model = mobilenet_v2(pretrained=True)
        algo = it.GLIMECVInterpreter(paddle_model)
        algo.set_global_weights(algo)
        algo.set_global_weights("imagenet_global_weights_avg.npy")

        np.random.seed(42)
        img_path = np.random.randint(0,
                                     255,
                                     size=(1, 64, 64, 3),
                                     dtype=np.uint8)
        exp = algo.interpret(img_path,
                             prior_method='ridge',
                             num_samples=50,
                             batch_size=50,
                             resize_to=64,
                             crop_to=64,
                             visual=False)

        algo.compute_global_weights('normlime', [algo.lime_results])

        k = list(exp.keys())[0]
        result = np.zeros(len(exp[k]))
        for sp_id, v in exp[k]:
            result[sp_id] = v

        result = np.array([k, *result])

        desired = np.array([
            4.90000000e+02, 5.57969743e-02, 1.55487054e-02, 3.55165317e-02,
            1.40118726e-02, 6.44251208e-02, 3.98409511e-03, -4.13299968e-03,
            -3.18464635e-02, -2.44578178e-02, 1.69825564e-02, 1.46489598e-03,
            5.90007685e-02, 2.48074096e-02, -7.30806851e-02, -4.49090874e-02,
            1.59183040e-02, -2.56270650e-02, 9.35910465e-02, 3.60380997e-02,
            1.34488334e-01, 6.50497344e-02, 1.77734308e-02, 3.19729481e-02,
            4.52839121e-02, 9.33561008e-02, 1.15080587e-02, 1.38845717e-02,
            9.43465624e-02, 2.92089127e-02, 3.20564642e-02, -1.74206462e-03,
            8.34846462e-02, 2.95507758e-02, 3.25349795e-02, -1.97837795e-03,
            1.27033323e-01, -9.81423012e-03, 3.89915593e-02, 2.96282821e-02,
            7.63927405e-02, 1.74757999e-02, 9.21592761e-02, 3.58518795e-02,
            3.37004215e-02, 6.16672433e-02, 7.55628559e-02, 5.53482498e-02,
            7.44542520e-03, 1.64932902e-02, 1.10531985e-02, 8.53582658e-02,
            6.09448481e-02, -2.51819750e-03, 7.52857977e-02, -2.16946058e-02,
            -3.11996488e-02, -7.93963945e-02, 6.08324498e-02, 4.14586328e-02,
            1.84622894e-02, 2.46146574e-02, -1.73424450e-02, -7.64635222e-03
        ])

        assert_arrays_almost_equal(self, result, desired)

        os.remove('glime_avg_normlime_global_weights.zip')
        os.remove('imagenet_global_weights_avg.npy')
        os.remove('imagenet_global_weights_normlime.npy')
示例#10
0
    def test_grayscale(self):
        img = np.array(range(5 * 5), dtype=np.float32).reshape((5, 5)) / 25.0
        result = _grayscale(img)
        desired = np.array([[0, 10, 21, 32, 42], [53, 64, 75, 85, 96],
                            [107, 118, 128, 139,
                             150], [160, 171, 182, 193, 203],
                            [214, 225, 236, 246, 255]])

        assert_arrays_almost_equal(self, result, desired)
示例#11
0
    def test_overlay_heatmap(self):
        img = np.array(range(3 * 3 * 3), dtype=np.uint8).reshape((3, 3, 3))
        exp = np.array(range(3 * 3), dtype=np.float32).reshape((3, 3)) / 9.0
        result = overlay_heatmap(img, exp)
        desired = np.array([[[0, 0, 52], [1, 2, 103], [3, 53, 106]],
                            [[5, 106, 108], [57, 109, 60], [110, 111, 10]],
                            [[112, 62, 12], [114, 13, 13], [65, 15, 15]]])

        assert_arrays_almost_equal(self, result, desired)
示例#12
0
    def test_overlay_threshold(self):
        img = np.array(range(3 * 3 * 3), dtype=np.uint8).reshape((3, 3, 3))
        exp = np.array(range(3 * 3), dtype=np.float32).reshape((3, 3)) / 9.0
        result = overlay_threshold(img, exp)
        desired = np.array([[[0, 0, 0], [1, 18, 2], [2, 36, 3]],
                            [[3, 55, 4], [4, 73, 5], [6, 91, 6]],
                            [[7, 109, 8], [8, 127, 9], [9, 145, 10]]])

        assert_arrays_almost_equal(self, result, desired)
示例#13
0
    def test_overlay_grayscale(self):
        img = np.array(range(3 * 3 * 3), dtype=np.uint8).reshape((3, 3, 3))
        exp = np.array(range(3 * 3), dtype=np.float32).reshape((3, 3)) / 9.0
        result = overlay_grayscale(img, exp)
        desired = np.array([[[0, 0, 1], [1, 15, 3], [3, 29, 4]],
                            [[5, 44, 6], [7, 59, 8], [9, 73, 10]],
                            [[10, 88, 12], [12, 103, 13], [14, 117, 15]]])

        assert_arrays_almost_equal(self, result, desired)
示例#14
0
 def test_algo_2(self):
     paddle_model = mobilenet_v2(pretrained=True)
     np.random.seed(42)
     algo = it.IntGradNLPInterpreter(paddle_model, device='cpu')
     data = np.random.random(size=(1, 3, 64, 64)).astype(np.float32)
     exp = algo.interpret(data, steps=3, embedding_name='features.18.2', return_pred=False)
     result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
     desired = np.array([ 4.4516042e-07,  4.2583229e-06, -2.4716886e-05,  8.8872534e-05])
     assert_arrays_almost_equal(self, result, desired)
    def test_cv_multiple_inputs(self):
        np.random.seed(42)
        paddle_model = mobilenet_v2(pretrained=True)

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

        assert_arrays_almost_equal(self, result, np.array([2, 3, 224, 224]))
示例#16
0
    def test_algo(self):
        paddle_model = self.set_paddle_model()

        np.random.seed(42)
        img_path = np.random.randint(0, 255, size=(1, 224, 224, 3), dtype=np.uint8)
        algo = it.RolloutInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([0.00437814, 0.00064302, 0.00304579, 0.00634555])

        assert_arrays_almost_equal(self, result, desired)
示例#17
0
    def test_algo_random(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.IntGradCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, steps=3, num_random_trials=2, baselines='random', visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([ 4.9712045e-05,  2.4339566e-03, -1.9965716e-02,  2.3791827e-02])

        assert_arrays_almost_equal(self, result, desired)
    def test_algo(self):
        np.random.seed(42)
        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.GradShapCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([ 3.4148130e-07,  2.6306865e-05, -2.3330076e-04,  3.4065323e-04])

        assert_arrays_almost_equal(self, result, desired)
示例#19
0
 def test_shape(self):
     paddle_model = resnet50(pretrained=True)
     img_path = 'imgs/catdog.jpg'
     algo = it.OcclusionInterpreter(paddle_model, device='cpu')
     exp = algo.interpret(img_path,
                          sliding_window_shapes=(1, 32, 32),
                          strides=(1, 32, 32),
                          resize_to=64,
                          crop_to=64,
                          visual=False)
     result = np.array([*exp.shape])
     assert_arrays_almost_equal(self, result, np.array([1, 3, 64, 64]))
示例#20
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.IntGradCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, steps=3, num_random_trials=2, visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        print(repr(result))
        desired = np.array([ 4.1104166e-05,  2.1593589e-03, -1.8412363e-02,  2.2863150e-02])

        assert_arrays_almost_equal(self, result, desired)
示例#21
0
    def test_heatmap(self):
        exp = np.array(range(3 * 3), dtype=np.float32).reshape((3, 3)) / 9.0
        result = _heatmap(exp, resize_shape=(4, 4))
        desired = np.array([[[0, 0, 128], [0, 0, 204], [0, 44, 255],
                             [0, 124, 255]],
                            [[0, 108, 255], [0, 188, 255], [30, 255, 226],
                             [110, 255, 146]],
                            [[142, 255, 114], [222, 255, 34], [255, 192, 0],
                             [255, 112, 0]],
                            [[255, 128, 0], [255, 48, 0], [208, 0, 0],
                             [128, 0, 0]]])

        assert_arrays_almost_equal(self, result, desired)
示例#22
0
    def test_cv_multiple_inputs(self):
        paddle_model = self.set_paddle_model()

        img_path = [
            'tutorials/assets/catdog.png', 'tutorials/assets/catdog.png'
        ]
        algo = it.TAMInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path,
                             steps=2,
                             resize_to=256,
                             crop_to=224,
                             visual=False)
        result = np.array([*exp.shape])
        assert_arrays_almost_equal(self, result, np.array([2, 14, 14]))
示例#23
0
    def test_save(self):
        import matplotlib
        matplotlib.use('agg')  # non-GUI, for skipping.
        paddle_model = self.set_paddle_model()

        np.random.seed(42)
        img_path = np.random.randint(0, 255, size=(1, 224, 224, 3), dtype=np.uint8)
        algo = it.RolloutInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, visual=True, save_path='tmp.jpg')
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([0.00437814, 0.00064302, 0.00304579, 0.00634555])

        assert_arrays_almost_equal(self, result, desired)
        os.remove('tmp.jpg')
示例#24
0
    def test_cv_multiple_inputs(self):
        paddle_model = mobilenet_v2(pretrained=True)

        img_path = ['imgs/catdog.jpg', 'imgs/catdog.jpg']
        algo = it.GradCAMInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path,
                             'features.18.2',
                             resize_to=256,
                             crop_to=224,
                             visual=False)
        result = np.array([*exp.shape])
        desired = np.array([2, 7, 7])

        assert_arrays_almost_equal(self, result, desired)
示例#25
0
    def test_algo_layer(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.GradCAMInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, 'features.16.conv.3', visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array(
            [1.07594451e-03, 4.04298247e-04, 5.23229188e-04, 1.60590897e-03])

        assert_arrays_almost_equal(self, result, desired)
示例#26
0
    def test_algo(self):
        paddle_model = resnet50(pretrained=True)

        np.random.seed(42)
        img_path = np.random.randint(0,
                                     255,
                                     size=(1, 64, 64, 3),
                                     dtype=np.uint8)
        algo = it.LRPCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([0.00397945, 0.00368139, 0.00014466, 0.03765493])

        assert_arrays_almost_equal(self, result, desired)
示例#27
0
    def test_shape_v2(self):
        paddle_model = mobilenet_v2(pretrained=True)
        img_path = 'imgs/catdog.jpg'
        algo = it.SmoothGradInterpreterV2(paddle_model, device='cpu')
        exp = algo.interpret(img_path,
                             n_samples=2,
                             resize_to=256,
                             crop_to=224,
                             visual=False,
                             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')
示例#28
0
    def test_algo(self):
        paddle_model = self.set_paddle_model()

        np.random.seed(42)
        img_path = np.random.randint(0,
                                     255,
                                     size=(1, 224, 224, 3),
                                     dtype=np.uint8)
        algo = it.TAMInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, steps=2, visual=False)
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([0.00122923, 0.00068821, 0.00013711, 0.00403947])

        assert_arrays_almost_equal(self, result, desired)
    def test_save(self):
        import matplotlib
        matplotlib.use('agg')  # non-GUI, for skipping.

        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.GradShapCVInterpreter(paddle_model, device='cpu')
        exp = algo.interpret(img_path, visual=True, save_path='tmp.jpg')
        result = np.array([exp.mean(), exp.std(), exp.min(), exp.max()])
        desired = np.array([ 3.4148130e-07,  2.6306865e-05, -2.3330076e-04,  3.4065323e-04])

        assert_arrays_almost_equal(self, result, desired)
        os.remove('tmp.jpg')
示例#30
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)