lr_scheduler.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -------------------------------------------------------------------------
  2. # Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
  3. #
  4. # NVIDIA CORPORATION & AFFILIATES and its licensors retain all intellectual
  5. # property and proprietary rights in and to this software, related
  6. # documentation and any modifications thereto. Any use, reproduction,
  7. # disclosure or distribution of this software and related documentation
  8. # without an express license agreement from NVIDIA CORPORATION is strictly
  9. # prohibited.
  10. #
  11. # Written by Jiarui Xu
  12. # -------------------------------------------------------------------------
  13. from timm.scheduler.cosine_lr import CosineLRScheduler
  14. def build_scheduler(config, optimizer, n_iter_per_epoch):
  15. num_steps = int(config.epochs * n_iter_per_epoch)
  16. warmup_steps = int(config.warmup_epochs * n_iter_per_epoch)
  17. lr_scheduler = None
  18. if config.lr_scheduler.name == 'cosine':
  19. lr_scheduler = CosineLRScheduler(
  20. optimizer,
  21. t_initial=num_steps,
  22. # t_mul=1., ## this does not work with higher versions of timm
  23. lr_min=config.min_lr,
  24. warmup_lr_init=config.warmup_lr,
  25. warmup_t=warmup_steps,
  26. cycle_limit=1,
  27. t_in_epochs=False,
  28. )
  29. else:
  30. raise NotImplementedError(f'lr scheduler {config.lr_scheduler.name} not implemented')
  31. return lr_scheduler