Esempio n. 1
0
    def test_channels(self):
        x = np.arange(9).reshape(1, 1, 3, 3)
        preprocess = SpatialSmoothing(channel_index=1)
        smooth_x = preprocess(x)

        new_x = np.arange(9).reshape(1, 3, 3, 1)
        preprocess = SpatialSmoothing()
        new_smooth_x = preprocess(new_x)

        self.assertTrue((smooth_x[0, 0] == new_smooth_x[0, :, :, 0]).all())
    def test_channels(self):
        x = np.arange(9).reshape(1, 1, 3, 3)
        preprocess = SpatialSmoothing(channel_index=1)
        x_smooth, _ = preprocess(x)

        x_new = np.arange(9).reshape(1, 3, 3, 1)
        preprocess = SpatialSmoothing()
        x_new_smooth, _ = preprocess(x_new)

        self.assertTrue((x_smooth[0, 0] == x_new_smooth[0, :, :, 0]).all())
    def test_failure(self):
        x = np.arange(10).reshape(5, 2)
        preprocess = SpatialSmoothing(channel_index=1)
        with self.assertRaises(ValueError) as context:
            preprocess(x)

        self.assertIn('Feature vectors detected.', str(context.exception))
    def test_ones(self):
        m, n = 10, 2
        x = np.ones((1, m, n, 3))

        # Start to test
        for window_size in range(1, 20):
            preprocess = SpatialSmoothing(window_size=window_size)
            smoothed_x, _ = preprocess(x)
            self.assertTrue((smoothed_x == 1).all())
    def test_ones(self):
        m, n = 10, 2
        x = np.ones((1, m, n, 3))

        # Start to test
        for window_size in range(1, 20):
            with self.subTest("Sliding window size = {}".format(window_size)):
                preprocess = SpatialSmoothing()
                smoothed_x = preprocess(x, window_size)
                self.assertTrue((smoothed_x == 1).all())
Esempio n. 6
0
def defenses(model, X, adv_X, FS=False, SS=False, targeted=False):
    if FS:
        fs = FeatureSqueezing(bit_depth=1)(adv_X, clip_values=(-1, 1))
        succ_rate, _ = evaluate(model, X, fs, targeted=targeted)
        print("\nAfter FS %s success rate: %.2f%%" %
              (targeted, succ_rate * 100))
    if SS:
        ss = SpatialSmoothing(channel_index=1,
                              window_size=20)(adv_X, clip_values=(-1, 1))
        succ_rate, _ = evaluate(model, X, ss, targeted=targeted)
        print("\nAfter SS %s success rate: %.2f%%" %
              (targeted, succ_rate * 100))
    return 0
    def test_fix(self):
        x = np.array([[[[0.1], [0.2], [0.3]], [[0.7], [0.8], [0.9]],
                       [[0.4], [0.5], [0.6]]]]).astype(np.float32)

        # Start to test
        preprocess = SpatialSmoothing(window_size=3)
        x_smooth, _ = preprocess(x)
        self.assertTrue(
            (x_smooth == np.array([[[[0.2], [0.3], [0.3]], [[0.4], [0.5],
                                                            [0.6]],
                                    [[0.5], [0.6],
                                     [0.6]]]]).astype(np.float32)).all())

        preprocess = SpatialSmoothing(window_size=1)
        x_smooth, _ = preprocess(x)
        self.assertTrue((x_smooth == x).all())

        preprocess = SpatialSmoothing(window_size=2)
        x_smooth, _ = preprocess(x)
        self.assertTrue(
            (x_smooth == np.array([[[[0.1], [0.2], [0.3]], [[0.7], [0.7],
                                                            [0.8]],
                                    [[0.7], [0.7],
                                     [0.8]]]]).astype(np.float32)).all())
Esempio n. 8
0
    def test_fix(self):
        x = np.array([[[[1], [2], [3]], [[7], [8], [9]], [[4], [5], [6]]]])

        # Start to test
        preprocess = SpatialSmoothing()
        smooth_x = preprocess(x, window_size=3)
        self.assertTrue((smooth_x == np.array([[[[2], [3], [3]], [[4], [5],
                                                                  [6]],
                                                [[5], [6], [6]]]])).all())

        smooth_x = preprocess(x, window_size=1)
        self.assertTrue((smooth_x == x).all())

        smooth_x = preprocess(x, window_size=2)
        self.assertTrue((smooth_x == np.array([[[[1], [2], [3]], [[7], [7],
                                                                  [8]],
                                                [[7], [7], [8]]]])).all())