def test_avg_pool_2d(_ndarray_1x1x4x4):
    runtime = get_runtime()
    input_data = _ndarray_1x1x4x4
    param = ng.parameter(input_data.shape, name='A', dtype=np.float32)

    kernel_shape = [2, 2]
    spatial_dim_count = len(kernel_shape)
    pads_begin = [0] * spatial_dim_count
    pads_end = [0] * spatial_dim_count
    strides = [2, 2]
    exclude_pad = True
    expected = [[[[13.5, 15.5],
                  [21.5, 23.5]]]]

    avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end,
                                kernel_shape, exclude_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    expected = [[[[13.5, 14.5, 15.5],
                  [17.5, 18.5, 19.5],
                  [21.5, 22.5, 23.5]]]]
    strides = [1, 1]
    avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end,
                                kernel_shape, exclude_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    pads_begin = [1, 1]
    pads_end = [1, 1]
    strides = [2, 2]
    exclude_pad = True

    expected = [[[[11.0, 12.5, 14.0],
                  [17.0, 18.5, 20.0],
                  [23.0, 24.5, 26.0]]]]
    avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end,
                                kernel_shape, exclude_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    exclude_pad = False
    expected = [[[[2.75, 6.25, 3.5],
                  [8.5, 18.5, 10.0],
                  [5.75, 12.25, 6.5]]]]
    avg_pool_node = ng.avg_pool(param, strides, pads_begin, pads_end,
                                kernel_shape, exclude_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)
Example #2
0
def test_avg_pool_2d(_ndarray_1x1x4x4):
    runtime = get_runtime()
    input_data = _ndarray_1x1x4x4
    param = ng.parameter(input_data.shape, name='A', dtype=np.float32)

    window_shape = [2, 2]
    strides = [2, 2]
    expected = [[[[13.5, 15.5],
                  [21.5, 23.5]]]]

    avg_pool_node = ng.avg_pool(param, window_shape, strides)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    expected = [[[[13.5, 14.5, 15.5],
                  [17.5, 18.5, 19.5],
                  [21.5, 22.5, 23.5]]]]
    avg_pool_node = ng.avg_pool(param, window_shape)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    padding_below = [1, 1]
    padding_above = [1, 1]
    strides = [2, 2]
    include_pad = False

    expected = [[[[11.0, 12.5, 14.0],
                  [17.0, 18.5, 20.0],
                  [23.0, 24.5, 26.0]]]]
    avg_pool_node = ng.avg_pool(param, window_shape, strides, padding_below, padding_above,
                                include_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    include_pad = True
    expected = [[[[2.75, 6.25, 3.5],
                  [8.5, 18.5, 10.0],
                  [5.75, 12.25, 6.5]]]]
    avg_pool_node = ng.avg_pool(param, window_shape, strides, padding_below, padding_above,
                                include_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)
Example #3
0
def test_avg_pool_2d(_ndarray_1x1x4x4):
    runtime = get_runtime()
    input_data = _ndarray_1x1x4x4
    param = ng.parameter(input_data.shape, name='A', dtype=np.float32)

    window_shape = [2, 2]
    strides = [2, 2]
    expected = [[[[13.5, 15.5], [21.5, 23.5]]]]

    avg_pool_node = ng.avg_pool(param, window_shape, strides)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    expected = [[[[13.5, 14.5, 15.5], [17.5, 18.5, 19.5], [21.5, 22.5, 23.5]]]]
    avg_pool_node = ng.avg_pool(param, window_shape)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    padding_below = [1, 1]
    padding_above = [1, 1]
    strides = [2, 2]
    include_pad = False

    expected = [[[[11.0, 12.5, 14.0], [17.0, 18.5, 20.0], [23.0, 24.5, 26.0]]]]
    avg_pool_node = ng.avg_pool(param, window_shape, strides, padding_below,
                                padding_above, include_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)

    include_pad = True
    expected = [[[[2.75, 6.25, 3.5], [8.5, 18.5, 10.0], [5.75, 12.25, 6.5]]]]
    avg_pool_node = ng.avg_pool(param, window_shape, strides, padding_below,
                                padding_above, include_pad)
    computation = runtime.computation(avg_pool_node, param)
    result = computation(input_data)
    assert np.allclose(result, expected)
Example #4
0
def test_avg_pooling_3d(_ndarray_1x1x4x4):
    rt = get_runtime()
    data = _ndarray_1x1x4x4
    data = np.broadcast_to(data, (1, 1, 4, 4, 4))
    param = ng.parameter(list(data.shape))
    window_shape = [2, 2, 2]
    strides = [2, 2, 2]

    avgpool = ng.avg_pool(param, window_shape, strides)
    comp = rt.computation(avgpool, param)
    result = comp(data)
    result_ref = [[[[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5,
                                                                  23.5]]]]]
    assert np.allclose(result, result_ref)
Example #5
0
def test_avg_pooling_3d():
    manager_name = pytest.config.getoption('backend', default='CPU')
    rt = ng.runtime(manager_name=manager_name)

    data = np.arange(11, 27, dtype=np.float32)
    data = data.reshape((1, 1, 4, 4))
    data = np.broadcast_to(data, (1, 1, 4, 4, 4))

    param = ng.parameter(data.shape)

    avgpool = ng.avg_pool(param, [2, 2, 2], [2, 2, 2])
    comp = rt.computation(avgpool, param)
    result = comp(data)
    result_ref = [[[[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5,
                                                                  23.5]]]]]
    np.testing.assert_allclose(result, result_ref, rtol=0.001)
Example #6
0
def test_avg_pooling_3d(_ndarray_1x1x4x4):
    rt = get_runtime()
    data = _ndarray_1x1x4x4
    data = np.broadcast_to(data, (1, 1, 4, 4, 4))
    param = ng.parameter(list(data.shape))
    window_shape = [2, 2, 2]
    strides = [2, 2, 2]

    avgpool = ng.avg_pool(param, window_shape, strides)
    comp = rt.computation(avgpool, param)
    result = comp(data)
    result_ref = [[[[[13.5, 15.5],
                     [21.5, 23.5]],

                    [[13.5, 15.5],
                     [21.5, 23.5]]]]]
    assert np.allclose(result, result_ref)
Example #7
0
def test_avg_pooling_3d(_ndarray_1x1x4x4):
    rt = get_runtime()
    data = _ndarray_1x1x4x4
    data = np.broadcast_to(data, (1, 1, 4, 4, 4))
    param = ng.parameter(list(data.shape))
    kernel_shape = [2, 2, 2]
    strides = [2, 2, 2]
    spatial_dim_count = len(kernel_shape)
    pads_begin = [0] * spatial_dim_count
    pads_end = [0] * spatial_dim_count
    exclude_pad = True

    avgpool = ng.avg_pool(param, strides, pads_begin, pads_end, kernel_shape, exclude_pad)
    comp = rt.computation(avgpool, param)
    result = comp(data)
    result_ref = [[[[[13.5, 15.5], [21.5, 23.5]], [[13.5, 15.5], [21.5, 23.5]]]]]
    assert np.allclose(result, result_ref)
Example #8
0
def make_pooling_op(onnx_node, ng_inputs, kernel_shape=None):
    # type: (NodeWrapper, List[NgraphNode], List[int]) -> NgraphNode
    """
    Create an ngraph pooling Op based on an ONNX node.

    :param onnx_node: wrapped ONNX node for a pooling op
    :param ng_inputs: ngraph TensorOp input tensors
    :param kernel_shape: kernel shape for this op
    :return: ngraph pooling op
    """
    x = ng_inputs[0]

    op_type = get_op_type(onnx_node)

    # We assume data are in [D1,...,DN] format thus we subtract [N,C] dimensions.
    spatial_dims = len(x.shape) - 2  # get spatial dimensions

    if kernel_shape is None:
        kernel_shape = get_kernel_shape(onnx_node)
    kernel_shape = reduce_extra_dims(spatial_dims, kernel_shape, onnx_node)

    strides = get_strides(onnx_node, kernel_shape)
    padding_below, padding_above = get_pads(onnx_node, kernel_shape)

    strides = reduce_extra_dims(spatial_dims, strides, onnx_node)
    padding_above = reduce_extra_dims(spatial_dims, padding_above, onnx_node)
    padding_below = reduce_extra_dims(spatial_dims, padding_below, onnx_node)

    include_pad = onnx_node.get_attribute_value('count_include_pad', 0) != 0

    if op_type == 'avg':
        ng_op = ng.avg_pool(x, kernel_shape, strides, padding_below, padding_above,
                            include_padding=include_pad)
    elif op_type == 'max':
        ng_op = ng.max_pool(x, kernel_shape, strides, padding_below, padding_above)
    else:
        raise NotImplementedError('%s node (%s): Unsupported pooling type.',
                                  onnx_node.op_type, onnx_node.name)
    return ng_op