1# Copyright 2021 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 p_width = proposals[..., 2] - proposals[..., 0] + 1.0 36 p_height = proposals[..., 3] - proposals[..., 1] + 1.0 37 38 dx = (((gt[..., 0] + gt[..., 2]) * 0.5) - ((proposals[..., 0] + proposals[..., 2]) * 0.5)) / p_width 39 dy = (((gt[..., 1] + gt[..., 3]) * 0.5) - ((proposals[..., 1] + proposals[..., 3]) * 0.5)) / p_height 40 dw = np.log((gt[..., 2] - gt[..., 0] + 1.0) / p_width) 41 dh = np.log((gt[..., 3] - gt[..., 1] + 1.0) / p_height) 42 means = np.array(means, np.float32) 43 stds = np.array(stds, np.float32) 44 deltas = np.stack([(dx - means[0]) / stds[0], (dy - means[1]) / stds[1], 45 (dw - means[2]) / stds[2], (dh - means[3]) / stds[3]], axis=-1) 46 47 return deltas 48 49@pytest.mark.level0 50@pytest.mark.platform_x86_cpu 51@pytest.mark.env_onecard 52def test_boundingbox_encode(): 53 anchor = np.array([[4, 1, 6, 9], [2, 5, 5, 9]]).astype(np.float32) 54 gt = np.array([[3, 2, 7, 7], [1, 5, 5, 8]]).astype(np.float32) 55 means = (0.1, 0.1, 0.2, 0.2) 56 stds = (2.0, 2.0, 3.0, 3.0) 57 anchor_box = Tensor(anchor, mindspore.float32) 58 groundtruth_box = Tensor(gt, mindspore.float32) 59 expect_deltas = bbox2delta(anchor, gt, means, stds) 60 61 error = np.ones(shape=[2, 4]) * 1.0e-6 62 63 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 64 boundingbox_encode = NetBoundingBoxEncode(means, stds) 65 output = boundingbox_encode(anchor_box, groundtruth_box) 66 diff = output.asnumpy() - expect_deltas 67 assert np.all(abs(diff) < error) 68 69 context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU') 70 boundingbox_encode = NetBoundingBoxEncode(means, stds) 71 output = boundingbox_encode(anchor_box, groundtruth_box) 72 diff = output.asnumpy() - expect_deltas 73 assert np.all(abs(diff) < error) 74