示例#1
0
    def test_convolution_layer_tfl(self):

        iX = XLayer(type=['Input'],
                    name='in1',
                    shapes=[1, 3, 3, 2],
                    sizes=[32],
                    bottoms=[],
                    tops=[],
                    targets=[])

        kX = XLayer(type=['Constant'],
                    name='kernel',
                    shapes=[4, 3, 3, 2],
                    sizes=[54],
                    data=[
                        np.transpose(np.ones((4, 2, 3, 3), dtype=np.float32),
                                     (0, 2, 3, 1))
                    ],
                    bottoms=[],
                    tops=[],
                    targets=[])

        X = xlf.get_xop_factory_func('Convolution')(op_name='conv1',
                                                    kernel_size=[3, 3],
                                                    strides=[1, 1],
                                                    padding_hw=[1, 1],
                                                    dilation=[1, 1],
                                                    groups=1,
                                                    channels=4,
                                                    data_layout='NHWC',
                                                    kernel_layout='OHWI',
                                                    input_layer=iX,
                                                    weights_layer=kX)

        assert X.type[0] == 'Convolution'
        assert X.shapes == [1, 3, 3, 4]
        assert X.attrs['padding'] == [[0, 0], [1, 1], [1, 1], [0, 0]]
        assert X.attrs['data_layout'] == 'NHWC'
        assert X.attrs['kernel_layout'] == 'OIHW'
        assert X.attrs['shape'] == [1, 3, 3, 4]
        assert X.attrs['kernel_size'] == [3, 3]
        assert X.attrs['strides'] == [1, 1]
        assert X.attrs['groups'] == 1
        assert X.attrs['dilation'] == [1, 1]
        assert X.attrs['channels'] == [2, 4]

        np.testing.assert_array_equal(X.data.weights,
                                      np.ones((4, 2, 3, 3), dtype=np.float32))
        np.testing.assert_array_equal(X.data.biases,
                                      np.zeros((4), dtype=np.float32))

        from pyxir.graph.ops.l2_convolution import \
            conv2d_layout_transform

        conv2d_layout_transform(X, target_layout='NCHW')

        assert X.type[0] == 'Convolution'
        assert X.shapes == [1, 4, 3, 3]
        assert X.attrs['data_layout'] == 'NCHW'
        assert X.attrs['padding'] == [[0, 0], [0, 0], [1, 1], [1, 1]]
示例#2
0
def conv2d_test_util(
    in_shape,
    weight_shape,
    out_shape,
    padding=(0, 0),
    strides=(1, 1),
    dilation=(1, 1),
    groups=1,
    data_layout="NCHW",
    kernel_layout="OIHW",
    target_kernel_layout="OIHW",
    conv_transpose=False,
):
    iX = px.ops.input("in", shape=list(in_shape))
    kX = px.ops.constant("kernel", np.ones(weight_shape, dtype=np.float32))
    kernel_size = [
        weight_shape[kernel_layout.index("H")],
        weight_shape[kernel_layout.index("W")],
    ]

    if not conv_transpose:
        in_ch = weight_shape[kernel_layout.index("I")] * groups
        channels = weight_shape[kernel_layout.index("O")]
        expected_type = "Convolution"
        X = px.ops.conv2d(
            op_name="conv",
            input_layer=iX,
            weights_layer=kX,
            kernel_size=kernel_size,
            strides=list(strides),
            padding_hw=list(padding),
            dilation=list(dilation),
            groups=groups,
            channels=channels,
            data_layout=data_layout,
            kernel_layout=kernel_layout,
            target_kernel_layout=target_kernel_layout,
        )
    else:
        in_ch = weight_shape[kernel_layout.index("I")] * groups
        channels = weight_shape[kernel_layout.index("O")]
        expected_type = "Conv2DTranspose"
        X = px.ops.conv2d_transpose(
            op_name="conv",
            input_layer=iX,
            weights_layer=kX,
            kernel_size=kernel_size,
            strides=list(strides),
            padding_hw=list(padding),
            dilation=list(dilation),
            groups=groups,
            channels=channels,
            data_layout=data_layout,
            kernel_layout=kernel_layout,
            target_kernel_layout=target_kernel_layout,
        )
        # OIHW
        weight_shape = (
            weight_shape[1],
            weight_shape[0],
            weight_shape[2],
            weight_shape[3],
        )

    layout_idx = tuple(["NCHW".index(e) for e in data_layout])
    padding_nchw = [
        [0, 0],
        [0, 0],
        [padding[0], padding[1]],
        [padding[2], padding[3]],
    ]
    padding = [padding_nchw[e] for e in layout_idx]

    assert X.type[0] == expected_type
    assert X.shapes == list(
        out_shape), "Expected out shape: {0}, but got: {1}".format(
            out_shape, X.shapes)
    assert X.attrs[
        "padding"] == padding, "Expected padding: {0}, but got: {1}".format(
            padding, X.attrs["padding"])
    assert X.attrs["data_layout"] == data_layout
    assert X.attrs["kernel_layout"] == target_kernel_layout
    assert X.attrs["shape"] == list(out_shape)
    assert X.attrs["kernel_size"] == kernel_size
    assert X.attrs["strides"] == list(strides)
    assert X.attrs["groups"] == groups
    assert X.attrs["dilation"] == list(dilation)
    assert X.attrs["channels"] == [in_ch, channels]

    np.testing.assert_array_equal(X.data.weights,
                                  np.ones(weight_shape, dtype=np.float32))
    np.testing.assert_array_equal(
        X.data.biases,
        np.zeros((channels, ), dtype=np.float32),
    )

    conv2d_layout_transform(X, target_layout="NHWC")

    layout_idx = tuple([data_layout.index(e) for e in "NHWC"])
    trans_layout_idx = tuple(["NCHW".index(e) for e in "NHWC"])
    trans_out_shape = [out_shape[e] for e in layout_idx]
    assert X.type[0] == expected_type
    assert X.shapes == trans_out_shape, "Expected out shape: {0}, but got: {1}".format(
        trans_out_shape, X.shapes)
    assert X.attrs["data_layout"] == "NHWC"
    padding = [padding_nchw[e] for e in trans_layout_idx]
    assert X.attrs[
        "padding"] == padding, "Expected padding: {0}, but got: {1}".format(
            padding, X.attrs["padding"])