export_utils.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. from paddlex.ppdet.data.source.category import get_categories
  21. from paddlex.ppdet.utils.logger import setup_logger
  22. logger = setup_logger('ppdet.engine')
  23. # Global dictionary
  24. TRT_MIN_SUBGRAPH = {
  25. 'YOLO': 3,
  26. 'SSD': 60,
  27. 'RCNN': 40,
  28. 'RetinaNet': 40,
  29. 'S2ANet': 80,
  30. 'EfficientDet': 40,
  31. 'Face': 3,
  32. 'TTFNet': 60,
  33. 'FCOS': 16,
  34. 'SOLOv2': 60,
  35. 'HigherHRNet': 3,
  36. 'HRNet': 3,
  37. 'DeepSORT': 3,
  38. 'JDE': 10,
  39. 'FairMOT': 5,
  40. 'GFL': 16,
  41. 'PicoDet': 3,
  42. }
  43. KEYPOINT_ARCH = ['HigherHRNet', 'TopDownHRNet']
  44. MOT_ARCH = ['DeepSORT', 'JDE', 'FairMOT']
  45. def _parse_reader(reader_cfg, dataset_cfg, metric, arch, image_shape):
  46. preprocess_list = []
  47. anno_file = dataset_cfg.get_anno()
  48. clsid2catid, catid2name = get_categories(metric, anno_file, arch)
  49. label_list = [str(cat) for cat in catid2name.values()]
  50. fuse_normalize = reader_cfg.get('fuse_normalize', False)
  51. sample_transforms = reader_cfg['sample_transforms']
  52. for st in sample_transforms[1:]:
  53. for key, value in st.items():
  54. p = {'type': key}
  55. if key == 'Resize':
  56. if int(image_shape[1]) != -1:
  57. value['target_size'] = image_shape[1:]
  58. if fuse_normalize and key == 'NormalizeImage':
  59. continue
  60. p.update(value)
  61. preprocess_list.append(p)
  62. batch_transforms = reader_cfg.get('batch_transforms', None)
  63. if batch_transforms:
  64. for bt in batch_transforms:
  65. for key, value in bt.items():
  66. # for deploy/infer, use PadStride(stride) instead PadBatch(pad_to_stride)
  67. if key == 'PadBatch':
  68. preprocess_list.append({
  69. 'type': 'PadStride',
  70. 'stride': value['pad_to_stride']
  71. })
  72. break
  73. return preprocess_list, label_list
  74. def _parse_tracker(tracker_cfg):
  75. tracker_params = {}
  76. for k, v in tracker_cfg.items():
  77. tracker_params.update({k: v})
  78. return tracker_params
  79. def _dump_infer_config(config, path, image_shape, model):
  80. arch_state = False
  81. from paddlex.ppdet.core.config.yaml_helpers import setup_orderdict
  82. setup_orderdict()
  83. use_dynamic_shape = True if image_shape[1] == -1 else False
  84. infer_cfg = OrderedDict({
  85. 'mode': 'fluid',
  86. 'draw_threshold': 0.5,
  87. 'metric': config['metric'],
  88. 'use_dynamic_shape': use_dynamic_shape
  89. })
  90. infer_arch = config['architecture']
  91. if infer_arch in MOT_ARCH:
  92. if infer_arch == 'DeepSORT':
  93. tracker_cfg = config['DeepSORTTracker']
  94. else:
  95. tracker_cfg = config['JDETracker']
  96. infer_cfg['tracker'] = _parse_tracker(tracker_cfg)
  97. for arch, min_subgraph_size in TRT_MIN_SUBGRAPH.items():
  98. if arch in infer_arch:
  99. infer_cfg['arch'] = arch
  100. infer_cfg['min_subgraph_size'] = min_subgraph_size
  101. arch_state = True
  102. break
  103. if not arch_state:
  104. logger.error(
  105. 'Architecture: {} is not supported for exporting model now.\n'.
  106. format(infer_arch) +
  107. 'Please set TRT_MIN_SUBGRAPH in ppdet/engine/export_utils.py')
  108. os._exit(0)
  109. if 'mask_head' in config[config['architecture']] and config[config[
  110. 'architecture']]['mask_head']:
  111. infer_cfg['mask'] = True
  112. label_arch = 'detection_arch'
  113. if infer_arch in KEYPOINT_ARCH:
  114. label_arch = 'keypoint_arch'
  115. if infer_arch in MOT_ARCH:
  116. label_arch = 'mot_arch'
  117. reader_cfg = config['TestMOTReader']
  118. dataset_cfg = config['TestMOTDataset']
  119. else:
  120. reader_cfg = config['TestReader']
  121. dataset_cfg = config['TestDataset']
  122. infer_cfg['Preprocess'], infer_cfg['label_list'] = _parse_reader(
  123. reader_cfg, dataset_cfg, config['metric'], label_arch, image_shape)
  124. yaml.dump(infer_cfg, open(path, 'w'))
  125. logger.info("Export inference config file to {}".format(
  126. os.path.join(path)))