def test_relu(self):
        top = hm.zeros((4, 12, 15, 15))
        bottom = hm.random((4, 12, 15, 15), _range=(-1, 1))

        @compose
        def fn(bottom, top):
            top = ReluForward(bottom)
            return top

        fn(bottom, top)
        top.sync_host()

        expected = np.copy(bottom)
        expected[expected < 0] = 0
        self._check(top, expected)

        top_diff = hm.random(top.shape)
        bottom_diff = hmarray(top.shape)

        @compose
        def fn(top_diff, bottom, bottom_diff):
            bottom_diff = ReluBackward(bottom, top_diff)
            return bottom_diff

        fn(top_diff, bottom, bottom_diff)
        bottom_diff.sync_host()

        expected = np.copy(top_diff)
        expected[bottom < 0] = 0
        self._check(bottom_diff, expected)
Exemplo n.º 2
0
    def test_convolve(self):
        data = hm.random((48, 48), _range=(0, 1))
        filters = hm.random((3, 3), _range=(-1, 1))
        output = hmarray.zeros_like(data)

        @compose
        def fn(data, filters, output):
            output = Convolve2D(data, filters)
            return output

        output = fn(data, filters, output)
        expected = convolve(data, filters)
        output.sync_host()
        # np.testing.assert_array_almost_equal(output[2:-2, 2:-2], expected[2:-2, 2:-2], decimal=4)
        np.testing.assert_array_almost_equal(output, expected, decimal=4)
Exemplo n.º 3
0
    def test_forward(self):
        @compose
        def fn(a, b, c, d):
            d = ConcatForward(a, b, c)
            return d

        a = hm.random((16, 12, 55, 55))
        b = hm.random((16, 12, 55, 55))
        c = hm.random((16, 12, 55, 55))
        d = hm.zeros((16, 36, 55, 55))
        d = fn(a, b, c, d)
        d.sync_host()
        np.testing.assert_array_almost_equal(d[:16, 0:12, ...], a)
        np.testing.assert_array_almost_equal(d[:16, 12:24, ...], b)
        np.testing.assert_array_almost_equal(d[:16, 24:36, ...], c)
    def test_convolve(self):
        data = hm.random((48, 48), _range=(0, 1))
        filters = hm.random((3, 3), _range=(-1, 1))
        output = hmarray.zeros_like(data)

        @compose
        def fn(data, filters, output):
            output = Convolve2D(data, filters)
            return output

        output = fn(data, filters, output)
        expected = convolve(data, filters)
        output.sync_host()
        # np.testing.assert_array_almost_equal(output[2:-2, 2:-2], expected[2:-2, 2:-2], decimal=4)
        np.testing.assert_array_almost_equal(output, expected, decimal=4)
Exemplo n.º 5
0
    def test_forward(self):
        @compose
        def fn(bottom, label, top):
            top = SoftmaxForward(bottom, label)
            return top

        bottom = hm.random((6, 13, 1), _range=(-5, 5))
        label = hmarray((6, ))
        for i in range(6):
            label = i % 6
        top = hmarray((6, 13, 1))
        fn(bottom, label, top)

        top.sync_host()
        for i in range(6):
            sum = 0
            for c in range(13):
                sum += top[i, c, 0]
            self.assertTrue(sum > .999)
            self.assertTrue(sum < 1.001)
            scale = 0
            for c in range(13):
                scale += exp(bottom[i, c, 0])
            for c in range(13):
                self.assertGreaterEqual(top[i, c, 0] + 1e-4,
                                        exp(bottom[i, c, 0]) / scale)
                self.assertLessEqual(top[i, c, 0] - 1e-4,
                                     exp(bottom[i, c, 0]) / scale)
Exemplo n.º 6
0
def get_data():
    # data = np.asarray([
    #     transformer.preprocess('data', im),
    # ]).view(hmarray)
    data = hm.random((128, 3, 227, 227), _range=(0, 255))

    # data *= hmarray.random((5, 3, 227, 227), _range=(0, 2))
    # data -= hmarray.random((5, 3, 227, 227), _range=(-20, +20))
    data.sync_ocl()
    return data
def get_data():
    # data = np.asarray([
    #     transformer.preprocess('data', im),
    # ]).view(hmarray)
    data = hm.random((128, 3, 227, 227), _range=(0, 255))

    # data *= hmarray.random((5, 3, 227, 227), _range=(0, 2))
    # data -= hmarray.random((5, 3, 227, 227), _range=(-20, +20))
    data.sync_ocl()
    return data
    def test_conv_forward(self):
        @compose
        def fn(a, weights, out, bias):
            out = ConvForward(a, weights, bias, kernel_size=(11, 11),
                              padding=(0, 0), stride=(4, 4))
            return out

        a = hm.random((3, 3, 27, 27), _range=(0, 255))
        weights = hm.random((12, 363), _range=(-.2, .2))
        out = hm.zeros((3, 12, 5, 5))
        bias = hm.random((12, ))
        fn(a, weights, out, bias)

        weights = weights.reshape(12, 3, 11, 11)
        expected = np.zeros((3, 12, 5, 5), np.float32)
        reference_conv(a, weights, bias, expected, (4, 4), (0, 0))
        out.sync_host()
        np.testing.assert_array_almost_equal(out, expected,
                                             decimal=1)
    def test_pool(self):
        shape = (3, 16, 24, 24)
        a = hm.random(shape, _range=(0, 255))
        actual_mask = hmarray((3, 16, 12, 12))
        actual = hmarray((3, 16, 12, 12))
        expected_mask = hmarray((3, 16, 12, 12))
        expected = hmarray((3, 16, 12, 12))
        expected.fill(float('-inf'))

        @compose
        def fn(bottom, mask, top):
            top, mask = PoolForward(bottom,
                                    kernel_size=(2, 2),
                                    padding=(0, 0),
                                    stride=(2, 2))
            return top, mask

        fn(a, actual_mask, actual)
        actual.sync_host()
        actual_mask.sync_host()
        reference_pool(a, expected, expected_mask, (2, 2), (2, 2), (0, 0))
        self._check(actual, expected)
        self._check(actual_mask, expected_mask)
        bottom_diff = hm.zeros(shape)
        expected_bottom_diff = hm.zeros(shape)
        mask = actual_mask
        top_diff = hm.random((3, 16, 12, 12))

        @compose
        def fn(top_diff, mask, bottom_diff):
            bottom_diff = PoolBackward(top_diff,
                                       mask,
                                       kernel_size=(2, 2),
                                       padding=(0, 0),
                                       stride=(2, 2))
            return bottom_diff

        fn(top_diff, mask, bottom_diff)
        bottom_diff.sync_host()
        reference_pool_backward(top_diff, mask, expected_bottom_diff, (2, 2),
                                (2, 2), (0, 0))
        self._check(bottom_diff, expected_bottom_diff)
Exemplo n.º 10
0
    def test_conv_backward(self):
        # TODO: Check bias diff

        @compose
        def fn(top_diff, weights, bottom, bottom_diff, weights_diff,
               bias_diff):
            bottom_diff, weights_diff, bias_diff = \
                ConvBackward(bottom, top_diff, weights,
                             kernel_size=(11, 11), padding=(0, 0),
                             stride=(4, 4))
            return bottom_diff, weights_diff, bias_diff

        top_diff = hm.random((3, 12, 25), _range=(0, 5))
        bottom = hm.random((3, 3, 27, 27), _range=(0, 255))
        weights = hm.random((12, 363), _range=(-.2, .2))

        weights_diff = hm.zeros((12, 363))
        bias_diff = hm.zeros((12, ))
        bottom_diff = hm.zeros((3, 3, 27, 27))

        fn(top_diff, weights, bottom, bottom_diff, weights_diff, bias_diff)

        expected_weights_diff = np.zeros_like(weights)
        expected_bottom_diff = np.zeros_like(bottom_diff)

        for i in range(top_diff.shape[0]):
            col_data = reference_im2col(bottom[i], (11, 11), (4, 4), (0, 0))
            expected_weights_diff += top_diff[i].dot(col_data.T)
            expected_bottom_diff[i] = reference_col2im(
                weights.T.dot(top_diff[i]), (11, 11), (4, 4), (0, 0),
                expected_bottom_diff[i].shape)
        weights_diff.sync_host()
        np.testing.assert_array_almost_equal(weights_diff,
                                             expected_weights_diff,
                                             decimal=2)
        bottom_diff.sync_host()
        np.testing.assert_array_almost_equal(bottom_diff,
                                             expected_bottom_diff,
                                             decimal=2)
Exemplo n.º 11
0
    def test_pool(self):
        shape = (3, 16, 24, 24)
        a = hm.random(shape, _range=(0, 255))
        actual_mask = hmarray((3, 16, 12, 12))
        actual = hmarray((3, 16, 12, 12))
        expected_mask = hmarray((3, 16, 12, 12))
        expected = hmarray((3, 16, 12, 12))
        expected.fill(float('-inf'))

        @compose
        def fn(bottom, mask, top):
            top, mask = PoolForward(bottom, kernel_size=(2, 2),
                                    padding=(0, 0), stride=(2, 2))
            return top, mask

        fn(a, actual_mask, actual)
        actual.sync_host()
        actual_mask.sync_host()
        reference_pool(a, expected, expected_mask, (2, 2), (2, 2), (0, 0))
        self._check(actual, expected)
        self._check(actual_mask, expected_mask)
        bottom_diff = hm.zeros(shape)
        expected_bottom_diff = hm.zeros(shape)
        mask = actual_mask
        top_diff = hm.random((3, 16, 12, 12))

        @compose
        def fn(top_diff, mask, bottom_diff):
            bottom_diff = PoolBackward(top_diff, mask,
                                       kernel_size=(2, 2),
                                       padding=(0, 0),
                                       stride=(2, 2))
            return bottom_diff

        fn(top_diff, mask, bottom_diff)
        bottom_diff.sync_host()
        reference_pool_backward(top_diff, mask, expected_bottom_diff,
                                (2, 2), (2, 2), (0, 0))
        self._check(bottom_diff, expected_bottom_diff)
Exemplo n.º 12
0
    def test_conv_forward(self):
        @compose
        def fn(a, weights, out, bias):
            out = ConvForward(a,
                              weights,
                              bias,
                              kernel_size=(11, 11),
                              padding=(0, 0),
                              stride=(4, 4))
            return out

        a = hm.random((3, 3, 27, 27), _range=(0, 255))
        weights = hm.random((12, 363), _range=(-.2, .2))
        out = hm.zeros((3, 12, 5, 5))
        bias = hm.random((12, ))
        fn(a, weights, out, bias)

        weights = weights.reshape(12, 3, 11, 11)
        expected = np.zeros((3, 12, 5, 5), np.float32)
        reference_conv(a, weights, bias, expected, (4, 4), (0, 0))
        out.sync_host()
        np.testing.assert_array_almost_equal(out, expected, decimal=1)
Exemplo n.º 13
0
    def test_conv_backward(self):
        # TODO: Check bias diff

        @compose
        def fn(top_diff, weights, bottom, bottom_diff, weights_diff,
               bias_diff):
            bottom_diff, weights_diff, bias_diff = \
                ConvBackward(bottom, top_diff, weights,
                             kernel_size=(11, 11), padding=(0, 0),
                             stride=(4, 4))
            return bottom_diff, weights_diff, bias_diff

        top_diff = hm.random((3, 12, 25), _range=(0, 5))
        bottom = hm.random((3, 3, 27, 27), _range=(0, 255))
        weights = hm.random((12, 363), _range=(-.2, .2))

        weights_diff = hm.zeros((12, 363))
        bias_diff = hm.zeros((12, ))
        bottom_diff = hm.zeros((3, 3, 27, 27))

        fn(top_diff, weights, bottom, bottom_diff, weights_diff, bias_diff)

        expected_weights_diff = np.zeros_like(weights)
        expected_bottom_diff = np.zeros_like(bottom_diff)

        for i in range(top_diff.shape[0]):
            col_data = reference_im2col(bottom[i], (11, 11), (4, 4), (0, 0))
            expected_weights_diff += top_diff[i].dot(col_data.T)
            expected_bottom_diff[i] = reference_col2im(
                weights.T.dot(top_diff[i]), (11, 11), (4, 4), (0, 0),
                expected_bottom_diff[i].shape)
        weights_diff.sync_host()
        np.testing.assert_array_almost_equal(weights_diff,
                                             expected_weights_diff, decimal=2)
        bottom_diff.sync_host()
        np.testing.assert_array_almost_equal(bottom_diff, expected_bottom_diff,
                                             decimal=2)
Exemplo n.º 14
0
    def test_simple(self):
        a = hm.random((3, 16, 27, 27))
        scale = hmarray((3, 16, 27, 27))
        actual = hmarray((3, 16, 27, 27))

        @compose
        def fn(bottom, scale, top):
            top, scale = LrnForward(bottom, alpha=alpha, beta=beta,
                                    local_size=local_size, k=1)
            return top, scale

        fn(a, scale, actual)
        actual.sync_host()
        expected = reference_lrn(a)
        self._check(actual, expected)
Exemplo n.º 15
0
    def test_simple(self):
        a = hm.random((3, 16, 27, 27))
        scale = hmarray((3, 16, 27, 27))
        actual = hmarray((3, 16, 27, 27))

        @compose
        def fn(bottom, scale, top):
            top, scale = LrnForward(bottom,
                                    alpha=alpha,
                                    beta=beta,
                                    local_size=local_size,
                                    k=1)
            return top, scale

        fn(a, scale, actual)
        actual.sync_host()
        expected = reference_lrn(a)
        self._check(actual, expected)
Exemplo n.º 16
0
    def test_avg(self):
        shape = (3, 16, 24, 24)
        a = hm.random(shape, _range=(0, 255))
        actual_mask = hmarray((3, 16, 12, 12))
        actual = hmarray((3, 16, 12, 12))
        expected = hmarray((3, 16, 12, 12))
        expected.fill(float('-inf'))

        @compose
        def fn(bottom, mask, top):
            top = AvePoolForward(bottom, kernel_size=(2, 2),
                                 padding=(0, 0), stride=(2, 2))
            return top

        fn(a, actual_mask, actual)
        actual.sync_host()
        reference_ave_pool(a, expected, (2, 2), (2, 2), (0, 0))
        self._check(actual, expected)
    def test_avg(self):
        shape = (3, 16, 24, 24)
        a = hm.random(shape, _range=(0, 255))
        actual_mask = hmarray((3, 16, 12, 12))
        actual = hmarray((3, 16, 12, 12))
        expected = hmarray((3, 16, 12, 12))
        expected.fill(float('-inf'))

        @compose
        def fn(bottom, mask, top):
            top = AvePoolForward(bottom,
                                 kernel_size=(2, 2),
                                 padding=(0, 0),
                                 stride=(2, 2))
            return top

        fn(a, actual_mask, actual)
        actual.sync_host()
        reference_ave_pool(a, expected, (2, 2), (2, 2), (0, 0))
        self._check(actual, expected)
 def setUp(self):
     self.a = hm.random((256, 256), _range=(0, 255))
     self.b = hm.random((256, 256), _range=(0, 255))
     self.c = hm.random((256, 256), _range=(0, 255))
Exemplo n.º 19
0
 def setUp(self):
     self.a = hm.random((256, 256), _range=(0, 255))
     self.b = hm.random((256, 256), _range=(0, 255))
     self.c = hm.random((256, 256), _range=(0, 255))