def test_backward_cpu_weights(self):
        print("test_backward_cpu_weights")
        cp = convolution2DParam(self.w_shape,
                                1, 1,
                                1, 1,
                                1, 1,
                                1, 1)

        self.check_backward_weights(self.x, self.w, self.b, cp, self.gy)
    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,
                                     1, 1, 1)

        self.pp_fwd = pooling2DParam((self.bs, self.oc, 6, 6), 3, 3, 2, 2, 0,
                                     0, 0, 0, pooling2DParam.pooling_max)
        self.pp_bwd = pooling2DParam(self.x.shape, 3, 3, 2, 2, 0, 0, 0, 0,
                                     pooling2DParam.pooling_max)

        self.check_forward_options = {'atol': 1e-5, 'rtol': 1e-4}
        self.check_backward_options = {'atol': 1e-5, 'rtol': 1e-4}
    def check_backward_data(self, x, w, b, cp):
        out_c, in_c, kh, kw = w.shape
        n, out_c, in_h, in_w = x.shape
        self.pd = self.sy * (in_h - 1) + (
            kh + (kh - 1) * (self.dy - 1)) - self.outh - self.ph
        self.pr = self.sx * (in_w - 1) + (
            kw + (kw - 1) * (self.dx - 1)) - self.outw - self.pw

        _set_cover_all(self, x, w)
        # create conv parameter
        # for IA specific
        param = convolution2DParam(x.shape,
                                   self.dy, self.dx,
                                   self.sy, self.sx,
                                   self.ph, self.pw,
                                   self.pd, self.pr)
        y_act = convolution2D.BackwardData(w, x, param)
        if b is not None:
            y_act += b.reshape(1, b.size, 1, 1)
        y_act = numpy.array(y_act, dtype=self.dtype)

        x = numpy.array(x, dtype=self.dtype)
        w = numpy.array(w, dtype=self.dtype)

        gcol = numpy.tensordot(w, x, (0, 1)).astype(x.dtype, copy=False)
        # - k, m, n: shape of out_channel
        # - b: number of inputs
        # - h, w: height and width of kernels
        # k, m, n, b, h, w -> b, k, m, n, h, w
        gcol = numpy.rollaxis(gcol, 3)
        y_expect = col2im_cpu(
            gcol, self.sy, self.sx, self.ph, self.pw, self.outh, self.outw,
            dy=self.dy, dx=self.dx)
        # b, k, h, w
        if b is not None:
            y_expect += b.reshape(1, b.size, 1, 1)

        numpy.testing.assert_allclose(
            y_act, y_expect, **self.check_backward_options)
    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)
        self.b = numpy.random.uniform(-1, 1, self.b_shape).astype(self.dtype)
        self.b = ideep4py.mdarray(self.b)

        self.cp = convolution2DParam(self.x_shape,
                                     1, 1,
                                     1, 1,
                                     1, 1,
                                     1, 1)

        stride = 1
        pad = 1
        dilate = 1
        self.sy, self.sx = stride, stride
        self.ph, self.pw = pad, pad
        self.n = self.x_shape[0]
        self.outc = self.w_shape[0]
        self.outh = self.x_shape[2]
        self.outw = self.x_shape[3]
        self.cover_all = self.cover_all
        self.dy, self.dx = dilate, dilate

        self.gy = numpy.random.uniform(
            -1, 1,
            (self.n, self.outc, self.outh, self.outw)).astype(self.dtype)
        self.gy = ideep4py.mdarray(self.gy)

        self.check_forward_options = {'atol': 1e-3, 'rtol': 1e-2}
        self.check_backward_options = {'atol': 1e-3, 'rtol': 1e-2}
Beispiel #5
0
import numpy
import ideep4py

# from ideep4py import convolution2DParam, conv_test
from ideep4py import intVector, convolution2DParam, convolution2D

x = numpy.ndarray(shape=(1, 32, 224, 224), dtype=numpy.float32, order='C')
x = ideep4py.mdarray(x)

w = numpy.ndarray(shape=(32, 32, 3, 3), dtype=numpy.float32, order='C')
w = ideep4py.mdarray(w)

b = numpy.ndarray(shape=(32, ), dtype=numpy.float32, order='C')
b = ideep4py.mdarray(b)

cp = convolution2DParam()
cp.out_dims = intVector()
cp.out_dims.push_back(1)
cp.out_dims.push_back(32)
cp.out_dims.push_back(224)
cp.out_dims.push_back(224)
cp.sy = cp.sx = 1
cp.pad_lh = cp.pad_lw = cp.pad_rh = cp.pad_rw = 1

print("fwd with bias")
y = convolution2D.Forward(x, w, b, cp)
print("==============")
y = convolution2D.Forward(x, w, b, cp)
print("==============")
y = convolution2D.Forward(y, w, b, cp)