3
0

utils.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. import os
  16. import ast
  17. import argparse
  18. def argsparser():
  19. parser = argparse.ArgumentParser(description=__doc__)
  20. parser.add_argument(
  21. "--model_dir",
  22. type=str,
  23. default=None,
  24. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  25. "'infer_cfg.yml', created by tools/export_model.py."),
  26. required=True)
  27. parser.add_argument(
  28. "--image_file", type=str, default=None, help="Path of image file.")
  29. parser.add_argument(
  30. "--image_dir",
  31. type=str,
  32. default=None,
  33. help="Dir of image file, `image_file` has a higher priority.")
  34. parser.add_argument(
  35. "--batch_size", type=int, default=1, help="batch_size for inference.")
  36. parser.add_argument(
  37. "--video_file",
  38. type=str,
  39. default=None,
  40. help="Path of video file, `video_file` or `camera_id` has a highest priority."
  41. )
  42. parser.add_argument(
  43. "--camera_id",
  44. type=int,
  45. default=-1,
  46. help="device id of camera to predict.")
  47. parser.add_argument(
  48. "--threshold", type=float, default=0.5, help="Threshold of score.")
  49. parser.add_argument(
  50. "--output_dir",
  51. type=str,
  52. default="output",
  53. help="Directory of output visualization files.")
  54. parser.add_argument(
  55. "--run_mode",
  56. type=str,
  57. default='paddle',
  58. help="mode of running(paddle/trt_fp32/trt_fp16/trt_int8)")
  59. parser.add_argument(
  60. "--device",
  61. type=str,
  62. default='cpu',
  63. help="Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU."
  64. )
  65. parser.add_argument(
  66. "--use_gpu",
  67. type=ast.literal_eval,
  68. default=False,
  69. help="Deprecated, please use `--device`.")
  70. parser.add_argument(
  71. "--run_benchmark",
  72. type=ast.literal_eval,
  73. default=False,
  74. help="Whether to predict a image_file repeatedly for benchmark")
  75. parser.add_argument(
  76. "--enable_mkldnn",
  77. type=ast.literal_eval,
  78. default=False,
  79. help="Whether use mkldnn with CPU.")
  80. parser.add_argument(
  81. "--enable_mkldnn_bfloat16",
  82. type=ast.literal_eval,
  83. default=False,
  84. help="Whether use mkldnn bfloat16 inference with CPU.")
  85. parser.add_argument(
  86. "--cpu_threads", type=int, default=1, help="Num of threads with CPU.")
  87. parser.add_argument(
  88. "--trt_min_shape", type=int, default=1, help="min_shape for TensorRT.")
  89. parser.add_argument(
  90. "--trt_max_shape",
  91. type=int,
  92. default=1280,
  93. help="max_shape for TensorRT.")
  94. parser.add_argument(
  95. "--trt_opt_shape",
  96. type=int,
  97. default=640,
  98. help="opt_shape for TensorRT.")
  99. parser.add_argument(
  100. "--trt_calib_mode",
  101. type=bool,
  102. default=False,
  103. help="If the model is produced by TRT offline quantitative "
  104. "calibration, trt_calib_mode need to set True.")
  105. parser.add_argument(
  106. '--save_images',
  107. action='store_true',
  108. help='Save visualization image results.')
  109. parser.add_argument(
  110. '--save_mot_txts',
  111. action='store_true',
  112. help='Save tracking results (txt).')
  113. parser.add_argument(
  114. '--save_mot_txt_per_img',
  115. action='store_true',
  116. help='Save tracking results (txt) for each image.')
  117. parser.add_argument(
  118. '--scaled',
  119. type=bool,
  120. default=False,
  121. help="Whether coords after detector outputs are scaled, False in JDE YOLOv3 "
  122. "True in general detector.")
  123. parser.add_argument(
  124. "--tracker_config", type=str, default=None, help=("tracker donfig"))
  125. parser.add_argument(
  126. "--reid_model_dir",
  127. type=str,
  128. default=None,
  129. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  130. "'infer_cfg.yml', created by tools/export_model.py."))
  131. parser.add_argument(
  132. "--reid_batch_size",
  133. type=int,
  134. default=50,
  135. help="max batch_size for reid model inference.")
  136. parser.add_argument(
  137. '--use_dark',
  138. type=ast.literal_eval,
  139. default=True,
  140. help='whether to use darkpose to get better keypoint position predict ')
  141. parser.add_argument(
  142. "--action_file",
  143. type=str,
  144. default=None,
  145. help="Path of input file for action recognition.")
  146. parser.add_argument(
  147. "--window_size",
  148. type=int,
  149. default=50,
  150. help="Temporal size of skeleton feature for action recognition.")
  151. parser.add_argument(
  152. "--random_pad",
  153. type=ast.literal_eval,
  154. default=False,
  155. help="Whether do random padding for action recognition.")
  156. parser.add_argument(
  157. "--save_results",
  158. type=bool,
  159. default=False,
  160. help="Whether save detection result to file using coco format")
  161. return parser
  162. class Times(object):
  163. def __init__(self):
  164. self.time = 0.
  165. # start time
  166. self.st = 0.
  167. # end time
  168. self.et = 0.
  169. def start(self):
  170. self.st = time.time()
  171. def end(self, repeats=1, accumulative=True):
  172. self.et = time.time()
  173. if accumulative:
  174. self.time += (self.et - self.st) / repeats
  175. else:
  176. self.time = (self.et - self.st) / repeats
  177. def reset(self):
  178. self.time = 0.
  179. self.st = 0.
  180. self.et = 0.
  181. def value(self):
  182. return round(self.time, 4)
  183. class Timer(Times):
  184. def __init__(self, with_tracker=False):
  185. super(Timer, self).__init__()
  186. self.with_tracker = with_tracker
  187. self.preprocess_time_s = Times()
  188. self.inference_time_s = Times()
  189. self.postprocess_time_s = Times()
  190. self.tracking_time_s = Times()
  191. self.img_num = 0
  192. def info(self, average=False):
  193. pre_time = self.preprocess_time_s.value()
  194. infer_time = self.inference_time_s.value()
  195. post_time = self.postprocess_time_s.value()
  196. track_time = self.tracking_time_s.value()
  197. total_time = pre_time + infer_time + post_time
  198. if self.with_tracker:
  199. total_time = total_time + track_time
  200. total_time = round(total_time, 4)
  201. print("------------------ Inference Time Info ----------------------")
  202. print("total_time(ms): {}, img_num: {}".format(total_time * 1000,
  203. self.img_num))
  204. preprocess_time = round(pre_time / max(1, self.img_num),
  205. 4) if average else pre_time
  206. postprocess_time = round(post_time / max(1, self.img_num),
  207. 4) if average else post_time
  208. inference_time = round(infer_time / max(1, self.img_num),
  209. 4) if average else infer_time
  210. tracking_time = round(track_time / max(1, self.img_num),
  211. 4) if average else track_time
  212. average_latency = total_time / max(1, self.img_num)
  213. qps = 0
  214. if total_time > 0:
  215. qps = 1 / average_latency
  216. print("average latency time(ms): {:.2f}, QPS: {:2f}".format(
  217. average_latency * 1000, qps))
  218. if self.with_tracker:
  219. print(
  220. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}, tracking_time(ms): {:.2f}".
  221. format(preprocess_time * 1000, inference_time * 1000,
  222. postprocess_time * 1000, tracking_time * 1000))
  223. else:
  224. print(
  225. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
  226. format(preprocess_time * 1000, inference_time * 1000,
  227. postprocess_time * 1000))
  228. def report(self, average=False):
  229. dic = {}
  230. pre_time = self.preprocess_time_s.value()
  231. infer_time = self.inference_time_s.value()
  232. post_time = self.postprocess_time_s.value()
  233. track_time = self.tracking_time_s.value()
  234. dic['preprocess_time_s'] = round(pre_time / max(1, self.img_num),
  235. 4) if average else pre_time
  236. dic['inference_time_s'] = round(infer_time / max(1, self.img_num),
  237. 4) if average else infer_time
  238. dic['postprocess_time_s'] = round(post_time / max(1, self.img_num),
  239. 4) if average else post_time
  240. dic['img_num'] = self.img_num
  241. total_time = pre_time + infer_time + post_time
  242. if self.with_tracker:
  243. dic['tracking_time_s'] = round(track_time / max(1, self.img_num),
  244. 4) if average else track_time
  245. total_time = total_time + track_time
  246. dic['total_time_s'] = round(total_time, 4)
  247. return dic
  248. def get_current_memory_mb():
  249. """
  250. It is used to Obtain the memory usage of the CPU and GPU during the running of the program.
  251. And this function Current program is time-consuming.
  252. """
  253. import pynvml
  254. import psutil
  255. import GPUtil
  256. gpu_id = int(os.environ.get('CUDA_VISIBLE_DEVICES', 0))
  257. pid = os.getpid()
  258. p = psutil.Process(pid)
  259. info = p.memory_full_info()
  260. cpu_mem = info.uss / 1024. / 1024.
  261. gpu_mem = 0
  262. gpu_percent = 0
  263. gpus = GPUtil.getGPUs()
  264. if gpu_id is not None and len(gpus) > 0:
  265. gpu_percent = gpus[gpu_id].load
  266. pynvml.nvmlInit()
  267. handle = pynvml.nvmlDeviceGetHandleByIndex(0)
  268. meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
  269. gpu_mem = meminfo.used / 1024. / 1024.
  270. return round(cpu_mem, 4), round(gpu_mem, 4), round(gpu_percent, 4)