|
|
@@ -0,0 +1,150 @@ |
|
|
|
import numpy as np |
|
|
|
import torch |
|
|
|
import mindspore as ms |
|
|
|
import ms_adapter.pytorch as ms_torch |
|
|
|
|
|
|
|
def test_maxunpool1d_with_2dim(): |
|
|
|
ms.context.set_context(mode=ms.PYNATIVE_MODE) |
|
|
|
N = np.random.randint(1, 65) |
|
|
|
C = np.random.randint(1, 513) |
|
|
|
tensor = np.random.randn(N, C).astype(np.float32) |
|
|
|
|
|
|
|
torch_tensor = torch.tensor(tensor) |
|
|
|
kernel_size = np.random.randint(1, C + 1) |
|
|
|
padding = np.random.randint(0, kernel_size/2 + 1) |
|
|
|
stride = 1 |
|
|
|
|
|
|
|
torch_pooling, torch_indices = torch.nn.functional.max_pool1d(torch_tensor, kernel_size, stride, padding, return_indices=True) |
|
|
|
torch_output = torch.nn.functional.max_unpool1d(torch_pooling, torch_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
ms_pooling = ms_torch.tensor(torch_pooling.numpy()) |
|
|
|
ms_indices = ms_torch.tensor(torch_indices.numpy()) |
|
|
|
|
|
|
|
ms_output = ms_torch.nn.functional.max_unpool1d(ms_pooling, ms_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
assert np.allclose(ms_output.numpy(), torch_output.numpy()) |
|
|
|
|
|
|
|
|
|
|
|
def test_maxunpool1d_with_3dim(): |
|
|
|
ms.context.set_context(mode=ms.PYNATIVE_MODE) |
|
|
|
B = np.random.randint(1, 65) |
|
|
|
N = np.random.randint(1, 65) |
|
|
|
C = np.random.randint(1, 513) |
|
|
|
tensor = np.random.randn(B, N, C).astype(np.float32) |
|
|
|
|
|
|
|
torch_tensor = torch.tensor(tensor) |
|
|
|
kernel_size = np.random.randint(1, C + 1) |
|
|
|
padding = np.random.randint(0, kernel_size/2 + 1) |
|
|
|
stride = 1 |
|
|
|
|
|
|
|
torch_pooling, torch_indices = torch.nn.functional.max_pool1d(torch_tensor, kernel_size, stride, padding, return_indices=True) |
|
|
|
torch_output = torch.nn.functional.max_unpool1d(torch_pooling, torch_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
ms_pooling = ms_torch.tensor(torch_pooling.numpy()) |
|
|
|
ms_indices = ms_torch.tensor(torch_indices.numpy()) |
|
|
|
|
|
|
|
ms_output = ms_torch.nn.functional.max_unpool1d(ms_pooling, ms_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
assert np.allclose(ms_output.numpy(), torch_output.numpy()) |
|
|
|
|
|
|
|
|
|
|
|
def test_maxunpool2d_with_3dim(): |
|
|
|
ms.context.set_context(mode=ms.PYNATIVE_MODE) |
|
|
|
H = W = np.random.randint(1, 65) |
|
|
|
|
|
|
|
C = np.random.randint(1, 513) |
|
|
|
tensor = np.random.randn(C, H, W).astype(np.float32) |
|
|
|
|
|
|
|
torch_tensor = torch.tensor(tensor) |
|
|
|
kernel_size = np.random.randint(1, H + 1) |
|
|
|
padding = np.random.randint(0, kernel_size/2 + 1) |
|
|
|
stride = 1 |
|
|
|
|
|
|
|
torch_pooling, torch_indices = torch.nn.functional.max_pool2d(torch_tensor, kernel_size, stride, padding, return_indices=True) |
|
|
|
torch_output = torch.nn.functional.max_unpool2d(torch_pooling, torch_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
ms_pooling = ms_torch.tensor(torch_pooling.numpy()) |
|
|
|
ms_indices = ms_torch.tensor(torch_indices.numpy()) |
|
|
|
|
|
|
|
ms_output = ms_torch.nn.functional.max_unpool2d(ms_pooling, ms_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
assert np.allclose(ms_output.numpy(), torch_output.numpy()) |
|
|
|
|
|
|
|
|
|
|
|
def test_maxunpool2d_with_4dim(): |
|
|
|
ms.context.set_context(mode=ms.PYNATIVE_MODE) |
|
|
|
H = W = np.random.randint(1, 65) |
|
|
|
C = np.random.randint(1, 513) |
|
|
|
N = np.random.randint(1, 32) |
|
|
|
tensor = np.random.randn(N, C, H, W).astype(np.float32) |
|
|
|
|
|
|
|
torch_tensor = torch.tensor(tensor) |
|
|
|
kernel_size = np.random.randint(1, H + 1) |
|
|
|
padding = np.random.randint(0, kernel_size/2 + 1) |
|
|
|
stride = 1 |
|
|
|
|
|
|
|
torch_pooling, torch_indices = torch.nn.functional.max_pool2d(torch_tensor, kernel_size, stride, padding, return_indices=True) |
|
|
|
torch_output = torch.nn.functional.max_unpool2d(torch_pooling, torch_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
ms_pooling = ms_torch.tensor(torch_pooling.numpy()) |
|
|
|
ms_indices = ms_torch.tensor(torch_indices.numpy()) |
|
|
|
|
|
|
|
ms_output = ms_torch.nn.functional.max_unpool2d(ms_pooling, ms_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
assert np.allclose(ms_output.numpy(), torch_output.numpy()) |
|
|
|
|
|
|
|
|
|
|
|
def test_maxunpool3d_with_4dim(): |
|
|
|
ms.context.set_context(mode=ms.PYNATIVE_MODE) |
|
|
|
H = W = np.random.randint(1, 65) |
|
|
|
C = np.random.randint(1, 1025) |
|
|
|
D = np.random.randint(1, H + 1) |
|
|
|
tensor = np.random.randn(C, D, H, W).astype(np.float32) |
|
|
|
|
|
|
|
torch_tensor = torch.tensor(tensor) |
|
|
|
kernel_size = np.random.randint(1, D + 1) |
|
|
|
padding = np.random.randint(0, kernel_size/2 + 1) |
|
|
|
stride = 1 |
|
|
|
|
|
|
|
torch_pooling, torch_indices = torch.nn.functional.max_pool3d(torch_tensor, kernel_size, stride, padding, return_indices=True) |
|
|
|
torch_output = torch.nn.functional.max_unpool3d(torch_pooling, torch_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
ms_pooling = ms_torch.tensor(torch_pooling.numpy()) |
|
|
|
ms_indices = ms_torch.tensor(torch_indices.numpy()) |
|
|
|
|
|
|
|
ms_output = ms_torch.nn.functional.max_unpool3d(ms_pooling, ms_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
assert np.allclose(ms_output.numpy(), torch_output.numpy()) |
|
|
|
|
|
|
|
|
|
|
|
def test_maxunpool3d_with_5dim(): |
|
|
|
ms.context.set_context(mode=ms.PYNATIVE_MODE) |
|
|
|
H = W = np.random.randint(1, 33) |
|
|
|
C = np.random.randint(1, 513) |
|
|
|
D = np.random.randint(1, H + 1) |
|
|
|
N = np.random.randint(1, 17) |
|
|
|
tensor = np.random.randn(N, C, D, H, W).astype(np.float32) |
|
|
|
|
|
|
|
torch_tensor = torch.tensor(tensor) |
|
|
|
kernel_size = np.random.randint(1, D + 1) |
|
|
|
padding = np.random.randint(0, kernel_size/2 + 1) |
|
|
|
stride = 1 |
|
|
|
|
|
|
|
torch_pooling, torch_indices = torch.nn.functional.max_pool3d(torch_tensor, kernel_size, stride, padding, return_indices=True) |
|
|
|
torch_output = torch.nn.functional.max_unpool3d(torch_pooling, torch_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
ms_pooling = ms_torch.tensor(torch_pooling.numpy()) |
|
|
|
ms_indices = ms_torch.tensor(torch_indices.numpy()) |
|
|
|
|
|
|
|
ms_output = ms_torch.nn.functional.max_unpool3d(ms_pooling, ms_indices, kernel_size, stride, padding) |
|
|
|
|
|
|
|
assert np.allclose(ms_output.numpy(), torch_output.numpy()) |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
test_maxunpool1d_with_2dim() |
|
|
|
test_maxunpool1d_with_3dim() |
|
|
|
test_maxunpool2d_with_3dim() |
|
|
|
test_maxunpool2d_with_4dim() |
|
|
|
test_maxunpool3d_with_4dim() |
|
|
|
test_maxunpool3d_with_5dim() |
|
|
|
|