| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from __future__ import absolute_import
- from __future__ import division
- from __future__ import print_function
- import os
- import yaml
- from collections import OrderedDict
- from paddlex.ppdet.data.source.category import get_categories
- from paddlex.ppdet.utils.logger import setup_logger
- logger = setup_logger('ppdet.engine')
- # Global dictionary
- TRT_MIN_SUBGRAPH = {
- 'YOLO': 3,
- 'SSD': 60,
- 'RCNN': 40,
- 'RetinaNet': 40,
- 'S2ANet': 80,
- 'EfficientDet': 40,
- 'Face': 3,
- 'TTFNet': 60,
- 'FCOS': 16,
- 'SOLOv2': 60,
- 'HigherHRNet': 3,
- 'HRNet': 3,
- 'DeepSORT': 3,
- 'JDE': 10,
- 'FairMOT': 5,
- 'GFL': 16,
- 'PicoDet': 3,
- }
- KEYPOINT_ARCH = ['HigherHRNet', 'TopDownHRNet']
- MOT_ARCH = ['DeepSORT', 'JDE', 'FairMOT']
- def _parse_reader(reader_cfg, dataset_cfg, metric, arch, image_shape):
- preprocess_list = []
- anno_file = dataset_cfg.get_anno()
- clsid2catid, catid2name = get_categories(metric, anno_file, arch)
- label_list = [str(cat) for cat in catid2name.values()]
- fuse_normalize = reader_cfg.get('fuse_normalize', False)
- sample_transforms = reader_cfg['sample_transforms']
- for st in sample_transforms[1:]:
- for key, value in st.items():
- p = {'type': key}
- if key == 'Resize':
- if int(image_shape[1]) != -1:
- value['target_size'] = image_shape[1:]
- if fuse_normalize and key == 'NormalizeImage':
- continue
- p.update(value)
- preprocess_list.append(p)
- batch_transforms = reader_cfg.get('batch_transforms', None)
- if batch_transforms:
- for bt in batch_transforms:
- for key, value in bt.items():
- # for deploy/infer, use PadStride(stride) instead PadBatch(pad_to_stride)
- if key == 'PadBatch':
- preprocess_list.append({
- 'type': 'PadStride',
- 'stride': value['pad_to_stride']
- })
- break
- return preprocess_list, label_list
- def _parse_tracker(tracker_cfg):
- tracker_params = {}
- for k, v in tracker_cfg.items():
- tracker_params.update({k: v})
- return tracker_params
- def _dump_infer_config(config, path, image_shape, model):
- arch_state = False
- from paddlex.ppdet.core.config.yaml_helpers import setup_orderdict
- setup_orderdict()
- use_dynamic_shape = True if image_shape[1] == -1 else False
- infer_cfg = OrderedDict({
- 'mode': 'fluid',
- 'draw_threshold': 0.5,
- 'metric': config['metric'],
- 'use_dynamic_shape': use_dynamic_shape
- })
- infer_arch = config['architecture']
- if infer_arch in MOT_ARCH:
- if infer_arch == 'DeepSORT':
- tracker_cfg = config['DeepSORTTracker']
- else:
- tracker_cfg = config['JDETracker']
- infer_cfg['tracker'] = _parse_tracker(tracker_cfg)
- for arch, min_subgraph_size in TRT_MIN_SUBGRAPH.items():
- if arch in infer_arch:
- infer_cfg['arch'] = arch
- infer_cfg['min_subgraph_size'] = min_subgraph_size
- arch_state = True
- break
- if not arch_state:
- logger.error(
- 'Architecture: {} is not supported for exporting model now.\n'.
- format(infer_arch) +
- 'Please set TRT_MIN_SUBGRAPH in ppdet/engine/export_utils.py')
- os._exit(0)
- if 'mask_head' in config[config['architecture']] and config[config[
- 'architecture']]['mask_head']:
- infer_cfg['mask'] = True
- label_arch = 'detection_arch'
- if infer_arch in KEYPOINT_ARCH:
- label_arch = 'keypoint_arch'
- if infer_arch in MOT_ARCH:
- label_arch = 'mot_arch'
- reader_cfg = config['TestMOTReader']
- dataset_cfg = config['TestMOTDataset']
- else:
- reader_cfg = config['TestReader']
- dataset_cfg = config['TestDataset']
- infer_cfg['Preprocess'], infer_cfg['label_list'] = _parse_reader(
- reader_cfg, dataset_cfg, config['metric'], label_arch, image_shape)
- yaml.dump(infer_cfg, open(path, 'w'))
- logger.info("Export inference config file to {}".format(
- os.path.join(path)))
|