evaluator.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 pathlib import Path
  15. from ..base import BaseEvaluator
  16. from .model_list import MODELS
  17. from ..formula_recognition.model_list import MODELS as MODELS_LaTeX
  18. MODELS = MODELS + MODELS_LaTeX
  19. class TextRecEvaluator(BaseEvaluator):
  20. """Text Recognition Model Evaluator"""
  21. entities = MODELS
  22. def update_config(self):
  23. """update evalution config"""
  24. if self.eval_config.log_interval:
  25. self.pdx_config.update_log_interval(self.eval_config.log_interval)
  26. if self.global_config["model"] == "LaTeX_OCR_rec":
  27. self.pdx_config.update_dataset(
  28. self.global_config.dataset_dir, "LaTeXOCRDataSet"
  29. )
  30. elif "PP-OCRv3" in self.global_config["model"]:
  31. self.pdx_config.update_dataset(
  32. self.global_config.dataset_dir, "SimpleDataSet"
  33. )
  34. else:
  35. self.pdx_config.update_dataset(
  36. self.global_config.dataset_dir, "MSTextRecDataset"
  37. )
  38. label_dict_path = None
  39. if self.eval_config.get("label_dict_path"):
  40. label_dict_path = self.eval_config.label_dict_path
  41. else:
  42. label_dict_path = (
  43. Path(self.eval_config.weight_path).parent / "label_dict.txt"
  44. )
  45. if not label_dict_path.exists():
  46. label_dict_path = None
  47. if label_dict_path is not None:
  48. self.pdx_config.update_label_dict_path(label_dict_path)
  49. def get_eval_kwargs(self) -> dict:
  50. """get key-value arguments of model evalution function
  51. Returns:
  52. dict: the arguments of evaluation function.
  53. """
  54. return {
  55. "weight_path": self.eval_config.weight_path,
  56. "device": self.get_device(),
  57. }