export_utils.py 4.5 KB

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