Esempio n. 1
0
def test_Pad(tmpdir):
    shape = (4, 5)
    data = np.random.rand(*shape).astype(np.float32)

    model = C.pad(data, pattern=[(1,1),(2,2)], mode=C.ops.CONSTANT_PAD, constant_value=1)
    verify_no_input(model, tmpdir, 'Pad_0')

    x = C.input_variable(shape)
    model = C.pad(x, pattern=[(1,1),(2,2)], mode=C.ops.REFLECT_PAD)

    verify_one_input(model, data, tmpdir, 'Pad_1')
Esempio n. 2
0
def test_Pad(tmpdir):
    shape = (4, 5)
    data = np.random.rand(*shape).astype(np.float32)

    model = C.pad(data, pattern=[(1,1),(2,2)], mode=C.ops.CONSTANT_PAD, constant_value=1)
    verify_no_input(model, tmpdir, 'Pad_0')

    x = C.input_variable(shape)
    model = C.pad(x, pattern=[(1,1),(2,2)], mode=C.ops.REFLECT_PAD)

    verify_one_input(model, data, tmpdir, 'Pad_1')    
Esempio n. 3
0
def pad(x, pattern, mode=C.CONSTANT_PAD, constant_value=0, name=''):
    """
    Pads a tensor in the sequence axis according to the specified patterns.
    Three padding modes are supported: CONSTANT / REFLECT / SYMMETRIC.

    Arguments:
        x: tensor to be padded.
        pattern (tuple with 2 integers): how many values to add before and after the contents in the sequence axis.
        mode (int): padding mode: C.ops.CONSTANT_PAD, C.ops.REFLECT_PAD and C.ops.SYMMETRIC_PAD
        constant_value: the value used to fill the padding cells, only meaningful under CONSTANT mode.
        name (str, optional): the name of the Function instance in the network

    Returns:
        :class:`~cntk.ops.functions.Function`
    """
    if not all(isinstance(i, int) for i in pattern) or not isinstance(pattern, tuple):
        raise ValueError(f"pattern {pattern} must be a tuple with 2 integers")

    ndim = len(x.shape)
    null_pattern = [(0, 0)] * ndim
    final_pattern = [pattern] + null_pattern

    b, valid = C.sequence.unpack(x, padding_value=0).outputs
    c = C.pad(b, final_pattern, mode=mode, constant_value=constant_value)
    seq_length = C.reduce_sum(valid, axis=0) + C.Constant(sum(pattern))
    d = C.to_sequence(c, seq_length, name=name)
    return d
Esempio n. 4
0
def test_pad():
    x = C.constant(value=np.arange(6).reshape((2, 3)))
    pad1 = C.pad(x, [(1, 1), (2, 2)]).eval()
    expect1 = np.lib.pad([[0, 1, 2], [3, 4, 5]], ((1, 1), (2, 2)), 'constant')
    assert np.array_equal(pad1, expect1)

    pad2 = C.pad(x, [(1, 1), (2, 2)], mode=1).eval()
    expect2 = np.lib.pad([[0, 1, 2], [3, 4, 5]], ((1, 1), (2, 2)), 'reflect')
    assert np.array_equal(pad2, expect2)

    pad3 = C.pad(x, [(1, 1), (2, 2)], mode=2).eval()
    expect3 = np.lib.pad([[0, 1, 2], [3, 4, 5]], ((1, 1), (2, 2)), 'symmetric')
    assert np.array_equal(pad3, expect3)

    #test inferred dimension and free dimension
    x = C.input((C.InferredDimension, 3))
    data = np.arange(12).reshape((2, 2, 3))
    pad4 = C.pad(x, [(1, 1), (2, 2)], mode=1).eval({x: data})
    expect4 = np.lib.pad([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]],
                         ((0, 0), (1, 1), (2, 2)), 'reflect')
    assert np.array_equal(pad4, expect4)

    x = C.input((C.FreeDimension, 3))
    pad5 = C.pad(x, [(1, 1), (2, 2)], mode=2).eval({x: data})
    expect5 = np.lib.pad([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]],
                         ((0, 0), (1, 1), (2, 2)), 'symmetric')
    assert np.array_equal(pad5, expect5)

    #test grad
    x = C.parameter(init=np.arange(6).reshape((2, 3)))
    p = C.pad(x, mode=C.ops.SYMMETRIC_PAD, pattern=[(1, 0), (2, 1)])
    grad = p.grad({}, [x])
    expect_grad = np.asarray([[4., 4., 4.], [2., 2., 2.]])
    assert np.array_equal(grad, expect_grad)

    p2 = C.pad(x, mode=C.ops.REFLECT_PAD, pattern=[(1, 1), (2, 2)])
    grad2 = p2.grad({}, [x])
    expect_grad2 = np.asarray([[4., 6., 4.], [4., 6., 4.]])
    assert np.array_equal(grad2, expect_grad2)
Esempio n. 5
0
def test_pad():
    x = C.constant(value=np.arange(6).reshape((2,3)))
    pad1 = C.pad(x, [(1, 1), (2, 2)]).eval()
    expect1 = np.lib.pad([[0, 1, 2], [3, 4, 5]], ((1, 1), (2, 2)), 'constant')
    assert np.array_equal(pad1, expect1)

    pad2 = C.pad(x, [(1, 1), (2, 2)], mode=1).eval()
    expect2 = np.lib.pad([[0, 1, 2], [3, 4, 5]], ((1, 1), (2, 2)), 'reflect')
    assert np.array_equal(pad2, expect2)

    pad3 = C.pad(x, [(1, 1), (2, 2)], mode=2).eval()
    expect3 = np.lib.pad([[0, 1, 2], [3, 4, 5]], ((1, 1), (2, 2)), 'symmetric')
    assert np.array_equal(pad3, expect3)

    #test inferred dimension and free dimension
    x = C.input((C.InferredDimension, 3))
    data = np.arange(12).reshape((2, 2, 3))
    pad4 = C.pad(x, [(1, 1), (2, 2)], mode=1).eval({x:data})
    expect4 = np.lib.pad([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]],
                         ((0,0),(1,1),(2,2)), 'reflect')
    assert np.array_equal(pad4, expect4)

    x = C.input((C.FreeDimension, 3))
    pad5 = C.pad(x, [(1, 1), (2, 2)], mode=2).eval({x: data})
    expect5 = np.lib.pad([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]],
                         ((0, 0), (1, 1), (2, 2)), 'symmetric')
    assert np.array_equal(pad5, expect5)

    #test grad
    x = C.parameter(init=np.arange(6).reshape((2,3)))
    p = C.pad(x, mode=C.ops.SYMMETRIC_PAD, pattern=[(1, 0), (2, 1)])
    grad = p.grad({}, [x])
    expect_grad = np.asarray([[4., 4., 4.],[2., 2., 2.]])
    assert np.array_equal(grad, expect_grad)

    p2 = C.pad(x, mode=C.ops.REFLECT_PAD, pattern=[(1, 1), (2, 2)])
    grad2 = p2.grad({}, [x])
    expect_grad2 = np.asarray([[4., 6., 4.], [4., 6., 4.]])
    assert np.array_equal(grad2, expect_grad2)