未验证 提交 75c975f0 编写于 作者: L Leo Guo 提交者: GitHub

Modify the unittests of the assign_value, iou_similarity, one_hot_v2,...

Modify the unittests of the assign_value, iou_similarity, one_hot_v2, reduce_mean, roi_align op. test=kunlun (#44061)
上级 ca9339eb
...@@ -10,7 +10,7 @@ set(XPU_RT_LIB_NAME "libxpurt.so") ...@@ -10,7 +10,7 @@ set(XPU_RT_LIB_NAME "libxpurt.so")
if(NOT DEFINED XPU_BASE_URL) if(NOT DEFINED XPU_BASE_URL)
set(XPU_BASE_URL_WITHOUT_DATE set(XPU_BASE_URL_WITHOUT_DATE
"https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev") "https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev")
set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220601") set(XPU_BASE_URL "${XPU_BASE_URL_WITHOUT_DATE}/20220703")
else() else()
set(XPU_BASE_URL "${XPU_BASE_URL}") set(XPU_BASE_URL "${XPU_BASE_URL}")
endif() endif()
...@@ -19,14 +19,14 @@ endif() ...@@ -19,14 +19,14 @@ endif()
if(NOT DEFINED XPU_XDNN_BASE_URL) if(NOT DEFINED XPU_XDNN_BASE_URL)
set(XPU_XDNN_BASE_URL_WITHOUT_DATE set(XPU_XDNN_BASE_URL_WITHOUT_DATE
"https://klx-sdk-release-public.su.bcebos.com/xdnn/dev") "https://klx-sdk-release-public.su.bcebos.com/xdnn/dev")
set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220601") set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL_WITHOUT_DATE}/20220703")
else() else()
set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL}") set(XPU_XDNN_BASE_URL "${XPU_XDNN_BASE_URL}")
endif() endif()
if(WITH_AARCH64) if(WITH_AARCH64)
set(XPU_XRE_DIR_NAME "xre-kylin_aarch64") set(XPU_XRE_DIR_NAME "xre-kylin_aarch64")
set(XPU_XDNN_DIR_NAME "XDNN-kylin_aarch64") set(XPU_XDNN_DIR_NAME "xdnn-kylin_aarch64")
set(XPU_XCCL_DIR_NAME "xccl-kylin_aarch64") set(XPU_XCCL_DIR_NAME "xccl-kylin_aarch64")
set(XPU_XDNN_URL set(XPU_XDNN_URL
"${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz" "${XPU_XDNN_BASE_URL}/${XPU_XDNN_DIR_NAME}.tar.gz"
...@@ -40,7 +40,7 @@ elseif(WITH_SUNWAY) ...@@ -40,7 +40,7 @@ elseif(WITH_SUNWAY)
CACHE STRING "" FORCE) CACHE STRING "" FORCE)
elseif(WITH_BDCENTOS) elseif(WITH_BDCENTOS)
set(XPU_XRE_DIR_NAME "xre-bdcentos_x86_64") set(XPU_XRE_DIR_NAME "xre-bdcentos_x86_64")
set(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") set(XPU_XDNN_DIR_NAME "xdnn-bdcentos_x86_64")
set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64")
# ubuntu and centos: use output by XDNN API team # ubuntu and centos: use output by XDNN API team
set(XPU_XDNN_URL set(XPU_XDNN_URL
...@@ -48,7 +48,7 @@ elseif(WITH_BDCENTOS) ...@@ -48,7 +48,7 @@ elseif(WITH_BDCENTOS)
CACHE STRING "" FORCE) CACHE STRING "" FORCE)
elseif(WITH_UBUNTU) elseif(WITH_UBUNTU)
set(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64") set(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64")
set(XPU_XDNN_DIR_NAME "XDNN-ubuntu_x86_64") set(XPU_XDNN_DIR_NAME "xdnn-ubuntu_x86_64")
set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64")
# ubuntu and centos: use output by XDNN API team # ubuntu and centos: use output by XDNN API team
set(XPU_XDNN_URL set(XPU_XDNN_URL
...@@ -56,7 +56,7 @@ elseif(WITH_UBUNTU) ...@@ -56,7 +56,7 @@ elseif(WITH_UBUNTU)
CACHE STRING "" FORCE) CACHE STRING "" FORCE)
elseif(WITH_CENTOS) elseif(WITH_CENTOS)
set(XPU_XRE_DIR_NAME "xre-centos7_x86_64") set(XPU_XRE_DIR_NAME "xre-centos7_x86_64")
set(XPU_XDNN_DIR_NAME "XDNN-bdcentos_x86_64") set(XPU_XDNN_DIR_NAME "xdnn-bdcentos_x86_64")
set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64")
# ubuntu and centos: use output by XDNN API team # ubuntu and centos: use output by XDNN API team
set(XPU_XDNN_URL set(XPU_XDNN_URL
...@@ -64,7 +64,7 @@ elseif(WITH_CENTOS) ...@@ -64,7 +64,7 @@ elseif(WITH_CENTOS)
CACHE STRING "" FORCE) CACHE STRING "" FORCE)
else() else()
set(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64") set(XPU_XRE_DIR_NAME "xre-ubuntu_x86_64")
set(XPU_XDNN_DIR_NAME "XDNN-ubuntu_x86_64") set(XPU_XDNN_DIR_NAME "xdnn-ubuntu_x86_64")
set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64") set(XPU_XCCL_DIR_NAME "xccl-bdcentos_x86_64")
# default: use output by XDNN API team # default: use output by XDNN API team
set(XPU_XDNN_URL set(XPU_XDNN_URL
......
# 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.
from __future__ import print_function
import unittest
import numpy
import sys
sys.path.append("..")
import paddle.fluid as fluid
import paddle.fluid.framework as framework
import paddle.fluid.layers as layers
from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
import paddle
paddle.enable_static()
class XPUTestAssignValueOp(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'assign_value'
self.use_dynamic_create_class = False
class TestAssignValueOp(XPUOpTest):
def init(self):
self.dtype = self.in_type
self.place = paddle.XPUPlace(0)
self.op_type = 'assign_value'
def setUp(self):
self.init()
self.inputs = {}
self.attrs = {}
self.init_data()
self.attrs["shape"] = self.value.shape
self.attrs["dtype"] = framework.convert_np_dtype_to_dtype_(
self.value.dtype)
self.outputs = {"Out": self.value}
def init_data(self):
self.value = numpy.random.random(size=(2, 5)).astype(self.dtype)
self.attrs["fp32_values"] = [float(v) for v in self.value.flat]
def test_forward(self):
self.check_output_with_place(self.place)
class TestAssignValueOp2(TestAssignValueOp):
def init_data(self):
self.value = numpy.random.random(size=(2, 5)).astype(numpy.int32)
self.attrs["int32_values"] = [int(v) for v in self.value.flat]
class TestAssignValueOp3(TestAssignValueOp):
def init_data(self):
self.value = numpy.random.random(size=(2, 5)).astype(numpy.int64)
self.attrs["int64_values"] = [int(v) for v in self.value.flat]
class TestAssignValueOp4(TestAssignValueOp):
def init_data(self):
self.value = numpy.random.choice(a=[False, True],
size=(2, 5)).astype(numpy.bool)
self.attrs["bool_values"] = [int(v) for v in self.value.flat]
class TestAssignApi(unittest.TestCase):
def setUp(self):
self.init_dtype()
self.value = (-100 + 200 * numpy.random.random(size=(2, 5))).astype(
self.dtype)
self.place = fluid.XPUPlace(0)
def init_dtype(self):
self.dtype = "float32"
def test_assign(self):
main_program = fluid.Program()
with fluid.program_guard(main_program):
x = layers.create_tensor(dtype=self.dtype)
layers.assign(input=self.value, output=x)
exe = fluid.Executor(self.place)
[fetched_x] = exe.run(main_program, feed={}, fetch_list=[x])
self.assertTrue(numpy.array_equal(fetched_x, self.value),
"fetch_x=%s val=%s" % (fetched_x, self.value))
self.assertEqual(fetched_x.dtype, self.value.dtype)
class TestAssignApi2(TestAssignApi):
def init_dtype(self):
self.dtype = "int32"
class TestAssignApi3(TestAssignApi):
def init_dtype(self):
self.dtype = "int64"
class TestAssignApi4(TestAssignApi):
def setUp(self):
self.init_dtype()
self.value = numpy.random.choice(a=[False, True],
size=(2, 5)).astype(numpy.bool)
self.place = fluid.XPUPlace(0)
def init_dtype(self):
self.dtype = "bool"
support_types = get_xpu_op_support_types('assign_value')
for stype in support_types:
create_test_class(globals(), XPUTestAssignValueOp, stype)
if __name__ == '__main__':
unittest.main()
...@@ -22,99 +22,116 @@ import unittest ...@@ -22,99 +22,116 @@ import unittest
import numpy as np import numpy as np
import numpy.random as random import numpy.random as random
import sys import sys
import math
from op_test import OpTest sys.path.append("..")
from op_test_xpu import XPUOpTest from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
import paddle import paddle
paddle.enable_static() paddle.enable_static()
class TestXPUIOUSimilarityOp(XPUOpTest): class XPUTestIOUSimilarityOp(XPUOpTestWrapper):
def test_check_output(self): def __init__(self):
if paddle.is_compiled_with_xpu(): self.op_name = 'iou_similarity'
place = paddle.XPUPlace(0) self.use_dynamic_create_class = False
self.check_output_with_place(place)
class TestXPUIOUSimilarityOp(XPUOpTest):
def setUp(self):
self.op_type = "iou_similarity" def init(self):
self.boxes1 = random.rand(2, 4).astype('float32') self.dtype = self.in_type
self.boxes2 = random.rand(3, 4).astype('float32') self.place = paddle.XPUPlace(0)
self.output = random.rand(2, 3).astype('float32') self.op_type = 'iou_similarity'
self.box_normalized = False
# run python iou computation def test_check_output(self):
self._compute_iou() self.check_output_with_place(self.place)
self.inputs = {'X': self.boxes1, 'Y': self.boxes2}
self.attrs = {"box_normalized": self.box_normalized, 'use_xpu': True} def setUp(self):
self.outputs = {'Out': self.output} self.init()
self.boxes1 = random.rand(2, 4).astype(self.dtype)
def _compute_iou(self, ): self.boxes2 = random.rand(3, 4).astype(self.dtype)
for row in range(self.boxes1.shape[0]): self.output = random.rand(2, 3).astype(self.dtype)
for col in range(self.boxes2.shape[0]): self.box_normalized = False
xmin1, ymin1, xmax1, ymax1 = self.boxes1[row] # run python iou computation
xmin2, ymin2, xmax2, ymax2 = self.boxes2[col] self._compute_iou()
if not self.box_normalized: self.inputs = {'X': self.boxes1, 'Y': self.boxes2}
area1 = (ymax1 - ymin1 + 1) * (xmax1 - xmin1 + 1) self.attrs = {
area2 = (ymax2 - ymin2 + 1) * (xmax2 - xmin2 + 1) "box_normalized": self.box_normalized,
else: 'use_xpu': True
area1 = (ymax1 - ymin1) * (xmax1 - xmin1) }
area2 = (ymax2 - ymin2) * (xmax2 - xmin2) self.outputs = {'Out': self.output}
inter_xmax = min(xmax1, xmax2) def _compute_iou(self, ):
inter_ymax = min(ymax1, ymax2) for row in range(self.boxes1.shape[0]):
inter_xmin = max(xmin1, xmin2) for col in range(self.boxes2.shape[0]):
inter_ymin = max(ymin1, ymin2) xmin1, ymin1, xmax1, ymax1 = self.boxes1[row]
inter_height = inter_ymax - inter_ymin xmin2, ymin2, xmax2, ymax2 = self.boxes2[col]
inter_width = inter_xmax - inter_xmin if not self.box_normalized:
if not self.box_normalized: area1 = (ymax1 - ymin1 + 1) * (xmax1 - xmin1 + 1)
inter_height += 1 area2 = (ymax2 - ymin2 + 1) * (xmax2 - xmin2 + 1)
inter_width += 1 else:
inter_height = max(inter_height, 0) area1 = (ymax1 - ymin1) * (xmax1 - xmin1)
inter_width = max(inter_width, 0) area2 = (ymax2 - ymin2) * (xmax2 - xmin2)
inter_area = inter_width * inter_height
union_area = area1 + area2 - inter_area inter_xmax = min(xmax1, xmax2)
sim_score = inter_area / union_area inter_ymax = min(ymax1, ymax2)
self.output[row, col] = sim_score inter_xmin = max(xmin1, xmin2)
inter_ymin = max(ymin1, ymin2)
inter_height = inter_ymax - inter_ymin
class TestXPUIOUSimilarityOpWithLoD(TestXPUIOUSimilarityOp): inter_width = inter_xmax - inter_xmin
if not self.box_normalized:
def test_check_output(self): inter_height += 1
if paddle.is_compiled_with_xpu(): inter_width += 1
place = paddle.XPUPlace(0) inter_height = max(inter_height, 0)
self.check_output_with_place(place, check_dygraph=False) inter_width = max(inter_width, 0)
inter_area = inter_width * inter_height
def setUp(self): union_area = area1 + area2 - inter_area
super(TestXPUIOUSimilarityOpWithLoD, self).setUp() sim_score = inter_area / union_area
self.boxes1_lod = [[1, 1]] self.output[row, col] = sim_score
self.output_lod = [[1, 1]]
self.box_normalized = False class TestXPUIOUSimilarityOpWithLoD(TestXPUIOUSimilarityOp):
# run python iou computation
self._compute_iou() def test_check_output(self):
self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2} self.check_output_with_place(self.place, check_dygraph=False)
self.attrs = {"box_normalized": self.box_normalized}
self.outputs = {'Out': (self.output, self.output_lod)} def setUp(self):
super().setUp()
self.boxes1_lod = [[1, 1]]
class TestXPUIOUSimilarityOpWithBoxNormalized(TestXPUIOUSimilarityOp): self.output_lod = [[1, 1]]
self.box_normalized = False
def test_check_output(self): # run python iou computation
if paddle.is_compiled_with_xpu(): self._compute_iou()
place = paddle.XPUPlace(0) self.inputs = {
self.check_output_with_place(place, check_dygraph=False) 'X': (self.boxes1, self.boxes1_lod),
'Y': self.boxes2
def setUp(self): }
super(TestXPUIOUSimilarityOpWithBoxNormalized, self).setUp() self.attrs = {"box_normalized": self.box_normalized}
self.boxes1_lod = [[1, 1]] self.outputs = {'Out': (self.output, self.output_lod)}
self.output_lod = [[1, 1]]
self.box_normalized = True class TestXPUIOUSimilarityOpWithBoxNormalized(TestXPUIOUSimilarityOp):
# run python iou computation
self._compute_iou() def test_check_output(self):
self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2} self.check_output_with_place(self.place, check_dygraph=False)
self.attrs = {"box_normalized": self.box_normalized}
self.outputs = {'Out': (self.output, self.output_lod)} def setUp(self):
super().setUp()
self.boxes1_lod = [[1, 1]]
self.output_lod = [[1, 1]]
self.box_normalized = True
# run python iou computation
self._compute_iou()
self.inputs = {
'X': (self.boxes1, self.boxes1_lod),
'Y': self.boxes2
}
self.attrs = {"box_normalized": self.box_normalized}
self.outputs = {'Out': (self.output, self.output_lod)}
support_types = get_xpu_op_support_types('iou_similarity')
for stype in support_types:
create_test_class(globals(), XPUTestIOUSimilarityOp, stype)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -18,136 +18,130 @@ import unittest ...@@ -18,136 +18,130 @@ import unittest
import numpy as np import numpy as np
import paddle import paddle
import paddle.fluid.core as core import paddle.fluid.core as core
import paddle.fluid as fluid
import sys import sys
sys.path.append("..") sys.path.append("..")
from op_test_xpu import XPUOpTest from op_test_xpu import XPUOpTest
import paddle.fluid as fluid from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
from paddle.fluid import Program, program_guard
import time
paddle.enable_static() paddle.enable_static()
class TestOneHotOp(XPUOpTest): class XPUTestOneHotOp(XPUOpTestWrapper):
def setUp(self): def __init__(self):
self.use_xpu = True self.op_name = 'one_hot_v2'
self.op_type = 'one_hot_v2' self.use_dynamic_create_class = False
depth = 10
depth_np = np.array(10).astype('int32')
# dimension = 12
x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0])])
out = np.zeros(shape=(np.product(x.shape), depth)).astype('float32') class TestOneHotOp(XPUOpTest):
for i in range(np.product(x.shape)): def init(self):
out[i, x[i]] = 1.0 self.dtype = self.in_type
self.place = paddle.XPUPlace(0)
self.op_type = 'one_hot_v2'
self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np} def setUp(self):
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)} self.init()
self.outputs = {'Out': (out, x_lod)} depth = 10
depth_np = np.array(10).astype('int32')
# dimension = 12
x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0])])
def test_check_output(self): out = np.zeros(shape=(np.product(x.shape),
place = paddle.XPUPlace(0) depth)).astype(self.dtype)
self.check_output_with_place(place, check_dygraph=False)
for i in range(np.product(x.shape)):
out[i, x[i]] = 1.0
class TestOneHotOp_attr(XPUOpTest): self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np}
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32)}
self.outputs = {'Out': (out, x_lod)}
def setUp(self): def test_check_output(self):
self.op_type = 'one_hot_v2' self.check_output_with_place(self.place)
depth = 10
dimension = 12
x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1])
out = np.zeros(shape=(np.product(x.shape[:-1]), 1, class TestOneHotOp_attr(TestOneHotOp):
depth)).astype('float32')
for i in range(np.product(x.shape)): def setUp(self):
out[i, 0, x[i]] = 1.0 self.init()
depth = 10
dimension = 12
x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1])
self.inputs = {'X': (x, x_lod)} out = np.zeros(shape=(np.product(x.shape[:-1]), 1,
self.attrs = {'dtype': int(core.VarDesc.VarType.FP32), 'depth': depth} depth)).astype(self.dtype)
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self): for i in range(np.product(x.shape)):
place = paddle.XPUPlace(0) out[i, 0, x[i]] = 1.0
self.check_output_with_place(place, check_dygraph=False)
self.inputs = {'X': (x, x_lod)}
self.attrs = {
'dtype': int(core.VarDesc.VarType.FP32),
'depth': depth
}
self.outputs = {'Out': (out, x_lod)}
class TestOneHotOp_default_dtype(XPUOpTest): class TestOneHotOp_default_dtype(TestOneHotOp):
def setUp(self): def setUp(self):
self.op_type = 'one_hot_v2' self.init()
depth = 10 depth = 10
depth_np = np.array(10).astype('int32') depth_np = np.array(10).astype('int32')
dimension = 12 dimension = 12
x_lod = [[4, 1, 3, 3]] x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))] x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0])]) x = np.array(x).astype('int32').reshape([sum(x_lod[0])])
out = np.zeros(shape=(np.product(x.shape), depth)).astype('float32') out = np.zeros(shape=(np.product(x.shape),
depth)).astype(self.dtype)
for i in range(np.product(x.shape)): for i in range(np.product(x.shape)):
out[i, x[i]] = 1.0 out[i, x[i]] = 1.0
self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np} self.inputs = {'X': (x, x_lod), 'depth_tensor': depth_np}
self.attrs = {} self.attrs = {}
self.outputs = {'Out': (out, x_lod)} self.outputs = {'Out': (out, x_lod)}
def test_check_output(self): class TestOneHotOp_default_dtype_attr(TestOneHotOp):
place = paddle.XPUPlace(0)
self.check_output_with_place(place, check_dygraph=False)
def setUp(self):
self.init()
depth = 10
dimension = 12
x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1])
class TestOneHotOp_default_dtype_attr(XPUOpTest): out = np.zeros(shape=(np.product(x.shape[:-1]), 1,
depth)).astype(self.dtype)
def setUp(self): for i in range(np.product(x.shape)):
self.op_type = 'one_hot_v2' out[i, 0, x[i]] = 1.0
depth = 10
dimension = 12
x_lod = [[4, 1, 3, 3]]
x = [np.random.randint(0, depth - 1) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0]), 1])
out = np.zeros(shape=(np.product(x.shape[:-1]), 1, self.inputs = {'X': (x, x_lod)}
depth)).astype('float32') self.attrs = {'depth': depth}
self.outputs = {'Out': (out, x_lod)}
for i in range(np.product(x.shape)): class TestOneHotOp_out_of_range(TestOneHotOp):
out[i, 0, x[i]] = 1.0
self.inputs = {'X': (x, x_lod)} def setUp(self):
self.attrs = {'depth': depth} self.init()
self.outputs = {'Out': (out, x_lod)} depth = 10
x_lod = [[4, 1, 3, 3]]
x = [np.random.choice([-1, depth]) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0])])
def test_check_output(self): out = np.zeros(shape=(np.product(x.shape),
place = paddle.XPUPlace(0) depth)).astype(self.dtype)
self.check_output_with_place(place, check_dygraph=False)
self.inputs = {'X': (x, x_lod)}
class TestOneHotOp_out_of_range(XPUOpTest): self.attrs = {'depth': depth, 'allow_out_of_range': True}
self.outputs = {'Out': (out, x_lod)}
def setUp(self):
self.op_type = 'one_hot_v2'
depth = 10
x_lod = [[4, 1, 3, 3]]
x = [np.random.choice([-1, depth]) for i in range(sum(x_lod[0]))]
x = np.array(x).astype('int32').reshape([sum(x_lod[0])])
out = np.zeros(shape=(np.product(x.shape), depth)).astype('float32')
self.inputs = {'X': (x, x_lod)}
self.attrs = {'depth': depth, 'allow_out_of_range': True}
self.outputs = {'Out': (out, x_lod)}
def test_check_output(self):
place = paddle.XPUPlace(0)
self.check_output_with_place(place, check_dygraph=False)
class TestOneHotOpApi(unittest.TestCase): class TestOneHotOpApi(unittest.TestCase):
...@@ -200,6 +194,9 @@ class BadInputTestOnehotV2(unittest.TestCase): ...@@ -200,6 +194,9 @@ class BadInputTestOnehotV2(unittest.TestCase):
self.assertRaises(TypeError, test_bad_x) self.assertRaises(TypeError, test_bad_x)
support_types = get_xpu_op_support_types('one_hot_v2')
for stype in support_types:
create_test_class(globals(), XPUTestOneHotOp, stype)
if __name__ == '__main__': if __name__ == '__main__':
paddle.enable_static()
unittest.main() unittest.main()
...@@ -19,196 +19,180 @@ import numpy as np ...@@ -19,196 +19,180 @@ import numpy as np
import sys import sys
sys.path.append("..") sys.path.append("..")
from op_test import OpTest, skip_check_grad_ci from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
import paddle import paddle
import paddle.fluid.core as core
import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard
from paddle.fluid.framework import convert_np_dtype_to_dtype_
paddle.enable_static()
class TestMeanOp(OpTest):
def setUp(self): class XPUTestMeanOp(XPUOpTestWrapper):
self.op_type = "reduce_mean"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
def test_check_output(self): def __init__(self):
if paddle.is_compiled_with_xpu(): self.op_name = 'reduce_mean'
place = paddle.XPUPlace(0) self.use_dynamic_create_class = False
self.check_output_with_place(place)
def check_grad_(self): class TestMeanOp(XPUOpTest):
self.check_grad(['X'], 'Out')
def setUp(self):
self.dtype = self.in_type
self.place = paddle.XPUPlace(0)
self.op_type = "reduce_mean"
self.inputs = {'X': np.random.random((5, 6, 10)).astype(self.dtype)}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
class TestMeanOp5D(OpTest): def test_check_output(self):
self.check_output_with_place(self.place)
def setUp(self): def test_check_grad(self):
self.op_type = "reduce_mean" self.check_grad_with_place(self.place, ['X'], 'Out')
self.inputs = {
'X': np.random.random((1, 2, 5, 6, 10)).astype("float32")
}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
class TestMeanOp6D(OpTest):
def setUp(self):
self.op_type = "reduce_mean"
self.inputs = {
'X': np.random.random((1, 1, 2, 5, 6, 10)).astype("float32")
}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
class TestMeanOp8D(OpTest):
def setUp(self):
self.op_type = "reduce_mean"
self.inputs = {
'X': np.random.random((1, 3, 1, 2, 1, 4, 3, 10)).astype("float32")
}
self.attrs = {'dim': (0, 3), 'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=(0, 3))}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
class Test1DReduce(OpTest):
def setUp(self):
self.op_type = "reduce_mean"
self.inputs = {'X': np.random.random(120).astype("float32")}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
def test_check_output(self):
if paddle.is_compiled_with_xpu():
place = paddle.XPUPlace(0)
self.check_output_with_place(place)
def test_check_grad(self):
self.check_grad(['X'], 'Out')
class Test2DReduce0(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.attrs = {'dim': [0], 'use_xpu': True}
self.inputs = {'X': np.random.random((20, 10)).astype("float32")}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
class Test2DReduce1(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.attrs = {'dim': [1], 'use_xpu': True}
self.inputs = {'X': np.random.random((20, 10)).astype("float32")}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce0(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.attrs = {'dim': [1], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce1(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.attrs = {'dim': [2], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce2(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.attrs = {'dim': [-2], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce3(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.attrs = {'dim': [1, 2], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype("float32")}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class TestKeepDimReduce(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.inputs = {'X': np.random.random((5, 6, 10)).astype("float32")}
self.attrs = {'dim': [1], 'keep_dim': True, 'use_xpu': True}
self.outputs = {
'Out':
self.inputs['X'].mean(axis=tuple(self.attrs['dim']),
keepdims=self.attrs['keep_dim'])
}
class TestKeepDim8DReduce(Test1DReduce):
def setUp(self):
self.op_type = "reduce_mean"
self.inputs = {
'X': np.random.random((2, 5, 3, 2, 2, 3, 4, 2)).astype("float32")
}
self.attrs = {'dim': (3, 4, 5), 'keep_dim': True, 'use_xpu': True}
self.outputs = {
'Out':
self.inputs['X'].mean(axis=tuple(self.attrs['dim']),
keepdims=self.attrs['keep_dim'])
}
class TestMeanOp5D(TestMeanOp):
def setUp(self):
super().setUp()
self.inputs = {
'X': np.random.random((1, 2, 5, 6, 10)).astype(self.dtype)
}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
class TestMeanOp6D(TestMeanOp):
def setUp(self):
super().setUp()
self.inputs = {
'X': np.random.random((1, 1, 2, 5, 6, 10)).astype(self.dtype)
}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
class TestMeanOp8D(TestMeanOp):
def setUp(self):
super().setUp()
self.inputs = {
'X': np.random.random(
(1, 3, 1, 2, 1, 4, 3, 10)).astype(self.dtype)
}
self.attrs = {'dim': (0, 3), 'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=(0, 3))}
class XPUTestReduce(XPUOpTestWrapper):
def __init__(self):
self.op_name = 'reduce_mean'
self.use_dynamic_create_class = False
class Test1DReduce(XPUOpTest):
def setUp(self):
self.dtype = self.in_type
self.place = paddle.XPUPlace(0)
self.op_type = "reduce_mean"
self.inputs = {'X': np.random.random(120).astype(self.dtype)}
self.attrs = {'use_xpu': True}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
def test_check_output(self):
self.check_output_with_place(self.place)
# There is a api bug in checking grad when dim[0] > 0
# def test_check_grad(self):
# self.check_output_with_place(self.place, ['X'], 'Out')
class Test2DReduce0(Test1DReduce):
def setUp(self):
super().setUp()
self.attrs = {'dim': [0], 'use_xpu': True}
self.inputs = {'X': np.random.random((20, 10)).astype(self.dtype)}
self.outputs = {'Out': self.inputs['X'].mean(axis=0)}
class Test2DReduce1(Test1DReduce):
def setUp(self):
super().setUp()
self.attrs = {'dim': [1], 'use_xpu': True}
self.inputs = {'X': np.random.random((20, 10)).astype(self.dtype)}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce0(Test1DReduce):
def setUp(self):
super().setUp()
self.attrs = {'dim': [1], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce1(Test1DReduce):
def setUp(self):
super().setUp()
self.attrs = {'dim': [2], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce2(Test1DReduce):
def setUp(self):
super().setUp()
self.attrs = {'dim': [-2], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class Test3DReduce3(Test1DReduce):
def setUp(self):
super().setUp()
self.attrs = {'dim': [1, 2], 'use_xpu': True}
self.inputs = {'X': np.random.random((5, 6, 7)).astype(self.dtype)}
self.outputs = {
'Out': self.inputs['X'].mean(axis=tuple(self.attrs['dim']))
}
class TestKeepDimReduce(Test1DReduce):
def setUp(self):
super().setUp()
self.inputs = {'X': np.random.random((5, 6, 10)).astype(self.dtype)}
self.attrs = {'dim': [1], 'keep_dim': True, 'use_xpu': True}
self.outputs = {
'Out':
self.inputs['X'].mean(axis=tuple(self.attrs['dim']),
keepdims=self.attrs['keep_dim'])
}
class TestKeepDim8DReduce(Test1DReduce):
def setUp(self):
super().setUp()
self.inputs = {
'X': np.random.random(
(2, 5, 3, 2, 2, 3, 4, 2)).astype(self.dtype)
}
self.attrs = {'dim': (3, 4, 5), 'keep_dim': True, 'use_xpu': True}
self.outputs = {
'Out':
self.inputs['X'].mean(axis=tuple(self.attrs['dim']),
keepdims=self.attrs['keep_dim'])
}
support_types = get_xpu_op_support_types('reduce_mean')
for stype in support_types:
create_test_class(globals(), XPUTestMeanOp, stype)
create_test_class(globals(), XPUTestReduce, stype)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -20,208 +20,220 @@ import unittest ...@@ -20,208 +20,220 @@ import unittest
import math import math
import numpy as np import numpy as np
import paddle.fluid.core as core import paddle.fluid.core as core
from op_test import OpTest, skip_check_grad_ci
from op_test_xpu import XPUOpTest from op_test_xpu import XPUOpTest
import paddle import paddle
import paddle.fluid as fluid from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
from paddle.fluid import Program, program_guard
paddle.enable_static()
class TestROIAlignOp(XPUOpTest):
class XPUTestROIAlignOp(XPUOpTestWrapper):
def set_data(self):
self.init_test_case() def __init__(self):
self.make_rois() self.op_name = 'roi_align'
self.calc_roi_align() self.use_dynamic_create_class = False
self.inputs = { class TestROIAlignOp(XPUOpTest):
'X': self.x,
'ROIs': (self.rois[:, 1:5], self.rois_lod), def set_data(self):
} self.init_test_case()
self.attrs = { self.make_rois()
'spatial_scale': self.spatial_scale, self.calc_roi_align()
'pooled_height': self.pooled_height,
'pooled_width': self.pooled_width, self.inputs = {
'sampling_ratio': self.sampling_ratio, 'X': self.x,
'aligned': self.continuous_coordinate 'ROIs': (self.rois[:, 1:5], self.rois_lod),
} }
self.attrs = {
self.outputs = {'Out': self.out_data} 'spatial_scale': self.spatial_scale,
'pooled_height': self.pooled_height,
def init_test_case(self): 'pooled_width': self.pooled_width,
self.batch_size = 3 'sampling_ratio': self.sampling_ratio,
self.channels = 3 'aligned': self.continuous_coordinate
self.height = 8 }
self.width = 6
self.outputs = {'Out': self.out_data}
self.xpu_version = core.get_xpu_device_version(0)
def init_test_case(self):
# n, c, h, w self.batch_size = 3
self.x_dim = (self.batch_size, self.channels, self.height, self.width) self.channels = 3
self.height = 8
self.spatial_scale = 1.0 / 2.0 self.width = 6
self.pooled_height = 2
self.pooled_width = 2 self.xpu_version = core.get_xpu_device_version(0)
self.sampling_ratio = -1
if self.xpu_version == core.XPUVersion.XPU1: # n, c, h, w
self.continuous_coordinate = False self.x_dim = (self.batch_size, self.channels, self.height,
else: self.width)
self.continuous_coordinate = bool(np.random.randint(2))
self.x = np.random.random(self.x_dim).astype('float32') self.spatial_scale = 1.0 / 2.0
self.pooled_height = 2
def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h, roi_bin_grid_w, self.pooled_width = 2
bin_size_h, bin_size_w): self.sampling_ratio = -1
count = roi_bin_grid_h * roi_bin_grid_w if self.xpu_version == core.XPUVersion.XPU1:
bilinear_pos = np.zeros( self.continuous_coordinate = False
[self.channels, self.pooled_height, self.pooled_width, count, 4], else:
np.float32) self.continuous_coordinate = bool(np.random.randint(2))
bilinear_w = np.zeros([self.pooled_height, self.pooled_width, count, 4], self.x = np.random.random(self.x_dim).astype(self.dtype)
np.float32)
for ph in range(self.pooled_width): def pre_calc(self, x_i, roi_xmin, roi_ymin, roi_bin_grid_h,
for pw in range(self.pooled_height): roi_bin_grid_w, bin_size_h, bin_size_w):
c = 0 count = roi_bin_grid_h * roi_bin_grid_w
for iy in range(roi_bin_grid_h): bilinear_pos = np.zeros([
y = roi_ymin + ph * bin_size_h + (iy + 0.5) * \ self.channels, self.pooled_height, self.pooled_width, count, 4
bin_size_h / roi_bin_grid_h ], np.float32)
for ix in range(roi_bin_grid_w): bilinear_w = np.zeros(
x = roi_xmin + pw * bin_size_w + (ix + 0.5) * \ [self.pooled_height, self.pooled_width, count, 4], np.float32)
bin_size_w / roi_bin_grid_w for ph in range(self.pooled_width):
if y < -1.0 or y > self.height or \ for pw in range(self.pooled_height):
x < -1.0 or x > self.width: c = 0
continue for iy in range(roi_bin_grid_h):
if y <= 0: y = roi_ymin + ph * bin_size_h + (iy + 0.5) * \
y = 0 bin_size_h / roi_bin_grid_h
if x <= 0: for ix in range(roi_bin_grid_w):
x = 0 x = roi_xmin + pw * bin_size_w + (ix + 0.5) * \
y_low = int(y) bin_size_w / roi_bin_grid_w
x_low = int(x) if y < -1.0 or y > self.height or \
if y_low >= self.height - 1: x < -1.0 or x > self.width:
y = y_high = y_low = self.height - 1 continue
else: if y <= 0:
y_high = y_low + 1 y = 0
if x_low >= self.width - 1: if x <= 0:
x = x_high = x_low = self.width - 1 x = 0
else: y_low = int(y)
x_high = x_low + 1 x_low = int(x)
ly = y - y_low if y_low >= self.height - 1:
lx = x - x_low y = y_high = y_low = self.height - 1
hy = 1 - ly else:
hx = 1 - lx y_high = y_low + 1
for ch in range(self.channels): if x_low >= self.width - 1:
bilinear_pos[ch, ph, pw, c, 0] = x_i[ch, y_low, x = x_high = x_low = self.width - 1
x_low] else:
bilinear_pos[ch, ph, pw, c, 1] = x_i[ch, y_low, x_high = x_low + 1
x_high] ly = y - y_low
bilinear_pos[ch, ph, pw, c, 2] = x_i[ch, y_high, lx = x - x_low
x_low] hy = 1 - ly
bilinear_pos[ch, ph, pw, c, 3] = x_i[ch, y_high, hx = 1 - lx
x_high] for ch in range(self.channels):
bilinear_w[ph, pw, c, 0] = hy * hx bilinear_pos[ch, ph, pw, c, 0] = x_i[ch, y_low,
bilinear_w[ph, pw, c, 1] = hy * lx x_low]
bilinear_w[ph, pw, c, 2] = ly * hx bilinear_pos[ch, ph, pw, c, 1] = x_i[ch, y_low,
bilinear_w[ph, pw, c, 3] = ly * lx x_high]
c = c + 1 bilinear_pos[ch, ph, pw, c, 2] = x_i[ch, y_high,
return bilinear_pos, bilinear_w x_low]
bilinear_pos[ch, ph, pw, c, 3] = x_i[ch, y_high,
def calc_roi_align(self): x_high]
self.out_data = np.zeros( bilinear_w[ph, pw, c, 0] = hy * hx
(self.rois_num, self.channels, self.pooled_height, bilinear_w[ph, pw, c, 1] = hy * lx
self.pooled_width)).astype('float32') bilinear_w[ph, pw, c, 2] = ly * hx
bilinear_w[ph, pw, c, 3] = ly * lx
for i in range(self.rois_num): c = c + 1
roi = self.rois[i] return bilinear_pos, bilinear_w
roi_batch_id = int(roi[0])
x_i = self.x[roi_batch_id] def calc_roi_align(self):
roi_offset = 0.5 if self.continuous_coordinate else 0 self.out_data = np.zeros(
roi_xmin = roi[1] * self.spatial_scale - roi_offset (self.rois_num, self.channels, self.pooled_height,
roi_ymin = roi[2] * self.spatial_scale - roi_offset self.pooled_width)).astype(self.dtype)
roi_xmax = roi[3] * self.spatial_scale - roi_offset
roi_ymax = roi[4] * self.spatial_scale - roi_offset for i in range(self.rois_num):
roi_width = roi_xmax - roi_xmin roi = self.rois[i]
roi_height = roi_ymax - roi_ymin roi_batch_id = int(roi[0])
if not self.continuous_coordinate: x_i = self.x[roi_batch_id]
roi_width = max(roi_width, 1) roi_offset = 0.5 if self.continuous_coordinate else 0
roi_height = max(roi_height, 1) roi_xmin = roi[1] * self.spatial_scale - roi_offset
bin_size_h = float(roi_height) / float(self.pooled_height) roi_ymin = roi[2] * self.spatial_scale - roi_offset
bin_size_w = float(roi_width) / float(self.pooled_width) roi_xmax = roi[3] * self.spatial_scale - roi_offset
roi_bin_grid_h = self.sampling_ratio if self.sampling_ratio > 0 else \ roi_ymax = roi[4] * self.spatial_scale - roi_offset
math.ceil(roi_height / self.pooled_height) roi_width = roi_xmax - roi_xmin
roi_bin_grid_w = self.sampling_ratio if self.sampling_ratio > 0 else \ roi_height = roi_ymax - roi_ymin
math.ceil(roi_width / self.pooled_width) if not self.continuous_coordinate:
count = int(roi_bin_grid_h * roi_bin_grid_w) roi_width = max(roi_width, 1)
pre_size = count * self.pooled_width * self.pooled_height roi_height = max(roi_height, 1)
bilinear_pos, bilinear_w = self.pre_calc(x_i, roi_xmin, roi_ymin, bin_size_h = float(roi_height) / float(self.pooled_height)
int(roi_bin_grid_h), bin_size_w = float(roi_width) / float(self.pooled_width)
int(roi_bin_grid_w), roi_bin_grid_h = self.sampling_ratio if self.sampling_ratio > 0 else \
bin_size_h, bin_size_w) math.ceil(roi_height / self.pooled_height)
for ch in range(self.channels): roi_bin_grid_w = self.sampling_ratio if self.sampling_ratio > 0 else \
align_per_bin = (bilinear_pos[ch] * bilinear_w).sum(axis=-1) math.ceil(roi_width / self.pooled_width)
output_val = align_per_bin.mean(axis=-1) count = int(roi_bin_grid_h * roi_bin_grid_w)
self.out_data[i, ch, :, :] = output_val pre_size = count * self.pooled_width * self.pooled_height
bilinear_pos, bilinear_w = self.pre_calc(
def make_rois(self): x_i, roi_xmin, roi_ymin, int(roi_bin_grid_h),
rois = [] int(roi_bin_grid_w), bin_size_h, bin_size_w)
self.rois_lod = [[]] for ch in range(self.channels):
for bno in range(self.batch_size): align_per_bin = (bilinear_pos[ch] * bilinear_w).sum(axis=-1)
self.rois_lod[0].append(bno + 1) output_val = align_per_bin.mean(axis=-1)
for i in range(bno + 1): self.out_data[i, ch, :, :] = output_val
x1 = np.random.random_integers(
0, self.width // self.spatial_scale - self.pooled_width) def make_rois(self):
y1 = np.random.random_integers( rois = []
0, self.height // self.spatial_scale - self.pooled_height) self.rois_lod = [[]]
for bno in range(self.batch_size):
x2 = np.random.random_integers(x1 + self.pooled_width, self.rois_lod[0].append(bno + 1)
self.width // self.spatial_scale) for i in range(bno + 1):
y2 = np.random.random_integers( x1 = np.random.random_integers(
y1 + self.pooled_height, self.height // self.spatial_scale) 0, self.width // self.spatial_scale - self.pooled_width)
y1 = np.random.random_integers(
roi = [bno, x1, y1, x2, y2] 0,
rois.append(roi) self.height // self.spatial_scale - self.pooled_height)
self.rois_num = len(rois)
self.rois = np.array(rois).astype("float32") x2 = np.random.random_integers(
x1 + self.pooled_width,
def setUp(self): self.width // self.spatial_scale)
self.op_type = "roi_align" y2 = np.random.random_integers(
self.set_data() y1 + self.pooled_height,
self.height // self.spatial_scale)
def test_check_output(self):
if paddle.is_compiled_with_xpu(): roi = [bno, x1, y1, x2, y2]
paddle.enable_static() rois.append(roi)
place = paddle.XPUPlace(0) self.rois_num = len(rois)
self.check_output_with_place(place) self.rois = np.array(rois).astype(self.dtype)
def test_check_grad(self): def setUp(self):
if core.is_compiled_with_xpu(): self.set_xpu()
paddle.enable_static() self.op_type = "roi_align"
place = paddle.XPUPlace(0) self.place = paddle.XPUPlace(0)
self.check_grad_with_place(place, {'X'}, 'Out') self.dtype = self.in_type
self.set_data()
class TestROIAlignInLodOp(TestROIAlignOp): def set_xpu(self):
self.__class__.use_xpu = True
def set_data(self):
self.init_test_case() def test_check_output(self):
self.make_rois() self.check_output_with_place(self.place)
self.calc_roi_align()
def test_check_grad(self):
seq_len = self.rois_lod[0] self.check_grad_with_place(self.place, {'X'}, 'Out')
self.inputs = { class TestROIAlignInLodOp(TestROIAlignOp):
'X': self.x,
'ROIs': (self.rois[:, 1:5], self.rois_lod), def set_data(self):
'RoisNum': np.asarray(seq_len).astype('int32') self.init_test_case()
} self.make_rois()
self.calc_roi_align()
self.attrs = {
'spatial_scale': self.spatial_scale, seq_len = self.rois_lod[0]
'pooled_height': self.pooled_height,
'pooled_width': self.pooled_width, self.inputs = {
'sampling_ratio': self.sampling_ratio, 'X': self.x,
'aligned': self.continuous_coordinate 'ROIs': (self.rois[:, 1:5], self.rois_lod),
} 'RoisNum': np.asarray(seq_len).astype('int32')
}
self.outputs = {'Out': self.out_data}
self.attrs = {
'spatial_scale': self.spatial_scale,
'pooled_height': self.pooled_height,
'pooled_width': self.pooled_width,
'sampling_ratio': self.sampling_ratio,
'aligned': self.continuous_coordinate
}
self.outputs = {'Out': self.out_data}
support_types = get_xpu_op_support_types('roi_align')
for stype in support_types:
create_test_class(globals(), XPUTestROIAlignOp, stype)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册