test_target_assign_op.py 5.3 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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
#
# 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.

import unittest
import numpy as np
import random
from op_test import OpTest


def gen_match_and_neg_indices(num_prior, gt_lod, neg_lod):
    if len(gt_lod) != len(neg_lod):
        raise AssertionError("The input arguments are illegal.")

    batch_size = len(gt_lod) - 1

    match_indices = -1 * np.ones((batch_size, num_prior)).astype('int32')
    neg_indices = np.zeros((neg_lod[-1], 1)).astype('int32')

    for n in range(batch_size):
        gt_num = gt_lod[n + 1] - gt_lod[n]
        ids = random.sample([i for i in range(num_prior)], gt_num)
        match_indices[n, ids] = [i for i in range(gt_num)]

        ret_ids = set([i for i in range(num_prior)]) - set(ids)
        s = neg_lod[n]
        e = neg_lod[n + 1]
        l = e - s
        neg_ids = random.sample(ret_ids, l)
        neg_indices[s:e, :] = np.array(neg_ids).astype('int32').reshape(l, 1)

    return match_indices, neg_indices


def target_assign(encoded_box, gt_label, match_indices, neg_indices, gt_lod,
46
                  neg_lod, mismatch_value):
47 48 49 50 51 52 53 54
    batch_size, num_prior = match_indices.shape

    # init target bbox
    trg_box = np.zeros((batch_size, num_prior, 4)).astype('float32')
    # init weight for target bbox
    trg_box_wt = np.zeros((batch_size, num_prior, 1)).astype('float32')
    # init target label
    trg_label = np.ones((batch_size, num_prior, 1)).astype('int32')
55
    trg_label = trg_label * mismatch_value
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    # init weight for target label
    trg_label_wt = np.zeros((batch_size, num_prior, 1)).astype('float32')

    for i in range(batch_size):
        cur_indices = match_indices[i]
        col_ids = np.where(cur_indices > -1)
        col_val = cur_indices[col_ids]

        gt_start = gt_lod[i]
        # target bbox
        for v, c in zip(col_val + gt_start, col_ids[0].tolist()):
            trg_box[i][c][:] = encoded_box[v][c][:]
        # weight for target bbox
        trg_box_wt[i][col_ids] = 1.0

        trg_label[i][col_ids] = gt_label[col_val + gt_start]
        trg_label_wt[i][col_ids] = 1.0
        # set target label weight to 1.0 for the negative samples
74 75 76
        if neg_indices is not None:
            neg_ids = neg_indices[neg_lod[i]:neg_lod[i + 1]]
            trg_label_wt[i][neg_ids] = 1.0
77 78 79 80

    return trg_box, trg_box_wt, trg_label, trg_label_wt


81
class TestTargetAssginFloatType(OpTest):
82 83
    def setUp(self):
        self.op_type = "target_assign"
84 85 86 87 88 89 90 91 92 93 94 95 96 97
        num_prior = 120
        num_class = 21
        gt_lod = [0, 5, 11, 23]
        neg_lod = [0, 4, 7, 13]
        mismatch_value = 0
        batch_size = len(gt_lod) - 1
        num_gt = gt_lod[-1]

        encoded_box = np.random.random((num_gt, num_prior, 4)).astype('float32')
        gt_label = np.random.randint(
            num_class, size=(num_gt, 1)).astype('int32')

        match_indices, neg_indices = gen_match_and_neg_indices(num_prior,
                                                               gt_lod, neg_lod)
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        out, out_wt, _, _ = target_assign(encoded_box, gt_label, match_indices,
                                          neg_indices, gt_lod, neg_lod,
                                          mismatch_value)

        # assign regression targets
        x = encoded_box
        self.inputs = {
            'X': (x, [gt_lod]),
            'MatchIndices': match_indices,
        }
        self.attrs = {'mismatch_value': mismatch_value}
        self.outputs = {
            'Out': out,
            'OutWeight': out_wt,
        }

    def test_check_output(self):
        self.check_output()


class TestTargetAssginIntType(OpTest):
    def setUp(self):
        self.op_type = "target_assign"
122 123 124 125
        num_prior = 120
        num_class = 21
        gt_lod = [0, 5, 11, 23]
        neg_lod = [0, 4, 7, 13]
126
        mismatch_value = 0
127 128 129 130 131 132
        batch_size = len(gt_lod) - 1
        num_gt = gt_lod[-1]

        encoded_box = np.random.random((num_gt, num_prior, 4)).astype('float32')
        gt_label = np.random.randint(
            num_class, size=(num_gt, 1)).astype('int32')
133

134 135 136
        match_indices, neg_indices = gen_match_and_neg_indices(num_prior,
                                                               gt_lod, neg_lod)

137 138 139 140 141 142
        _, _, out, out_wt, = target_assign(encoded_box, gt_label, match_indices,
                                           neg_indices, gt_lod, neg_lod,
                                           mismatch_value)

        # assign cassification argets
        x = np.reshape(gt_label, (num_gt, 1, 1))
143
        self.inputs = {
144 145
            'X': (x, [gt_lod]),
            'MatchIndices': match_indices,
146 147
            'NegIndices': (neg_indices, [neg_lod]),
        }
148
        self.attrs = {'mismatch_value': mismatch_value}
149
        self.outputs = {
150 151
            'Out': out,
            'OutWeight': out_wt,
152 153 154 155 156 157 158 159
        }

    def test_check_output(self):
        self.check_output()


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