export_utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  38. KEYPOINT_ARCH = ['HigherHRNet', 'TopDownHRNet']
  39. def _parse_reader(reader_cfg, dataset_cfg, metric, arch, image_shape):
  40. preprocess_list = []
  41. anno_file = dataset_cfg.get_anno()
  42. clsid2catid, catid2name = get_categories(metric, anno_file, arch)
  43. label_list = [str(cat) for cat in catid2name.values()]
  44. sample_transforms = reader_cfg['sample_transforms']
  45. for st in sample_transforms[1:]:
  46. for key, value in st.items():
  47. p = {'type': key}
  48. if key == 'Resize':
  49. if int(image_shape[1]) != -1:
  50. value['target_size'] = image_shape[1:]
  51. p.update(value)
  52. preprocess_list.append(p)
  53. batch_transforms = reader_cfg.get('batch_transforms', None)
  54. if batch_transforms:
  55. for bt in batch_transforms:
  56. for key, value in bt.items():
  57. # for deploy/infer, use PadStride(stride) instead PadBatch(pad_to_stride)
  58. if key == 'PadBatch':
  59. preprocess_list.append({
  60. 'type': 'PadStride',
  61. 'stride': value['pad_to_stride']
  62. })
  63. break
  64. return preprocess_list, label_list
  65. def _dump_infer_config(config, path, image_shape, model):
  66. arch_state = False
  67. from paddlex.ppdet.core.config.yaml_helpers import setup_orderdict
  68. setup_orderdict()
  69. infer_cfg = OrderedDict({
  70. 'mode': 'fluid',
  71. 'draw_threshold': 0.5,
  72. 'metric': config['metric'],
  73. })
  74. infer_arch = config['architecture']
  75. for arch, min_subgraph_size in TRT_MIN_SUBGRAPH.items():
  76. if arch in infer_arch:
  77. infer_cfg['arch'] = arch
  78. infer_cfg['min_subgraph_size'] = min_subgraph_size
  79. arch_state = True
  80. break
  81. if not arch_state:
  82. logger.error(
  83. 'Architecture: {} is not supported for exporting model now'.format(
  84. infer_arch))
  85. os._exit(0)
  86. if 'Mask' in infer_arch:
  87. infer_cfg['mask'] = True
  88. label_arch = 'detection_arch'
  89. if infer_arch in KEYPOINT_ARCH:
  90. label_arch = 'keypoint_arch'
  91. infer_cfg['Preprocess'], infer_cfg['label_list'] = _parse_reader(
  92. config['TestReader'], config['TestDataset'], config['metric'],
  93. label_arch, image_shape)
  94. if infer_arch == 'S2ANet':
  95. # TODO: move background to num_classes
  96. if infer_cfg['label_list'][0] != 'background':
  97. infer_cfg['label_list'].insert(0, 'background')
  98. yaml.dump(infer_cfg, open(path, 'w'))
  99. logger.info("Export inference config file to {}".format(
  100. os.path.join(path)))