utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import time
  15. class Times(object):
  16. def __init__(self):
  17. self.time = 0.
  18. # start time
  19. self.st = 0.
  20. # end time
  21. self.et = 0.
  22. def start(self):
  23. self.st = time.time()
  24. def end(self, repeats=1, accumulative=True):
  25. self.et = time.time()
  26. if accumulative:
  27. self.time += (self.et - self.st) / repeats
  28. else:
  29. self.time = (self.et - self.st) / repeats
  30. def reset(self):
  31. self.time = 0.
  32. self.st = 0.
  33. self.et = 0.
  34. def value(self):
  35. return round(self.time, 4)
  36. class Timer(Times):
  37. def __init__(self, with_tracker=False):
  38. super(Timer, self).__init__()
  39. self.with_tracker = with_tracker
  40. self.preprocess_time_s = Times()
  41. self.inference_time_s = Times()
  42. self.postprocess_time_s = Times()
  43. self.tracking_time_s = Times()
  44. self.img_num = 0
  45. def info(self, average=False):
  46. pre_time = self.preprocess_time_s.value()
  47. infer_time = self.inference_time_s.value()
  48. post_time = self.postprocess_time_s.value()
  49. track_time = self.tracking_time_s.value()
  50. total_time = pre_time + infer_time + post_time
  51. if self.with_tracker:
  52. total_time = total_time + track_time
  53. total_time = round(total_time, 4)
  54. print("------------------ Inference Time Info ----------------------")
  55. print("total_time(ms): {}, img_num: {}".format(total_time * 1000,
  56. self.img_num))
  57. preprocess_time = round(pre_time / max(1, self.img_num),
  58. 4) if average else pre_time
  59. postprocess_time = round(post_time / max(1, self.img_num),
  60. 4) if average else post_time
  61. inference_time = round(infer_time / max(1, self.img_num),
  62. 4) if average else infer_time
  63. tracking_time = round(track_time / max(1, self.img_num),
  64. 4) if average else track_time
  65. average_latency = total_time / max(1, self.img_num)
  66. qps = 0
  67. if total_time > 0:
  68. qps = 1 / average_latency
  69. print("average latency time(ms): {:.2f}, QPS: {:2f}".format(
  70. average_latency * 1000, qps))
  71. if self.with_tracker:
  72. print(
  73. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}, tracking_time(ms): {:.2f}".
  74. format(preprocess_time * 1000, inference_time * 1000,
  75. postprocess_time * 1000, tracking_time * 1000))
  76. else:
  77. print(
  78. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
  79. format(preprocess_time * 1000, inference_time * 1000,
  80. postprocess_time * 1000))
  81. def report(self, average=False):
  82. dic = {}
  83. pre_time = self.preprocess_time_s.value()
  84. infer_time = self.inference_time_s.value()
  85. post_time = self.postprocess_time_s.value()
  86. track_time = self.tracking_time_s.value()
  87. dic['preprocess_time_s'] = round(pre_time / max(1, self.img_num),
  88. 4) if average else pre_time
  89. dic['inference_time_s'] = round(infer_time / max(1, self.img_num),
  90. 4) if average else infer_time
  91. dic['postprocess_time_s'] = round(post_time / max(1, self.img_num),
  92. 4) if average else post_time
  93. dic['img_num'] = self.img_num
  94. total_time = pre_time + infer_time + post_time
  95. if self.with_tracker:
  96. dic['tracking_time_s'] = round(track_time / max(1, self.img_num),
  97. 4) if average else track_time
  98. total_time = total_time + track_time
  99. dic['total_time_s'] = round(total_time, 4)
  100. return dic