image_classification.py 3.0 KB

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