image_classification.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 ...utils.func_register import FuncRegister
  16. from ...modules.image_classification.model_list import MODELS
  17. from ..components import *
  18. from ..results import TopkResult
  19. from .base import BasicPredictor
  20. class ClasPredictor(BasicPredictor):
  21. entities = [*MODELS]
  22. _FUNC_MAP = {}
  23. register = FuncRegister(_FUNC_MAP)
  24. def _build_components(self):
  25. self._add_component(ReadImage(format="RGB"))
  26. for cfg in self.config["PreProcess"]["transform_ops"]:
  27. tf_key = list(cfg.keys())[0]
  28. func = self._FUNC_MAP[tf_key]
  29. args = cfg.get(tf_key, {})
  30. op = func(self, **args) if args else func(self)
  31. self._add_component(op)
  32. predictor = ImagePredictor(
  33. model_dir=self.model_dir,
  34. model_prefix=self.MODEL_FILE_PREFIX,
  35. option=self.pp_option,
  36. )
  37. self._add_component(predictor)
  38. post_processes = self.config["PostProcess"]
  39. for key in post_processes:
  40. func = self._FUNC_MAP.get(key)
  41. args = post_processes.get(key, {})
  42. op = func(self, **args) if args else func(self)
  43. self._add_component(op)
  44. @register("ResizeImage")
  45. # TODO(gaotingquan): backend & interpolation
  46. def build_resize(
  47. self, resize_short=None, size=None, backend="cv2", interpolation="LINEAR"
  48. ):
  49. assert resize_short or size
  50. if resize_short:
  51. op = ResizeByShort(
  52. target_short_edge=resize_short, size_divisor=None, interp="LINEAR"
  53. )
  54. else:
  55. op = Resize(target_size=size)
  56. return op
  57. @register("CropImage")
  58. def build_crop(self, size=224):
  59. return Crop(crop_size=size)
  60. @register("NormalizeImage")
  61. def build_normalize(
  62. self,
  63. mean=[0.485, 0.456, 0.406],
  64. std=[0.229, 0.224, 0.225],
  65. scale=1 / 255,
  66. order="",
  67. channel_num=3,
  68. ):
  69. assert channel_num == 3
  70. assert order == ""
  71. return Normalize(scale=scale, mean=mean, std=std)
  72. @register("ToCHWImage")
  73. def build_to_chw(self):
  74. return ToCHWImage()
  75. @register("Topk")
  76. def build_topk(self, topk, label_list=None):
  77. return Topk(topk=int(topk), class_ids=label_list)
  78. @register("MultiLabelThreshOutput")
  79. def build_threshoutput(self, threshold, label_list=None):
  80. return MultiLabelThreshOutput(threshold=float(threshold), class_ids=label_list)
  81. def _pack_res(self, single):
  82. keys = ["input_path", "class_ids", "scores"]
  83. if "label_names" in single:
  84. keys.append("label_names")
  85. return TopkResult({key: single[key] for key in keys})