コード例 #1
0
 def visit(self, op, *args):
     self.computation.set_op_rank(op)
     # op.args[0] : delta
     # op.args[1] : data batch
     # op.args[2] (optional) : dbias
     # op.fprop.args[0] : data batch
     # op.fprop.args[1] : filters
     # op.fprop.conv_params : forward params
     delta = args[0]
     data = args[1]
     filters = op.fprop.args[1]
     conv_params = op.fprop.conv_params
     """
     print(delta.axes)
     print(filters.axes)
     print(data.axes)
     print(conv_params)
     """
     ngraph_update_conv = PyngConvolutionBackpropFilters(
         self.computation.lookup_cpp_op(data),
         Shape(list(filters.axes.lengths)),
         self.computation.lookup_cpp_op(delta),
         Strides([conv_params['str_h'], conv_params['str_w']]),
         Strides([conv_params['dil_h'], conv_params['dil_w']]),
         CoordinateDiff([conv_params['pad_h'], conv_params['pad_w']]),
         CoordinateDiff([conv_params['pad_h'], conv_params['pad_w']]),
         Strides([1, 1]))
     ngraph_update_conv.name = op.name.replace(
         '/', '_') + "_ConvolutionBackpropFilters"
     self.computation.register_cpp_op(op,
                                      ngraph_update_conv,
                                      set_name=False)
コード例 #2
0
 def visit(self, op, *args):
     self.computation.set_op_rank(op)
     # op.args[0] : inputs
     # op.args[1] : filters
     # op.args[2] (optional): bias
     # op.conv_params
     # op.channel_axes
     # op.spatial_axes
     if len(args) == 2:
         inputs = args[0]
         filters = args[1]
     else:
         raise RuntimeError("Not Implemented: Convolution with bias")
     """
     {'K': 16, 'T': 1, 'R': 5, 'S': 5, 'str_d': 1, 'pad_d': 0, 'dil_d': 1,
     'str_h': 1, 'pad_h': 0, 'dil_h': 1, 'str_w': 1, 'pad_w': 0, 'dil_w': 1}
     """
     """
     print(inputs.axes)
     print(op.axes)
     print(filters.axes)
     """
     # print(op_element_type.get_output_shape(0))
     ngraph_conv = PyngConvolution(
         self.computation.lookup_cpp_op(inputs),
         self.computation.lookup_cpp_op(filters),
         Strides([op.conv_params['str_h'], op.conv_params['str_w']]),
         Strides([op.conv_params['dil_h'], op.conv_params['dil_w']]),
         CoordinateDiff([op.conv_params['pad_h'], op.conv_params['pad_w']]),
         CoordinateDiff([op.conv_params['pad_h'], op.conv_params['pad_w']]),
         Strides([1, 1]))
     ngraph_conv.name = op.name.replace('/', '_') + "_Convolution"
     self.computation.register_cpp_op(op, ngraph_conv, set_name=False)
コード例 #3
0
def convolution(
        x,  # type: Node
        weights,  # type: Node
        strides=None,  # type: List[int]
        dilation=None,  # type: List[int]
        padding_above=None,  # type: List[int]
        padding_below=None,  # type: List[int]
        name=None,  # type: str
):
    # type: (...) -> Node
    """Return convolution node."""
    if strides is None:
        strides = [1] * (
            len(x.shape) - 2
        )  # Default to as many 1s as spatial dimensions of input.
    if dilation is None:
        dilation = [1] * (len(x.shape) - 2)
    if padding_above is None:
        padding_above = [0] * (len(x.shape) - 2)
    if padding_below is None:
        padding_below = [0] * (len(x.shape) - 2)

    return Convolution(x, weights, Strides(strides), Strides(dilation),
                       CoordinateDiff(padding_above),
                       CoordinateDiff(padding_below))
コード例 #4
0
def test_convolutionBackpropData():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    output_shape = Shape([1, 1, 17, 17])

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    window_strides = [1, 1]
    window_dilation = [1, 1]
    padding_below = [0, 0]
    padding_above = [0, 0]
    data_dilation = [2, 2]

    output_arr = convolution2d(image_arr[0][0], filter_arr[0][0],
                               window_strides, window_dilation, padding_below,
                               padding_above,
                               data_dilation).reshape(1, 1, 17, 17)

    A = Parameter(element_type, filter_shape)
    B = Parameter(element_type, output_shape)
    parameter_list = [A, B]

    function = Function(
        NodeVector([
            ConvolutionBackpropData(image_shape, A, B, Strides(window_strides),
                                    Strides(window_dilation),
                                    CoordinateDiff(padding_below),
                                    CoordinateDiff(padding_above),
                                    Strides(data_dilation))
        ]), parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    a = backend.make_primary_tensor_view(element_type, filter_shape)
    b = backend.make_primary_tensor_view(element_type, output_shape)

    a.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4)
    b.write(util.numpy_to_c(output_arr), 0, 17 * 17 * 4)

    result_arr = np.zeros(10 * 10, dtype=np.float32).reshape(1, 1, 10, 10)
    result = backend.make_primary_tensor_view(element_type,
                                              Shape([1, 1, 10, 10]))
    result.write(util.numpy_to_c(result_arr), 0, 10 * 10 * 4)
    cf.call([result], [a, b])

    result.read(util.numpy_to_c(result_arr), 0, 10 * 10 * 4)
    result_arr_ref = np.array(
        [[[[22, 60, 70, 80, 90, 100, 110, 120, 130, 54.],
           [105, 275, 300, 325, 350, 375, 400, 425, 450, 185.],
           [205, 525, 550, 575, 600, 625, 650, 675, 700, 285.],
           [305, 775, 800, 825, 850, 875, 900, 925, 950, 385.],
           [405, 1025, 1050, 1075, 1100, 1125, 1150, 1175, 1200, 485.],
           [505, 1275, 1300, 1325, 1350, 1375, 1400, 1425, 1450, 585.],
           [605, 1525, 1550, 1575, 1600, 1625, 1650, 1675, 1700, 685.],
           [705, 1775, 1800, 1825, 1850, 1875, 1900, 1925, 1950, 785.],
           [805, 2025, 2050, 2075, 2100, 2125, 2150, 2175, 2200, 885.],
           [342, 860, 870, 880, 890, 900, 910, 920, 930, 374.]]]])
    assert np.allclose(result_arr, result_arr_ref)
コード例 #5
0
def test_convolutionBackpropFilters():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    output_shape = Shape([1, 1, 17, 17])

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    window_strides = [1, 1]
    window_dilation = [1, 1]
    padding_below = [0, 0]
    padding_above = [0, 0]
    data_dilation = [2, 2]

    output_arr = convolution2d(image_arr[0][0], filter_arr[0][0],
                               window_strides, window_dilation, padding_below,
                               padding_above,
                               data_dilation).reshape(1, 1, 17, 17)

    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, output_shape)
    parameter_list = [A, B]

    function = Function(
        NodeVector([
            ConvolutionBackpropFilters(A, filter_shape, B,
                                       Strides(window_strides),
                                       Strides(window_dilation),
                                       CoordinateDiff(padding_below),
                                       CoordinateDiff(padding_above),
                                       Strides(data_dilation))
        ]), parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    a = backend.make_primary_tensor_view(element_type, image_shape)
    b = backend.make_primary_tensor_view(element_type, output_shape)

    a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4)
    b.write(util.numpy_to_c(output_arr), 0, 17 * 17 * 4)

    result_arr = np.zeros(3 * 3, dtype=np.float32).reshape(1, 1, 3, 3)
    result = backend.make_primary_tensor_view(element_type, Shape([1, 1, 3,
                                                                   3]))
    result.write(util.numpy_to_c(result_arr), 0, 3 * 3 * 4)
    cf.call([result], [a, b])

    result.read(util.numpy_to_c(result_arr), 0, 3 * 3 * 4)
    result_arr_ref = np.array([[[[923832, 413952, 939870.],
                                 [425832, 190752, 432960.],
                                 [1084212, 485232, 1100250.]]]])
    assert np.allclose(result_arr, result_arr_ref)
コード例 #6
0
def test_convolution_with_data_dilation():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)
    strides = [1, 1]
    dilation = [1, 1]
    padding_below = [0, 0]
    padding_above = [0, 0]
    data_dilation = [2, 2]

    function = Function(
        NodeVector([
            Convolution(A, B, Strides(strides), Strides(dilation),
                        CoordinateDiff(padding_below),
                        CoordinateDiff(padding_above), Strides(data_dilation))
        ]), parameter_list, 'test')
    backend, cf = make_backend_call_frame(function)

    a = backend.make_primary_tensor_view(element_type, image_shape)
    b = backend.make_primary_tensor_view(element_type, filter_shape)

    a.write(util.numpy_to_c(image_arr), 0, 10 * 10 * 4)
    b.write(util.numpy_to_c(filter_arr), 0, 3 * 3 * 4)

    result_arr = np.zeros(17 * 17, dtype=np.float32).reshape(1, 1, 17, 17)
    result = backend.make_primary_tensor_view(element_type,
                                              Shape([1, 1, 17, 17]))
    result.write(util.numpy_to_c(result_arr), 0, 17 * 17 * 4)
    cf.call([result], [a, b])

    result.read(util.numpy_to_c(result_arr), 0, 17 * 17 * 4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation, padding_below, padding_above,
                                   data_dilation).reshape(1, 1, 17, 17)
    assert np.allclose(result_arr, result_arr_ref)
コード例 #7
0
def convolution(
        data_batch,  # type: Node
        filter_weights,  # type: Node
        filter_strides=None,  # type: List[int]
        filter_dilation_strides=None,  # type: List[int]
        padding_below=None,  # type: List[int]
        padding_above=None,  # type: List[int]
        data_dilation_strides=None,  # type: List[int]
        name=None,  # type: str
):
    # type: (...) -> Node
    """Return node performing batched convolution operation.

    :param data_batch: The node providing data batch tensor.
    :param filter_weights: The node providing filters tensor.
    :param filter_strides: The kernel window movement strides.
    :param filter_dilation_strides: The filters dilation strides.
    :param padding_below: The number of zero padding elements to add on each axis below 0
                          coordinate.
    :param padding_above: The number of zero padding elements to add on each axis above max
                          coordinate.
    :param data_dilation_strides: The data batch dilation strides.
    :param name: The optional new name for output node.
    :return: New node performing batched convolution operation.
    """
    spatial_dim_count = len(data_batch.shape) - 2
    if filter_strides is None:
        filter_strides = [1] * spatial_dim_count
    if filter_dilation_strides is None:
        filter_dilation_strides = [1] * spatial_dim_count
    if padding_above is None:
        padding_above = [0] * spatial_dim_count
    if padding_below is None:
        padding_below = [0] * spatial_dim_count
    if data_dilation_strides is None:
        data_dilation_strides = [1] * spatial_dim_count

    return Convolution(data_batch, filter_weights, Strides(filter_strides),
                       Strides(filter_dilation_strides),
                       CoordinateDiff(padding_below),
                       CoordinateDiff(padding_above),
                       Strides(data_dilation_strides))
コード例 #8
0
def convolution_backprop_data(
        data_batch_shape,  # type: TensorShape
        filters,  # type: Node
        output_delta,  # type: Node
        window_movement_strides_forward=None,  # type: List[int]
        window_dilation_strides_forward=None,  # type: List[int]
        padding_below_forward=None,  # type: List[int]
        padding_above_forward=None,  # type: List[int]
        data_dilation_strides_forward=None,  # type: List[int]
        name=None,  # type: str
):
    # type: (...) -> Node
    """Return node performing a batched-convolution data batch-backprop operation.

    :param data_batch_shape: The shape of the data batch from forward-prop.
    :param filters: The node producing the filters from forward-prop.
    :param output_delta: The node producing output delta.
    :param window_movement_strides_forward: The window movement strides from forward-prop.
    :param window_dilation_strides_forward: The window dilation strides from forward-prop.
    :param padding_below_forward: The padding-below sizes from forward-prop.
    :param padding_above_forward: The padding-above sizes from forward-prop.
    :param data_dilation_strides_forward: The data dilation strides from forward-prop.
    """
    spatial_dim_count = len(data_batch_shape) - 2
    if window_movement_strides_forward is None:
        window_movement_strides_forward = [1] * spatial_dim_count
    if window_dilation_strides_forward is None:
        window_dilation_strides_forward = [1] * spatial_dim_count
    if padding_below_forward is None:
        padding_below_forward = [0] * spatial_dim_count
    if padding_above_forward is None:
        padding_above_forward = [0] * spatial_dim_count
    if data_dilation_strides_forward is None:
        data_dilation_strides_forward = [1] * spatial_dim_count

    return ConvolutionBackpropData(Shape(data_batch_shape), filters,
                                   output_delta,
                                   Strides(window_movement_strides_forward),
                                   Strides(window_dilation_strides_forward),
                                   CoordinateDiff(padding_below_forward),
                                   CoordinateDiff(padding_above_forward),
                                   Strides(data_dilation_strides_forward))
コード例 #9
0
ファイル: test_ops.py プロジェクト: tomdol/ngraph
def test_convolution_with_padding():

    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = np.zeros(9, dtype=np.float32).reshape(1, 1, 3, 3)
    filter_arr[0][0][1][1] = 1
    strides = [1, 1]
    dilation = [2, 2]
    padding_below = [0, 0]
    padding_above = [0, 0]

    function = Function([
        Convolution(A, B, Strides(strides), Strides(dilation),
                    CoordinateDiff(padding_below),
                    CoordinateDiff(padding_above))
    ], parameter_list, 'test')
    backend = Backend.create(test.BACKEND_NAME)

    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)

    a.write(util.numpy_to_c(image_arr), 10 * 10 * 4)
    b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4)

    result_arr = np.zeros(36, dtype=np.float32).reshape(1, 1, 6, 6)
    result = backend.create_tensor(element_type, Shape([1, 1, 6, 6]))
    result.write(util.numpy_to_c(result_arr), 6 * 6 * 4)
    handle = backend.compile(function)
    handle.call([result], [a, b])

    result.read(util.numpy_to_c(result_arr), 6 * 6 * 4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation, padding_below,
                                   padding_above).reshape(1, 1, 6, 6)
    assert np.allclose(result_arr, result_arr_ref)

    # test with non-zero padding
    element_type = Type.f32
    image_shape = Shape([1, 1, 10, 10])
    filter_shape = Shape([1, 1, 3, 3])
    A = Parameter(element_type, image_shape)
    B = Parameter(element_type, filter_shape)
    parameter_list = [A, B]

    image_arr = np.arange(100, dtype=np.float32).reshape(1, 1, 10, 10)
    filter_arr = (np.ones(9, dtype=np.float32).reshape(1, 1, 3, 3)) * -1
    filter_arr[0][0][1][1] = 1
    strides = [1, 1]
    dilation = [2, 2]
    padding_below = [2, 1]
    padding_above = [1, 2]

    function = Function([
        Convolution(A, B, Strides(strides), Strides(dilation),
                    CoordinateDiff(padding_below),
                    CoordinateDiff(padding_above))
    ], parameter_list, 'test')
    backend = Backend.create(test.BACKEND_NAME)

    a = backend.create_tensor(element_type, image_shape)
    b = backend.create_tensor(element_type, filter_shape)

    a.write(util.numpy_to_c(image_arr), 10 * 10 * 4)
    b.write(util.numpy_to_c(filter_arr), 3 * 3 * 4)

    result_arr = np.zeros(81, dtype=np.float32).reshape(1, 1, 9, 9)
    result = backend.create_tensor(element_type, Shape([1, 1, 9, 9]))
    result.write(util.numpy_to_c(result_arr), 9 * 9 * 4)
    handle = backend.compile(function)
    handle.call([result], [a, b])

    result.read(util.numpy_to_c(result_arr), 9 * 9 * 4)
    result_arr_ref = convolution2d(image_arr[0][0], filter_arr[0][0], strides,
                                   dilation, padding_below,
                                   padding_above).reshape(1, 1, 9, 9)
    assert np.allclose(result_arr, result_arr_ref)