Beispiel #1
0
def test_average_pooling_2d():
    ksize = (2, 3)
    stride = (1, 2)
    pad = (0, 1)

    inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((2, 3, 3, 3)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.AveragePooling2D(
        input_names,
        output_names,
        kernel=list(ksize),
        stride=list(stride),
        pad_h=[pad[0], pad[0] + stride[0] - 1],
        pad_w=[pad[1], pad[1] + stride[1] - 1],
        count_exclude_pad=False)
    result = nnoir.NNOIR(b'AveragePooling2D', b'nnoir2chainer_test', '0.1',
                         input_names, output_names, nodes, [function])
    result.dump('average_pooling_2d.nnoir')

    x = np.random.randn(2, 3, 4, 5).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('average_pooling_2d.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
def test_max_pooling_2d():
    kh, kw = 2, 3
    sy, sx = 1, 2
    pad_h, pad_w = 1, 2
    inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((2, 3, 5, 4)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.MaxPooling2D(input_names,
                                            output_names,
                                            kernel=(kh, kw),
                                            stride=(sy, sx),
                                            pad_h=(pad_h, pad_h + sy - 1),
                                            pad_w=(pad_w, pad_w + sx - 1))
    result = nnoir.NNOIR(b'MaxPooling2D', b'nnoir2chainer_test', '0.1',
                         input_names, output_names, nodes, [function])
    result.dump('max_pooling_2d.nnoir')

    x = np.random.randn(2, 3, 4, 5).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('max_pooling_2d.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #3
0
def test_batch_normalization():
    shape = (2, 3, 4, 5)
    channel = 3
    gamma = np.zeros(channel, dtype=np.float32)
    beta = np.zeros(channel, dtype=np.float32)
    avg_mean = np.zeros(channel, dtype=np.float32)
    avg_var = np.zeros(channel, dtype=np.float32)
    eps = 2e-05
    gamma[:] = 0.9
    beta[:] = 0.1
    avg_mean[:] = 0.2
    avg_var[:] = 0.8

    inputs = [nnoir.Value(b'v0', np.zeros(shape).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros(shape).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.BatchNormalization(input_names,
                                                  output_names,
                                                  eps=eps,
                                                  avg_mean=avg_mean,
                                                  avg_var=avg_var,
                                                  gamma=gamma,
                                                  beta=beta)
    result = nnoir.NNOIR(b'BatchNormalization', b'nnoir2chainer_test', '0.1',
                         input_names, output_names, nodes, [function])
    result.dump('batch_normalization.nnoir')

    x = np.random.randn(*shape).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('batch_normalization.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #4
0
def test_transpose():
    inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((4, 5, 2, 3)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Transpose(input_names, output_names, axes=(2, 3, 0, 1))
    result = nnoir.NNOIR(b'Transpose', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function])
    result.dump('transpose.nnoir')

    x = np.random.randn(2, 3, 4, 5).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('transpose.nnoir')
        y = m(x)
        assert(np.all(abs(y-ref).data < util.epsilon))
def test_clipped_relu():
    inputs = [nnoir.Value(b'v0', np.zeros((10, 10)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((10, 10)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.ClippedReLU(input_names, output_names, upper=40.0)
    result = nnoir.NNOIR(b'ClippedReLU', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function])
    result.dump('clipped_relu.nnoir')

    x = np.random.randn(10, 10).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('clipped_relu.nnoir')
        y = m(x)
        assert(np.all(abs(y-ref).data < util.epsilon))
def setup_nnoir(detector_path):
    logger.info("> use nnoir")
    import chainer
    from nnoir_chainer import NNOIRFunction
    detector = NNOIRFunction(os.path.join(detector_path, "detector.nnoir"))
    with open(os.path.join(detector_path, "detector_param.json"), 'r') as f:
        detector_param = json.load(f)
    logger.info("> param {}".format(detector_param))

    def model(x):
        # assume the layout of x is NCHW
        with chainer.using_config("train", False):
            mb_locs, mb_confs = detector(x)
        mb_locs, mb_confs = mb_locs.array, mb_confs.array
        return mb_locs, mb_confs

    return model, detector_param
def test_export_add_constant():
    class Model(chainer.Link):
        def __call__(self, x):
            return x + 1.0

    with chainer.using_config('train', False):
        model = Model()
        x = chainer.Variable(np.array([0, 1, 2]).astype(np.float32))
        y = model(x)
        g = Graph(model, (x, ), (y, ))
        result = g.to_nnoir()
        with open('export_add_constant.nnoir', 'w') as f:
            f.buffer.write(result)

        m = NNOIRFunction('export_add_constant.nnoir')
        z = m(x)
        assert (np.all(abs(z.data - y.data) < util.epsilon))
Beispiel #8
0
def test_depthwise_convolution_2d_dilate():
    sy, sx = 2, 3
    ph, pw = 1, 3
    kh, kw = 3, 4
    batch = 2
    in_ch = 6
    in_h = 10
    in_w = 9
    ch_mul = 2
    out_ch = in_ch * ch_mul
    out_h = 5
    out_w = 4
    dy, dx = (2, 3)
    inputs = [
        nnoir.Value(b'v0',
                    np.zeros((batch, in_ch, in_h, in_w)).astype('float32'))
    ]
    outputs = [
        nnoir.Value(b'v1',
                    np.zeros((batch, out_ch, out_h, out_w)).astype('float32'))
    ]
    W = np.random.randn(ch_mul, in_ch, kh, kw).astype('float32')
    b = np.random.randn(out_ch).astype('float32')

    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.DepthwiseConvolution2D(input_names,
                                                      output_names,
                                                      W=W,
                                                      b=b,
                                                      pad_h=(1, 1),
                                                      pad_w=(3, 3),
                                                      stride=(sy, sx),
                                                      dilate=(dy, dx))
    result = nnoir.NNOIR(b'DepthwiseConvolution2D', b'nnoir2chainer_test',
                         '0.1', input_names, output_names, nodes, [function])
    result.dump('depthwise_convolution_2d_dilate.nnoir')

    x = np.random.randn(batch, in_ch, in_h, in_w).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('depthwise_convolution_2d_dilate.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #9
0
def test_mul():
    inputs = [nnoir.Value(b'v0', np.zeros((10, 10)).astype('float32')),
              nnoir.Value(b'v1', np.zeros((10, 10)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((10, 10)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Mul(input_names, output_names)
    result = nnoir.NNOIR(b'Mul', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function])
    result.dump('mul.nnoir')

    x1 = np.random.randn(10, 10).astype('float32')
    x2 = np.random.randn(10, 10).astype('float32')
    ref = function.run(x1, x2)
    with chainer.using_config('train', False):
        m = NNOIRFunction('mul.nnoir')
        y = m(x1, x2)
        assert(np.all(abs(y-ref) < util.epsilon))
Beispiel #10
0
def test_bilinear_2d():
    in_shape = (2, 3, 9, 10)
    out_shape = (2, 3, 4, 5)
    inputs = [nnoir.Value(b'v0', np.zeros(in_shape).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros(out_shape).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Bilinear2D(input_names, output_names, size=(out_shape[2], out_shape[3]))

    result = nnoir.NNOIR(b'Bilinear2D', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function])
    result.dump('bilinear_2d.nnoir')

    x = np.random.randn(*in_shape).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('bilinear_2d.nnoir')
        y = m(x)
        assert(np.all(abs(y-ref).data < util.epsilon))
Beispiel #11
0
def test_swish():
    inputs = [nnoir.Value(b'v0', np.zeros((10, 10, 3)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((10, 10, 3)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Swish(input_names,
                                     output_names,
                                     beta=np.arange(10).astype(np.float32))
    result = nnoir.NNOIR(b'Swish', b'nnoir2chainer_test', '0.1', input_names,
                         output_names, nodes, [function])
    result.dump('swish.nnoir')

    x = np.random.randn(10, 10).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('swish.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #12
0
def test_add_constant():
    inputs = [nnoir.Value(b'v0', np.zeros((10, 10)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((10, 10)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.AddConstant(input_names,
                                           output_names,
                                           value=2.0)
    result = nnoir.NNOIR(b'AddConstant', b'nnoir2chainer_test', '0.1',
                         input_names, output_names, nodes, [function])
    result.dump('add_constant.nnoir')

    x = np.random.randn(10, 10).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('add_constant.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref) < util.epsilon))
def test_convolution_2d():
    batch = 2
    in_ch = 4
    in_h = 10
    in_w = 9
    out_ch = 7
    out_h = 6
    out_w = 3
    kh = 4
    kw = 3
    inputs = [
        nnoir.Value(b'v0',
                    np.zeros((batch, in_ch, in_h, in_w)).astype('float32'))
    ]
    outputs = [
        nnoir.Value(b'v1',
                    np.zeros((batch, out_ch, out_h, out_w)).astype('float32'))
    ]
    W = np.random.randn(out_ch, in_ch, kh, kw).astype('float32')
    b = np.random.randn(out_ch).astype('float32')

    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Convolution2D(input_names,
                                             output_names,
                                             W=W,
                                             b=b,
                                             pad_h=(2, 2),
                                             pad_w=(1, 1),
                                             stride=(2, 3),
                                             dilate=(1, 1),
                                             groups=1)
    result = nnoir.NNOIR(b'Convolution2D', b'nnoir2chainer_test', '0.1',
                         input_names, output_names, nodes, [function])
    result.dump('convolution_2d.nnoir')

    x = np.random.randn(batch, in_ch, in_h, in_w).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('convolution_2d.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #14
0
def test_bias():
    inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((2, 3, 4, 5)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    b = np.random.randn(3).astype('float32')
    params = {'axis': 1, 'b': b}
    function = nnoir.functions.Bias(input_names, output_names, **params)
    result = nnoir.NNOIR(b'Bias', b'nnoir2chainer_test', '0.1', input_names,
                         output_names, nodes, [function])
    result.dump('bias.nnoir')

    x = np.random.randn(2, 3, 4, 5).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('bias.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #15
0
def test_pad():
    inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((3, 5, 7, 10)).astype('float32'))]
    pads = ((1, 0), (1, 1), (1, 2), (0, 5))
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.ConstantPadding(input_names,
                                               output_names,
                                               pads=pads,
                                               value=1.0)
    result = nnoir.NNOIR(b'ConstantPadding', b'nnoir2chainer_test', '0.1',
                         input_names, output_names, nodes, [function])
    result.dump('pad.nnoir')

    x = np.random.randn(2, 3, 4, 5).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('pad.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #16
0
def test_linear():
    batch = 2
    in_ch = 3
    out_ch = 4
    inputs = [nnoir.Value(b'v0', np.zeros((batch, in_ch)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((batch, out_ch)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    W = np.random.randn(out_ch, in_ch).astype('float32')
    b = np.random.randn(out_ch).astype('float32')
    function = nnoir.functions.Linear(input_names, output_names, W=W, b=b)
    result = nnoir.NNOIR(b'Linear', b'nnoir2chainer_test', '0.1', input_names,
                         output_names, nodes, [function])
    result.dump('linear.nnoir')

    x = np.random.randn(batch, in_ch).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('linear.nnoir')
        y = m(x)
        assert (np.all(abs(y - ref).data < util.epsilon))
Beispiel #17
0
def test_concat():
    v0 = nnoir.Value(b'v0', np.zeros((10, 11)).astype('float32'))
    v1 = nnoir.Value(b'v1', np.zeros((10, 12)).astype('float32'))
    graph_inputs = [v0, v1]
    inputs = [v0,
              v0,
              v1]
    outputs = [nnoir.Value(b'v2', np.zeros((10, 34)).astype('float32'))]
    graph_input_names = [x.name for x in graph_inputs]
    nodes = graph_inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Concat(input_names, output_names, axis=1)
    result = nnoir.NNOIR(b'Concat', b'nnoir2chainer_test', '0.1', graph_input_names, output_names, nodes, [function])
    result.dump('concat.nnoir')

    x1 = np.random.randn(10, 11).astype('float32')
    x2 = np.random.randn(10, 12).astype('float32')
    ref = function.run(x1, x1, x2)
    with chainer.using_config('train', False):
        m = NNOIRFunction('concat.nnoir')
        y = m(x1, x2)
        assert(np.all(abs(y-ref).data < util.epsilon))
def test_unpooling_2d():
    kh, kw = 2, 3
    sy, sx = 1, 2
    batch = 2
    ch = 3
    in_h, in_w = 5, 6
    out_h, out_w = 4, 9
    ph, pw = 1, 2
    inputs = [nnoir.Value(b'v0', np.zeros((batch, ch, in_h, in_w)).astype('float32'))]
    outputs = [nnoir.Value(b'v2', np.zeros((batch, ch, out_h, out_w)).astype('float32'))]
    nodes = inputs + outputs
    input_names = [x.name for x in inputs]
    output_names = [x.name for x in outputs]
    function = nnoir.functions.Unpooling2D(input_names, output_names, kh=kh, kw=kw, sy=sy, sx=sx, ph=ph, pw=pw,
                                           cover_all=False, outh=out_h, outw=out_w)
    result = nnoir.NNOIR(b'Unpooling2D', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function])
    result.dump('unpooling_2d.nnoir')

    x = np.random.randn(batch, ch, in_h, in_w).astype('float32')
    ref = function.run(x)
    with chainer.using_config('train', False):
        m = NNOIRFunction('unpooling_2d.nnoir')
        y = m(x)
        assert(np.all(abs(y-ref).data < util.epsilon))