export_utils.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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('paddlex.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. sample_transforms = reader_cfg['sample_transforms']
  51. for st in sample_transforms[1:]:
  52. for key, value in st.items():
  53. p = {'type': key}
  54. if key == 'Resize':
  55. if int(image_shape[1]) != -1:
  56. value['target_size'] = image_shape[1:]
  57. p.update(value)
  58. preprocess_list.append(p)
  59. batch_transforms = reader_cfg.get('batch_transforms', None)
  60. if batch_transforms:
  61. for bt in batch_transforms:
  62. for key, value in bt.items():
  63. # for deploy/infer, use PadStride(stride) instead PadBatch(pad_to_stride)
  64. if key == 'PadBatch':
  65. preprocess_list.append({
  66. 'type': 'PadStride',
  67. 'stride': value['pad_to_stride']
  68. })
  69. break
  70. return preprocess_list, label_list
  71. def _parse_tracker(tracker_cfg):
  72. tracker_params = {}
  73. for k, v in tracker_cfg.items():
  74. tracker_params.update({k: v})
  75. return tracker_params
  76. def _dump_infer_config(config, path, image_shape, model):
  77. arch_state = False
  78. from paddlex.ppdet.core.config.yaml_helpers import setup_orderdict
  79. setup_orderdict()
  80. use_dynamic_shape = True if image_shape[1] == -1 else False
  81. infer_cfg = OrderedDict({
  82. 'mode': 'fluid',
  83. 'draw_threshold': 0.5,
  84. 'metric': config['metric'],
  85. 'use_dynamic_shape': use_dynamic_shape
  86. })
  87. infer_arch = config['architecture']
  88. if infer_arch in MOT_ARCH:
  89. if infer_arch == 'DeepSORT':
  90. tracker_cfg = config['DeepSORTTracker']
  91. else:
  92. tracker_cfg = config['JDETracker']
  93. infer_cfg['tracker'] = _parse_tracker(tracker_cfg)
  94. for arch, min_subgraph_size in TRT_MIN_SUBGRAPH.items():
  95. if arch in infer_arch:
  96. infer_cfg['arch'] = arch
  97. infer_cfg['min_subgraph_size'] = min_subgraph_size
  98. arch_state = True
  99. break
  100. if not arch_state:
  101. logger.error(
  102. 'Architecture: {} is not supported for exporting model now.\n'.
  103. format(infer_arch) +
  104. 'Please set TRT_MIN_SUBGRAPH in ppdet/engine/export_utils.py')
  105. os._exit(0)
  106. if 'Mask' in infer_arch:
  107. infer_cfg['mask'] = True
  108. label_arch = 'detection_arch'
  109. if infer_arch in KEYPOINT_ARCH:
  110. label_arch = 'keypoint_arch'
  111. if infer_arch in MOT_ARCH:
  112. label_arch = 'mot_arch'
  113. reader_cfg = config['TestMOTReader']
  114. dataset_cfg = config['TestMOTDataset']
  115. else:
  116. reader_cfg = config['TestReader']
  117. dataset_cfg = config['TestDataset']
  118. infer_cfg['Preprocess'], infer_cfg['label_list'] = _parse_reader(
  119. reader_cfg, dataset_cfg, config['metric'], label_arch, image_shape)
  120. yaml.dump(infer_cfg, open(path, 'w'))
  121. logger.info("Export inference config file to {}".format(
  122. os.path.join(path)))