__init__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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 copy
  15. import importlib
  16. from . import topk
  17. from .topk import Topk, MultiLabelTopk
  18. def build_postprocess(config):
  19. config = copy.deepcopy(config)
  20. model_name = config.pop("name")
  21. mod = importlib.import_module(__name__)
  22. postprocess_func = getattr(mod, model_name)(**config)
  23. return postprocess_func
  24. class DistillationPostProcess(object):
  25. def __init__(self, model_name="Student", key=None, func="Topk", **kargs):
  26. super().__init__()
  27. self.func = eval(func)(**kargs)
  28. self.model_name = model_name
  29. self.key = key
  30. def __call__(self, x, file_names=None):
  31. x = x[self.model_name]
  32. if self.key is not None:
  33. x = x[self.key]
  34. return self.func(x, file_names=file_names)