utils.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. import numpy as np
  19. def argsparser():
  20. parser = argparse.ArgumentParser(description=__doc__)
  21. parser.add_argument(
  22. "--model_dir",
  23. type=str,
  24. default=None,
  25. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  26. "'infer_cfg.yml', created by tools/export_model.py."),
  27. required=True)
  28. parser.add_argument(
  29. "--image_file", type=str, default=None, help="Path of image file.")
  30. parser.add_argument(
  31. "--image_dir",
  32. type=str,
  33. default=None,
  34. help="Dir of image file, `image_file` has a higher priority.")
  35. parser.add_argument(
  36. "--batch_size", type=int, default=1, help="batch_size for inference.")
  37. parser.add_argument(
  38. "--video_file",
  39. type=str,
  40. default=None,
  41. help="Path of video file, `video_file` or `camera_id` has a highest priority."
  42. )
  43. parser.add_argument(
  44. "--camera_id",
  45. type=int,
  46. default=-1,
  47. help="device id of camera to predict.")
  48. parser.add_argument(
  49. "--threshold", type=float, default=0.5, help="Threshold of score.")
  50. parser.add_argument(
  51. "--output_dir",
  52. type=str,
  53. default="output",
  54. help="Directory of output visualization files.")
  55. parser.add_argument(
  56. "--run_mode",
  57. type=str,
  58. default='paddle',
  59. help="mode of running(paddle/trt_fp32/trt_fp16/trt_int8)")
  60. parser.add_argument(
  61. "--device",
  62. type=str,
  63. default='cpu',
  64. help="Choose the device you want to run, it can be: CPU/GPU/XPU, default is CPU."
  65. )
  66. parser.add_argument(
  67. "--use_gpu",
  68. type=ast.literal_eval,
  69. default=False,
  70. help="Deprecated, please use `--device`.")
  71. parser.add_argument(
  72. "--run_benchmark",
  73. type=ast.literal_eval,
  74. default=False,
  75. help="Whether to predict a image_file repeatedly for benchmark")
  76. parser.add_argument(
  77. "--enable_mkldnn",
  78. type=ast.literal_eval,
  79. default=False,
  80. help="Whether use mkldnn with CPU.")
  81. parser.add_argument(
  82. "--enable_mkldnn_bfloat16",
  83. type=ast.literal_eval,
  84. default=False,
  85. help="Whether use mkldnn bfloat16 inference with CPU.")
  86. parser.add_argument(
  87. "--cpu_threads", type=int, default=1, help="Num of threads with CPU.")
  88. parser.add_argument(
  89. "--trt_min_shape", type=int, default=1, help="min_shape for TensorRT.")
  90. parser.add_argument(
  91. "--trt_max_shape",
  92. type=int,
  93. default=1280,
  94. help="max_shape for TensorRT.")
  95. parser.add_argument(
  96. "--trt_opt_shape",
  97. type=int,
  98. default=640,
  99. help="opt_shape for TensorRT.")
  100. parser.add_argument(
  101. "--trt_calib_mode",
  102. type=bool,
  103. default=False,
  104. help="If the model is produced by TRT offline quantitative "
  105. "calibration, trt_calib_mode need to set True.")
  106. parser.add_argument(
  107. '--save_images',
  108. type=ast.literal_eval,
  109. default=True,
  110. help='Save visualization image results.')
  111. parser.add_argument(
  112. '--save_mot_txts',
  113. action='store_true',
  114. help='Save tracking results (txt).')
  115. parser.add_argument(
  116. '--save_mot_txt_per_img',
  117. action='store_true',
  118. help='Save tracking results (txt) for each image.')
  119. parser.add_argument(
  120. '--scaled',
  121. type=bool,
  122. default=False,
  123. help="Whether coords after detector outputs are scaled, False in JDE YOLOv3 "
  124. "True in general detector.")
  125. parser.add_argument(
  126. "--tracker_config", type=str, default=None, help=("tracker donfig"))
  127. parser.add_argument(
  128. "--reid_model_dir",
  129. type=str,
  130. default=None,
  131. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  132. "'infer_cfg.yml', created by tools/export_model.py."))
  133. parser.add_argument(
  134. "--reid_batch_size",
  135. type=int,
  136. default=50,
  137. help="max batch_size for reid model inference.")
  138. parser.add_argument(
  139. '--use_dark',
  140. type=ast.literal_eval,
  141. default=True,
  142. help='whether to use darkpose to get better keypoint position predict ')
  143. parser.add_argument(
  144. "--action_file",
  145. type=str,
  146. default=None,
  147. help="Path of input file for action recognition.")
  148. parser.add_argument(
  149. "--window_size",
  150. type=int,
  151. default=50,
  152. help="Temporal size of skeleton feature for action recognition.")
  153. parser.add_argument(
  154. "--random_pad",
  155. type=ast.literal_eval,
  156. default=False,
  157. help="Whether do random padding for action recognition.")
  158. parser.add_argument(
  159. "--save_results",
  160. action='store_true',
  161. default=False,
  162. help="Whether save detection result to file using coco format")
  163. parser.add_argument(
  164. '--use_coco_category',
  165. action='store_true',
  166. default=False,
  167. help='Whether to use the coco format dictionary `clsid2catid`')
  168. parser.add_argument(
  169. "--slice_infer",
  170. action='store_true',
  171. help="Whether to slice the image and merge the inference results for small object detection."
  172. )
  173. parser.add_argument(
  174. '--slice_size',
  175. nargs='+',
  176. type=int,
  177. default=[640, 640],
  178. help="Height of the sliced image.")
  179. parser.add_argument(
  180. "--overlap_ratio",
  181. nargs='+',
  182. type=float,
  183. default=[0.25, 0.25],
  184. help="Overlap height ratio of the sliced image.")
  185. parser.add_argument(
  186. "--combine_method",
  187. type=str,
  188. default='nms',
  189. help="Combine method of the sliced images' detection results, choose in ['nms', 'nmm', 'concat']."
  190. )
  191. parser.add_argument(
  192. "--match_threshold",
  193. type=float,
  194. default=0.6,
  195. help="Combine method matching threshold.")
  196. parser.add_argument(
  197. "--match_metric",
  198. type=str,
  199. default='ios',
  200. help="Combine method matching metric, choose in ['iou', 'ios'].")
  201. return parser
  202. class Times(object):
  203. def __init__(self):
  204. self.time = 0.
  205. # start time
  206. self.st = 0.
  207. # end time
  208. self.et = 0.
  209. def start(self):
  210. self.st = time.time()
  211. def end(self, repeats=1, accumulative=True):
  212. self.et = time.time()
  213. if accumulative:
  214. self.time += (self.et - self.st) / repeats
  215. else:
  216. self.time = (self.et - self.st) / repeats
  217. def reset(self):
  218. self.time = 0.
  219. self.st = 0.
  220. self.et = 0.
  221. def value(self):
  222. return round(self.time, 4)
  223. class Timer(Times):
  224. def __init__(self, with_tracker=False):
  225. super(Timer, self).__init__()
  226. self.with_tracker = with_tracker
  227. self.preprocess_time_s = Times()
  228. self.inference_time_s = Times()
  229. self.postprocess_time_s = Times()
  230. self.tracking_time_s = Times()
  231. self.img_num = 0
  232. def info(self, average=False):
  233. pre_time = self.preprocess_time_s.value()
  234. infer_time = self.inference_time_s.value()
  235. post_time = self.postprocess_time_s.value()
  236. track_time = self.tracking_time_s.value()
  237. total_time = pre_time + infer_time + post_time
  238. if self.with_tracker:
  239. total_time = total_time + track_time
  240. total_time = round(total_time, 4)
  241. print("------------------ Inference Time Info ----------------------")
  242. print("total_time(ms): {}, img_num: {}".format(total_time * 1000,
  243. self.img_num))
  244. preprocess_time = round(pre_time / max(1, self.img_num),
  245. 4) if average else pre_time
  246. postprocess_time = round(post_time / max(1, self.img_num),
  247. 4) if average else post_time
  248. inference_time = round(infer_time / max(1, self.img_num),
  249. 4) if average else infer_time
  250. tracking_time = round(track_time / max(1, self.img_num),
  251. 4) if average else track_time
  252. average_latency = total_time / max(1, self.img_num)
  253. qps = 0
  254. if total_time > 0:
  255. qps = 1 / average_latency
  256. print("average latency time(ms): {:.2f}, QPS: {:2f}".format(
  257. average_latency * 1000, qps))
  258. if self.with_tracker:
  259. print(
  260. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}, tracking_time(ms): {:.2f}".
  261. format(preprocess_time * 1000, inference_time * 1000,
  262. postprocess_time * 1000, tracking_time * 1000))
  263. else:
  264. print(
  265. "preprocess_time(ms): {:.2f}, inference_time(ms): {:.2f}, postprocess_time(ms): {:.2f}".
  266. format(preprocess_time * 1000, inference_time * 1000,
  267. postprocess_time * 1000))
  268. def report(self, average=False):
  269. dic = {}
  270. pre_time = self.preprocess_time_s.value()
  271. infer_time = self.inference_time_s.value()
  272. post_time = self.postprocess_time_s.value()
  273. track_time = self.tracking_time_s.value()
  274. dic['preprocess_time_s'] = round(pre_time / max(1, self.img_num),
  275. 4) if average else pre_time
  276. dic['inference_time_s'] = round(infer_time / max(1, self.img_num),
  277. 4) if average else infer_time
  278. dic['postprocess_time_s'] = round(post_time / max(1, self.img_num),
  279. 4) if average else post_time
  280. dic['img_num'] = self.img_num
  281. total_time = pre_time + infer_time + post_time
  282. if self.with_tracker:
  283. dic['tracking_time_s'] = round(track_time / max(1, self.img_num),
  284. 4) if average else track_time
  285. total_time = total_time + track_time
  286. dic['total_time_s'] = round(total_time, 4)
  287. return dic
  288. def get_current_memory_mb():
  289. """
  290. It is used to Obtain the memory usage of the CPU and GPU during the running of the program.
  291. And this function Current program is time-consuming.
  292. """
  293. import pynvml
  294. import psutil
  295. import GPUtil
  296. gpu_id = int(os.environ.get('CUDA_VISIBLE_DEVICES', 0))
  297. pid = os.getpid()
  298. p = psutil.Process(pid)
  299. info = p.memory_full_info()
  300. cpu_mem = info.uss / 1024. / 1024.
  301. gpu_mem = 0
  302. gpu_percent = 0
  303. gpus = GPUtil.getGPUs()
  304. if gpu_id is not None and len(gpus) > 0:
  305. gpu_percent = gpus[gpu_id].load
  306. pynvml.nvmlInit()
  307. handle = pynvml.nvmlDeviceGetHandleByIndex(0)
  308. meminfo = pynvml.nvmlDeviceGetMemoryInfo(handle)
  309. gpu_mem = meminfo.used / 1024. / 1024.
  310. return round(cpu_mem, 4), round(gpu_mem, 4), round(gpu_percent, 4)
  311. def multiclass_nms(bboxs, num_classes, match_threshold=0.6, match_metric='iou'):
  312. final_boxes = []
  313. for c in range(num_classes):
  314. idxs = bboxs[:, 0] == c
  315. if np.count_nonzero(idxs) == 0: continue
  316. r = nms(bboxs[idxs, 1:], match_threshold, match_metric)
  317. final_boxes.append(np.concatenate([np.full((r.shape[0], 1), c), r], 1))
  318. return final_boxes
  319. def nms(dets, match_threshold=0.6, match_metric='iou'):
  320. """ Apply NMS to avoid detecting too many overlapping bounding boxes.
  321. Args:
  322. dets: shape [N, 5], [score, x1, y1, x2, y2]
  323. match_metric: 'iou' or 'ios'
  324. match_threshold: overlap thresh for match metric.
  325. """
  326. if dets.shape[0] == 0:
  327. return dets[[], :]
  328. scores = dets[:, 0]
  329. x1 = dets[:, 1]
  330. y1 = dets[:, 2]
  331. x2 = dets[:, 3]
  332. y2 = dets[:, 4]
  333. areas = (x2 - x1 + 1) * (y2 - y1 + 1)
  334. order = scores.argsort()[::-1]
  335. ndets = dets.shape[0]
  336. suppressed = np.zeros((ndets), dtype=np.int32)
  337. for _i in range(ndets):
  338. i = order[_i]
  339. if suppressed[i] == 1:
  340. continue
  341. ix1 = x1[i]
  342. iy1 = y1[i]
  343. ix2 = x2[i]
  344. iy2 = y2[i]
  345. iarea = areas[i]
  346. for _j in range(_i + 1, ndets):
  347. j = order[_j]
  348. if suppressed[j] == 1:
  349. continue
  350. xx1 = max(ix1, x1[j])
  351. yy1 = max(iy1, y1[j])
  352. xx2 = min(ix2, x2[j])
  353. yy2 = min(iy2, y2[j])
  354. w = max(0.0, xx2 - xx1 + 1)
  355. h = max(0.0, yy2 - yy1 + 1)
  356. inter = w * h
  357. if match_metric == 'iou':
  358. union = iarea + areas[j] - inter
  359. match_value = inter / union
  360. elif match_metric == 'ios':
  361. smaller = min(iarea, areas[j])
  362. match_value = inter / smaller
  363. else:
  364. raise ValueError()
  365. if match_value >= match_threshold:
  366. suppressed[j] = 1
  367. keep = np.where(suppressed == 0)[0]
  368. dets = dets[keep, :]
  369. return dets
  370. coco_clsid2catid = {
  371. 0: 1,
  372. 1: 2,
  373. 2: 3,
  374. 3: 4,
  375. 4: 5,
  376. 5: 6,
  377. 6: 7,
  378. 7: 8,
  379. 8: 9,
  380. 9: 10,
  381. 10: 11,
  382. 11: 13,
  383. 12: 14,
  384. 13: 15,
  385. 14: 16,
  386. 15: 17,
  387. 16: 18,
  388. 17: 19,
  389. 18: 20,
  390. 19: 21,
  391. 20: 22,
  392. 21: 23,
  393. 22: 24,
  394. 23: 25,
  395. 24: 27,
  396. 25: 28,
  397. 26: 31,
  398. 27: 32,
  399. 28: 33,
  400. 29: 34,
  401. 30: 35,
  402. 31: 36,
  403. 32: 37,
  404. 33: 38,
  405. 34: 39,
  406. 35: 40,
  407. 36: 41,
  408. 37: 42,
  409. 38: 43,
  410. 39: 44,
  411. 40: 46,
  412. 41: 47,
  413. 42: 48,
  414. 43: 49,
  415. 44: 50,
  416. 45: 51,
  417. 46: 52,
  418. 47: 53,
  419. 48: 54,
  420. 49: 55,
  421. 50: 56,
  422. 51: 57,
  423. 52: 58,
  424. 53: 59,
  425. 54: 60,
  426. 55: 61,
  427. 56: 62,
  428. 57: 63,
  429. 58: 64,
  430. 59: 65,
  431. 60: 67,
  432. 61: 70,
  433. 62: 72,
  434. 63: 73,
  435. 64: 74,
  436. 65: 75,
  437. 66: 76,
  438. 67: 77,
  439. 68: 78,
  440. 69: 79,
  441. 70: 80,
  442. 71: 81,
  443. 72: 82,
  444. 73: 84,
  445. 74: 85,
  446. 75: 86,
  447. 76: 87,
  448. 77: 88,
  449. 78: 89,
  450. 79: 90
  451. }
  452. def gaussian_radius(bbox_size, min_overlap):
  453. height, width = bbox_size
  454. a1 = 1
  455. b1 = (height + width)
  456. c1 = width * height * (1 - min_overlap) / (1 + min_overlap)
  457. sq1 = np.sqrt(b1**2 - 4 * a1 * c1)
  458. radius1 = (b1 + sq1) / (2 * a1)
  459. a2 = 4
  460. b2 = 2 * (height + width)
  461. c2 = (1 - min_overlap) * width * height
  462. sq2 = np.sqrt(b2**2 - 4 * a2 * c2)
  463. radius2 = (b2 + sq2) / 2
  464. a3 = 4 * min_overlap
  465. b3 = -2 * min_overlap * (height + width)
  466. c3 = (min_overlap - 1) * width * height
  467. sq3 = np.sqrt(b3**2 - 4 * a3 * c3)
  468. radius3 = (b3 + sq3) / 2
  469. return min(radius1, radius2, radius3)
  470. def gaussian2D(shape, sigma_x=1, sigma_y=1):
  471. m, n = [(ss - 1.) / 2. for ss in shape]
  472. y, x = np.ogrid[-m:m + 1, -n:n + 1]
  473. h = np.exp(-(x * x / (2 * sigma_x * sigma_x) + y * y / (2 * sigma_y *
  474. sigma_y)))
  475. h[h < np.finfo(h.dtype).eps * h.max()] = 0
  476. return h
  477. def draw_umich_gaussian(heatmap, center, radius, k=1):
  478. """
  479. draw_umich_gaussian, refer to https://github.com/xingyizhou/CenterNet/blob/master/src/lib/utils/image.py#L126
  480. """
  481. diameter = 2 * radius + 1
  482. gaussian = gaussian2D(
  483. (diameter, diameter), sigma_x=diameter / 6, sigma_y=diameter / 6)
  484. x, y = int(center[0]), int(center[1])
  485. height, width = heatmap.shape[0:2]
  486. left, right = min(x, radius), min(width - x, radius + 1)
  487. top, bottom = min(y, radius), min(height - y, radius + 1)
  488. masked_heatmap = heatmap[y - top:y + bottom, x - left:x + right]
  489. masked_gaussian = gaussian[radius - top:radius + bottom, radius - left:
  490. radius + right]
  491. if min(masked_gaussian.shape) > 0 and min(masked_heatmap.shape) > 0:
  492. np.maximum(masked_heatmap, masked_gaussian * k, out=masked_heatmap)
  493. return heatmap