image_classification.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 numpy as np
  15. from functools import partial, wraps
  16. from ...modules.image_classification.model_list import MODELS
  17. from ..components import *
  18. from .base import BasePredictor
  19. def register(register_map, key):
  20. """register the option setting func"""
  21. def decorator(func):
  22. register_map[key] = func
  23. @wraps(func)
  24. def wrapper(self, *args, **kwargs):
  25. return func(self, *args, **kwargs)
  26. return wrapper
  27. return decorator
  28. class ClasPredictor(BasePredictor):
  29. entities = MODELS
  30. INPUT_KEYS = "x"
  31. OUTPUT_KEYS = "topk_res"
  32. DEAULT_INPUTS = {"x": "x"}
  33. DEAULT_OUTPUTS = {"topk_res": "topk_res"}
  34. _REGISTER_MAP = {}
  35. register2self = partial(register, _REGISTER_MAP)
  36. def _build_components(self):
  37. ops = {}
  38. ops["ReadImage"] = ReadImage(batch_size=self.kwargs.get("batch_size", 1))
  39. for cfg in self.config["PreProcess"]["transform_ops"]:
  40. tf_key = list(cfg.keys())[0]
  41. func = self._REGISTER_MAP.get(tf_key)
  42. args = cfg.get(tf_key, {})
  43. op = func(self, **args) if args else func(self)
  44. ops[tf_key] = op
  45. kernel_option = PaddlePredictorOption()
  46. # kernel_option.set_device(self.device)
  47. predictor = ImagePredictor(
  48. model_dir=self.model_dir,
  49. model_prefix=self.MODEL_FILE_PREFIX,
  50. option=kernel_option,
  51. )
  52. predictor.set_inputs({"imgs": "img"})
  53. ops["predictor"] = predictor
  54. post_processes = self.config["PostProcess"]
  55. for key in post_processes:
  56. func = self._REGISTER_MAP.get(key)
  57. args = post_processes.get(key, {})
  58. op = func(self, **args) if args else func(self)
  59. ops[key] = op
  60. return ops
  61. @register2self("ResizeImage")
  62. def build_resize(self, resize_short=None, size=None):
  63. assert resize_short or size
  64. if resize_short:
  65. op = ResizeByShort(
  66. target_short_edge=resize_short, size_divisor=None, interp="LINEAR"
  67. )
  68. else:
  69. op = Resize(target_size=size)
  70. return op
  71. @register2self("CropImage")
  72. def build_crop(self, size=224):
  73. return Crop(crop_size=size)
  74. @register2self("NormalizeImage")
  75. def build_normalize(
  76. self,
  77. mean=[0.485, 0.456, 0.406],
  78. std=[0.229, 0.224, 0.225],
  79. scale=1 / 255,
  80. order="",
  81. channel_num=3,
  82. ):
  83. assert channel_num == 3
  84. assert order == ""
  85. return Normalize(mean=mean, std=std)
  86. @register2self("ToCHWImage")
  87. def build_to_chw(self):
  88. return ToCHWImage()
  89. @register2self("Topk")
  90. def build_topk(self, topk, label_list=None):
  91. return Topk(topk=int(topk), class_ids=label_list)