predictor.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. from copy import deepcopy
  15. from ..inference.models import create_model
  16. from ..inference.utils.pp_option import PaddlePredictorOption
  17. from ..utils.config import AttrDict
  18. class Predictor(object):
  19. def __init__(self, config):
  20. model_name = config.Global.model
  21. predict_config = deepcopy(config.Predict)
  22. model_dir = predict_config.pop("model_dir", None)
  23. # if model_dir is None, using official
  24. model = model_name if model_dir is None else model_dir
  25. self.input_path = predict_config.pop("input_path")
  26. pp_option = PaddlePredictorOption(**predict_config.pop("kernel_option", {}))
  27. self.model = create_model(model, pp_option=pp_option, **predict_config)
  28. def predict(self):
  29. for res in self.model(self.input_path):
  30. res.print()
  31. def build_predictor(config: AttrDict):
  32. """build predictor by config for dev"""
  33. return Predictor(config)