You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lr_schedule.py 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """lr generator for fasterrcnn"""
  16. import math
  17. def linear_warmup_learning_rate(current_step, warmup_steps, base_lr, init_lr):
  18. lr_inc = (float(base_lr) - float(init_lr)) / float(warmup_steps)
  19. learning_rate = float(init_lr) + lr_inc * current_step
  20. return learning_rate
  21. def a_cosine_learning_rate(current_step, base_lr, warmup_steps, decay_steps):
  22. base = float(current_step - warmup_steps) / float(decay_steps)
  23. learning_rate = (1 + math.cos(base * math.pi)) / 2 * base_lr
  24. return learning_rate
  25. def dynamic_lr(config, rank_size=1):
  26. """dynamic learning rate generator"""
  27. base_lr = config.base_lr
  28. base_step = (config.base_step // rank_size) + rank_size
  29. total_steps = int(base_step * config.total_epoch)
  30. warmup_steps = int(config.warmup_step)
  31. lr = []
  32. for i in range(total_steps):
  33. if i < warmup_steps:
  34. lr.append(linear_warmup_learning_rate(i, warmup_steps, base_lr, base_lr * config.warmup_ratio))
  35. else:
  36. lr.append(a_cosine_learning_rate(i, base_lr, warmup_steps, total_steps))
  37. return lr