trainer_cot.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (c) 2022 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. from ppdet.core.workspace import create
  15. from ppdet.utils.logger import setup_logger
  16. logger = setup_logger('ppdet.engine')
  17. from . import Trainer
  18. __all__ = ['TrainerCot']
  19. class TrainerCot(Trainer):
  20. """
  21. Trainer for label-cotuning
  22. calculate the relationship between base_classes and novel_classes
  23. """
  24. def __init__(self, cfg, mode='train'):
  25. super(TrainerCot, self).__init__(cfg, mode)
  26. self.cotuning_init()
  27. def cotuning_init(self):
  28. num_classes_novel = self.cfg['num_classes']
  29. self.load_weights(self.cfg.pretrain_weights)
  30. self.model.eval()
  31. relationship = self.model.relationship_learning(self.loader, num_classes_novel)
  32. self.model.init_cot_head(relationship)
  33. self.optimizer = create('OptimizerBuilder')(self.lr, self.model)