latency.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import tensorflow as tf
  2. from tensorflow import nn
  3. import tensorflow.keras as keras
  4. from tensorflow.keras import layers
  5. from tensorflow.keras.models import Model, Sequential
  6. import numpy as np
  7. import time
  8. import sys
  9. sys.path.append('../hostLib/')
  10. from hostLib.layers.conv2d import Conv2D as Conv2DFPGA
  11. from hostLib import load_op
  12. def run(inputShape, filterShape, n):
  13. input = tf.random.uniform(shape=inputShape)
  14. filter = tf.random.uniform(shape=filterShape)
  15. for i in range(n):
  16. start = time.time()
  17. nn.convolution(input, filter)
  18. elapsed_time = time.time() - start
  19. print("CPU Conv2D OP: {:.6f} s".format(elapsed_time))
  20. for i in range(n):
  21. start = time.time()
  22. load_op.op_lib.MyConv2D_1(input=input, filter=filter)
  23. elapsed_time = time.time() - start
  24. print("FPGA Conv2D OP: {:.6f} s".format(elapsed_time))
  25. input = tf.random.uniform(shape=[1,228,228,1])
  26. filter = tf.random.uniform(shape=[5,5,1,1])
  27. nn.convolution(input, filter)
  28. load_op.op_lib.MyConv2D_1(input=input, filter=filter)
  29. #run((1,228,228,1), (5,5,1,1), 1000)
  30. def runLayer(inputShape, n):
  31. input_x = tf.random.uniform(shape=inputShape)
  32. input_y = tf.image.resize(input_x, (224,224))
  33. model = Sequential()
  34. model.add(layers.Conv2D(
  35. 1,
  36. kernel_size=5,
  37. strides=(1, 1),
  38. padding='valid',
  39. data_format='channels_last',
  40. dilation_rate=(1, 1),
  41. activation=None,
  42. use_bias=False,
  43. trainable=True
  44. ))
  45. model.compile(loss=keras.losses.categorical_crossentropy,
  46. optimizer=keras.optimizers.Adadelta())
  47. for i in range(n):
  48. start = time.time()
  49. model.evaluate(input_x, input_y, verbose=0)
  50. elapsed_time = time.time() - start
  51. print("CPU Conv2D layer: {:.6f} s".format(elapsed_time))
  52. model = Sequential()
  53. model.add(Conv2DFPGA(1))
  54. model.compile(loss=keras.losses.categorical_crossentropy,
  55. optimizer=keras.optimizers.Adadelta())
  56. for i in range(n):
  57. start = time.time()
  58. model.evaluate(input_x, input_y, verbose=0)
  59. elapsed_time = time.time() - start
  60. print("FPGA Conv2D layer: {:.6f} s".format(elapsed_time))
  61. runLayer((1,228,228,1), 1000)