示例#1
0
def max_pool(x,                      # type: Node
             window_shape,           # type: TensorShape
             strides=None,           # type: List[int]
             padding_above=None,     # type: List[int]
             padding_below=None,     # type: List[int]
             name=None,              # type: str
             ):
    # type: (...) -> Node
    """Return max pooling node."""
    if strides is None:
        strides = [1] * len(window_shape)  # Default to as many 1s as spatial dimensions of input.
    if padding_above is None:
        padding_above = [0] * len(window_shape)
    if padding_below is None:
        padding_below = [0] * len(window_shape)

    return MaxPool(x, Shape(window_shape), Strides(strides),
                   Shape(padding_above), Shape(padding_below))
示例#2
0
文件: test_ops.py 项目: tomdol/ngraph
def test_max_pool():

    #test 1d
    element_type = Type.f32
    shape = Shape([1, 1, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(10, dtype=np.float32).reshape(1, 1, 10)
    window_shape = [3]

    function = Function([MaxPool(A, Shape(window_shape))], parameter_list,
                        'test')
    backend = Backend.create(test.BACKEND_NAME)

    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1, 1, 8]))

    a.write(util.numpy_to_c(input_arr), 10 * 4)

    result_arr = np.zeros(8, dtype=np.float32).reshape(1, 1, 8)
    result.write(util.numpy_to_c(result_arr), 8 * 4)
    handle = backend.compile(function)
    handle.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 32)

    result_arr_ref = (np.arange(8) + 2).reshape(1, 1, 8)
    assert np.allclose(result_arr, result_arr_ref)

    #test 1d with strides
    strides = [2]

    function = Function([MaxPool(A, Shape(window_shape), Strides(strides))],
                        parameter_list, 'test')
    backend = Backend.create(test.BACKEND_NAME)

    size = 4
    result = backend.create_tensor(element_type, Shape([1, 1, size]))
    result_arr = np.zeros(size, dtype=np.float32).reshape(1, 1, size)

    result.write(util.numpy_to_c(result_arr), size * 4)
    handle = backend.compile(function)
    handle.call([result], [a])
    result.read(util.numpy_to_c(result_arr), size * 4)

    result_arr_ref = ((np.arange(size) + 1) * 2).reshape(1, 1, size)
    assert np.allclose(result_arr, result_arr_ref)

    #test 2d
    element_type = Type.f32
    shape = Shape([1, 1, 10, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    window_shape = [3, 3]

    function = Function([MaxPool(A, Shape(window_shape))], parameter_list,
                        'test')
    backend = Backend.create(test.BACKEND_NAME)

    a = backend.create_tensor(element_type, shape)
    result = backend.create_tensor(element_type, Shape([1, 1, 8, 8]))

    a.write(util.numpy_to_c(input_arr), 10 * 10 * 4)

    result_arr = np.zeros(64, dtype=np.float32).reshape(1, 1, 8, 8)
    result.write(util.numpy_to_c(result_arr), 8 * 8 * 4)
    handle = backend.compile(function)
    handle.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 8 * 8 * 4)

    result_arr_ref = ((np.arange(100).reshape(10,
                                              10))[2:,
                                                   2:]).reshape(1, 1, 8, 8)
    assert np.allclose(result_arr, result_arr_ref)

    #test 2d with strides
    strides = [2, 2]

    function = Function([MaxPool(A, Shape(window_shape), Strides(strides))],
                        parameter_list, 'test')
    backend = Backend.create(test.BACKEND_NAME)

    size = 4
    result = backend.create_tensor(element_type, Shape([1, 1, size, size]))
    result_arr = np.zeros(size * size,
                          dtype=np.float32).reshape(1, 1, size, size)

    result.write(util.numpy_to_c(result_arr), size * size * 4)
    handle = backend.compile(function)
    handle.call([result], [a])
    result.read(util.numpy_to_c(result_arr), size * size * 4)

    result_arr_ref = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(
        1, 1, size, size)
    assert np.allclose(result_arr, result_arr_ref)
示例#3
0
def test_max_pool():

    #test 1d
    element_type = Type.f32
    shape = Shape([1, 1, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(10, dtype=np.float32).reshape(1, 1, 10)
    window_shape = [3]

    function = Function(NodeVector([MaxPool(A, Shape(window_shape))]),
                        parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    a = backend.make_primary_tensor_view(element_type, shape)
    result = backend.make_primary_tensor_view(element_type, Shape([1, 1, 8]))

    a.write(util.numpy_to_c(input_arr), 0, 10 * 4)

    result_arr = np.zeros(8, dtype=np.float32).reshape(1, 1, 8)
    result.write(util.numpy_to_c(result_arr), 0, 8 * 4)
    cf.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 0, 32)

    result_arr_ref = (np.arange(8) + 2).reshape(1, 1, 8)
    assert np.allclose(result_arr, result_arr_ref)

    #test 1d with strides
    strides = [2]

    function = Function(
        NodeVector([MaxPool(A, Shape(window_shape), Strides(strides))]),
        parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    size = 4
    result = backend.make_primary_tensor_view(element_type, Shape([1, 1,
                                                                   size]))
    result_arr = np.zeros(size, dtype=np.float32).reshape(1, 1, size)

    result.write(util.numpy_to_c(result_arr), 0, size * 4)
    cf.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 0, size * 4)

    result_arr_ref = ((np.arange(size) + 1) * 2).reshape(1, 1, size)
    assert np.allclose(result_arr, result_arr_ref)

    #test 2d
    element_type = Type.f32
    shape = Shape([1, 1, 10, 10])
    A = Parameter(element_type, shape)
    parameter_list = [A]

    input_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    window_shape = [3, 3]

    function = Function(NodeVector([MaxPool(A, Shape(window_shape))]),
                        parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    a = backend.make_primary_tensor_view(element_type, shape)
    result = backend.make_primary_tensor_view(element_type, Shape([1, 1, 8,
                                                                   8]))

    a.write(util.numpy_to_c(input_arr), 0, 10 * 10 * 4)

    result_arr = np.zeros(64, dtype=np.float32).reshape(1, 1, 8, 8)
    result.write(util.numpy_to_c(result_arr), 0, 8 * 8 * 4)
    cf.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 0, 8 * 8 * 4)

    result_arr_ref = ((np.arange(100).reshape(10,
                                              10))[2:,
                                                   2:]).reshape(1, 1, 8, 8)
    assert np.allclose(result_arr, result_arr_ref)

    #test 2d with strides
    strides = [2, 2]

    function = Function(
        NodeVector([MaxPool(A, Shape(window_shape), Strides(strides))]),
        parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    size = 4
    result = backend.make_primary_tensor_view(element_type,
                                              Shape([1, 1, size, size]))
    result_arr = np.zeros(size * size,
                          dtype=np.float32).reshape(1, 1, size, size)

    result.write(util.numpy_to_c(result_arr), 0, size * size * 4)
    cf.call([result], [a])
    result.read(util.numpy_to_c(result_arr), 0, size * size * 4)

    result_arr_ref = ((np.arange(100).reshape(10, 10))[2::2, 2::2]).reshape(
        1, 1, size, size)
    assert np.allclose(result_arr, result_arr_ref)