image_classification.py 3.3 KB

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