evaluator.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # !/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. ################################################################################
  4. #
  5. # Copyright (c) 2024 Baidu.com, Inc. All Rights Reserved
  6. #
  7. ################################################################################
  8. """
  9. Author: PaddlePaddle Authors
  10. """
  11. import tarfile
  12. from pathlib import Path
  13. from ..base import BaseEvaluator
  14. from .model_list import MODELS
  15. class TSCLSEvaluator(BaseEvaluator):
  16. """ TS Classification Model Evaluator """
  17. entities = MODELS
  18. def update_config(self):
  19. """update evalution config
  20. """
  21. self.pdx_config.update_dataset(self.global_config.dataset_dir,
  22. "TSCLSDataset")
  23. def get_eval_kwargs(self) -> dict:
  24. """get key-value arguments of model evalution function
  25. Returns:
  26. dict: the arguments of evaluation function.
  27. """
  28. return {
  29. "weight_path": self.eval_config.weight_path,
  30. "device": self.get_device(using_device_number=1)
  31. }
  32. def uncompress_tar_file(self):
  33. """unpackage the tar file containing training outputs and update weight path
  34. """
  35. if tarfile.is_tarfile(self.eval_config.weight_path):
  36. dest_path = Path(self.eval_config.weight_path).parent
  37. with tarfile.open(self.eval_config.weight_path, 'r') as tar:
  38. tar.extractall(path=dest_path)
  39. self.eval_config.weight_path = dest_path.joinpath(
  40. "best_accuracy.pdparams/best_model/model.pdparams")
  41. def evaluate(self):
  42. """firstly, update evaluation config, then evaluate model, finally return the evaluation result
  43. """
  44. self.uncompress_tar_file()
  45. return super().evaluate()