Exemplo n.º 1
0
    def test_global_pooling_layer(self):

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

        X = xlf.get_xop_factory_func('GlobalPooling')(op_name='gp1',
                                                      pool_type='Max',
                                                      layout='NCHW',
                                                      input_layer=iX)

        assert X.type[0] == 'Pooling'
        assert X.shapes == [1, 2, 1, 1]
        assert X.attrs['padding'] == [[0, 0], [0, 0], [0, 0], [0, 0]]
        assert X.attrs['insize'] == [7, 7]
        assert X.attrs['outsize'] == [1, 1]
        assert X.attrs['data_layout'] == 'NCHW'
        assert X.attrs['strides'] == [1, 1]
        assert X.attrs['kernel_size'] == [7, 7]
        assert X.attrs['pool_type'] == 'Max'

        from pyxir.graph.ops.l2_convolution import \
            pooling_layout_transform

        pooling_layout_transform(X, target_layout='NHWC')

        assert X.type[0] == 'Pooling'
        assert X.shapes == [1, 1, 1, 2]
        assert X.attrs['data_layout'] == 'NHWC'
Exemplo n.º 2
0
def global_pool2d_test_util(
    in_shape,
    out_shape,
    pool_type,
    data_layout="NCHW",
):
    iX = px.ops.input("in", shape=list(in_shape))
    X = px.ops.global_pool2d(
        op_name="pool",
        input_layer=iX,
        pool_type=pool_type,
        layout=data_layout,
    )
    expected_padding = [[0, 0], [0, 0], [0, 0], [0, 0]]

    layout_idx = tuple(["NCHW".index(e) for e in data_layout])
    B_idx, C_idx, H_idx, W_idx = layout_idx

    assert X.type[0] == "Pooling"
    assert X.shapes == list(
        out_shape), "Expected out shape: {0}, but got: {1}".format(
            out_shape, X.shapes)
    assert (X.attrs["padding"] == expected_padding
            ), "Expected padding: {0}, but got: {1}".format(
                expected_padding, X.attrs["padding"])

    assert X.attrs["insize"] == [in_shape[H_idx], in_shape[W_idx]]
    assert X.attrs["outsize"] == [1, 1]
    assert X.attrs["data_layout"] == data_layout
    assert X.attrs["strides"] == [1, 1]
    assert X.attrs["kernel_size"] == [in_shape[H_idx], in_shape[W_idx]]
    assert X.attrs["pool_type"] == pool_type

    # Test layout transform
    pooling_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] == "Pooling"
    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"
    assert (X.attrs["padding"] == expected_padding
            ), "Expected padding: {0}, but got: {1}".format(
                expected_padding, X.attrs["padding"])
Exemplo n.º 3
0
    def test_pooling_layer(self):

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

        X = xlf.get_xop_factory_func('Pooling')(op_name='pool1',
                                                input_layer=iX,
                                                pool_type='Avg',
                                                pool_size=[3, 3],
                                                strides=[2, 2],
                                                padding=[1, 1],
                                                layout='NCHW',
                                                ceil_mode=True,
                                                count_include_pad=True)

        assert X.type[0] == 'Pooling'
        assert X.shapes == [1, 2, 3, 3]
        assert X.attrs['padding'] == [[0, 0], [0, 0], [1, 1], [1, 1]]
        assert X.attrs['insize'] == [5, 5]
        assert X.attrs['outsize'] == [3, 3]
        assert X.attrs['data_layout'] == 'NCHW'
        assert X.attrs['strides'] == [2, 2]
        assert X.attrs['kernel_size'] == [3, 3]
        assert X.attrs['pool_type'] == 'Avg'

        from pyxir.graph.ops.l2_convolution import \
            pooling_layout_transform

        pooling_layout_transform(X, target_layout='NHWC')

        assert X.type[0] == 'Pooling'
        assert X.shapes == [1, 3, 3, 2]
        assert X.attrs['padding'] == [[0, 0], [1, 1], [1, 1], [0, 0]]
        assert X.attrs['data_layout'] == 'NHWC'
Exemplo n.º 4
0
def pool2d_test_util(
        in_shape,
        out_shape,
        pool_type,
        pool_size,
        padding=(0, 0, 0, 0),
        strides=(1, 1),
        data_layout="NCHW",
):
    iX = px.ops.input("in", shape=list(in_shape))
    X = px.ops.pool2d(
        op_name="pool",
        input_layer=iX,
        pool_type=pool_type,
        pool_size=list(pool_size),
        strides=list(strides),
        padding=list(padding),
        layout=data_layout,
        ceil_mode=True,
        count_include_pad=True,
    )

    layout_idx = tuple(["NCHW".index(e) for e in data_layout])
    B_idx, C_idx, H_idx, W_idx = layout_idx

    padding_nchw = [
        [0, 0],
        [0, 0],
        [padding[0], padding[2]],
        [padding[1], padding[3]],
    ]
    padding = [padding_nchw[e] for e in layout_idx]

    assert X.type[0] == "Pooling"
    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["insize"] == [in_shape[H_idx], in_shape[W_idx]]
    assert X.attrs["outsize"] == [out_shape[H_idx], out_shape[W_idx]]
    assert X.attrs["data_layout"] == data_layout
    assert X.attrs["strides"] == list(strides)
    assert X.attrs["kernel_size"] == list(pool_size)
    assert X.attrs["pool_type"] == pool_type

    # Test layout transform
    pooling_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] == "Pooling"
    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"])