test_target_assign_op.py 5.5 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
#
# 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

17 18 19
import unittest
import numpy as np
import random
20
from op_test import OpTest
21 22 23 24 25 26


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.")

27
    batch_size = len(gt_lod)
28 29

    match_indices = -1 * np.ones((batch_size, num_prior)).astype('int32')
30
    neg_indices = np.zeros((sum(neg_lod), 1)).astype('int32')
31

32
    offset = 0
33
    for n in range(batch_size):
34
        gt_num = gt_lod[n]
35 36 37 38
        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)
39
        l = neg_lod[n]
40
        neg_ids = random.sample(ret_ids, l)
41 42 43
        neg_indices[offset:offset + neg_lod[n], :] = np.array(neg_ids).astype(
            'int32').reshape(l, 1)
        offset += neg_lod[n]
44 45 46 47 48

    return match_indices, neg_indices


def target_assign(encoded_box, gt_label, match_indices, neg_indices, gt_lod,
49
                  neg_lod, mismatch_value):
50 51 52 53 54 55 56 57
    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')
58
    trg_label = trg_label * mismatch_value
59 60 61
    # init weight for target label
    trg_label_wt = np.zeros((batch_size, num_prior, 1)).astype('float32')

62 63
    gt_offset = 0
    neg_offset = 0
64 65 66 67 68 69
    for i in range(batch_size):
        cur_indices = match_indices[i]
        col_ids = np.where(cur_indices > -1)
        col_val = cur_indices[col_ids]

        # target bbox
70
        for v, c in zip(col_val + gt_offset, col_ids[0].tolist()):
71 72 73 74
            trg_box[i][c][:] = encoded_box[v][c][:]
        # weight for target bbox
        trg_box_wt[i][col_ids] = 1.0

75
        trg_label[i][col_ids] = gt_label[col_val + gt_offset]
76 77
        trg_label_wt[i][col_ids] = 1.0
        # set target label weight to 1.0 for the negative samples
78
        if neg_indices is not None:
79
            neg_ids = neg_indices[neg_offset:neg_offset + neg_lod[i]]
80
            trg_label_wt[i][neg_ids] = 1.0
81 82 83
        # update offset
        gt_offset += gt_lod[i]
        neg_offset += neg_lod[i]
84 85 86 87

    return trg_box, trg_box_wt, trg_label, trg_label_wt


88
class TestTargetAssginFloatType(OpTest):
89 90
    def setUp(self):
        self.op_type = "target_assign"
91 92
        num_prior = 120
        num_class = 21
93 94
        gt_lod = [5, 6, 12]
        neg_lod = [4, 3, 6]
95
        mismatch_value = 0
96 97
        batch_size = len(gt_lod)
        num_gt = sum(gt_lod)
98 99 100 101 102 103 104

        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)
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        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"
129 130
        num_prior = 120
        num_class = 21
131 132
        gt_lod = [5, 6, 12]
        neg_lod = [4, 3, 6]
133
        mismatch_value = 0
134 135
        batch_size = len(gt_lod)
        num_gt = sum(gt_lod)
136 137 138 139

        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')
140

141 142 143
        match_indices, neg_indices = gen_match_and_neg_indices(num_prior,
                                                               gt_lod, neg_lod)

144 145 146 147 148 149
        _, _, 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))
150
        self.inputs = {
151 152
            'X': (x, [gt_lod]),
            'MatchIndices': match_indices,
153 154
            'NegIndices': (neg_indices, [neg_lod]),
        }
155
        self.attrs = {'mismatch_value': mismatch_value}
156
        self.outputs = {
157 158
            'Out': out,
            'OutWeight': out_wt,
159 160 161 162 163 164 165 166
        }

    def test_check_output(self):
        self.check_output()


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