export_utils.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (c) 2020 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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import yaml
  19. from collections import OrderedDict
  20. import paddle
  21. from ppdet.data.source.category import get_categories
  22. from ppdet.utils.logger import setup_logger
  23. logger = setup_logger('ppdet.engine')
  24. # Global dictionary
  25. TRT_MIN_SUBGRAPH = {
  26. 'YOLO': 3,
  27. 'PPYOLOE': 3,
  28. 'SSD': 60,
  29. 'RCNN': 40,
  30. 'RetinaNet': 40,
  31. 'S2ANet': 80,
  32. 'EfficientDet': 40,
  33. 'Face': 3,
  34. 'TTFNet': 60,
  35. 'FCOS': 16,
  36. 'SOLOv2': 60,
  37. 'HigherHRNet': 3,
  38. 'HRNet': 3,
  39. 'DeepSORT': 3,
  40. 'ByteTrack': 10,
  41. 'CenterTrack': 5,
  42. 'JDE': 10,
  43. 'FairMOT': 5,
  44. 'GFL': 16,
  45. 'PicoDet': 3,
  46. 'CenterNet': 5,
  47. 'TOOD': 5,
  48. 'YOLOX': 8,
  49. 'YOLOF': 40,
  50. 'METRO_Body': 3,
  51. 'DETR': 3,
  52. }
  53. KEYPOINT_ARCH = ['HigherHRNet', 'TopDownHRNet']
  54. MOT_ARCH = ['JDE', 'FairMOT', 'DeepSORT', 'ByteTrack', 'CenterTrack']
  55. TO_STATIC_SPEC = {
  56. 'yolov3_darknet53_270e_coco': [{
  57. 'im_id': paddle.static.InputSpec(
  58. name='im_id', shape=[-1, 1], dtype='float32'),
  59. 'is_crowd': paddle.static.InputSpec(
  60. name='is_crowd', shape=[-1, 50], dtype='float32'),
  61. 'gt_bbox': paddle.static.InputSpec(
  62. name='gt_bbox', shape=[-1, 50, 4], dtype='float32'),
  63. 'curr_iter': paddle.static.InputSpec(
  64. name='curr_iter', shape=[-1], dtype='float32'),
  65. 'image': paddle.static.InputSpec(
  66. name='image', shape=[-1, 3, -1, -1], dtype='float32'),
  67. 'im_shape': paddle.static.InputSpec(
  68. name='im_shape', shape=[-1, 2], dtype='float32'),
  69. 'scale_factor': paddle.static.InputSpec(
  70. name='scale_factor', shape=[-1, 2], dtype='float32'),
  71. 'target0': paddle.static.InputSpec(
  72. name='target0', shape=[-1, 3, 86, -1, -1], dtype='float32'),
  73. 'target1': paddle.static.InputSpec(
  74. name='target1', shape=[-1, 3, 86, -1, -1], dtype='float32'),
  75. 'target2': paddle.static.InputSpec(
  76. name='target2', shape=[-1, 3, 86, -1, -1], dtype='float32'),
  77. }],
  78. }
  79. def apply_to_static(config, model):
  80. filename = config.get('filename', None)
  81. spec = TO_STATIC_SPEC.get(filename, None)
  82. model = paddle.jit.to_static(model, input_spec=spec)
  83. logger.info("Successfully to apply @to_static with specs: {}".format(spec))
  84. return model
  85. def _prune_input_spec(input_spec, program, targets):
  86. # try to prune static program to figure out pruned input spec
  87. # so we perform following operations in static mode
  88. device = paddle.get_device()
  89. paddle.enable_static()
  90. paddle.set_device(device)
  91. pruned_input_spec = [{}]
  92. program = program.clone()
  93. program = program._prune(targets=targets)
  94. global_block = program.global_block()
  95. for name, spec in input_spec[0].items():
  96. try:
  97. v = global_block.var(name)
  98. pruned_input_spec[0][name] = spec
  99. except Exception:
  100. pass
  101. paddle.disable_static(place=device)
  102. return pruned_input_spec
  103. def _parse_reader(reader_cfg, dataset_cfg, metric, arch, image_shape):
  104. preprocess_list = []
  105. anno_file = dataset_cfg.get_anno()
  106. clsid2catid, catid2name = get_categories(metric, anno_file, arch)
  107. label_list = [str(cat) for cat in catid2name.values()]
  108. fuse_normalize = reader_cfg.get('fuse_normalize', False)
  109. sample_transforms = reader_cfg['sample_transforms']
  110. for st in sample_transforms[1:]:
  111. for key, value in st.items():
  112. p = {'type': key}
  113. if key == 'Resize':
  114. if int(image_shape[1]) != -1:
  115. value['target_size'] = image_shape[1:]
  116. value['interp'] = value.get('interp', 1) # cv2.INTER_LINEAR
  117. if fuse_normalize and key == 'NormalizeImage':
  118. continue
  119. p.update(value)
  120. preprocess_list.append(p)
  121. batch_transforms = reader_cfg.get('batch_transforms', None)
  122. if batch_transforms:
  123. for bt in batch_transforms:
  124. for key, value in bt.items():
  125. # for deploy/infer, use PadStride(stride) instead PadBatch(pad_to_stride)
  126. if key == 'PadBatch':
  127. preprocess_list.append({
  128. 'type': 'PadStride',
  129. 'stride': value['pad_to_stride']
  130. })
  131. break
  132. return preprocess_list, label_list
  133. def _parse_tracker(tracker_cfg):
  134. tracker_params = {}
  135. for k, v in tracker_cfg.items():
  136. tracker_params.update({k: v})
  137. return tracker_params
  138. def _dump_infer_config(config, path, image_shape, model):
  139. arch_state = False
  140. from ppdet.core.config.yaml_helpers import setup_orderdict
  141. setup_orderdict()
  142. use_dynamic_shape = True if image_shape[2] == -1 else False
  143. infer_cfg = OrderedDict({
  144. 'mode': 'paddle',
  145. 'draw_threshold': 0.5,
  146. 'metric': config['metric'],
  147. 'use_dynamic_shape': use_dynamic_shape
  148. })
  149. export_onnx = config.get('export_onnx', False)
  150. export_eb = config.get('export_eb', False)
  151. infer_arch = config['architecture']
  152. if 'RCNN' in infer_arch and export_onnx:
  153. logger.warning(
  154. "Exporting RCNN model to ONNX only support batch_size = 1")
  155. infer_cfg['export_onnx'] = True
  156. infer_cfg['export_eb'] = export_eb
  157. if infer_arch in MOT_ARCH:
  158. if infer_arch == 'DeepSORT':
  159. tracker_cfg = config['DeepSORTTracker']
  160. elif infer_arch == 'CenterTrack':
  161. tracker_cfg = config['CenterTracker']
  162. else:
  163. tracker_cfg = config['JDETracker']
  164. infer_cfg['tracker'] = _parse_tracker(tracker_cfg)
  165. for arch, min_subgraph_size in TRT_MIN_SUBGRAPH.items():
  166. if arch in infer_arch:
  167. infer_cfg['arch'] = arch
  168. infer_cfg['min_subgraph_size'] = min_subgraph_size
  169. arch_state = True
  170. break
  171. if infer_arch == 'PPYOLOEWithAuxHead':
  172. infer_arch = 'PPYOLOE'
  173. if infer_arch in ['PPYOLOE', 'YOLOX', 'YOLOF']:
  174. infer_cfg['arch'] = infer_arch
  175. infer_cfg['min_subgraph_size'] = TRT_MIN_SUBGRAPH[infer_arch]
  176. arch_state = True
  177. if not arch_state:
  178. logger.error(
  179. 'Architecture: {} is not supported for exporting model now.\n'.
  180. format(infer_arch) +
  181. 'Please set TRT_MIN_SUBGRAPH in ppdet/engine/export_utils.py')
  182. os._exit(0)
  183. if 'mask_head' in config[config['architecture']] and config[config[
  184. 'architecture']]['mask_head']:
  185. infer_cfg['mask'] = True
  186. label_arch = 'detection_arch'
  187. if infer_arch in KEYPOINT_ARCH:
  188. label_arch = 'keypoint_arch'
  189. if infer_arch in MOT_ARCH:
  190. if config['metric'] in ['COCO', 'VOC']:
  191. # MOT model run as Detector
  192. reader_cfg = config['TestReader']
  193. dataset_cfg = config['TestDataset']
  194. else:
  195. # 'metric' in ['MOT', 'MCMOT', 'KITTI']
  196. label_arch = 'mot_arch'
  197. reader_cfg = config['TestMOTReader']
  198. dataset_cfg = config['TestMOTDataset']
  199. else:
  200. reader_cfg = config['TestReader']
  201. dataset_cfg = config['TestDataset']
  202. infer_cfg['Preprocess'], infer_cfg['label_list'] = _parse_reader(
  203. reader_cfg, dataset_cfg, config['metric'], label_arch, image_shape[1:])
  204. if infer_arch == 'PicoDet':
  205. if hasattr(config, 'export') and config['export'].get(
  206. 'post_process',
  207. False) and not config['export'].get('benchmark', False):
  208. infer_cfg['arch'] = 'GFL'
  209. head_name = 'PicoHeadV2' if config['PicoHeadV2'] else 'PicoHead'
  210. infer_cfg['NMS'] = config[head_name]['nms']
  211. # In order to speed up the prediction, the threshold of nms
  212. # is adjusted here, which can be changed in infer_cfg.yml
  213. config[head_name]['nms']["score_threshold"] = 0.3
  214. config[head_name]['nms']["nms_threshold"] = 0.5
  215. infer_cfg['fpn_stride'] = config[head_name]['fpn_stride']
  216. yaml.dump(infer_cfg, open(path, 'w'))
  217. logger.info("Export inference config file to {}".format(os.path.join(path)))