test_retinanet_detection_output.py 20.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#  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.

import unittest
import numpy as np
import math
from op_test import OpTest
from test_anchor_generator_op import anchor_generator_in_python
from test_multiclass_nms_op import nms
21 22
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
H
hong 已提交
23
import paddle
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44


def multiclass_nms(prediction, class_num, keep_top_k, nms_threshold):
    selected_indices = {}
    num_det = 0
    for c in range(class_num):
        if c not in prediction.keys():
            continue
        cls_dets = prediction[c]
        all_scores = np.zeros(len(cls_dets))
        for i in range(all_scores.shape[0]):
            all_scores[i] = cls_dets[i][4]
        indices = nms(cls_dets, all_scores, 0.0, nms_threshold, -1, False, 1.0)
        selected_indices[c] = indices
        num_det += len(indices)

    score_index = []
    for c, indices in selected_indices.items():
        for idx in indices:
            score_index.append((prediction[c][idx][4], c, idx))

45 46 47
    sorted_score_index = sorted(score_index,
                                key=lambda tup: tup[0],
                                reverse=True)
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
    if keep_top_k > -1 and num_det > keep_top_k:
        sorted_score_index = sorted_score_index[:keep_top_k]
        num_det = keep_top_k
    nmsed_outs = []
    for s, c, idx in sorted_score_index:
        xmin = prediction[c][idx][0]
        ymin = prediction[c][idx][1]
        xmax = prediction[c][idx][2]
        ymax = prediction[c][idx][3]
        nmsed_outs.append([c + 1, s, xmin, ymin, xmax, ymax])

    return nmsed_outs, num_det


def retinanet_detection_out(boxes_list, scores_list, anchors_list, im_info,
                            score_threshold, nms_threshold, nms_top_k,
                            keep_top_k):
    class_num = scores_list[0].shape[-1]
    im_height, im_width, im_scale = im_info

    num_level = len(scores_list)
    prediction = {}
    for lvl in range(num_level):
        scores_per_level = scores_list[lvl]
        scores_per_level = scores_per_level.flatten()
        bboxes_per_level = boxes_list[lvl]
        bboxes_per_level = bboxes_per_level.flatten()
        anchors_per_level = anchors_list[lvl]
        anchors_per_level = anchors_per_level.flatten()

        thresh = score_threshold if lvl < (num_level - 1) else 0.0
        selected_indices = np.argwhere(scores_per_level > thresh)
        scores = scores_per_level[selected_indices]
        sorted_indices = np.argsort(-scores, axis=0, kind='mergesort')
        if nms_top_k > -1 and nms_top_k < sorted_indices.shape[0]:
            sorted_indices = sorted_indices[:nms_top_k]

        for i in range(sorted_indices.shape[0]):
            idx = selected_indices[sorted_indices[i]]
            idx = idx[0][0]
            a = int(idx / class_num)
            c = int(idx % class_num)
            box_offset = a * 4
            anchor_box_width = anchors_per_level[
                box_offset + 2] - anchors_per_level[box_offset] + 1
            anchor_box_height = anchors_per_level[
                box_offset + 3] - anchors_per_level[box_offset + 1] + 1
            anchor_box_center_x = anchors_per_level[
                box_offset] + anchor_box_width / 2
            anchor_box_center_y = anchors_per_level[box_offset +
                                                    1] + anchor_box_height / 2

            target_box_center_x = bboxes_per_level[
                box_offset] * anchor_box_width + anchor_box_center_x
            target_box_center_y = bboxes_per_level[
                box_offset + 1] * anchor_box_height + anchor_box_center_y
104 105 106 107
            target_box_width = math.exp(
                bboxes_per_level[box_offset + 2]) * anchor_box_width
            target_box_height = math.exp(
                bboxes_per_level[box_offset + 3]) * anchor_box_height
108 109 110 111 112 113 114 115 116 117 118 119

            pred_box_xmin = target_box_center_x - target_box_width / 2
            pred_box_ymin = target_box_center_y - target_box_height / 2
            pred_box_xmax = target_box_center_x + target_box_width / 2 - 1
            pred_box_ymax = target_box_center_y + target_box_height / 2 - 1

            pred_box_xmin = pred_box_xmin / im_scale
            pred_box_ymin = pred_box_ymin / im_scale
            pred_box_xmax = pred_box_xmax / im_scale
            pred_box_ymax = pred_box_ymax / im_scale

            pred_box_xmin = max(
120 121
                min(pred_box_xmin,
                    np.round(im_width / im_scale) - 1), 0.)
122
            pred_box_ymin = max(
123 124
                min(pred_box_ymin,
                    np.round(im_height / im_scale) - 1), 0.)
125
            pred_box_xmax = max(
126 127
                min(pred_box_xmax,
                    np.round(im_width / im_scale) - 1), 0.)
128
            pred_box_ymax = max(
129 130
                min(pred_box_ymax,
                    np.round(im_height / im_scale) - 1), 0.)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

            if c not in prediction.keys():
                prediction[c] = []
            prediction[c].append([
                pred_box_xmin, pred_box_ymin, pred_box_xmax, pred_box_ymax,
                scores_per_level[idx]
            ])

    nmsed_outs, nmsed_num = multiclass_nms(prediction, class_num, keep_top_k,
                                           nms_threshold)
    return nmsed_outs, nmsed_num


def batched_retinanet_detection_out(boxes, scores, anchors, im_info,
                                    score_threshold, nms_threshold, nms_top_k,
                                    keep_top_k):
    batch_size = scores[0].shape[0]
    det_outs = []
    lod = []

    for n in range(batch_size):
        boxes_per_batch = []
        scores_per_batch = []

        num_level = len(scores)
        for lvl in range(num_level):
            boxes_per_batch.append(boxes[lvl][n])
            scores_per_batch.append(scores[lvl][n])

        nmsed_outs, nmsed_num = retinanet_detection_out(
            boxes_per_batch, scores_per_batch, anchors, im_info[n],
            score_threshold, nms_threshold, nms_top_k, keep_top_k)
        lod.append(nmsed_num)
        if nmsed_num == 0:
            continue

        det_outs.extend(nmsed_outs)
    return det_outs, lod


class TestRetinanetDetectionOutOp1(OpTest):
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    def set_argument(self):
        self.score_threshold = 0.05
        self.min_level = 3
        self.max_level = 7
        self.nms_threshold = 0.3
        self.nms_top_k = 1000
        self.keep_top_k = 200

        self.scales_per_octave = 3
        self.aspect_ratios = [1.0, 2.0, 0.5]
        self.anchor_scale = 4
        self.anchor_strides = [8, 16, 32, 64, 128]

        self.box_size = 4
        self.class_num = 80
        self.batch_size = 1
        self.input_channels = 20

        self.layer_h = []
        self.layer_w = []
        num_levels = self.max_level - self.min_level + 1
        for i in range(num_levels):
            self.layer_h.append(2**(num_levels - i))
            self.layer_w.append(2**(num_levels - i))

    def init_test_input(self):
        anchor_num = len(self.aspect_ratios) * self.scales_per_octave
        num_levels = self.max_level - self.min_level + 1
        self.scores_list = []
        self.bboxes_list = []
        self.anchors_list = []

        for i in range(num_levels):
            layer_h = self.layer_h[i]
            layer_w = self.layer_w[i]

            input_feat = np.random.random((self.batch_size, self.input_channels,
                                           layer_h, layer_w)).astype('float32')
            score = np.random.random(
                (self.batch_size, self.class_num * anchor_num, layer_h,
                 layer_w)).astype('float32')
            score = np.transpose(score, [0, 2, 3, 1])
            score = score.reshape((self.batch_size, -1, self.class_num))
            box = np.random.random((self.batch_size, self.box_size * anchor_num,
                                    layer_h, layer_w)).astype('float32')
            box = np.transpose(box, [0, 2, 3, 1])
            box = box.reshape((self.batch_size, -1, self.box_size))
            anchor_sizes = []
            for octave in range(self.scales_per_octave):
                anchor_sizes.append(
                    float(self.anchor_strides[i] * (2**octave)) /
                    float(self.scales_per_octave) * self.anchor_scale)
            anchor, var = anchor_generator_in_python(
                input_feat=input_feat,
                anchor_sizes=anchor_sizes,
                aspect_ratios=self.aspect_ratios,
                variances=[1.0, 1.0, 1.0, 1.0],
                stride=[self.anchor_strides[i], self.anchor_strides[i]],
                offset=0.5)
            anchor = np.reshape(anchor, [-1, 4])
            self.scores_list.append(score.astype('float32'))
            self.bboxes_list.append(box.astype('float32'))
            self.anchors_list.append(anchor.astype('float32'))

        self.im_info = np.array([[256., 256., 1.5]]).astype(
            'float32')  #im_height, im_width, scale

    def setUp(self):
        self.set_argument()
        self.init_test_input()

        nmsed_outs, lod = batched_retinanet_detection_out(
            self.bboxes_list, self.scores_list, self.anchors_list, self.im_info,
            self.score_threshold, self.nms_threshold, self.nms_top_k,
            self.keep_top_k)
        nmsed_outs = np.array(nmsed_outs).astype('float32')
        self.op_type = 'retinanet_detection_output'
        self.inputs = {
            'BBoxes': [('b0', self.bboxes_list[0]), ('b1', self.bboxes_list[1]),
                       ('b2', self.bboxes_list[2]), ('b3', self.bboxes_list[3]),
                       ('b4', self.bboxes_list[4])],
            'Scores': [('s0', self.scores_list[0]), ('s1', self.scores_list[1]),
                       ('s2', self.scores_list[2]), ('s3', self.scores_list[3]),
                       ('s4', self.scores_list[4])],
257 258 259 260 261 262 263 264
            'Anchors': [('a0', self.anchors_list[0]),
                        ('a1', self.anchors_list[1]),
                        ('a2', self.anchors_list[2]),
                        ('a3', self.anchors_list[3]),
                        ('a4', self.anchors_list[4])],
            'ImInfo': (self.im_info, [[
                1,
            ]])
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        }
        self.outputs = {'Out': (nmsed_outs, [lod])}
        self.attrs = {
            'score_threshold': self.score_threshold,
            'nms_top_k': self.nms_top_k,
            'nms_threshold': self.nms_threshold,
            'keep_top_k': self.keep_top_k,
            'nms_eta': 1.,
        }

    def test_check_output(self):
        self.check_output()


class TestRetinanetDetectionOutOp2(OpTest):
280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    def set_argument(self):
        self.score_threshold = 0.05
        self.min_level = 3
        self.max_level = 7
        self.nms_threshold = 0.3
        self.nms_top_k = 1000
        self.keep_top_k = 200

        self.scales_per_octave = 3
        self.aspect_ratios = [1.0, 2.0, 0.5]
        self.anchor_scale = 4
        self.anchor_strides = [8, 16, 32, 64, 128]

        self.box_size = 4
        self.class_num = 80
        self.batch_size = 1
        self.input_channels = 20
        # Here test the case there the shape of each FPN level
        # is irrelevant.
        self.layer_h = [1, 4, 8, 8, 16]
        self.layer_w = [1, 4, 8, 8, 16]


class TestRetinanetDetectionOutOpNo3(TestRetinanetDetectionOutOp1):
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    def set_argument(self):
        # Here set 2.0 to test the case there is no outputs.
        # In practical use, 0.0 < score_threshold < 1.0
        self.score_threshold = 2.0
        self.min_level = 3
        self.max_level = 7
        self.nms_threshold = 0.3
        self.nms_top_k = 1000
        self.keep_top_k = 200

        self.scales_per_octave = 3
        self.aspect_ratios = [1.0, 2.0, 0.5]
        self.anchor_scale = 4
        self.anchor_strides = [8, 16, 32, 64, 128]

        self.box_size = 4
        self.class_num = 80
        self.batch_size = 1
        self.input_channels = 20

        self.layer_h = []
        self.layer_w = []
        num_levels = self.max_level - self.min_level + 1
        for i in range(num_levels):
            self.layer_h.append(2**(num_levels - i))
            self.layer_w.append(2**(num_levels - i))


class TestRetinanetDetectionOutOpNo4(TestRetinanetDetectionOutOp1):
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    def set_argument(self):
        self.score_threshold = 0.05
        self.min_level = 2
        self.max_level = 5
        self.nms_threshold = 0.3
        self.nms_top_k = 1000
        self.keep_top_k = 200

        self.scales_per_octave = 3
        self.aspect_ratios = [1.0, 2.0, 0.5]
        self.anchor_scale = 4
        self.anchor_strides = [8, 16, 32, 64, 128]

        self.box_size = 4
        self.class_num = 80
        self.batch_size = 1
        self.input_channels = 20

        self.layer_h = []
        self.layer_w = []
        num_levels = self.max_level - self.min_level + 1
        for i in range(num_levels):
            self.layer_h.append(2**(num_levels - i))
            self.layer_w.append(2**(num_levels - i))

    def setUp(self):
        self.set_argument()
        self.init_test_input()

        nmsed_outs, lod = batched_retinanet_detection_out(
            self.bboxes_list, self.scores_list, self.anchors_list, self.im_info,
            self.score_threshold, self.nms_threshold, self.nms_top_k,
            self.keep_top_k)
        nmsed_outs = np.array(nmsed_outs).astype('float32')
        self.op_type = 'retinanet_detection_output'
        self.inputs = {
372 373 374
            'BBoxes': [('b0', self.bboxes_list[0]), ('b1', self.bboxes_list[1]),
                       ('b2', self.bboxes_list[2]),
                       ('b3', self.bboxes_list[3])],
375 376 377
            'Scores': [('s0', self.scores_list[0]), ('s1', self.scores_list[1]),
                       ('s2', self.scores_list[2]),
                       ('s3', self.scores_list[3])],
378 379 380 381 382 383 384
            'Anchors': [('a0', self.anchors_list[0]),
                        ('a1', self.anchors_list[1]),
                        ('a2', self.anchors_list[2]),
                        ('a3', self.anchors_list[3])],
            'ImInfo': (self.im_info, [[
                1,
            ]])
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        }
        self.outputs = {'Out': (nmsed_outs, [lod])}
        self.attrs = {
            'score_threshold': self.score_threshold,
            'nms_top_k': self.nms_top_k,
            'nms_threshold': self.nms_threshold,
            'keep_top_k': self.keep_top_k,
            'nms_eta': 1.,
        }

    def test_check_output(self):
        self.check_output()


class TestRetinanetDetectionOutOpNo5(TestRetinanetDetectionOutOp1):
400

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    def set_argument(self):
        self.score_threshold = 0.05
        self.min_level = 3
        self.max_level = 7
        self.nms_threshold = 0.3
        self.nms_top_k = 100
        self.keep_top_k = 10

        self.scales_per_octave = 3
        self.aspect_ratios = [1.0, 2.0, 0.5]
        self.anchor_scale = 4
        self.anchor_strides = [8, 16, 32, 64, 128]

        self.box_size = 4
        self.class_num = 80
        self.batch_size = 1
        self.input_channels = 20

        self.layer_h = []
        self.layer_w = []
        num_levels = self.max_level - self.min_level + 1
        for i in range(num_levels):
            self.layer_h.append(2**(num_levels - i))
            self.layer_w.append(2**(num_levels - i))


427
class TestRetinanetDetectionOutOpError(unittest.TestCase):
428

429 430
    def test_errors(self):
        with program_guard(Program(), Program()):
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
            bboxes_low1 = fluid.data(name='bboxes_low1',
                                     shape=[1, 44, 4],
                                     dtype='float32')
            bboxes_high1 = fluid.data(name='bboxes_high1',
                                      shape=[1, 11, 4],
                                      dtype='float32')
            scores_low1 = fluid.data(name='scores_low1',
                                     shape=[1, 44, 10],
                                     dtype='float32')
            scores_high1 = fluid.data(name='scores_high1',
                                      shape=[1, 11, 10],
                                      dtype='float32')
            anchors_low1 = fluid.data(name='anchors_low1',
                                      shape=[44, 4],
                                      dtype='float32')
            anchors_high1 = fluid.data(name='anchors_high1',
                                       shape=[11, 4],
                                       dtype='float32')
            im_info1 = fluid.data(name="im_info1",
                                  shape=[1, 3],
                                  dtype='float32')

            # The `bboxes` must be list, each element must be Variable and
454 455 456 457 458 459 460 461 462 463 464
            # its Tensor data type must be one of float32 and float64.
            def test_bboxes_type():
                fluid.layers.retinanet_detection_output(
                    bboxes=bboxes_low1,
                    scores=[scores_low1, scores_high1],
                    anchors=[anchors_low1, anchors_high1],
                    im_info=im_info1)

            self.assertRaises(TypeError, test_bboxes_type)

            def test_bboxes_tensor_dtype():
465 466 467
                bboxes_high2 = fluid.data(name='bboxes_high2',
                                          shape=[1, 11, 4],
                                          dtype='int32')
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_high2, 5],
                    scores=[scores_low1, scores_high1],
                    anchors=[anchors_low1, anchors_high1],
                    im_info=im_info1)

            self.assertRaises(TypeError, test_bboxes_tensor_dtype)

            # The `scores` must be list, each element must be Variable and its
            # Tensor data type must be one of float32 and float64.
            def test_scores_type():
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_low1, bboxes_high1],
                    scores=scores_low1,
                    anchors=[anchors_low1, anchors_high1],
                    im_info=im_info1)

            self.assertRaises(TypeError, test_scores_type)

            def test_scores_tensor_dtype():
488 489 490
                scores_high2 = fluid.data(name='scores_high2',
                                          shape=[1, 11, 10],
                                          dtype='int32')
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_low1, bboxes_high1],
                    scores=[scores_high2, 5],
                    anchors=[anchors_low1, anchors_high1],
                    im_info=im_info1)

            self.assertRaises(TypeError, test_scores_tensor_dtype)

            # The `anchors` must be list, each element must be Variable and its
            # Tensor data type must be one of float32 and float64.
            def test_anchors_type():
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_low1, bboxes_high1],
                    scores=[scores_low1, scores_high1],
                    anchors=anchors_low1,
                    im_info=im_info1)

            self.assertRaises(TypeError, test_anchors_type)

            def test_anchors_tensor_dtype():
511 512 513
                anchors_high2 = fluid.data(name='anchors_high2',
                                           shape=[11, 4],
                                           dtype='int32')
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_low1, bboxes_high1],
                    scores=[scores_low1, scores_high1],
                    anchors=[anchors_high2, 5],
                    im_info=im_info1)

            self.assertRaises(TypeError, test_anchors_tensor_dtype)

            # The `im_info` must be Variable and the data type of `im_info`
            # Tensor must be one of float32 and float64.
            def test_iminfo_type():
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_low1, bboxes_high1],
                    scores=[scores_low1, scores_high1],
                    anchors=[anchors_low1, anchors_high1],
                    im_info=[2, 3, 4])

            self.assertRaises(TypeError, test_iminfo_type)

            def test_iminfo_tensor_dtype():
534 535 536
                im_info2 = fluid.data(name='im_info2',
                                      shape=[1, 3],
                                      dtype='int32')
537 538 539 540 541 542 543 544 545
                fluid.layers.retinanet_detection_output(
                    bboxes=[bboxes_low1, bboxes_high1],
                    scores=[scores_low1, scores_high1],
                    anchors=[anchors_low1, anchors_high1],
                    im_info=im_info2)

            self.assertRaises(TypeError, test_iminfo_tensor_dtype)


546
if __name__ == '__main__':
H
hong 已提交
547
    paddle.enable_static()
548
    unittest.main()