-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
test_im2col.py
45 lines (35 loc) · 1.41 KB
/
test_im2col.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
import unittest
import numpy as np
from dezero import Variable
import dezero.functions as F
from dezero.utils import gradient_check, array_equal
from dezero import utils
class TestIm2col(unittest.TestCase):
def test_forward1(self):
n, c, h, w = 1, 1, 3, 3
x = np.arange(n * c * h * w).reshape((n, c, h, w))
y = F.im2col(x, 3, 3, 0, to_matrix=True)
expected = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8]])
res = array_equal(y.data, expected)
self.assertTrue(res)
def test_backward1(self):
n, c, h, w = 1, 1, 3, 3
x = np.arange(n * c * h * w).reshape((n, c, h, w))
f = lambda x: F.im2col(x, 3, 3, 0, to_matrix=True)
self.assertTrue(gradient_check(f, x))
def test_backward2(self):
n, c, h, w = 1, 1, 3, 3
x = np.arange(n * c * h * w).reshape((n, c, h, w))
f = lambda x: F.im2col(x, 3, 3, 0, to_matrix=False)
self.assertTrue(gradient_check(f, x))
class TestCol2in(unittest.TestCase):
def test_backward1(self):
n, c, h, w = 1, 1, 3, 3
x = np.random.rand(1, 9)
f = lambda x: F.col2im(x, (n, c, h, w), 3, 3, 0, to_matrix=True)
self.assertTrue(gradient_check(f, x))
def test_backward2(self):
n, c, h, w = 1, 1, 3, 3
x = np.random.rand(1, 1, 3, 3, 1, 1)
f = lambda x: F.col2im(x, (n, c, h, w), 3, 3, 0, to_matrix=False)
self.assertTrue(gradient_check(f, x))