• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17import pytest
18
19import mindspore
20import mindspore.context as context
21import mindspore.nn as nn
22from mindspore import Tensor
23from mindspore.ops import operations as P
24
25
26class NetBoundingBoxEncode(nn.Cell):
27    def __init__(self, means=(0.0, 0.0, 0.0, 0.0), stds=(1.0, 1.0, 1.0, 1.0)):
28        super(NetBoundingBoxEncode, self).__init__()
29        self.encode = P.BoundingBoxEncode(means=means, stds=stds)
30
31    def construct(self, anchor, groundtruth):
32        return self.encode(anchor, groundtruth)
33
34def bbox2delta(proposals, gt, means, stds):
35    px = (proposals[..., 0] + proposals[..., 2]) * 0.5
36    py = (proposals[..., 1] + proposals[..., 3]) * 0.5
37    pw = proposals[..., 2] - proposals[..., 0] + 1.0
38    ph = proposals[..., 3] - proposals[..., 1] + 1.0
39
40    gx = (gt[..., 0] + gt[..., 2]) * 0.5
41    gy = (gt[..., 1] + gt[..., 3]) * 0.5
42    gw = gt[..., 2] - gt[..., 0] + 1.0
43    gh = gt[..., 3] - gt[..., 1] + 1.0
44
45    dx = (gx - px) / pw
46    dy = (gy - py) / ph
47    dw = np.log(gw / pw)
48    dh = np.log(gh / ph)
49    means = np.array(means, np.float32)
50    stds = np.array(stds, np.float32)
51    deltas = np.stack([(dx - means[0]) / stds[0], (dy - means[1]) / stds[1],
52                       (dw - means[2]) / stds[2], (dh - means[3]) / stds[3]], axis=-1)
53
54    return deltas
55
56@pytest.mark.level0
57@pytest.mark.platform_x86_gpu_training
58@pytest.mark.env_onecard
59def test_boundingbox_encode():
60    anchor = np.array([[4, 1, 6, 9], [2, 5, 5, 9]]).astype(np.float32)
61    gt = np.array([[3, 2, 7, 7], [1, 5, 5, 8]]).astype(np.float32)
62    means = (0.1, 0.1, 0.2, 0.2)
63    stds = (2.0, 2.0, 3.0, 3.0)
64    anchor_box = Tensor(anchor, mindspore.float32)
65    groundtruth_box = Tensor(gt, mindspore.float32)
66    expect_deltas = bbox2delta(anchor, gt, means, stds)
67
68    error = np.ones(shape=[2, 4]) * 1.0e-6
69
70    context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
71    boundingbox_encode = NetBoundingBoxEncode(means, stds)
72    output = boundingbox_encode(anchor_box, groundtruth_box)
73    diff = output.asnumpy() - expect_deltas
74    assert np.all(abs(diff) < error)
75
76    context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
77    boundingbox_encode = NetBoundingBoxEncode(means, stds)
78    output = boundingbox_encode(anchor_box, groundtruth_box)
79    diff = output.asnumpy() - expect_deltas
80    assert np.all(abs(diff) < error)
81