|
|
@ -13,6 +13,7 @@
|
|
|
|
# limitations under the License.
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import unittest
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
@ -21,16 +22,28 @@ import paddle.fluid.core as core
|
|
|
|
from op_test import OpTest
|
|
|
|
from op_test import OpTest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def adaptive_start_index(index, input_size, output_size):
|
|
|
|
|
|
|
|
return int(np.floor(index * input_size / output_size))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def adaptive_end_index(index, input_size, output_size):
|
|
|
|
|
|
|
|
return int(np.ceil((index + 1) * input_size / output_size))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def max_pool3D_forward_naive(x,
|
|
|
|
def max_pool3D_forward_naive(x,
|
|
|
|
ksize,
|
|
|
|
ksize,
|
|
|
|
strides,
|
|
|
|
strides,
|
|
|
|
paddings,
|
|
|
|
paddings,
|
|
|
|
global_pool=0,
|
|
|
|
global_pool=0,
|
|
|
|
ceil_mode=False,
|
|
|
|
ceil_mode=False,
|
|
|
|
exclusive=True):
|
|
|
|
exclusive=True,
|
|
|
|
|
|
|
|
adaptive=False):
|
|
|
|
N, C, D, H, W = x.shape
|
|
|
|
N, C, D, H, W = x.shape
|
|
|
|
if global_pool == 1:
|
|
|
|
if global_pool == 1:
|
|
|
|
ksize = [D, H, W]
|
|
|
|
ksize = [D, H, W]
|
|
|
|
|
|
|
|
if adaptive:
|
|
|
|
|
|
|
|
D_out, H_out, W_out = ksize
|
|
|
|
|
|
|
|
else:
|
|
|
|
D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1
|
|
|
|
D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1
|
|
|
|
) // strides[0] + 1 if ceil_mode else (
|
|
|
|
) // strides[0] + 1 if ceil_mode else (
|
|
|
|
H - ksize[0] + 2 * paddings[0]) // strides[0] + 1
|
|
|
|
H - ksize[0] + 2 * paddings[0]) // strides[0] + 1
|
|
|
@ -42,14 +55,26 @@ def max_pool3D_forward_naive(x,
|
|
|
|
W - ksize[2] + 2 * paddings[2]) // strides[2] + 1
|
|
|
|
W - ksize[2] + 2 * paddings[2]) // strides[2] + 1
|
|
|
|
out = np.zeros((N, C, D_out, H_out, W_out))
|
|
|
|
out = np.zeros((N, C, D_out, H_out, W_out))
|
|
|
|
for k in range(D_out):
|
|
|
|
for k in range(D_out):
|
|
|
|
|
|
|
|
if adaptive:
|
|
|
|
|
|
|
|
d_start = adaptive_start_index(k, D, ksize[0])
|
|
|
|
|
|
|
|
d_end = adaptive_end_index(k, D, ksize[0])
|
|
|
|
|
|
|
|
else:
|
|
|
|
d_start = np.max((k * strides[0] - paddings[0], 0))
|
|
|
|
d_start = np.max((k * strides[0] - paddings[0], 0))
|
|
|
|
d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D))
|
|
|
|
d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D))
|
|
|
|
for i in range(H_out):
|
|
|
|
for i in range(H_out):
|
|
|
|
h_start = np.max((i * strides[0] - paddings[0], 0))
|
|
|
|
if adaptive:
|
|
|
|
h_end = np.min((i * strides[0] + ksize[0] - paddings[0], H))
|
|
|
|
h_start = adaptive_start_index(i, H, ksize[1])
|
|
|
|
|
|
|
|
h_end = adaptive_end_index(i, H, ksize[1])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
h_start = np.max((i * strides[1] - paddings[1], 0))
|
|
|
|
|
|
|
|
h_end = np.min((i * strides[1] + ksize[1] - paddings[1], H))
|
|
|
|
for j in range(W_out):
|
|
|
|
for j in range(W_out):
|
|
|
|
w_start = np.max((j * strides[1] - paddings[1], 0))
|
|
|
|
if adaptive:
|
|
|
|
w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W))
|
|
|
|
w_start = adaptive_start_index(j, W, ksize[2])
|
|
|
|
|
|
|
|
w_end = adaptive_end_index(j, W, ksize[2])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
w_start = np.max((j * strides[2] - paddings[2], 0))
|
|
|
|
|
|
|
|
w_end = np.min((j * strides[2] + ksize[2] - paddings[2], W))
|
|
|
|
x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end]
|
|
|
|
x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end]
|
|
|
|
|
|
|
|
|
|
|
|
out[:, :, k, i, j] = np.max(x_masked, axis=(2, 3, 4))
|
|
|
|
out[:, :, k, i, j] = np.max(x_masked, axis=(2, 3, 4))
|
|
|
@ -62,10 +87,14 @@ def avg_pool3D_forward_naive(x,
|
|
|
|
paddings,
|
|
|
|
paddings,
|
|
|
|
global_pool=0,
|
|
|
|
global_pool=0,
|
|
|
|
ceil_mode=False,
|
|
|
|
ceil_mode=False,
|
|
|
|
exclusive=True):
|
|
|
|
exclusive=True,
|
|
|
|
|
|
|
|
adaptive=False):
|
|
|
|
N, C, D, H, W = x.shape
|
|
|
|
N, C, D, H, W = x.shape
|
|
|
|
if global_pool == 1:
|
|
|
|
if global_pool == 1:
|
|
|
|
ksize = [D, H, W]
|
|
|
|
ksize = [D, H, W]
|
|
|
|
|
|
|
|
if adaptive:
|
|
|
|
|
|
|
|
D_out, H_out, W_out = ksize
|
|
|
|
|
|
|
|
else:
|
|
|
|
D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1
|
|
|
|
D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1
|
|
|
|
) // strides[0] + 1 if ceil_mode else (
|
|
|
|
) // strides[0] + 1 if ceil_mode else (
|
|
|
|
H - ksize[0] + 2 * paddings[0]) // strides[0] + 1
|
|
|
|
H - ksize[0] + 2 * paddings[0]) // strides[0] + 1
|
|
|
@ -77,18 +106,30 @@ def avg_pool3D_forward_naive(x,
|
|
|
|
W - ksize[2] + 2 * paddings[2]) // strides[2] + 1
|
|
|
|
W - ksize[2] + 2 * paddings[2]) // strides[2] + 1
|
|
|
|
out = np.zeros((N, C, D_out, H_out, W_out))
|
|
|
|
out = np.zeros((N, C, D_out, H_out, W_out))
|
|
|
|
for k in range(D_out):
|
|
|
|
for k in range(D_out):
|
|
|
|
|
|
|
|
if adaptive:
|
|
|
|
|
|
|
|
d_start = adaptive_start_index(k, D, ksize[0])
|
|
|
|
|
|
|
|
d_end = adaptive_end_index(k, D, ksize[0])
|
|
|
|
|
|
|
|
else:
|
|
|
|
d_start = np.max((k * strides[0] - paddings[0], 0))
|
|
|
|
d_start = np.max((k * strides[0] - paddings[0], 0))
|
|
|
|
d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D))
|
|
|
|
d_end = np.min((k * strides[0] + ksize[0] - paddings[0], D))
|
|
|
|
for i in range(H_out):
|
|
|
|
for i in range(H_out):
|
|
|
|
h_start = np.max((i * strides[0] - paddings[0], 0))
|
|
|
|
if adaptive:
|
|
|
|
h_end = np.min((i * strides[0] + ksize[0] - paddings[0], H))
|
|
|
|
h_start = adaptive_start_index(i, H, ksize[1])
|
|
|
|
|
|
|
|
h_end = adaptive_end_index(i, H, ksize[1])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
h_start = np.max((i * strides[1] - paddings[1], 0))
|
|
|
|
|
|
|
|
h_end = np.min((i * strides[1] + ksize[1] - paddings[1], H))
|
|
|
|
for j in range(W_out):
|
|
|
|
for j in range(W_out):
|
|
|
|
w_start = np.max((j * strides[1] - paddings[1], 0))
|
|
|
|
if adaptive:
|
|
|
|
w_end = np.min((j * strides[1] + ksize[1] - paddings[1], W))
|
|
|
|
w_start = adaptive_start_index(j, W, ksize[2])
|
|
|
|
|
|
|
|
w_end = adaptive_end_index(j, W, ksize[2])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
w_start = np.max((j * strides[2] - paddings[2], 0))
|
|
|
|
|
|
|
|
w_end = np.min((j * strides[2] + ksize[2] - paddings[2], W))
|
|
|
|
x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end]
|
|
|
|
x_masked = x[:, :, d_start:d_end, h_start:h_end, w_start:w_end]
|
|
|
|
|
|
|
|
|
|
|
|
field_size = (d_end - d_start) * (h_end - h_start) * (w_end - w_start) \
|
|
|
|
field_size = (d_end - d_start) * (h_end - h_start) * (w_end - w_start) \
|
|
|
|
if exclusive else ksize[0] * ksize[1] * ksize[2]
|
|
|
|
if (exclusive or adaptive) else ksize[0] * ksize[1] * ksize[2]
|
|
|
|
out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3,
|
|
|
|
out[:, :, k, i, j] = np.sum(x_masked, axis=(2, 3,
|
|
|
|
4)) / field_size
|
|
|
|
4)) / field_size
|
|
|
|
return out
|
|
|
|
return out
|
|
|
@ -105,13 +146,14 @@ class TestPool3d_Op(OpTest):
|
|
|
|
self.init_pool_type()
|
|
|
|
self.init_pool_type()
|
|
|
|
self.init_ceil_mode()
|
|
|
|
self.init_ceil_mode()
|
|
|
|
self.init_exclusive()
|
|
|
|
self.init_exclusive()
|
|
|
|
|
|
|
|
self.init_adaptive()
|
|
|
|
|
|
|
|
|
|
|
|
if self.global_pool:
|
|
|
|
if self.global_pool:
|
|
|
|
self.paddings = [0 for _ in range(len(self.paddings))]
|
|
|
|
self.paddings = [0 for _ in range(len(self.paddings))]
|
|
|
|
input = np.random.random(self.shape).astype(self.dtype)
|
|
|
|
input = np.random.random(self.shape).astype(self.dtype)
|
|
|
|
output = self.pool3D_forward_naive(
|
|
|
|
output = self.pool3D_forward_naive(
|
|
|
|
input, self.ksize, self.strides, self.paddings, self.global_pool,
|
|
|
|
input, self.ksize, self.strides, self.paddings, self.global_pool,
|
|
|
|
self.ceil_mode, self.exclusive).astype(self.dtype)
|
|
|
|
self.ceil_mode, self.exclusive, self.adaptive).astype(self.dtype)
|
|
|
|
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}
|
|
|
|
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(input)}
|
|
|
|
|
|
|
|
|
|
|
|
self.attrs = {
|
|
|
|
self.attrs = {
|
|
|
@ -124,7 +166,8 @@ class TestPool3d_Op(OpTest):
|
|
|
|
'ceil_mode': self.ceil_mode,
|
|
|
|
'ceil_mode': self.ceil_mode,
|
|
|
|
'data_format':
|
|
|
|
'data_format':
|
|
|
|
'AnyLayout', # TODO(dzhwinter) : should be fix latter
|
|
|
|
'AnyLayout', # TODO(dzhwinter) : should be fix latter
|
|
|
|
'exclusive': self.exclusive
|
|
|
|
'exclusive': self.exclusive,
|
|
|
|
|
|
|
|
'adaptive': self.adaptive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.outputs = {'Out': output}
|
|
|
|
self.outputs = {'Out': output}
|
|
|
@ -171,6 +214,9 @@ class TestPool3d_Op(OpTest):
|
|
|
|
def init_exclusive(self):
|
|
|
|
def init_exclusive(self):
|
|
|
|
self.exclusive = True
|
|
|
|
self.exclusive = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_adaptive(self):
|
|
|
|
|
|
|
|
self.adaptive = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCase1(TestPool3d_Op):
|
|
|
|
class TestCase1(TestPool3d_Op):
|
|
|
|
def init_test_case(self):
|
|
|
|
def init_test_case(self):
|
|
|
@ -353,5 +399,10 @@ class TestCUDNNAvgInclude(TestCUDNNCase3):
|
|
|
|
self.exclusive = False
|
|
|
|
self.exclusive = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestAvgPoolAdaptive(TestCase1):
|
|
|
|
|
|
|
|
def init_adaptive(self):
|
|
|
|
|
|
|
|
self.adaptive = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
unittest.main()
|
|
|
|