_image_classification.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
  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 abc
  15. from .._utils.cli import (
  16. add_simple_inference_args,
  17. get_subcommand_args,
  18. perform_simple_inference,
  19. )
  20. from .base import PaddleXPredictorWrapper, PredictorCLISubcommandExecutor
  21. class ImageClassification(PaddleXPredictorWrapper):
  22. def __init__(
  23. self,
  24. *,
  25. topk=None,
  26. **kwargs,
  27. ):
  28. self._extra_init_args = {
  29. "topk": topk,
  30. }
  31. super().__init__(**kwargs)
  32. def _get_extra_paddlex_predictor_init_args(self):
  33. return self._extra_init_args
  34. class ImageClassificationSubcommandExecutor(PredictorCLISubcommandExecutor):
  35. def _update_subparser(self, subparser):
  36. add_simple_inference_args(subparser)
  37. subparser.add_argument(
  38. "--topk",
  39. type=int,
  40. help="Top-k value for prediction results.",
  41. )
  42. @property
  43. @abc.abstractmethod
  44. def wrapper_cls(self):
  45. raise NotImplementedError
  46. def execute_with_args(self, args):
  47. params = get_subcommand_args(args)
  48. perform_simple_inference(self.wrapper_cls, params)