未验证 提交 2ed84a67 编写于 作者: L littletomatodonkey 提交者: GitHub

Add API for pad op. (#27943)

* add pad apis
* rm pad2d test_layer
* fix code example
上级 3718b2e7
......@@ -606,20 +606,6 @@ class TestBilinearInterpOpAPI(unittest.TestCase):
self.assertTrue(np.allclose(res, expect_res))
class TestUpsampleBilinear2dInterpOpAPI2_0(unittest.TestCase):
def test_case(self):
# dygraph
x_data = np.random.random((1, 3, 6, 6)).astype("float32")
upsample = paddle.nn.UpsamplingBilinear2d(scale_factor=[2, 2])
with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(x_data)
interp = upsample(x)
expect = bilinear_interp_np(
x_data, out_h=12, out_w=12, align_corners=True)
self.assertTrue(np.allclose(interp.numpy(), expect))
class TestBilinearInterpOpAPI_dy(unittest.TestCase):
def test_case(self):
import paddle
......
......@@ -316,23 +316,6 @@ class TestLayer(LayerTest):
self.assertTrue(np.allclose(static_ret, dy_ret_value))
def test_pad2d(self):
with self.static_graph():
t = layers.data(name='t', shape=[-1, 3, 5, 5], dtype='float32')
ret = layers.pad2d(t, paddings=[1, 1, 1, 1])
static_ret = self.get_static_graph_result(
feed={'t': np.ones(
[3, 3, 5, 5], dtype='float32')},
fetch_list=[ret])[0]
with self.dynamic_graph():
t = np.ones([3, 3, 5, 5], dtype='float32')
my_pad2d = paddle.nn.layer.Pad2D(paddings=1)
dy_ret = my_pad2d(base.to_variable(t))
dy_ret_value = dy_ret.numpy()
self.assertTrue(np.allclose(static_ret, dy_ret_value))
def test_matmul(self):
with self.static_graph():
t = layers.data(name='t', shape=[3, 3], dtype='float32')
......
......@@ -526,20 +526,6 @@ class TestNearestAPI(unittest.TestCase):
self.assertTrue(np.allclose(results[i + 1], expect_res))
class TestUpsampleNearest2dInterpOpAPI2_0(unittest.TestCase):
def test_case(self):
# dygraph
x_data = np.random.random((1, 3, 6, 6)).astype("float32")
upsample = paddle.nn.UpsamplingNearest2d(scale_factor=[2, 2])
with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(x_data)
interp = upsample(x)
expect = nearest_neighbor_interp_np(
x_data, out_h=12, out_w=12, align_corners=False)
self.assertTrue(np.allclose(interp.numpy(), expect))
class TestNearestInterpException(unittest.TestCase):
def test_exception(self):
input = fluid.data(name="input", shape=[1, 3, 6, 6], dtype="float32")
......
......@@ -314,7 +314,6 @@ class TestPadAPI(unittest.TestCase):
def test_dygraph_1(self):
paddle.disable_static()
input_shape = (1, 2, 3, 4, 5)
pad = [1, 2, 1, 1, 3, 4]
mode = "constant"
......@@ -342,7 +341,6 @@ class TestPadAPI(unittest.TestCase):
def test_dygraph_2(self):
paddle.disable_static()
input_shape = (2, 3, 4, 5)
pad = [1, 1, 3, 4]
mode = "constant"
......@@ -370,38 +368,8 @@ class TestPadAPI(unittest.TestCase):
self.assertTrue(np.allclose(y1.numpy(), np_out1))
self.assertTrue(np.allclose(y2.numpy(), np_out2))
def test_dygraph_2(self):
paddle.disable_static()
input_shape = (2, 3, 4, 5)
pad = [1, 1, 3, 4]
mode = "constant"
value = 100
input_data = np.random.rand(*input_shape).astype(np.float32)
np_out1 = self._get_numpy_out(
input_data, pad, mode, value, data_format="NCHW")
np_out2 = self._get_numpy_out(
input_data, pad, mode, value, data_format="NHWC")
tensor_data = paddle.to_tensor(input_data)
tensor_pad = paddle.to_tensor(pad, dtype="int32")
y1 = F.pad(tensor_data,
pad=tensor_pad,
mode=mode,
value=value,
data_format="NCHW")
y2 = F.pad(tensor_data,
pad=tensor_pad,
mode=mode,
value=value,
data_format="NHWC")
self.assertTrue(np.allclose(y1.numpy(), np_out1))
self.assertTrue(np.allclose(y2.numpy(), np_out2))
def test_dygraph_3(self):
paddle.disable_static()
input_shape = (3, 4, 5)
pad = [3, 4]
mode = "constant"
......@@ -455,6 +423,8 @@ class TestPad1dAPI(unittest.TestCase):
out = np.pad(input_data, pad, mode=mode)
elif mode == "replicate":
out = np.pad(input_data, pad, mode="edge")
elif mode == "circular":
out = np.pad(input_data, pad, mode="wrap")
return out
......@@ -471,9 +441,10 @@ class TestPad1dAPI(unittest.TestCase):
value = 100
input_data = np.random.rand(*input_shape).astype(np.float32)
pad_reflection = nn.ReflectionPad1d(padding=pad)
pad_replication = nn.ReplicationPad1d(padding=pad)
pad_constant = nn.ConstantPad1d(padding=pad, value=value)
pad_reflection = nn.Pad1D(padding=pad, mode="reflect")
pad_replication = nn.Pad1D(padding=pad, mode="replicate")
pad_constant = nn.Pad1D(padding=pad, mode="constant", value=value)
pad_circular = nn.Pad1D(padding=pad, mode="circular")
data = paddle.to_tensor(input_data)
......@@ -492,6 +463,11 @@ class TestPad1dAPI(unittest.TestCase):
input_data, pad, "constant", value=value, data_format="NCL")
self.assertTrue(np.allclose(output.numpy(), np_out))
output = pad_circular(data)
np_out = self._get_numpy_out(
input_data, pad, "circular", value=value, data_format="NCL")
self.assertTrue(np.allclose(output.numpy(), np_out))
class TestPad2dAPI(unittest.TestCase):
def _get_numpy_out(self,
......@@ -521,6 +497,8 @@ class TestPad2dAPI(unittest.TestCase):
out = np.pad(input_data, pad, mode=mode)
elif mode == "replicate":
out = np.pad(input_data, pad, mode="edge")
elif mode == "circular":
out = np.pad(input_data, pad, mode="wrap")
return out
......@@ -537,10 +515,10 @@ class TestPad2dAPI(unittest.TestCase):
value = 100
input_data = np.random.rand(*input_shape).astype(np.float32)
pad_reflection = nn.ReflectionPad2d(padding=pad)
pad_replication = nn.ReplicationPad2d(padding=pad)
pad_constant = nn.ConstantPad2d(padding=pad, value=value)
pad_zero = nn.ZeroPad2d(padding=pad)
pad_reflection = nn.Pad2D(padding=pad, mode="reflect")
pad_replication = nn.Pad2D(padding=pad, mode="replicate")
pad_constant = nn.Pad2D(padding=pad, mode="constant", value=value)
pad_circular = nn.Pad2D(padding=pad, mode="circular")
data = paddle.to_tensor(input_data)
......@@ -559,9 +537,9 @@ class TestPad2dAPI(unittest.TestCase):
input_data, pad, "constant", value=value, data_format="NCHW")
self.assertTrue(np.allclose(output.numpy(), np_out))
output = pad_zero(data)
output = pad_circular(data)
np_out = self._get_numpy_out(
input_data, pad, "constant", value=0, data_format="NCHW")
input_data, pad, "circular", data_format="NCHW")
self.assertTrue(np.allclose(output.numpy(), np_out))
......@@ -595,6 +573,8 @@ class TestPad3dAPI(unittest.TestCase):
out = np.pad(input_data, pad, mode=mode)
elif mode == "replicate":
out = np.pad(input_data, pad, mode="edge")
elif mode == "circular":
out = np.pad(input_data, pad, mode="wrap")
return out
......@@ -611,11 +591,18 @@ class TestPad3dAPI(unittest.TestCase):
value = 100
input_data = np.random.rand(*input_shape).astype(np.float32)
pad_replication = nn.ReplicationPad3d(padding=pad)
pad_constant = nn.ConstantPad3d(padding=pad, value=value)
pad_reflection = nn.Pad3D(padding=pad, mode="reflect")
pad_replication = nn.Pad3D(padding=pad, mode="replicate")
pad_constant = nn.Pad3D(padding=pad, mode="constant", value=value)
pad_circular = nn.Pad3D(padding=pad, mode="circular")
data = paddle.to_tensor(input_data)
output = pad_reflection(data)
np_out = self._get_numpy_out(
input_data, pad, "reflect", data_format="NCDHW")
self.assertTrue(np.allclose(output.numpy(), np_out))
output = pad_replication(data)
np_out = self._get_numpy_out(
input_data, pad, "replicate", data_format="NCDHW")
......@@ -626,6 +613,11 @@ class TestPad3dAPI(unittest.TestCase):
input_data, pad, "constant", value=value, data_format="NCDHW")
self.assertTrue(np.allclose(output.numpy(), np_out))
output = pad_circular(data)
np_out = self._get_numpy_out(
input_data, pad, "circular", data_format="NCDHW")
self.assertTrue(np.allclose(output.numpy(), np_out))
class TestPad3dOpError(unittest.TestCase):
def test_errors(self):
......@@ -673,32 +665,30 @@ class TestPad3dOpError(unittest.TestCase):
class TestPadDataformatError(unittest.TestCase):
def test_errors(self):
def test_ncl():
paddle.disable_static(paddle.CPUPlace())
input_shape = (1, 2, 3, 4)
pad = paddle.to_tensor(np.array([2, 1, 2, 1]).astype('int32'))
data = np.arange(
np.prod(input_shape), dtype=np.float64).reshape(input_shape) + 1
my_pad = nn.ReplicationPad1d(padding=pad, data_format="NCL")
my_pad = nn.Pad1D(padding=pad, mode="replicate", data_format="NCL")
data = paddle.to_tensor(data)
result = my_pad(data)
def test_nchw():
paddle.disable_static(paddle.CPUPlace())
input_shape = (1, 2, 4)
pad = paddle.to_tensor(np.array([2, 1, 2, 1]).astype('int32'))
data = np.arange(
np.prod(input_shape), dtype=np.float64).reshape(input_shape) + 1
my_pad = nn.ReplicationPad1d(padding=pad, data_format="NCHW")
my_pad = nn.Pad1D(padding=pad, mode="replicate", data_format="NCHW")
data = paddle.to_tensor(data)
result = my_pad(data)
def test_ncdhw():
paddle.disable_static(paddle.CPUPlace())
input_shape = (1, 2, 3, 4)
pad = paddle.to_tensor(np.array([2, 1, 2, 1]).astype('int32'))
data = np.arange(
np.prod(input_shape), dtype=np.float64).reshape(input_shape) + 1
my_pad = nn.ReplicationPad1d(padding=pad, data_format="NCDHW")
my_pad = nn.Pad1D(
padding=pad, mode="replicate", data_format="NCDHW")
data = paddle.to_tensor(data)
result = my_pad(data)
......
......@@ -71,22 +71,16 @@ from .layer.activation import Tanhshrink #DEFINE_ALIAS
from .layer.activation import ThresholdedReLU #DEFINE_ALIAS
from .layer.activation import LogSoftmax #DEFINE_ALIAS
from .layer.activation import Maxout #DEFINE_ALIAS
from .layer.common import ReflectionPad1d #DEFINE_ALIAS
from .layer.common import ReplicationPad1d #DEFINE_ALIAS
from .layer.common import ConstantPad1d #DEFINE_ALIAS
from .layer.common import ReflectionPad2d #DEFINE_ALIAS
from .layer.common import ReplicationPad2d #DEFINE_ALIAS
from .layer.common import ConstantPad2d #DEFINE_ALIAS
from .layer.common import ZeroPad2d #DEFINE_ALIAS
from .layer.common import ReplicationPad3d #DEFINE_ALIAS
from .layer.common import ConstantPad3d #DEFINE_ALIAS
from .layer.common import BilinearTensorProduct #DEFINE_ALIAS
from .layer.common import Pool2D #DEFINE_ALIAS
from .layer.common import Pad1D #DEFINE_ALIAS
from .layer.common import Pad2D #DEFINE_ALIAS
from .layer.common import Pad3D #DEFINE_ALIAS
from .layer.common import CosineSimilarity #DEFINE_ALIAS
from .layer.common import Embedding #DEFINE_ALIAS
from .layer.common import Linear #DEFINE_ALIAS
from .layer.common import Flatten #DEFINE_ALIAS
from .layer.common import Upsample #DEFINE_ALIAS
from .layer.common import UpsamplingNearest2d #DEFINE_ALIAS
from .layer.common import UpsamplingBilinear2d #DEFINE_ALIAS
from .layer.common import Bilinear #DEFINE_ALIAS
from .layer.common import Dropout #DEFINE_ALIAS
from .layer.common import Dropout2d #DEFINE_ALIAS
......
......@@ -44,23 +44,14 @@ from .activation import LogSoftmax #DEFINE_ALIAS
from .common import BilinearTensorProduct #DEFINE_ALIAS
from .common import Bilinear #DEFINE_ALIAS
from .common import Pool2D #DEFINE_ALIAS
from .common import Pad1D #DEFINE_ALIAS
from .common import Pad2D #DEFINE_ALIAS
from .common import ReflectionPad1d #DEFINE_ALIAS
from .common import ReplicationPad1d #DEFINE_ALIAS
from .common import ConstantPad1d #DEFINE_ALIAS
from .common import ReflectionPad2d #DEFINE_ALIAS
from .common import ReplicationPad2d #DEFINE_ALIAS
from .common import ConstantPad2d #DEFINE_ALIAS
from .common import ZeroPad2d #DEFINE_ALIAS
from .common import ReplicationPad3d #DEFINE_ALIAS
from .common import ConstantPad3d #DEFINE_ALIAS
from .common import Pad3D #DEFINE_ALIAS
from .common import CosineSimilarity #DEFINE_ALIAS
from .common import Embedding #DEFINE_ALIAS
from .common import Linear #DEFINE_ALIAS
from .common import Flatten #DEFINE_ALIAS
from .common import Upsample #DEFINE_ALIAS
from .common import UpsamplingNearest2d #DEFINE_ALIAS
from .common import UpsamplingBilinear2d #DEFINE_ALIAS
from .common import Dropout #DEFINE_ALIAS
from .common import Dropout2d #DEFINE_ALIAS
from .common import Dropout3d #DEFINE_ALIAS
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册