예제 #1
0
from ideep4py import convolution2D
from ideep4py import linear

try:
    import testing
    from testing import condition
    from testing.conv import col2im_cpu
    from testing.conv import im2col_cpu
except Exception as ex:
    print('*** testing directory is missing: %s' % ex)
    sys.exit(-1)


@testing.parameterize(*testing.product({
    'dtype': [numpy.float32],
    'bs': [256],
    'ic': [256],
    'oc': [256],
}))
class TestPooling2DPyF32(unittest.TestCase):
    def setUp(self):
        self.x = numpy.random.uniform(
            -1, 1, (self.bs, self.ic, 13, 13)).astype(self.dtype)
        self.W = numpy.random.uniform(-1, 1, (self.oc, self.ic, 3, 3)).astype(
            self.dtype)

        self.linear_gy = numpy.random.uniform(-1, 1, (self.bs, 4096)).astype(
            self.dtype)
        self.linear_W = \
            numpy.random.uniform(-1, 1, (4096, 9216)).astype(self.dtype)

        self.cp = convolution2DParam((self.bs, self.oc, 13, 13), 1, 1, 1, 1, 1,
예제 #2
0
import numpy
import ideep4py
import testing
import unittest


@testing.parameterize(*testing.product({
    'dtype': [numpy.float32],
    'shape': [(3, 16, 2, 4), (2, 7, 1, 1), (2, 7, 3, 2), (1, 32, 224, 224),
              (2, 2, 2, 2), (3, 4), (1, 1)],
}))
class TestTanh(unittest.TestCase):
    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.y = numpy.tanh(self.x)
        self.check_options = {'atol': 1e-5, 'rtol': 1e-4}

    def test_tanh(self):
        mx = ideep4py.mdarray(self.x)
        x2 = numpy.array(mx)
        numpy.testing.assert_allclose(self.x, x2)

    def test_tanhForward(self):
        mx = ideep4py.mdarray(self.x)
        my = ideep4py._ideep4py.tanh.Forward(mx)
        y2 = numpy.array(my)
        numpy.testing.assert_allclose(self.y, y2, **self.check_options)

    def test_tanhBackward(self):
        x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
예제 #3
0
from ideep4py import dropout

try:
    import testing
except Exception as ex:
    print('*** testing directory is missing: %s' % ex)
    sys.exit(-1)


def _dropout(x, creator):
    return x * creator.mask


@testing.parameterize(*testing.product({
    'dropout_ratio': [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
    'dtype': [
        numpy.float32,
    ],
}))
@testing.fix_random()
class TestDropoutF32(unittest.TestCase):
    def setUp(self):
        self.x = numpy.random.rand(128, 3, 224, 224).astype(self.dtype)
        self.x_md = ideep4py.mdarray(self.x)
        self.gy = numpy.random.rand(128, 3, 224, 224).astype(self.dtype)

    def check_forward(self, x, x_md):
        mask, y = dropout.Forward(x_md, self.dropout_ratio)
        y = numpy.array(y, dtype=self.dtype)
        y_expect = x * mask
        numpy.testing.assert_allclose(y, y_expect)
예제 #4
0
import sys
import unittest
import numpy
import ideep4py
from ideep4py import linear

try:
    import testing
    from testing import condition
except Exception as ex:
    print('*** testing directory is missing: %s' % ex)
    sys.exit(-1)


@testing.parameterize(*testing.product({
    'x_dtype': [numpy.float32],
    'W_dtype': [numpy.float32],
}))
class TestLinearPyF32(unittest.TestCase):
    def setUp(self):
        self.W = numpy.random.uniform(-1, 1, (2, 3)).astype(self.W_dtype)
        self.b = numpy.random.uniform(-1, 1, 2).astype(self.x_dtype)

        self.x = numpy.random.uniform(-1, 1, (4, 3)).astype(self.x_dtype)
        self.gy = numpy.random.uniform(-1, 1, (4, 2)).astype(self.x_dtype)

        self.check_forward_options = {'atol': 5e-4, 'rtol': 5e-3}
        self.check_backward_options = {'atol': 5e-4, 'rtol': 5e-3}

    def check_forward(self, x, W, b, y_expect):
        with_bias = True if b is not None else False
예제 #5
0
import unittest

import numpy

import ideep4py
from ideep4py import relu

try:
    import testing
except Exception as ex:
    print('*** testing directory is missing: %s' % ex)
    sys.exit(-1)


@testing.parameterize(*testing.product({
    'shape': [(3, 2), (224, 224)],
    'dtype': [numpy.float32, ],
}))
@testing.fix_random()
class TestReluPyF32(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.y = numpy.maximum(self.x, 0, dtype=(self.x).dtype)
        self.gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.gx = (self.x > 0) * self.gy

    def check_forward(self, x, y):
        mx = ideep4py.mdarray(x)
        x2 = numpy.array(mx)
        numpy.testing.assert_allclose(x, x2)
        my = relu.Forward(mx)
예제 #6
0
    from testing import condition
    from testing.conv import col2im_cpu
except Exception as ex:
    print('*** testing directory is missing: %s' % ex)
    sys.exit(-1)

if bool(int(os.environ.get('ENALE_TRAVIS_TEST', '0'))):
    bs_list = [1, 2, 4, 5, 6, 8, 10, 16, 24, 32, 64, ]
else:
    bs_list = [1, 2, 4, 5, 6, 8, 10, 16, 24, 32, 64, 96, 128, 196, 256, ]
print('bs_list: ', bs_list)


@testing.parameterize(*testing.product({
    'dtype': [numpy.float32],
    'channel': [1, 2, 4, 8, 10, 16, 24, 32, 64, ],
    'bs': bs_list,
    'stride': [2, ],
}))
class TestPooling2DPyF32(unittest.TestCase):

    def setUp(self):
        self.x = numpy.random.uniform(
            -1, 1, (self.bs, self.channel, 4, 3)).astype(self.dtype)
        self.gy = numpy.random.uniform(
            -1, 1, (self.bs, self.channel, 2, 2)).astype(self.dtype)

        self.pp_fwd = pooling2DParam(
            self.gy.shape, 3, 3, self.stride, self.stride, 1, 1,
            1, 1, pooling2DParam.pooling_avg_include_padding)
        self.pp_bwd = pooling2DParam(
            (self.bs, self.channel, 4, 3), 3, 3, self.stride, self.stride,
예제 #7
0
def _x_hat(x, mean, inv_std):
    x_mu = x - mean
    x_mu *= inv_std
    return x_mu


def _batch_normalization(expander, gamma, beta, x, mean, var):
    mean = mean[expander]
    std = numpy.sqrt(var)[expander]
    y_expect = (gamma[expander] * (x - mean) / std + beta[expander])
    return y_expect


@testing.parameterize(*(testing.product({
    'param_shape': [(3, ), ],
    'ndim': [2, ],
    'dtype': [numpy.float32],
})))
class TestBatchNormalizationF32(unittest.TestCase):

    def setUp(self):
        self.eps = 2e-5
        self.expander = (None, Ellipsis) + (None,) * self.ndim
        self.gamma = numpy.random.uniform(.5, 1,
                                          self.param_shape).astype(self.dtype)
        self.beta = numpy.random.uniform(-1, 1,
                                         self.param_shape).astype(self.dtype)
        self.head_ndim = self.gamma.ndim + 1
        shape = (5,) + self.param_shape + (2,) * self.ndim
        self.x = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
        self.gy = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
예제 #8
0
                                 self.ph, d=self.dy) or
        in_w != get_conv_outsize(self.outw, kw, self.sx,
                                 self.pw, d=self.dx))


if bool(int(os.environ.get('ENALE_TRAVIS_TEST', '0'))):
    bs_list = [1, 2, 4, 5, 8, 10, 16, 32, 64, ]
else:
    bs_list = [1, 2, 4, 5, 8, 10, 16, 32, 64, 96, 128, 192, 256, 512, ]
print('bs_list: ', bs_list)


@testing.parameterize(*testing.product({
    'dtype': [numpy.float32, ],
    'cover_all': [False, True],
    'channel': [1, 2, 4, 8, 10, ],
    'bs': bs_list,
    'with_bias': [True, ],
}))
@testing.fix_random()
class TestConvolution2DPyF32(unittest.TestCase):

    def setUp(self):
        self.x_shape = (self.bs, self.channel, 224, 224)
        self.w_shape = (self.channel, self.channel, 3, 3)
        self.b_shape = self.channel

        self.x = numpy.random.uniform(-1, 1, self.x_shape).astype(self.dtype)
        self.x = ideep4py.mdarray(self.x)
        self.w = numpy.random.uniform(-1, 1, self.w_shape).astype(self.dtype)
        self.w = ideep4py.mdarray(self.w)
예제 #9
0
import six
import ideep4py
from ideep4py import localResponseNormalizationParam
from ideep4py import localResponseNormalization

try:
    import testing
except Exception as ex:
    print('*** testing directory is missing: %s' % ex)
    sys.exit(-1)


@testing.parameterize(*testing.product({
    'dtype': [numpy.float32],
    'shape': [
        (2, 7, 1, 1),
        (2, 7, 3, 2),
    ],
}))
class TestLocalResponseNormalizationPyF32(unittest.TestCase):
    def setUp(self):
        self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.pp = localResponseNormalizationParam(
            5, 2, 1e-4, .75,
            ideep4py.localResponseNormalizationParam.lrn_across_channels)
        self.check_forward_options = {'atol': 1e-4, 'rtol': 1e-3}
        self.check_backward_options = {'atol': 1e-4, 'rtol': 1e-3}

    def check_forward(self, x, pp):
        x_mdarray = ideep4py.mdarray(x)