落水人员检测
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

71 rinda
2.6KB

  1. # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. # Created by: Hang Zhang
  3. # ECE Department, Rutgers University
  4. # Email: zhang.hang@rutgers.edu
  5. # Copyright (c) 2017
  6. # This source code is licensed under the MIT-style license found in the
  7. # LICENSE file in the root directory of this source tree
  8. # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  9. import math
  10. class LR_Scheduler(object):
  11. """Learning Rate Scheduler
  12. Step mode: ``lr = baselr * 0.1 ^ {floor(epoch-1 / lr_step)}``
  13. Cosine mode: ``lr = baselr * 0.5 * (1 + cos(iter/maxiter))``
  14. Poly mode: ``lr = baselr * (1 - iter/maxiter) ^ 0.9``
  15. Args:
  16. args:
  17. :attr:`args.lr_scheduler` lr scheduler mode (`cos`, `poly`),
  18. :attr:`args.lr` base learning rate, :attr:`args.epochs` number of epochs,
  19. :attr:`args.lr_step`
  20. iters_per_epoch: number of iterations per epoch
  21. """
  22. def __init__(self, mode, base_lr, num_epochs, iters_per_epoch=0,
  23. lr_step=0, warmup_epochs=0):
  24. self.mode = mode
  25. print('Using {} LR Scheduler!'.format(self.mode))
  26. self.lr = base_lr
  27. if mode == 'step':
  28. assert lr_step
  29. self.lr_step = lr_step
  30. self.iters_per_epoch = iters_per_epoch
  31. self.N = num_epochs * iters_per_epoch
  32. self.epoch = -1
  33. self.warmup_iters = warmup_epochs * iters_per_epoch
  34. def __call__(self, optimizer, i, epoch, best_pred):
  35. T = epoch * self.iters_per_epoch + i
  36. if self.mode == 'cos':
  37. lr = 0.5 * self.lr * (1 + math.cos(1.0 * T / self.N * math.pi))
  38. elif self.mode == 'poly':
  39. lr = self.lr * pow((1 - 1.0 * T / self.N), 0.9)
  40. elif self.mode == 'step':
  41. lr = self.lr * (0.1 ** (epoch // self.lr_step))
  42. else:
  43. raise NotImplemented
  44. # warm up lr schedule
  45. if self.warmup_iters > 0 and T < self.warmup_iters:
  46. lr = lr * 1.0 * T / self.warmup_iters
  47. if epoch > self.epoch:
  48. print('\n=>Epoches %i, learning rate = %.4f, \
  49. previous best = %.4f' % (epoch, lr, best_pred))
  50. self.epoch = epoch
  51. assert lr >= 0
  52. self._adjust_learning_rate(optimizer, lr)
  53. def _adjust_learning_rate(self, optimizer, lr):
  54. if len(optimizer.param_groups) == 1:
  55. optimizer.param_groups[0]['lr'] = lr
  56. else:
  57. # enlarge the lr at the head
  58. optimizer.param_groups[0]['lr'] = lr
  59. for i in range(1, len(optimizer.param_groups)):
  60. optimizer.param_groups[i]['lr'] = lr * 10