lr_scheduler.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. # -------------------------------------------------------------------------
  2. # Copyright (c) 2021-2022, NVIDIA Corporation & Affiliates. All rights reserved.
  3. #
  4. # This work is made available under the Nvidia Source Code License.
  5. # To view a copy of this license, visit
  6. # https://github.com/NVlabs/GroupViT/blob/main/LICENSE
  7. #
  8. # Written by Jiarui Xu
  9. # -------------------------------------------------------------------------
  10. from timm.scheduler.cosine_lr import CosineLRScheduler
  11. def build_scheduler(config, optimizer, n_iter_per_epoch):
  12. num_steps = int(config.epochs * n_iter_per_epoch)
  13. warmup_steps = int(config.warmup_epochs * n_iter_per_epoch)
  14. lr_scheduler = None
  15. if config.lr_scheduler.name == 'cosine':
  16. lr_scheduler = CosineLRScheduler(
  17. optimizer,
  18. t_initial=num_steps,
  19. t_mul=1.,
  20. lr_min=config.min_lr,
  21. warmup_lr_init=config.warmup_lr,
  22. warmup_t=warmup_steps,
  23. cycle_limit=1,
  24. t_in_epochs=False,
  25. )
  26. else:
  27. raise NotImplementedError(f'lr scheduler {config.lr_scheduler.name} not implemented')
  28. return lr_scheduler