|
|
@ -16,32 +16,43 @@ from __future__ import print_function
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import unittest
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
import paddle
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
from paddle.fluid import Program, program_guard
|
|
|
|
|
|
|
|
import paddle.fluid.core as core
|
|
|
|
import paddle.fluid.core as core
|
|
|
|
|
|
|
|
import paddle.nn.functional as F
|
|
|
|
from op_test import OpTest
|
|
|
|
from op_test import OpTest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
np.random.seed(1)
|
|
|
|
|
|
|
|
|
|
|
|
def maxout_forward_naive(input, groups, channel_axis):
|
|
|
|
|
|
|
|
s0, s1, s2, s3 = input.shape
|
|
|
|
def maxout_forward_naive(x, groups, channel_axis):
|
|
|
|
if channel_axis == 3:
|
|
|
|
s0, s1, s2, s3 = x.shape
|
|
|
|
return np.ndarray([s0, s1, s2, s3 // groups, groups], \
|
|
|
|
if channel_axis == 1:
|
|
|
|
buffer = input, dtype=input.dtype).max(axis=(4))
|
|
|
|
return np.ndarray([s0, s1 // groups, groups, s2, s3], \
|
|
|
|
return np.ndarray([s0, s1 // groups, groups, s2, s3], \
|
|
|
|
buffer = x, dtype=x.dtype).max(axis=2)
|
|
|
|
buffer = input, dtype=input.dtype).max(axis=(2))
|
|
|
|
return np.ndarray([s0, s1, s2, s3 // groups, groups], \
|
|
|
|
|
|
|
|
buffer = x, dtype=x.dtype).max(axis=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMaxOutOp(OpTest):
|
|
|
|
class TestMaxOutOp(OpTest):
|
|
|
|
def setUp(self):
|
|
|
|
def setUp(self):
|
|
|
|
self.op_type = "maxout"
|
|
|
|
self.op_type = "maxout"
|
|
|
|
self.init_test_case()
|
|
|
|
self.dtype = 'float64'
|
|
|
|
input = np.random.random(self.shape)
|
|
|
|
self.shape = [3, 6, 2, 4]
|
|
|
|
output = self.MaxOut_forward_naive(input, self.groups, self.axis)
|
|
|
|
self.groups = 2
|
|
|
|
|
|
|
|
self.axis = 1
|
|
|
|
|
|
|
|
self.set_attrs()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
|
|
|
|
|
|
|
|
out = maxout_forward_naive(x, self.groups, self.axis)
|
|
|
|
|
|
|
|
|
|
|
|
self.inputs = {'X': input}
|
|
|
|
self.inputs = {'X': x}
|
|
|
|
self.attrs = {'groups': self.groups, 'axis': self.axis}
|
|
|
|
self.attrs = {'groups': self.groups, 'axis': self.axis}
|
|
|
|
|
|
|
|
self.outputs = {'Out': out}
|
|
|
|
|
|
|
|
|
|
|
|
self.outputs = {'Out': output}
|
|
|
|
def set_attrs(self):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_output(self):
|
|
|
|
def test_check_output(self):
|
|
|
|
self.check_output()
|
|
|
|
self.check_output()
|
|
|
@ -49,65 +60,89 @@ class TestMaxOutOp(OpTest):
|
|
|
|
def test_check_grad(self):
|
|
|
|
def test_check_grad(self):
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
self.check_grad(['X'], 'Out')
|
|
|
|
|
|
|
|
|
|
|
|
def init_test_case(self):
|
|
|
|
|
|
|
|
self.MaxOut_forward_naive = maxout_forward_naive
|
|
|
|
|
|
|
|
self.shape = [100, 6, 2, 2]
|
|
|
|
|
|
|
|
self.groups = 2
|
|
|
|
|
|
|
|
self.axis = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMaxOutOpAxis(TestMaxOutOp):
|
|
|
|
class TestMaxOutOpAxis0(TestMaxOutOp):
|
|
|
|
def init_test_case(self):
|
|
|
|
def set_attrs(self):
|
|
|
|
self.MaxOut_forward_naive = maxout_forward_naive
|
|
|
|
self.axis = -1
|
|
|
|
self.shape = [100, 2, 2, 6] # NHWC format
|
|
|
|
|
|
|
|
self.groups = 2
|
|
|
|
|
|
|
|
self.axis = 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMaxOutOpAxisAPI(unittest.TestCase):
|
|
|
|
class TestMaxOutOpAxis1(TestMaxOutOp):
|
|
|
|
def test_axis(self):
|
|
|
|
def set_attrs(self):
|
|
|
|
data1 = fluid.data(name='data1', shape=[3, 6, 2, 2], dtype='float32')
|
|
|
|
self.axis = 3
|
|
|
|
data2 = fluid.data(name='data2', shape=[3, 2, 2, 6], dtype='float32')
|
|
|
|
|
|
|
|
out1 = fluid.layers.maxout(data1, groups=2, axis=1)
|
|
|
|
|
|
|
|
out2 = fluid.layers.maxout(data2, groups=2, axis=-1)
|
|
|
|
|
|
|
|
data1_np = np.random.random((3, 6, 2, 2)).astype("float32")
|
|
|
|
|
|
|
|
data2_np = np.transpose(data1_np, [0, 2, 3, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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={"data1": data1_np,
|
|
|
|
|
|
|
|
"data2": data2_np},
|
|
|
|
|
|
|
|
fetch_list=[out1, out2],
|
|
|
|
|
|
|
|
return_numpy=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
class TestMaxOutOpFP32(TestMaxOutOp):
|
|
|
|
np.allclose(results[0], np.transpose(results[1], (0, 3, 1, 2))))
|
|
|
|
def set_attrs(self):
|
|
|
|
|
|
|
|
self.dtype = 'float32'
|
|
|
|
|
|
|
|
|
|
|
|
def test_exception(self):
|
|
|
|
|
|
|
|
input = fluid.data(name="input", shape=[2, 4, 6, 6], dtype="float32")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _attr_axis():
|
|
|
|
class TestMaxOutOpGroups(TestMaxOutOp):
|
|
|
|
out = fluid.layers.maxout(input, groups=2, axis=2)
|
|
|
|
def set_attrs(self):
|
|
|
|
|
|
|
|
self.groups = 3
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, _attr_axis)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMaxoutAPI(unittest.TestCase):
|
|
|
|
|
|
|
|
# test paddle.nn.Maxout, paddle.nn.functional.maxout
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
self.x_np = np.random.uniform(-1, 1, [2, 6, 5, 4]).astype(np.float64)
|
|
|
|
|
|
|
|
self.groups = 2
|
|
|
|
|
|
|
|
self.axis = 1
|
|
|
|
|
|
|
|
self.place=paddle.CUDAPlace(0) if core.is_compiled_with_cuda() \
|
|
|
|
|
|
|
|
else paddle.CPUPlace()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_static_api(self):
|
|
|
|
|
|
|
|
with paddle.static.program_guard(paddle.static.Program()):
|
|
|
|
|
|
|
|
x = paddle.data('X', self.x_np.shape, self.x_np.dtype)
|
|
|
|
|
|
|
|
out1 = F.maxout(x, self.groups, self.axis)
|
|
|
|
|
|
|
|
m = paddle.nn.Maxout(self.groups, self.axis)
|
|
|
|
|
|
|
|
out2 = m(x)
|
|
|
|
|
|
|
|
exe = paddle.static.Executor(self.place)
|
|
|
|
|
|
|
|
res = exe.run(feed={'X': self.x_np}, fetch_list=[out1, out2])
|
|
|
|
|
|
|
|
out_ref = maxout_forward_naive(self.x_np, self.groups, self.axis)
|
|
|
|
|
|
|
|
for r in res:
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(out_ref, r))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_dygraph_api(self):
|
|
|
|
|
|
|
|
paddle.disable_static(self.place)
|
|
|
|
|
|
|
|
x = paddle.to_tensor(self.x_np)
|
|
|
|
|
|
|
|
out1 = F.maxout(x, self.groups, self.axis)
|
|
|
|
|
|
|
|
m = paddle.nn.Maxout(self.groups, self.axis)
|
|
|
|
|
|
|
|
out2 = m(x)
|
|
|
|
|
|
|
|
out_ref = maxout_forward_naive(self.x_np, self.groups, self.axis)
|
|
|
|
|
|
|
|
for r in [out1, out2]:
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(out_ref, r.numpy()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out3 = F.maxout(x, self.groups, -1)
|
|
|
|
|
|
|
|
out3_ref = maxout_forward_naive(self.x_np, self.groups, -1)
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(out3_ref, out3.numpy()))
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fluid_api(self):
|
|
|
|
|
|
|
|
with fluid.program_guard(fluid.Program()):
|
|
|
|
|
|
|
|
x = fluid.data('X', self.x_np.shape, self.x_np.dtype)
|
|
|
|
|
|
|
|
out = fluid.layers.maxout(x, groups=self.groups, axis=self.axis)
|
|
|
|
|
|
|
|
exe = fluid.Executor(self.place)
|
|
|
|
|
|
|
|
res = exe.run(feed={'X': self.x_np}, fetch_list=[out])
|
|
|
|
|
|
|
|
out_ref = maxout_forward_naive(self.x_np, self.groups, self.axis)
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(out_ref, res[0]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
paddle.disable_static(self.place)
|
|
|
|
|
|
|
|
x = paddle.to_tensor(self.x_np)
|
|
|
|
|
|
|
|
out = paddle.fluid.layers.maxout(x, groups=self.groups, axis=self.axis)
|
|
|
|
|
|
|
|
self.assertTrue(np.allclose(out_ref, out.numpy()))
|
|
|
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
|
|
|
|
class TestMaxOutOpError(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_errors(self):
|
|
|
|
def test_errors(self):
|
|
|
|
with program_guard(Program()):
|
|
|
|
with paddle.static.program_guard(paddle.static.Program()):
|
|
|
|
# The input type must be Variable.
|
|
|
|
# The input type must be Variable.
|
|
|
|
self.assertRaises(TypeError, fluid.layers.maxout, 1, 2)
|
|
|
|
self.assertRaises(TypeError, F.maxout, 1)
|
|
|
|
# The input dtype must be float16, float32, float64.
|
|
|
|
# The input dtype must be float16, float32, float64.
|
|
|
|
x_int32 = fluid.data(name='x_int32', shape=[12, 10], dtype='int32')
|
|
|
|
x_int32 = paddle.data(
|
|
|
|
self.assertRaises(TypeError, fluid.layers.maxout, x_int32, 2)
|
|
|
|
name='x_int32', shape=[2, 4, 6, 8], dtype='int32')
|
|
|
|
# support the input dtype is float32
|
|
|
|
self.assertRaises(TypeError, F.maxout, x_int32)
|
|
|
|
x_fp32 = fluid.data(name='x_fp32', shape=[12, 10], dtype='float32')
|
|
|
|
|
|
|
|
fluid.layers.maxout(x_fp32, 2)
|
|
|
|
x_float32 = paddle.data(name='x_float32', shape=[2, 4, 6, 8])
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, F.maxout, x_float32, 2, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|