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 NetBoundingBoxDecode(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(NetBoundingBoxDecode, self).__init__() 29 self.decode = P.BoundingBoxDecode(max_shape=(768, 1280), means=means, stds=stds, 30 wh_ratio_clip=0.016) 31 32 def construct(self, anchor, groundtruth): 33 return self.decode(anchor, groundtruth) 34 35@pytest.mark.level0 36@pytest.mark.platform_x86_cpu 37@pytest.mark.env_onecard 38def test_boundingbox_decode(): 39 anchor = np.array([[4, 1, 2, 1], [2, 2, 2, 3]], np.float32) 40 deltas = np.array([[3, 1, 2, 2], [1, 2, 1, 4]], np.float32) 41 means = (0.1, 0.1, 0.2, 0.2) 42 stds = (2.0, 2.0, 3.0, 3.0) 43 anchor_box = Tensor(anchor, mindspore.float32) 44 deltas_box = Tensor(deltas, mindspore.float32) 45 expect_deltas = np.array([[28.6500, 0.0000, 0.0000, 33.8500], 46 [0.0000, 0.0000, 15.8663, 72.7000]], np.float32) 47 48 error = np.ones(shape=[2, 4]) * 1.0e-4 49 50 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 51 boundingbox_decode = NetBoundingBoxDecode(means, stds) 52 output = boundingbox_decode(anchor_box, deltas_box) 53 diff = output.asnumpy() - expect_deltas 54 assert np.all(abs(diff) < error) 55 56 context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU') 57 boundingbox_decode = NetBoundingBoxDecode(means, stds) 58 output = boundingbox_decode(anchor_box, deltas_box) 59 diff = output.asnumpy() - expect_deltas 60 assert np.all(abs(diff) < error) 61