-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtfconv_test.py
executable file
·109 lines (91 loc) · 2.8 KB
/
tfconv_test.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
#!/usr/bin/env python3
"""
Tests for tensorflow.nn.conv2d() behaviour with different sized kernels.
"""
import tensorflow as tf
import numpy as np
import unittest
def equal(a, b, threshold = 0.001):
n,h,w,c = a.shape
a = a.reshape((h,w))
b = b.reshape((h,w))
out = np.all(abs(a - b) < threshold)
if not out:
print('MISMATCH')
print('got:')
print(a)
print('expected:')
print(b)
return out
class ConvTestCase(unittest.TestCase):
def test_conv(self):
in1 = np.array([
[.0,.0,.0,.0,.0,.0], # 0
[.0,.0,.0,.0,.0,.0], # 1
[.0,1.,.0,2.,.0,.0], # 2
[.0,.0,.0,.0,.0,.0], # 3
[.0,.0,.0,1.,.0,.0], # 4
[.0,.0,.0,.0,.0,.0], # 5
[.0,.0,.0,.0,.0,.0], # 6
], dtype=np.float32).reshape((1,7,6,1)) # NHWC
# Odd-sized kernel.
k1 = np.array([
[.3,.4,.0],
[.2,.0,.0],
[.1,.0,.0],
], dtype=np.float32).reshape((3,3,1,1)) # HWOI
out1a = np.array([
[.0,.0,.0,.0,.0,.0], # 0
[.0,.0,.1,.0,.2,.0], # 1
[.0,.0,.2,.0,.4,.0], # 2
[.0,.4,.3,.8,.7,.0], # 3
[.0,.0,.0,.0,.2,.0], # 4
[.0,.0,.0,.4,.3,.0], # 5
[.0,.0,.0,.0,.0,.0], # 6
], dtype=np.float32).reshape((1,7,6,1))
# Same vs valid.
conv1a = tf.nn.conv2d(in1, k1, strides = [1,1,1,1],
padding='SAME', data_format='NHWC').numpy()
assert equal(conv1a, out1a)
out1b = np.array([
[.0,.1,.0,.2], # 1
[.0,.2,.0,.4], # 2
[.4,.3,.8,.7], # 3
[.0,.0,.0,.2], # 4
[.0,.0,.4,.3], # 5
], dtype=np.float32).reshape((1,5,4,1))
# Valid vs same.
conv1b = tf.nn.conv2d(in1, k1, strides = [1,1,1,1],
padding='VALID', data_format='NHWC').numpy()
assert equal(conv1b, out1b)
# Even-sized kernel.
k2 = np.array([
[.1,.2],
[.3,.4],
], dtype=np.float32).reshape((2,2,1,1)) # HWOI
out2a = np.array([
[.0,.0,.0,.0,.0,.0], # 0
[.4,.3,.8,.6,.0,.0], # 1
[.2,.1,.4,.2,.0,.0], # 2
[.0,.0,.4,.3,.0,.0], # 3
[.0,.0,.2,.1,.0,.0], # 4
[.0,.0,.0,.0,.0,.0], # 5
[.0,.0,.0,.0,.0,.0], # 6
], dtype=np.float32).reshape((1,7,6,1))
conv2a = tf.nn.conv2d(in1, k2, strides = [1,1,1,1],
padding='SAME', data_format='NHWC').numpy()
assert equal(conv2a, out2a)
out2b = np.array([
[.0,.0,.0,.0,.0], # 0
[.4,.3,.8,.6,.0], # 1
[.2,.1,.4,.2,.0], # 2
[.0,.0,.4,.3,.0], # 3
[.0,.0,.2,.1,.0], # 4
[.0,.0,.0,.0,.0], # 5
], dtype=np.float32).reshape((1,6,5,1))
conv2b = tf.nn.conv2d(in1, k2, strides = [1,1,1,1],
padding='VALID', data_format='NHWC').numpy()
assert equal(conv2b, out2b)
if __name__ == '__main__':
unittest.main()
# vim:set ts=2 sw=2 sts=2 et: