web_service.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # Copyright (c) 2022 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. import copy
  15. from paddle_serving_server.web_service import WebService, Op
  16. from paddle_serving_server.proto import general_model_config_pb2 as m_config
  17. import google.protobuf.text_format
  18. import os
  19. import numpy as np
  20. import base64
  21. from PIL import Image
  22. import io
  23. from preprocess_ops import Compose
  24. from postprocess_ops import HRNetPostProcess
  25. from argparse import ArgumentParser, RawDescriptionHelpFormatter
  26. import yaml
  27. # Global dictionary
  28. SUPPORT_MODELS = {
  29. 'YOLO', 'PPYOLOE', 'RCNN', 'SSD', 'Face', 'FCOS', 'SOLOv2', 'TTFNet',
  30. 'S2ANet', 'JDE', 'FairMOT', 'DeepSORT', 'GFL', 'PicoDet', 'CenterNet',
  31. 'TOOD', 'RetinaNet', 'StrongBaseline', 'STGCN', 'YOLOX', 'HRNet'
  32. }
  33. GLOBAL_VAR = {}
  34. class ArgsParser(ArgumentParser):
  35. def __init__(self):
  36. super(ArgsParser, self).__init__(
  37. formatter_class=RawDescriptionHelpFormatter)
  38. self.add_argument(
  39. "-c",
  40. "--config",
  41. default="deploy/serving/python/config.yml",
  42. help="configuration file to use")
  43. self.add_argument(
  44. "--model_dir",
  45. type=str,
  46. default=None,
  47. help=("Directory include:'model.pdiparams', 'model.pdmodel', "
  48. "'infer_cfg.yml', created by tools/export_model.py."),
  49. required=True)
  50. self.add_argument(
  51. "-o", "--opt", nargs='+', help="set configuration options")
  52. def parse_args(self, argv=None):
  53. args = super(ArgsParser, self).parse_args(argv)
  54. assert args.config is not None, \
  55. "Please specify --config=configure_file_path."
  56. args.service_config = self._parse_opt(args.opt, args.config)
  57. print("args config:", args.service_config)
  58. args.model_config = PredictConfig(args.model_dir)
  59. return args
  60. def _parse_helper(self, v):
  61. if v.isnumeric():
  62. if "." in v:
  63. v = float(v)
  64. else:
  65. v = int(v)
  66. elif v == "True" or v == "False":
  67. v = (v == "True")
  68. return v
  69. def _parse_opt(self, opts, conf_path):
  70. f = open(conf_path)
  71. config = yaml.load(f, Loader=yaml.Loader)
  72. if not opts:
  73. return config
  74. for s in opts:
  75. s = s.strip()
  76. k, v = s.split('=')
  77. v = self._parse_helper(v)
  78. if "devices" in k:
  79. v = str(v)
  80. print(k, v, type(v))
  81. cur = config
  82. parent = cur
  83. for kk in k.split("."):
  84. if kk not in cur:
  85. cur[kk] = {}
  86. parent = cur
  87. cur = cur[kk]
  88. else:
  89. parent = cur
  90. cur = cur[kk]
  91. parent[k.split(".")[-1]] = v
  92. return config
  93. class PredictConfig(object):
  94. """set config of preprocess, postprocess and visualize
  95. Args:
  96. model_dir (str): root path of infer_cfg.yml
  97. """
  98. def __init__(self, model_dir):
  99. # parsing Yaml config for Preprocess
  100. deploy_file = os.path.join(model_dir, 'infer_cfg.yml')
  101. with open(deploy_file) as f:
  102. yml_conf = yaml.safe_load(f)
  103. self.check_model(yml_conf)
  104. self.arch = yml_conf['arch']
  105. self.preprocess_infos = yml_conf['Preprocess']
  106. self.min_subgraph_size = yml_conf['min_subgraph_size']
  107. self.label_list = yml_conf['label_list']
  108. self.use_dynamic_shape = yml_conf['use_dynamic_shape']
  109. self.draw_threshold = yml_conf.get("draw_threshold", 0.5)
  110. self.mask = yml_conf.get("mask", False)
  111. self.tracker = yml_conf.get("tracker", None)
  112. self.nms = yml_conf.get("NMS", None)
  113. self.fpn_stride = yml_conf.get("fpn_stride", None)
  114. if self.arch == 'RCNN' and yml_conf.get('export_onnx', False):
  115. print(
  116. 'The RCNN export model is used for ONNX and it only supports batch_size = 1'
  117. )
  118. self.print_config()
  119. def check_model(self, yml_conf):
  120. """
  121. Raises:
  122. ValueError: loaded model not in supported model type
  123. """
  124. for support_model in SUPPORT_MODELS:
  125. if support_model in yml_conf['arch']:
  126. return True
  127. raise ValueError("Unsupported arch: {}, expect {}".format(yml_conf[
  128. 'arch'], SUPPORT_MODELS))
  129. def print_config(self):
  130. print('----------- Model Configuration -----------')
  131. print('%s: %s' % ('Model Arch', self.arch))
  132. print('%s: ' % ('Transform Order'))
  133. for op_info in self.preprocess_infos:
  134. print('--%s: %s' % ('transform op', op_info['type']))
  135. print('--------------------------------------------')
  136. class DetectorOp(Op):
  137. def init_op(self):
  138. self.preprocess_pipeline = Compose(GLOBAL_VAR['preprocess_ops'])
  139. def preprocess(self, input_dicts, data_id, log_id):
  140. (_, input_dict), = input_dicts.items()
  141. inputs = []
  142. for key, data in input_dict.items():
  143. data = base64.b64decode(data.encode('utf8'))
  144. byte_stream = io.BytesIO(data)
  145. img = Image.open(byte_stream).convert("RGB")
  146. inputs.append(self.preprocess_pipeline(img))
  147. inputs = self.collate_inputs(inputs)
  148. return inputs, False, None, ""
  149. def postprocess(self, input_dicts, fetch_dict, data_id, log_id):
  150. (_, input_dict), = input_dicts.items()
  151. if GLOBAL_VAR['model_config'].arch in ["HRNet"]:
  152. result = self.parse_keypoint_result(input_dict, fetch_dict)
  153. else:
  154. result = self.parse_detection_result(input_dict, fetch_dict)
  155. return result, None, ""
  156. def collate_inputs(self, inputs):
  157. collate_inputs = {k: [] for k in inputs[0].keys()}
  158. for info in inputs:
  159. for k in collate_inputs.keys():
  160. collate_inputs[k].append(info[k])
  161. return {
  162. k: np.stack(v)
  163. for k, v in collate_inputs.items() if k in GLOBAL_VAR['feed_vars']
  164. }
  165. def parse_detection_result(self, input_dict, fetch_dict):
  166. bboxes = fetch_dict[GLOBAL_VAR['fetch_vars'][0]]
  167. bboxes_num = fetch_dict[GLOBAL_VAR['fetch_vars'][1]]
  168. if GLOBAL_VAR['model_config'].mask:
  169. masks = fetch_dict[GLOBAL_VAR['fetch_vars'][2]]
  170. idx = 0
  171. results = {}
  172. for img_name, num in zip(input_dict.keys(), bboxes_num):
  173. if num == 0:
  174. results[img_name] = 'No object detected!'
  175. else:
  176. result = []
  177. bbox = bboxes[idx:idx + num]
  178. for line in bbox:
  179. if line[0] > -1 and line[1] > GLOBAL_VAR[
  180. 'model_config'].draw_threshold:
  181. result.append(
  182. f"{int(line[0])} {line[1]} "
  183. f"{line[2]} {line[3]} {line[4]} {line[5]}")
  184. if len(result) == 0:
  185. result = 'No object detected!'
  186. results[img_name] = result
  187. idx += num
  188. return results
  189. def parse_keypoint_result(self, input_dict, fetch_dict):
  190. heatmap = fetch_dict["conv2d_441.tmp_1"]
  191. keypoint_postprocess = HRNetPostProcess()
  192. im_shape = []
  193. for key, data in input_dict.items():
  194. data = base64.b64decode(data.encode('utf8'))
  195. byte_stream = io.BytesIO(data)
  196. img = Image.open(byte_stream).convert("RGB")
  197. im_shape.append([img.width, img.height])
  198. im_shape = np.array(im_shape)
  199. center = np.round(im_shape / 2.)
  200. scale = im_shape / 200.
  201. kpts, scores = keypoint_postprocess(heatmap, center, scale)
  202. results = {"keypoint": kpts, "scores": scores}
  203. return results
  204. class DetectorService(WebService):
  205. def get_pipeline_response(self, read_op):
  206. return DetectorOp(name="ppdet", input_ops=[read_op])
  207. def get_model_vars(model_dir, service_config):
  208. serving_server_dir = os.path.join(model_dir, "serving_server")
  209. # rewrite model_config
  210. service_config['op']['ppdet']['local_service_conf'][
  211. 'model_config'] = serving_server_dir
  212. serving_server_conf = os.path.join(serving_server_dir,
  213. "serving_server_conf.prototxt")
  214. with open(serving_server_conf, 'r') as f:
  215. model_var = google.protobuf.text_format.Merge(
  216. str(f.read()), m_config.GeneralModelConfig())
  217. feed_vars = [var.name for var in model_var.feed_var]
  218. fetch_vars = [var.name for var in model_var.fetch_var]
  219. return feed_vars, fetch_vars
  220. if __name__ == '__main__':
  221. # load config and prepare the service
  222. FLAGS = ArgsParser().parse_args()
  223. feed_vars, fetch_vars = get_model_vars(FLAGS.model_dir,
  224. FLAGS.service_config)
  225. GLOBAL_VAR['feed_vars'] = feed_vars
  226. GLOBAL_VAR['fetch_vars'] = fetch_vars
  227. GLOBAL_VAR['preprocess_ops'] = FLAGS.model_config.preprocess_infos
  228. GLOBAL_VAR['model_config'] = FLAGS.model_config
  229. # define the service
  230. uci_service = DetectorService(name="ppdet")
  231. uci_service.prepare_pipeline_config(yml_dict=FLAGS.service_config)
  232. # start the service
  233. uci_service.run_service()