test_target_assign_op.py 5.4 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
#
# 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.")

25
    batch_size = len(gt_lod)
26 27

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

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

    return match_indices, neg_indices


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

60 61
    gt_offset = 0
    neg_offset = 0
62 63 64 65 66 67
    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
68
        for v, c in zip(col_val + gt_offset, col_ids[0].tolist()):
69 70 71 72
            trg_box[i][c][:] = encoded_box[v][c][:]
        # weight for target bbox
        trg_box_wt[i][col_ids] = 1.0

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

    return trg_box, trg_box_wt, trg_label, trg_label_wt


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

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

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

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

139 140 141
        match_indices, neg_indices = gen_match_and_neg_indices(num_prior,
                                                               gt_lod, neg_lod)

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

    def test_check_output(self):
        self.check_output()


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