cornerpool_lib.py 8.4 KB
Newer Older
W
wangguanzhong 已提交
1 2 3
import os
import paddle.fluid as fluid

W
wangguanzhong 已提交
4 5
use_cpp = False

W
wangguanzhong 已提交
6
file_dir = os.path.dirname(os.path.abspath(__file__))
W
wangguanzhong 已提交
7 8 9 10 11 12 13
try:
    fluid.load_op_library(os.path.join(file_dir, 'src/cornerpool_lib.so'))
    use_cpp = True
except:
    print(
        'Warning: cornerpool_lib.so not found, use python version instead which may drop the inference speed. Compile in ppdet/ext_op at first if you need cpp version.'
    )
W
wangguanzhong 已提交
14 15 16 17 18 19 20 21 22 23 24

from paddle.fluid.layer_helper import LayerHelper

__all__ = [
    'bottom_pool',
    'top_pool',
    'right_pool',
    'left_pool',
]


W
wangguanzhong 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
def cornerpool_op(layer_type, input, name):
    helper = LayerHelper(layer_type, input=input, name=name)
    dtype = helper.input_dtype()
    output = helper.create_variable_for_type_inference(dtype)
    max_map = helper.create_variable_for_type_inference(dtype)
    helper.append_op(
        type=layer_type,
        inputs={"X": input},
        outputs={"Output": output,
                 "MaxMap": max_map})
    return output


W
wangguanzhong 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
def bottom_pool(input, is_test=False, name=None):
    """
    This layer calculates the bottom pooling output based on the input.
    Scan the input from top to bottm for the vertical max-pooling.
    The output has the same shape with input.
    Args:
        input(Variable): This input is a Tensor with shape [N, C, H, W].
            The data type is float32 or float64.
    Returns:
        Variable(Tensor): The output of bottom_pool, with shape [N, C, H, W].
        The data type is float32 or float64.
    Examples:
        ..code-block:: python
            import paddle.fluid as fluid
            import cornerpool_lib
            input = fluid.data(
                name='input', shape=[2, 64, 10, 10], dtype='float32')
            output = corner_pool.bottom_pool(input)
    """
    if is_test:
W
wangguanzhong 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        if use_cpp:
            output = cornerpool_op("bottom_pool", input, name)
            return output

        def cond(i, output):
            return i < H

        def body(i, output):
            cur = fluid.layers.slice(output, [2], [i], [H])
            next = fluid.layers.slice(output, [2], [0], [H - i])
            max_v = fluid.layers.elementwise_max(cur, next)
            orig = fluid.layers.slice(output, [2], [0], [i])
            output = fluid.layers.concat([orig, max_v], axis=2)
            i = i * 2
            return [i, output]

        H = fluid.layers.shape(input)[2]
        i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1)
        output = input
        output = fluid.layers.while_loop(cond, body, [i, output])
        return output[-1]

W
wangguanzhong 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    H = input.shape[2]
    i = 1
    output = input
    while i < H:
        cur = output[:, :, i:, :]
        next = output[:, :, :H - i, :]
        max_v = fluid.layers.elementwise_max(cur, next)
        output = fluid.layers.concat([output[:, :, :i, :], max_v], axis=2)
        i *= 2

    return output


def top_pool(input, is_test=False, name=None):
    """
    This layer calculates the top pooling output based on the input.
    Scan the input from bottom to top for the vertical max-pooling.
    The output has the same shape with input.
    Args:
        input(Variable): This input is a Tensor with shape [N, C, H, W].
            The data type is float32 or float64.
    Returns:
        Variable(Tensor): The output of top_pool, with shape [N, C, H, W].
        The data type is float32 or float64.
    Examples:
        ..code-block:: python
            import paddle.fluid as fluid
            import cornerpool_lib
            input = fluid.data(
                name='input', shape=[2, 64, 10, 10], dtype='float32')
            output = corner_pool.top_pool(input)
    """
    if is_test:
W
wangguanzhong 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        if use_cpp:
            output = cornerpool_op("top_pool", input, name)
            return output

        def cond(i, output):
            return i < H

        def body(i, output):
            cur = fluid.layers.slice(output, [2], [0], [H - i])
            next = fluid.layers.slice(output, [2], [i], [H])
            max_v = fluid.layers.elementwise_max(cur, next)
            orig = fluid.layers.slice(output, [2], [H - i], [H])
            output = fluid.layers.concat([max_v, orig], axis=2)
            i = i * 2
            return [i, output]

        H = fluid.layers.shape(input)[2]
        i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1)
        output = input
        output = fluid.layers.while_loop(cond, body, [i, output])
        return output[-1]
W
wangguanzhong 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

    H = input.shape[2]
    i = 1
    output = input
    while i < H:
        cur = output[:, :, :H - i, :]
        next = output[:, :, i:, :]
        max_v = fluid.layers.elementwise_max(cur, next)
        output = fluid.layers.concat([max_v, output[:, :, H - i:, :]], axis=2)
        i *= 2

    return output


def right_pool(input, is_test=False, name=None):
    """
    This layer calculates the right pooling output based on the input.
    Scan the input from left to right for the horizontal max-pooling.
    The output has the same shape with input.
    Args:
        input(Variable): This input is a Tensor with shape [N, C, H, W].
            The data type is float32 or float64.
    Returns:
        Variable(Tensor): The output of right_pool, with shape [N, C, H, W].
        The data type is float32 or float64.
    Examples:
        ..code-block:: python
            import paddle.fluid as fluid
            import cornerpool_lib
            input = fluid.data(
                name='input', shape=[2, 64, 10, 10], dtype='float32')
            output = corner_pool.right_pool(input)
    """
    if is_test:
W
wangguanzhong 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        if use_cpp:
            output = cornerpool_op("right_pool", input, name)
            return output

        def cond(i, output):
            return i < W

        def body(i, output):
            cur = fluid.layers.slice(output, [3], [i], [W])
            next = fluid.layers.slice(output, [3], [0], [W - i])
            max_v = fluid.layers.elementwise_max(cur, next)
            orig = fluid.layers.slice(output, [3], [0], [i])
            output = fluid.layers.concat([orig, max_v], axis=-1)
            i = i * 2
            return [i, output]

        W = fluid.layers.shape(input)[3]
        i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1)
        output = input
        output = fluid.layers.while_loop(cond, body, [i, output])
        return output[-1]
W
wangguanzhong 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    W = input.shape[3]
    i = 1
    output = input
    while i < W:
        cur = output[:, :, :, i:]
        next = output[:, :, :, :W - i]
        max_v = fluid.layers.elementwise_max(cur, next)
        output = fluid.layers.concat([output[:, :, :, :i], max_v], axis=-1)
        i *= 2

    return output


def left_pool(input, is_test=False, name=None):
    """
    This layer calculates the left pooling output based on the input.
    Scan the input from right to left for the horizontal max-pooling.
    The output has the same shape with input.
    Args:
        input(Variable): This input is a Tensor with shape [N, C, H, W].
            The data type is float32 or float64.
    Returns:
        Variable(Tensor): The output of left_pool, with shape [N, C, H, W].
        The data type is float32 or float64.
    Examples:
        ..code-block:: python
            import paddle.fluid as fluid
            import cornerpool_lib
            input = fluid.data(
                name='input', shape=[2, 64, 10, 10], dtype='float32')
            output = corner_pool.left_pool(input)
    """
    if is_test:
W
wangguanzhong 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        if use_cpp:
            output = cornerpool_op("left_pool", input, name)
            return output

        def cond(i, output):
            return i < W

        def body(i, output):
            cur = fluid.layers.slice(output, [3], [0], [W - i])
            next = fluid.layers.slice(output, [3], [i], [W])
            max_v = fluid.layers.elementwise_max(cur, next)
            orig = fluid.layers.slice(output, [3], [W - i], [W])
            output = fluid.layers.concat([max_v, orig], axis=-1)
            i = i * 2
            return [i, output]

        W = fluid.layers.shape(input)[3]
        i = fluid.layers.fill_constant(shape=[1], dtype='int32', value=1)
        output = input
        output = fluid.layers.while_loop(cond, body, [i, output])
        return output[-1]
W
wangguanzhong 已提交
244 245 246 247 248 249 250 251 252 253 254 255

    W = input.shape[3]
    i = 1
    output = input
    while i < W:
        cur = output[:, :, :, :W - i]
        next = output[:, :, :, i:]
        max_v = fluid.layers.elementwise_max(cur, next)
        output = fluid.layers.concat([max_v, output[:, :, :, W - i:]], axis=-1)
        i *= 2

    return output