__init__.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #copyright (c) 2021 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 paddle import nn
  15. import copy
  16. from collections import OrderedDict
  17. from .metrics import TopkAcc, mAP, mINP, Recallk, Precisionk
  18. from .metrics import DistillationTopkAcc
  19. from .metrics import GoogLeNetTopkAcc
  20. from .metrics import HammingDistance, AccuracyScore
  21. class CombinedMetrics(nn.Layer):
  22. def __init__(self, config_list):
  23. super().__init__()
  24. self.metric_func_list = []
  25. assert isinstance(config_list, list), (
  26. 'operator config should be a list')
  27. for config in config_list:
  28. assert isinstance(config,
  29. dict) and len(config) == 1, "yaml format error"
  30. metric_name = list(config)[0]
  31. metric_params = config[metric_name]
  32. if metric_params is not None:
  33. self.metric_func_list.append(
  34. eval(metric_name)(**metric_params))
  35. else:
  36. self.metric_func_list.append(eval(metric_name)())
  37. def __call__(self, *args, **kwargs):
  38. metric_dict = OrderedDict()
  39. for idx, metric_func in enumerate(self.metric_func_list):
  40. metric_dict.update(metric_func(*args, **kwargs))
  41. return metric_dict
  42. def build_metrics(config):
  43. metrics_list = CombinedMetrics(copy.deepcopy(config))
  44. return metrics_list