op_test.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import tensorflow as tf
  2. from tensorflow import nn
  3. import numpy as np
  4. from IPython import embed
  5. import sys
  6. sys.path.append('../hostLib/')
  7. from hostLib.layers.conv2d import Conv2D as Conv2DFPGA
  8. from hostLib import load_op
  9. class FPGALibTest(tf.test.TestCase):
  10. def testDummyOp(self):
  11. input = [1337, 42, 2**31-1, -1]
  12. with self.session():
  13. result = load_op.op_lib.MyDummy(input=input)
  14. self.assertAllEqual(result, input)
  15. def testDummyOp100(self):
  16. with self.session():
  17. for i in range(100):
  18. input = [i+1, 42, 2**31-1, -1]
  19. result = load_op.op_lib.MyDummy(input=input)
  20. self.assertAllEqual(result, input)
  21. def testDummyOpBig(self):
  22. input = [i+1 for i in range(1024)]
  23. with self.session():
  24. result = load_op.op_lib.MyDummyBig(input=input)
  25. self.assertAllEqual(result, input)
  26. def testDummyOpBig100(self):
  27. with self.session():
  28. for i in range(100):
  29. input = [i*100+k+1 for k in range(1024)]
  30. result = load_op.op_lib.MyDummyBig(input=input)
  31. self.assertAllEqual(result, input)
  32. def testConv2DSingle(self):
  33. img = np.ndarray((228,228), dtype=float)
  34. img.fill(0)
  35. for a in range(228):
  36. for b in range(228):
  37. img[a][b] = (a)*228+(b)
  38. kernel = np.ndarray((5,5), dtype=float)
  39. kernel.fill(0)
  40. kernel[2][2] = 1
  41. input = tf.constant(np.expand_dims(img, (0, 3)), dtype=float)
  42. filter = tf.constant(np.expand_dims(kernel, (2, 3)), dtype=float)
  43. with self.session():
  44. ref = nn.convolution(input, filter)
  45. result = load_op.op_lib.MyConv2D(input=input, filter=filter)
  46. self.assertAllClose(result, ref)
  47. def testConv2DRandom(self):
  48. input = tf.random.uniform(shape=[1,228,228,1])
  49. filter = tf.random.uniform(shape=[5,5,1,1])
  50. with self.session():
  51. ref = nn.convolution(input, filter)
  52. result = load_op.op_lib.MyConv2D(input=input, filter=filter)
  53. self.assertAllClose(result, ref)
  54. def testConv2DMulti(self):
  55. input = tf.random.uniform(shape=[2,228,228,3])
  56. filter = tf.random.uniform(shape=[5,5,3,4])
  57. with self.session():
  58. ref = nn.convolution(input, filter)
  59. result = load_op.op_lib.MyConv2D(input=input, filter=filter)
  60. self.assertAllClose(result, ref)
  61. if __name__ == "__main__":
  62. tf.test.main()