-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsparse_conv2d_test.py
42 lines (31 loc) · 1.4 KB
/
sparse_conv2d_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
#!/usr/bin/env python3
"""
Tests for the SparseConv2d Tensorflow operation.
"""
import unittest
import numpy as np
print('import tensorflow')
import tensorflow as tf
print('import libsparse_conv2d')
sparse_conv2d_m = tf.load_op_library('/root/repos/tensorflow/bazel-bin/tensorflow/core/user_ops/sparse_conv2d.so')
class InnerProductOpTest(unittest.TestCase):
def test_sparse_conv2d(self):
with tf.Session('') as sess:
x = tf.placeholder(tf.float32, shape=(1, 228, 228, 3))
conv = sparse_conv2d_m.sparse_conv2d(x, [[0, 0]], [1.0], dense_shape=[11, 11, 3, 96], strides=[4, 4])
self.assertListEqual([1, 55, 55, 96], conv.get_shape().as_list())
def test_sparse_conv2d_simple(self):
with tf.Session('') as sess:
x = tf.placeholder(tf.float32, shape=(1, 11, 11, 3))
conv = sparse_conv2d_m.sparse_conv2d(x, [[0, 0]], [1.0], dense_shape=[11, 11, 3, 96], strides=[4, 4])
inp = np.zeros((1, 11, 11, 3))
out = sess.run(conv, feed_dict={x: inp})
self.assertEqual(0, np.count_nonzero(out))
inp[0][1][1][0] = 1
out = sess.run(conv, feed_dict={x: inp})
self.assertEqual(0, np.count_nonzero(out))
inp[0][0][0][0] = 1
out = sess.run(conv, feed_dict={x: inp})
self.assertEqual(1, np.count_nonzero(out))
if __name__ == '__main__':
unittest.main()