image_classification.py 3.4 KB

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