-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_maxpool.py
111 lines (86 loc) · 3.45 KB
/
test_maxpool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sys; import os; sys.path.insert(1, os.path.join(os.getcwd(), "numpy_nn"))
import unittest
from typing import Callable
import torch
import numpy as np
from test_layer import TestLayer
from numpy_nn.modules.np_nn import MaxPool2d
# np_seed = 0
# torch_seed = 0
# np.random.seed(np_seed)
# torch.manual_seed(torch_seed)
class TestMaxPool2d(TestLayer):
def setUp(self) -> None:
pass
def _test_maxpool_with_args(self, batch_size: int, height: int,
width: int, n_channels: int, kernel_size: int,
stride: int, padding: int, atol: float = 1e-5,
random_sampler: Callable = np.random.rand,
print_tensors: bool = False,
print_results: bool = False):
output_width = (width + 2 * padding - kernel_size) // stride + 1
output_height = (height + 2 * padding - kernel_size) // stride + 1
my_pool_args = torch_pool_args = [kernel_size, stride, padding]
my_pool = MaxPool2d(*my_pool_args)
torch_pool = torch.nn.MaxPool2d(*torch_pool_args)
input_shape = [batch_size, n_channels, height, width]
output_shape=[batch_size, n_channels, output_height, output_width]
self._test_module_randomly(
my_pool,
torch_pool,
input_shape=input_shape,
output_shape=output_shape,
atol=atol,
random_sampler = random_sampler,
print_tensors=print_tensors,
print_results=print_results)
def test_maxpool_1(self):
"""
Maxpool test
"""
batch_size = 10
n_channels = 3
height = 16
width = 16
kernel_size = 3
stride = 2
padding = 1
n_iters = 3
for sampler in (np.random.rand, np.random.randn):
for _ in range(n_iters):
with self.subTest(sampler = sampler):
self._test_maxpool_with_args(batch_size,
height,
width,
n_channels,
kernel_size,
stride,
padding,
atol=1e-6,
random_sampler=sampler)
def test_maxpool_2(self):
"""
Maxpool test
"""
batch_size = 2
n_channels = 6
height = 4
width = 3
kernel_size = 2
stride = 1
padding = 0
n_iters = 3
for sampler in (np.random.rand, np.random.randn):
for _ in range(n_iters):
with self.subTest(sampler = sampler):
self._test_maxpool_with_args(batch_size,
height,
width,
n_channels,
kernel_size,
stride,
padding,
atol=1e-6,
random_sampler=sampler)
if __name__ == "__main__":
unittest.main()