test_yolo_box_op.py 3.6 KB
Newer Older
D
dengkaipeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

from __future__ import division

import unittest
import numpy as np
from op_test import OpTest

from paddle.fluid import core


def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-1.0 * x))


def YoloBox(x, attrs):
    n, c, h, w = x.shape
    anchors = attrs['anchors']
    an_num = int(len(anchors) // 2)
    class_num = attrs['class_num']
    conf_thresh = attrs['conf_thresh']
    downsample = attrs['downsample']
    input_size = downsample * h

    x = x.reshape((n, an_num, 5 + class_num, h, w)).transpose((0, 1, 3, 4, 2))

    pred_box = x[:, :, :, :, :4].copy()
    grid_x = np.tile(np.arange(w).reshape((1, w)), (h, 1))
    grid_y = np.tile(np.arange(h).reshape((h, 1)), (1, w))
    pred_box[:, :, :, :, 0] = (grid_x + sigmoid(pred_box[:, :, :, :, 0])) / w
    pred_box[:, :, :, :, 1] = (grid_y + sigmoid(pred_box[:, :, :, :, 1])) / h

    anchors = [(anchors[i], anchors[i + 1]) for i in range(0, len(anchors), 2)]
    anchors_s = np.array(
        [(an_w / input_size, an_h / input_size) for an_w, an_h in anchors])
    anchor_w = anchors_s[:, 0:1].reshape((1, an_num, 1, 1))
    anchor_h = anchors_s[:, 1:2].reshape((1, an_num, 1, 1))
    pred_box[:, :, :, :, 2] = np.exp(pred_box[:, :, :, :, 2]) * anchor_w
    pred_box[:, :, :, :, 3] = np.exp(pred_box[:, :, :, :, 3]) * anchor_h

    pred_conf = sigmoid(x[:, :, :, :, 4:5])
    pred_conf[pred_conf < conf_thresh] = 0.
    pred_score = sigmoid(x[:, :, :, :, 5:]) * pred_conf
    pred_box = pred_box * (pred_conf > 0.).astype('float32')

    pred_box = pred_box.reshape((n, -1, 4))
    pred_box[:, :, :
             2], pred_box[:, :, 2:
                          4] = pred_box[:, :, :
                                        2] - pred_box[:, :, 2:
                                                      4] / 2., pred_box[:, :, :
                                                                        2] + pred_box[:, :,
                                                                                      2:
                                                                                      4] / 2.0
    pred_box = pred_box * input_size

    return pred_box, pred_score.reshape((n, -1, class_num))


class TestYoloBoxOp(OpTest):
    def setUp(self):
        self.initTestCase()
        self.op_type = 'yolo_box'
        x = np.random.random(self.x_shape).astype('float32')

        self.attrs = {
            "anchors": self.anchors,
            "class_num": self.class_num,
            "conf_thresh": self.conf_thresh,
            "downsample": self.downsample,
        }

        self.inputs = {'X': x, }
        boxes, scores = YoloBox(x, self.attrs)
        self.outputs = {
            "Boxes": boxes,
            "Scores": scores,
        }

    def test_check_output(self):
        self.check_output()

    def initTestCase(self):
        self.anchors = [10, 13, 16, 30, 33, 23]
        an_num = int(len(self.anchors) // 2)
        self.class_num = 2
        self.conf_thresh = 0.5
        self.downsample = 32
        self.x_shape = (3, an_num * (5 + self.class_num), 5, 5)


if __name__ == "__main__":
    unittest.main()