未验证 提交 790e7c38 编写于 作者: Y yunyaoXYY 提交者: GitHub

[Fluid Clean] Clean image_resize, resize_bilinear, resize_trilinear and resize_nearest. (#48691)

* clean fluild resize_trilinear

* clean fluild resize_bilinear

* clean fluild resize_nearest

* clean fluid image_resize

* fix test_trt_nearest_interp_op.py

* fix yolov3.py

* fix yolov3.py
上级 34a957e3
此差异已折叠。
......@@ -211,9 +211,10 @@ class Upsample(fluid.dygraph.Layer):
out_shape.stop_gradient = True
# reisze by actual_shape
out = fluid.layers.resize_nearest(
input=inputs, scale=self.scale, actual_shape=out_shape
out = paddle.nn.functional.interpolate(
x=inputs, size=out_shape, mode='nearest'
)
return out
......
......@@ -17,6 +17,7 @@ import unittest
import numpy as np
from inference_pass_test import InferencePassTest
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.core import AnalysisConfig, PassVersionChecker
......@@ -81,16 +82,14 @@ class TRTNearestInterpTest(InferencePassTest):
def append_nearest_interp(self, data):
if self.scale > 0.0:
return fluid.layers.resize_nearest(
return paddle.nn.functional.interpolate(
data,
scale=self.scale,
align_corners=self.align_corners,
scale_factor=self.scale,
data_format=self.data_layout,
)
return fluid.layers.resize_nearest(
return paddle.nn.functional.interpolate(
data,
out_shape=self.resize_shape,
align_corners=self.align_corners,
size=self.resize_shape,
data_format=self.data_layout,
)
......
......@@ -511,57 +511,6 @@ class TestBilinearInterp_attr_tensor_Case3(TestBilinearInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestBilinearInterpOpAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_bilinear(x, out_shape=[12, 12])
out2 = fluid.layers.resize_bilinear(x, out_shape=[12, dim])
out3 = fluid.layers.resize_bilinear(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_bilinear(
x, out_shape=[4, 4], actual_shape=actual_size
)
out5 = fluid.layers.resize_bilinear(x, scale=scale_tensor)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_mlu():
place = paddle.device.MLUPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = bilinear_interp_np(
x_data, out_h=12, out_w=12, align_corners=True
)
for res in results:
np.testing.assert_allclose(res, expect_res, rtol=1e-6)
class TestBilinearInterpOpAPI_dy(unittest.TestCase):
def test_case(self):
import paddle
......
......@@ -546,101 +546,5 @@ class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestNearestAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
y = fluid.data(name="y", shape=[2, 6, 6, 3], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_nearest(
y, out_shape=[12, 12], data_format='NHWC', align_corners=False
)
out2 = fluid.layers.resize_nearest(
x, out_shape=[12, dim], align_corners=False
)
out3 = fluid.layers.resize_nearest(
x, out_shape=shape_tensor, align_corners=False
)
out4 = fluid.layers.resize_nearest(
x, out_shape=[4, 4], actual_shape=actual_size, align_corners=False
)
out5 = fluid.layers.resize_nearest(
x, scale=scale_tensor, align_corners=False
)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
place = paddle.MLUPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"y": np.transpose(x_data, (0, 2, 3, 1)),
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = nearest_neighbor_interp_np(
x_data, out_h=12, out_w=12, align_corners=False
)
np.testing.assert_allclose(
results[0], np.transpose(expect_res, (0, 2, 3, 1))
)
for i in range(len(results) - 1):
np.testing.assert_allclose(results[i + 1], expect_res)
class TestNearestInterpException(unittest.TestCase):
def test_exception(self):
import paddle
input = fluid.data(name="input", shape=[1, 3, 6, 6], dtype="float32")
def attr_data_format():
# for 4-D input, data_format can only be NCHW or NHWC
out = fluid.layers.resize_nearest(
input, out_shape=[4, 8], data_format='NDHWC'
)
def attr_scale_type():
out = fluid.layers.resize_nearest(input, scale='scale')
def attr_scale_value():
out = fluid.layers.resize_nearest(input, scale=-0.3)
def input_shape_error():
x = paddle.randn([1, 3])
out = paddle.nn.functional.interpolate(x, scale_factor='scale')
def mode_error():
x = paddle.randn([1, 3])
out = paddle.nn.functional.interpolate(
x, scale_factor='scale', mode="BILINEAR"
)
self.assertRaises(ValueError, attr_data_format)
self.assertRaises(TypeError, attr_scale_type)
self.assertRaises(ValueError, attr_scale_value)
self.assertRaises(ValueError, input_shape_error)
self.assertRaises(ValueError, mode_error)
if __name__ == "__main__":
unittest.main()
......@@ -404,87 +404,5 @@ class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestNearestAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
y = fluid.data(name="y", shape=[2, 6, 6, 3], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_nearest(
y, out_shape=[12, 12], data_format='NHWC', align_corners=False
)
out2 = fluid.layers.resize_nearest(
x, out_shape=[12, dim], align_corners=False
)
out3 = fluid.layers.resize_nearest(
x, out_shape=shape_tensor, align_corners=False
)
out4 = fluid.layers.resize_nearest(
x, out_shape=[4, 4], actual_shape=actual_size, align_corners=False
)
out5 = fluid.layers.resize_nearest(
x, scale=scale_tensor, align_corners=False
)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
place = paddle.NPUPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"y": np.transpose(x_data, (0, 2, 3, 1)),
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = nearest_neighbor_interp_np(
x_data, out_h=12, out_w=12, align_corners=False
)
np.testing.assert_allclose(
results[0], np.transpose(expect_res, (0, 2, 3, 1))
)
for i in range(len(results) - 1):
np.testing.assert_allclose(results[i + 1], expect_res)
class TestNearestInterpException(unittest.TestCase):
def test_exception(self):
input = fluid.data(name="input", shape=[1, 3, 6, 6], dtype="float32")
def attr_data_format():
# for 4-D input, data_format can only be NCHW or NHWC
out = fluid.layers.resize_nearest(
input, out_shape=[4, 8], data_format='NDHWC'
)
def attr_scale_type():
out = fluid.layers.resize_nearest(input, scale='scale')
def attr_scale_value():
out = fluid.layers.resize_nearest(input, scale=-0.3)
self.assertRaises(ValueError, attr_data_format)
self.assertRaises(TypeError, attr_scale_type)
self.assertRaises(ValueError, attr_scale_value)
if __name__ == "__main__":
unittest.main()
......@@ -18,7 +18,6 @@ import numpy as np
from op_test import OpTest
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
paddle.enable_static()
......@@ -529,56 +528,5 @@ class TestBilinearInterp_attr_tensor_Case3(TestBilinearInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestBilinearInterpOpAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_bilinear(x, out_shape=[12, 12])
out2 = fluid.layers.resize_bilinear(x, out_shape=[12, dim])
out3 = fluid.layers.resize_bilinear(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_bilinear(
x, out_shape=[4, 4], actual_shape=actual_size
)
out5 = fluid.layers.resize_bilinear(x, scale=scale_tensor)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = bilinear_interp_np(
x_data, out_h=12, out_w=12, align_corners=True
)
for res in results:
np.testing.assert_allclose(res, expect_res, rtol=1e-05)
if __name__ == "__main__":
unittest.main()
......@@ -638,57 +638,6 @@ class TestBilinearInterp_attr_tensor_Case3(TestBilinearInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestBilinearInterpOpAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_bilinear(x, out_shape=[12, 12])
out2 = fluid.layers.resize_bilinear(x, out_shape=[12, dim])
out3 = fluid.layers.resize_bilinear(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_bilinear(
x, out_shape=[4, 4], actual_shape=actual_size
)
out5 = fluid.layers.resize_bilinear(x, scale=scale_tensor)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = bilinear_interp_np(
x_data, out_h=12, out_w=12, align_corners=True
)
for res in results:
np.testing.assert_allclose(res, expect_res, rtol=1e-05)
class TestBilinearInterpOpAPI_dy(unittest.TestCase):
def test_case(self):
import paddle
......
......@@ -3187,94 +3187,6 @@ class TestBook(LayerTest):
return values
return indices
def make_resize_bilinear(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x', shape=[3, 9, 6], dtype="float32")
output = layers.resize_bilinear(x, out_shape=[12, 12])
return output
def make_resize_bilinear_by_scale(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x', shape=[3, 9, 6], dtype="float32")
output = layers.resize_bilinear(x, scale=1.5)
return output
def make_resize_nearest(self):
try:
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x1', shape=[3, 9, 6], dtype="float32")
output = layers.resize_nearest(x, out_shape=[12, 12])
except ValueError:
pass
try:
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(
name='x2', shape=[3, 9, 6, 7], dtype="float32"
)
output = layers.resize_nearest(x, out_shape=[12, 12, 12])
except ValueError:
pass
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x', shape=[3, 9, 6], dtype="float32")
output = layers.resize_nearest(x, out_shape=[12, 12])
return output
def make_resize_nearest_by_scale(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x1', shape=[3, 9, 6], dtype="float32")
output = layers.resize_nearest(x, scale=1.8)
return output
def make_resize_trilinear(self):
try:
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x2', shape=[3, 9, 6], dtype="float32")
output = layers.resize_trilinear(x, out_shape=[12, 12, 12])
except ValueError:
pass
try:
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(
name='x', shape=[3, 9, 6, 7], dtype="float32"
)
output = layers.resize_trilinear(x, out_shape=[12, 12])
except ValueError:
pass
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x', shape=[3, 9, 6, 7], dtype="float32")
output = layers.resize_trilinear(x, out_shape=[12, 12, 12])
return output
def make_resize_trilinear_by_scale(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
):
x = self._get_data(name='x', shape=[3, 9, 6, 7], dtype="float32")
output = layers.resize_trilinear(x, scale=2.1)
return output
def make_polygon_box_transform(self):
with program_guard(
fluid.default_main_program(), fluid.default_startup_program()
......
......@@ -17,7 +17,6 @@ import unittest
import numpy as np
from op_test import OpTest
import paddle.fluid as fluid
import paddle.fluid.core as core
......@@ -459,85 +458,6 @@ class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestNearestAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
y = fluid.data(name="y", shape=[2, 6, 6, 3], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_nearest(
y, out_shape=[12, 12], data_format='NHWC'
)
out2 = fluid.layers.resize_nearest(x, out_shape=[12, dim])
out3 = fluid.layers.resize_nearest(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_nearest(
x, out_shape=[4, 4], actual_shape=actual_size
)
out5 = fluid.layers.resize_nearest(x, scale=scale_tensor)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"y": np.transpose(x_data, (0, 2, 3, 1)),
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = nearest_neighbor_interp_np(
x_data, out_h=12, out_w=12, align_corners=True
)
np.testing.assert_allclose(
results[0], np.transpose(expect_res, (0, 2, 3, 1)), rtol=1e-05
)
for i in range(len(results) - 1):
np.testing.assert_allclose(results[i + 1], expect_res, rtol=1e-05)
class TestNearestInterpException(unittest.TestCase):
def test_exception(self):
input = fluid.data(name="input", shape=[1, 3, 6, 6], dtype="float32")
def attr_data_format():
# for 4-D input, data_format can only be NCHW or NHWC
out = fluid.layers.resize_nearest(
input, out_shape=[4, 8], data_format='NDHWC'
)
def attr_scale_type():
out = fluid.layers.resize_nearest(input, scale='scale')
def attr_scale_value():
out = fluid.layers.resize_nearest(input, scale=-0.3)
self.assertRaises(ValueError, attr_data_format)
self.assertRaises(TypeError, attr_scale_type)
self.assertRaises(ValueError, attr_scale_value)
if __name__ == "__main__":
import paddle
......
......@@ -684,64 +684,6 @@ class TestNearestInterp_attr_tensor_Case3(TestNearestInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestNearestAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 6], dtype="float32")
y = fluid.data(name="y", shape=[2, 6, 6, 3], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[2], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[2], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_nearest(
y, out_shape=[12, 12], data_format='NHWC'
)
out2 = fluid.layers.resize_nearest(x, out_shape=[12, dim])
out3 = fluid.layers.resize_nearest(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_nearest(
x, out_shape=[4, 4], actual_shape=actual_size
)
out5 = fluid.layers.resize_nearest(x, scale=scale_tensor)
x_data = np.random.random((2, 3, 6, 6)).astype("float32")
dim_data = np.array([12]).astype("int32")
shape_data = np.array([12, 12]).astype("int32")
actual_size_data = np.array([12, 12]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"y": np.transpose(x_data, (0, 2, 3, 1)),
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = nearest_neighbor_interp_np(
x_data, out_h=12, out_w=12, align_corners=True
)
np.testing.assert_allclose(
results[0], np.transpose(expect_res, (0, 2, 3, 1)), rtol=1e-05
)
for i in range(len(results) - 1):
np.testing.assert_allclose(results[i + 1], expect_res, rtol=1e-05)
class TestNearestInterpOpAPI_dy(unittest.TestCase):
def test_case(self):
import paddle
......@@ -793,41 +735,6 @@ class TestNearestInterp3DOpAPI_dy(unittest.TestCase):
np.testing.assert_allclose(out.numpy(), expect_res, rtol=1e-05)
class TestNearestInterpException(unittest.TestCase):
def test_exception(self):
import paddle
input = fluid.data(name="input", shape=[1, 3, 6, 6], dtype="float32")
def attr_data_format():
# for 4-D input, data_format can only be NCHW or NHWC
out = fluid.layers.resize_nearest(
input, out_shape=[4, 8], data_format='NDHWC'
)
def attr_scale_type():
out = fluid.layers.resize_nearest(input, scale='scale')
def attr_scale_value():
out = fluid.layers.resize_nearest(input, scale=-0.3)
def input_shape_error():
x = paddle.randn([1, 3])
out = paddle.nn.functional.interpolate(x, scale_factor='scale')
def mode_error():
x = paddle.randn([1, 3])
out = paddle.nn.functional.interpolate(
x, scale_factor='scale', mode="BILINEAR"
)
self.assertRaises(ValueError, attr_data_format)
self.assertRaises(TypeError, attr_scale_type)
self.assertRaises(ValueError, attr_scale_value)
self.assertRaises(ValueError, input_shape_error)
self.assertRaises(ValueError, mode_error)
@unittest.skipIf(
not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
......
......@@ -17,9 +17,7 @@ import unittest
import numpy as np
from op_test import OpTest
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.nn.functional import interpolate
def trilinear_interp_np(
......@@ -623,85 +621,5 @@ class TestTrilinearInterp_attr_tensor_Case3(TestTrilinearInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestTrilinearInterpAPI(unittest.TestCase):
def test_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 9, 4], dtype="float32")
y = fluid.data(name="y", shape=[2, 6, 9, 4, 3], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[3], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[3], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_trilinear(
y, out_shape=[12, 18, 8], data_format='NDHWC'
)
out2 = fluid.layers.resize_trilinear(x, out_shape=[12, dim, 8])
out3 = fluid.layers.resize_trilinear(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_trilinear(
x, out_shape=[4, 4, 8], actual_shape=actual_size
)
out5 = fluid.layers.resize_trilinear(x, scale=scale_tensor)
out6 = interpolate(
x, scale_factor=scale_tensor, mode='trilinear', data_format="NCDHW"
)
out7 = interpolate(
x, size=[4, 4, 8], mode='trilinear', data_format="NCDHW"
)
out8 = interpolate(
x, size=shape_tensor, mode='trilinear', data_format="NCDHW"
)
x_data = np.random.random((2, 3, 6, 9, 4)).astype("float32")
dim_data = np.array([18]).astype("int32")
shape_data = np.array([12, 18, 8]).astype("int32")
actual_size_data = np.array([12, 18, 8]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"y": np.transpose(x_data, (0, 2, 3, 4, 1)),
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = trilinear_interp_np(
x_data, out_d=12, out_h=18, out_w=8, align_mode=1
)
np.testing.assert_allclose(
results[0], np.transpose(expect_res, (0, 2, 3, 4, 1)), rtol=1e-05
)
for i in range(len(results) - 1):
np.testing.assert_allclose(results[i + 1], expect_res, rtol=1e-05)
class TestTrilinearInterpOpException(unittest.TestCase):
def test_exception(self):
input = fluid.data(name="input", shape=[2, 3, 6, 9, 4], dtype="float32")
def attr_data_format():
# for 5-D input, data_format only can be NCDHW or NDHWC
out = fluid.layers.resize_trilinear(
input, out_shape=[4, 8, 4], data_format='NHWC'
)
self.assertRaises(ValueError, attr_data_format)
if __name__ == "__main__":
unittest.main()
......@@ -20,7 +20,6 @@ from op_test import OpTest
import paddle
import paddle.fluid as fluid
import paddle.fluid.core as core
from paddle.fluid.framework import _test_eager_guard
from paddle.nn.functional import interpolate
np.random.seed(123)
......@@ -741,100 +740,6 @@ class TestTrilinearInterp_attr_tensor_Case3(TestTrilinearInterpOp_attr_tensor):
self.scale_by_1Dtensor = True
class TestTrilinearInterpAPI(unittest.TestCase):
def test_imperative_case(self):
with _test_eager_guard():
self.func_case()
self.func_case()
def func_case(self):
x = fluid.data(name="x", shape=[2, 3, 6, 9, 4], dtype="float32")
y = fluid.data(name="y", shape=[2, 6, 9, 4, 3], dtype="float32")
dim = fluid.data(name="dim", shape=[1], dtype="int32")
shape_tensor = fluid.data(name="shape_tensor", shape=[3], dtype="int32")
actual_size = fluid.data(name="actual_size", shape=[3], dtype="int32")
scale_tensor = fluid.data(
name="scale_tensor", shape=[1], dtype="float32"
)
out1 = fluid.layers.resize_trilinear(
y, out_shape=[12, 18, 8], data_format='NDHWC'
)
out2 = fluid.layers.resize_trilinear(x, out_shape=[12, dim, 8])
out3 = fluid.layers.resize_trilinear(x, out_shape=shape_tensor)
out4 = fluid.layers.resize_trilinear(
x, out_shape=[4, 4, 8], actual_shape=actual_size
)
out5 = fluid.layers.resize_trilinear(x, scale=scale_tensor)
out6 = interpolate(
x, scale_factor=scale_tensor, mode='trilinear', data_format="NCDHW"
)
out7 = interpolate(
x, size=[4, 4, 8], mode='trilinear', data_format="NCDHW"
)
out8 = interpolate(
x, size=shape_tensor, mode='trilinear', data_format="NCDHW"
)
x_data = np.random.random((2, 3, 6, 9, 4)).astype("float32")
dim_data = np.array([18]).astype("int32")
shape_data = np.array([12, 18, 8]).astype("int32")
actual_size_data = np.array([12, 18, 8]).astype("int32")
scale_data = np.array([2.0]).astype("float32")
if core.is_compiled_with_cuda():
place = core.CUDAPlace(0)
else:
place = core.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
results = exe.run(
fluid.default_main_program(),
feed={
"x": x_data,
"y": np.transpose(x_data, (0, 2, 3, 4, 1)),
"dim": dim_data,
"shape_tensor": shape_data,
"actual_size": actual_size_data,
"scale_tensor": scale_data,
},
fetch_list=[out1, out2, out3, out4, out5],
return_numpy=True,
)
expect_res = trilinear_interp_np(
x_data, out_d=12, out_h=18, out_w=8, align_mode=1
)
np.testing.assert_allclose(
results[0], np.transpose(expect_res, (0, 2, 3, 4, 1)), rtol=1e-05
)
for i in range(len(results) - 1):
np.testing.assert_allclose(results[i + 1], expect_res, rtol=1e-05)
# Follow the calculation of preceding out6, out7, out8.
# To pass CI-coverage, calculate out9 without verifying accuracy.
# Preceding PR link: https://github.com/PaddlePaddle/Paddle/pull/26520/files#diff-ee0c2b73d08659e90a8f3ac48451a6588d35e1613742f864f9aad4394e12c290
with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(x_data)
out9 = interpolate(
x, size=[12, 18, 8], mode='trilinear', data_format="NCDHW"
)
class TestTrilinearInterpOpException(unittest.TestCase):
def test_exception(self):
input = fluid.data(name="input", shape=[2, 3, 6, 9, 4], dtype="float32")
def attr_data_format():
# for 5-D input, data_format only can be NCDHW or NDHWC
out = fluid.layers.resize_trilinear(
input, out_shape=[4, 8, 4], data_format='NHWC'
)
self.assertRaises(ValueError, attr_data_format)
@unittest.skipIf(
not fluid.core.is_compiled_with_cuda(), "core is not compiled with CUDA"
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册