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 NetIOU(nn.Cell): 27 def __init__(self, mode): 28 super(NetIOU, self).__init__() 29 self.encode = P.IOU(mode=mode) 30 31 def construct(self, anchor, groundtruth): 32 return self.encode(anchor, groundtruth) 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_cpu 36@pytest.mark.env_onecard 37def test_iou(): 38 pos1 = [[101, 169, 246, 429], [107, 150, 277, 400], [103, 130, 220, 400]] 39 pos2 = [[121, 138, 304, 374], [97, 130, 250, 400]] 40 mode = "iou" 41 pos1_box = Tensor(np.array(pos1), mindspore.float32) 42 pos2_box = Tensor(np.array(pos2), mindspore.float32) 43 expect_result = np.array([[0.46551168, 0.6898875, 0.4567706], [0.73686045, 0.74506813, 0.76623374]], np.float32) 44 45 error = np.ones(shape=[1]) * 1.0e-6 46 47 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 48 overlaps = NetIOU(mode) 49 output = overlaps(pos1_box, pos2_box) 50 diff = output.asnumpy() - expect_result 51 assert np.all(abs(diff) < error) 52 53 context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU') 54 overlaps = NetIOU(mode) 55 output = overlaps(pos1_box, pos2_box) 56 diff = output.asnumpy() - expect_result 57 assert np.all(abs(diff) < error) 58