exportor.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 os
  15. from pathlib import Path
  16. from abc import ABC, abstractmethod
  17. from .build_model import build_model
  18. from ...utils.device import get_device
  19. from ...utils.misc import AutoRegisterABCMetaClass
  20. from ...utils.config import AttrDict
  21. from ...utils.logging import *
  22. def build_exportor(config: AttrDict) -> "BaseExportor":
  23. """build model exportor
  24. Args:
  25. config (AttrDict): PaddleX pipeline config, which is loaded from pipeline yaml file.
  26. Returns:
  27. BaseExportor: the exportor, which is subclass of BaseExportor.
  28. """
  29. model_name = config.Global.model
  30. return BaseExportor.get(model_name)(config)
  31. class BaseExportor(ABC, metaclass=AutoRegisterABCMetaClass):
  32. """ Base Model Exportor """
  33. __is_base = True
  34. def __init__(self, config):
  35. """Initialize the instance.
  36. Args:
  37. config (AttrDict): PaddleX pipeline config, which is loaded from pipeline yaml file.
  38. """
  39. super().__init__()
  40. self.global_config = config.Global
  41. self.export_config = config.Export
  42. config_path = self.get_config_path(self.export_config.weight_path)
  43. self.pdx_config, self.pdx_model = build_model(
  44. self.global_config.model, config_path=config_path)
  45. def get_config_path(self, weight_path):
  46. """
  47. get config path
  48. Args:
  49. weight_path (str): The path to the weight
  50. Returns:
  51. config_path (str): The path to the config
  52. """
  53. config_path = Path(weight_path).parent / "config.yaml"
  54. if not config_path.exists():
  55. warning(
  56. f"The config file(`{config_path}`) related to weight file(`{weight_path}`) is not exist, use default instead."
  57. )
  58. config_path = None
  59. return config_path
  60. def export(self) -> dict:
  61. """execute model exporting
  62. Returns:
  63. dict: the export metrics
  64. """
  65. self.update_config()
  66. export_result = self.pdx_model.export(**self.get_export_kwargs())
  67. assert export_result.returncode == 0, f"Encountered an unexpected error({export_result.returncode}) in \
  68. exporting!"
  69. return None
  70. def get_device(self, using_device_number: int=None) -> str:
  71. """get device setting from config
  72. Args:
  73. using_device_number (int, optional): specify device number to use.
  74. Defaults to None, means that base on config setting.
  75. Returns:
  76. str: device setting, such as: `gpu:0,1`, `npu:0,1`, `cpu`.
  77. """
  78. # return get_device(
  79. # self.global_config.device, using_device_number=using_device_number)
  80. return get_device("cpu")
  81. def update_config(self):
  82. """update export config
  83. """
  84. pass
  85. def get_export_kwargs(self):
  86. """get key-value arguments of model export function
  87. """
  88. return {
  89. "weight_path": self.export_config.weight_path,
  90. "save_dir": self.global_config.output
  91. }