test_box_coder_op.py 8.6 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
G
gaoyuan 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

G
gaoyuan 已提交
17 18 19 20
import unittest
import numpy as np
import sys
import math
21
from op_test import OpTest
G
gaoyuan 已提交
22 23


24 25 26 27 28 29 30 31 32 33 34 35 36
def box_decoder(t_box, p_box, pb_v, output_box, norm, axis=0):
    pb_w = p_box[:, 2] - p_box[:, 0] + (norm == False)
    pb_h = p_box[:, 3] - p_box[:, 1] + (norm == False)
    pb_x = pb_w * 0.5 + p_box[:, 0]
    pb_y = pb_h * 0.5 + p_box[:, 1]
    shape = (1, p_box.shape[0]) if axis == 0 else (p_box.shape[0], 1)

    pb_w = pb_w.reshape(shape)
    pb_h = pb_h.reshape(shape)
    pb_x = pb_x.reshape(shape)
    pb_y = pb_y.reshape(shape)

    if pb_v.ndim == 2:
37 38 39
        var_shape = (1, pb_v.shape[0], pb_v.shape[1]) if axis == 0 else (
            pb_v.shape[0], 1, pb_v.shape[1])
        pb_v = pb_v.reshape(var_shape)
40 41 42 43 44
    if pb_v.ndim == 1:
        tb_x = pb_v[0] * t_box[:, :, 0] * pb_w + pb_x
        tb_y = pb_v[1] * t_box[:, :, 1] * pb_h + pb_y
        tb_w = np.exp(pb_v[2] * t_box[:, :, 2]) * pb_w
        tb_h = np.exp(pb_v[3] * t_box[:, :, 3]) * pb_h
J
jerrywgz 已提交
45
    else:
46 47 48 49 50 51 52 53 54
        tb_x = pb_v[:, :, 0] * t_box[:, :, 0] * pb_w + pb_x
        tb_y = pb_v[:, :, 1] * t_box[:, :, 1] * pb_h + pb_y
        tb_w = np.exp(pb_v[:, :, 2] * t_box[:, :, 2]) * pb_w
        tb_h = np.exp(pb_v[:, :, 3] * t_box[:, :, 3]) * pb_h
    output_box[:, :, 0] = tb_x - tb_w / 2
    output_box[:, :, 1] = tb_y - tb_h / 2
    output_box[:, :, 2] = tb_x + tb_w / 2 - (not norm)
    output_box[:, :, 3] = tb_y + tb_h / 2 - (not norm)

G
gaoyuan 已提交
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
def box_encoder(t_box, p_box, pb_v, output_box, norm):
    pb_w = p_box[:, 2] - p_box[:, 0] + (norm == False)
    pb_h = p_box[:, 3] - p_box[:, 1] + (norm == False)
    pb_x = pb_w * 0.5 + p_box[:, 0]
    pb_y = pb_h * 0.5 + p_box[:, 1]
    shape = (1, p_box.shape[0])

    pb_w = pb_w.reshape(shape)
    pb_h = pb_h.reshape(shape)
    pb_x = pb_x.reshape(shape)
    pb_y = pb_y.reshape(shape)

    if pb_v.ndim == 2:
        pb_v = pb_v.reshape(1, pb_v.shape[0], pb_v.shape[1])
    tb_x = ((t_box[:, 2] + t_box[:, 0]) / 2).reshape(t_box.shape[0], 1)
    tb_y = ((t_box[:, 3] + t_box[:, 1]) / 2).reshape(t_box.shape[0], 1)
    tb_w = (t_box[:, 2] - t_box[:, 0]).reshape(t_box.shape[0], 1) + (not norm)
    tb_h = (t_box[:, 3] - t_box[:, 1]).reshape(t_box.shape[0], 1) + (not norm)
    if pb_v.ndim == 1:
        output_box[:, :, 0] = (tb_x - pb_x) / pb_w / pb_v[0]
        output_box[:, :, 1] = (tb_y - pb_y) / pb_h / pb_v[1]
        output_box[:, :, 2] = np.log(np.fabs(tb_w / pb_w)) / pb_v[2]
        output_box[:, :, 3] = np.log(np.fabs(tb_h / pb_h)) / pb_v[3]
    else:
        output_box[:, :, 0] = (tb_x - pb_x) / pb_w / pb_v[:, :, 0]
        output_box[:, :, 1] = (tb_y - pb_y) / pb_h / pb_v[:, :, 1]
        output_box[:, :, 2] = np.log(np.fabs(tb_w / pb_w)) / pb_v[:, :, 2]
        output_box[:, :, 3] = np.log(np.fabs(tb_h / pb_h)) / pb_v[:, :, 3]
G
gaoyuan 已提交
84 85


86 87 88
def batch_box_coder(p_box, pb_v, t_box, lod, code_type, norm, axis=0):
    n = t_box.shape[0]
    m = p_box.shape[0]
J
jerrywgz 已提交
89
    if code_type == "DecodeCenterSize":
90
        m = t_box.shape[1]
G
gaoyuan 已提交
91
    output_box = np.zeros((n, m, 4), dtype=np.float32)
92 93
    cur_offset = 0
    for i in range(len(lod)):
Y
Yuan Gao 已提交
94
        if (code_type == "EncodeCenterSize"):
95 96 97
            box_encoder(t_box[cur_offset:(cur_offset + lod[i]), :], p_box, pb_v,
                        output_box[cur_offset:(cur_offset + lod[i]), :, :],
                        norm)
Y
Yuan Gao 已提交
98
        elif (code_type == "DecodeCenterSize"):
99
            box_decoder(t_box, p_box, pb_v, output_box, norm, axis)
100
        cur_offset += lod[i]
G
gaoyuan 已提交
101 102 103 104 105 106 107 108 109
    return output_box


class TestBoxCoderOp(OpTest):
    def test_check_output(self):
        self.check_output()

    def setUp(self):
        self.op_type = "box_coder"
110
        lod = [[1, 1, 1, 1, 1]]
111 112 113
        prior_box = np.random.random((81, 4)).astype('float32')
        prior_box_var = np.random.random((81, 4)).astype('float32')
        target_box = np.random.random((20, 81, 4)).astype('float32')
G
gaoyuan 已提交
114
        code_type = "DecodeCenterSize"
115
        box_normalized = False
J
jerrywgz 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
        output_box = batch_box_coder(prior_box, prior_box_var, target_box,
                                     lod[0], code_type, box_normalized)
        self.inputs = {
            'PriorBox': prior_box,
            'PriorBoxVar': prior_box_var,
            'TargetBox': target_box,
        }
        self.attrs = {
            'code_type': 'decode_center_size',
            'box_normalized': False
        }
        self.outputs = {'OutputBox': output_box}


130 131 132 133 134 135 136
class TestBoxCoderOpWithoutBoxVar(OpTest):
    def test_check_output(self):
        self.check_output()

    def setUp(self):
        self.op_type = "box_coder"
        lod = [[0, 1, 2, 3, 4, 5]]
137 138 139
        prior_box = np.random.random((81, 4)).astype('float32')
        prior_box_var = np.ones((81, 4)).astype('float32')
        target_box = np.random.random((20, 81, 4)).astype('float32')
140 141 142 143 144 145 146 147 148 149 150 151
        code_type = "DecodeCenterSize"
        box_normalized = False
        output_box = batch_box_coder(prior_box, prior_box_var, target_box,
                                     lod[0], code_type, box_normalized)

        self.inputs = {
            'PriorBox': prior_box,
            'TargetBox': target_box,
        }
        self.attrs = {
            'code_type': 'decode_center_size',
            'box_normalized': False
152
        }
G
gaoyuan 已提交
153 154 155 156 157 158 159 160 161
        self.outputs = {'OutputBox': output_box}


class TestBoxCoderOpWithLoD(OpTest):
    def test_check_output(self):
        self.check_output()

    def setUp(self):
        self.op_type = "box_coder"
162 163 164 165
        lod = [[10, 20, 20]]
        prior_box = np.random.random((20, 4)).astype('float32')
        prior_box_var = np.random.random((20, 4)).astype('float32')
        target_box = np.random.random((50, 4)).astype('float32')
G
gaoyuan 已提交
166
        code_type = "EncodeCenterSize"
167
        box_normalized = True
G
gaoyuan 已提交
168
        output_box = batch_box_coder(prior_box, prior_box_var, target_box,
169
                                     lod[0], code_type, box_normalized)
G
gaoyuan 已提交
170 171 172 173 174 175

        self.inputs = {
            'PriorBox': prior_box,
            'PriorBoxVar': prior_box_var,
            'TargetBox': (target_box, lod),
        }
176
        self.attrs = {'code_type': 'encode_center_size', 'box_normalized': True}
G
gaoyuan 已提交
177 178 179
        self.outputs = {'OutputBox': output_box}


J
jerrywgz 已提交
180 181 182 183 184 185 186
class TestBoxCoderOpWithAxis(OpTest):
    def test_check_output(self):
        self.check_output()

    def setUp(self):
        self.op_type = "box_coder"
        lod = [[1, 1, 1, 1, 1]]
187
        prior_box = np.random.random((30, 4)).astype('float32')
188
        prior_box_var = np.random.random((30, 4)).astype('float32')
189
        target_box = np.random.random((30, 81, 4)).astype('float32')
J
jerrywgz 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        code_type = "DecodeCenterSize"
        box_normalized = False
        axis = 1
        output_box = batch_box_coder(prior_box, prior_box_var, target_box,
                                     lod[0], code_type, box_normalized, axis)

        self.inputs = {
            'PriorBox': prior_box,
            'PriorBoxVar': prior_box_var,
            'TargetBox': target_box,
        }
        self.attrs = {
            'code_type': 'decode_center_size',
            'box_normalized': False,
            'axis': axis
        }
        self.outputs = {'OutputBox': output_box}


209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
class TestBoxCoderOpWithVariance(OpTest):
    def test_check_output(self):
        self.check_output()

    def setUp(self):
        self.op_type = "box_coder"
        lod = [[1, 1, 1, 1, 1]]
        prior_box = np.random.random((30, 4)).astype('float32')
        prior_box_var = np.random.random((4)).astype('float32')
        target_box = np.random.random((30, 81, 4)).astype('float32')
        code_type = "DecodeCenterSize"
        box_normalized = False
        axis = 1
        output_box = batch_box_coder(prior_box, prior_box_var, target_box,
                                     lod[0], code_type, box_normalized, axis)

        self.inputs = {
            'PriorBox': prior_box,
            'TargetBox': target_box,
        }
        self.attrs = {
            'code_type': 'decode_center_size',
            'box_normalized': False,
            'variance': prior_box_var.astype(np.float).flatten(),
            'axis': axis
        }
        self.outputs = {'OutputBox': output_box}


G
gaoyuan 已提交
238 239
if __name__ == '__main__':
    unittest.main()