|
| 1 | +import logging |
| 2 | + |
| 3 | +from torch.optim import SGD, Adam |
| 4 | +from torch.optim.lr_scheduler import LambdaLR, StepLR |
| 5 | + |
| 6 | + |
| 7 | +class LambdaStepLR(LambdaLR): |
| 8 | + |
| 9 | + def __init__(self, optimizer, lr_lambda, last_step=-1): |
| 10 | + super(LambdaStepLR, self).__init__(optimizer, lr_lambda, last_step) |
| 11 | + |
| 12 | + @property |
| 13 | + def last_step(self): |
| 14 | + """Use last_epoch for the step counter""" |
| 15 | + return self.last_epoch |
| 16 | + |
| 17 | + @last_step.setter |
| 18 | + def last_step(self, v): |
| 19 | + self.last_epoch = v |
| 20 | + |
| 21 | + |
| 22 | +class PolyLR(LambdaStepLR): |
| 23 | + """DeepLab learning rate policy""" |
| 24 | + |
| 25 | + def __init__(self, optimizer, max_iter, power=0.9, last_step=-1): |
| 26 | + super(PolyLR, self).__init__(optimizer, lambda s: (1 - s / (max_iter + 1))**power, last_step) |
| 27 | + |
| 28 | + |
| 29 | +class SquaredLR(LambdaStepLR): |
| 30 | + """ Used for SGD Lars""" |
| 31 | + |
| 32 | + def __init__(self, optimizer, max_iter, last_step=-1): |
| 33 | + super(SquaredLR, self).__init__(optimizer, lambda s: (1 - s / (max_iter + 1))**2, last_step) |
| 34 | + |
| 35 | + |
| 36 | +class ExpLR(LambdaStepLR): |
| 37 | + |
| 38 | + def __init__(self, optimizer, step_size, gamma=0.9, last_step=-1): |
| 39 | + # (0.9 ** 21.854) = 0.1, (0.95 ** 44.8906) = 0.1 |
| 40 | + # To get 0.1 every N using gamma 0.9, N * log(0.9)/log(0.1) = 0.04575749 N |
| 41 | + # To get 0.1 every N using gamma g, g ** N = 0.1 -> N * log(g) = log(0.1) -> g = np.exp(log(0.1) / N) |
| 42 | + super(ExpLR, self).__init__(optimizer, lambda s: gamma**(s / step_size), last_step) |
| 43 | + |
| 44 | + |
| 45 | +def initialize_optimizer(params, config): |
| 46 | + assert config.optimizer in ['SGD', 'Adagrad', 'Adam', 'RMSProp', 'Rprop', 'SGDLars'] |
| 47 | + |
| 48 | + if config.optimizer == 'SGD': |
| 49 | + return SGD( |
| 50 | + params, |
| 51 | + lr=config.lr, |
| 52 | + momentum=config.sgd_momentum, |
| 53 | + dampening=config.sgd_dampening, |
| 54 | + weight_decay=config.weight_decay) |
| 55 | + elif config.optimizer == 'Adam': |
| 56 | + return Adam( |
| 57 | + params, |
| 58 | + lr=config.lr, |
| 59 | + betas=(config.adam_beta1, config.adam_beta2), |
| 60 | + weight_decay=config.weight_decay) |
| 61 | + else: |
| 62 | + logging.error('Optimizer type not supported') |
| 63 | + raise ValueError('Optimizer type not supported') |
| 64 | + |
| 65 | + |
| 66 | +def initialize_scheduler(optimizer, config, last_step=-1): |
| 67 | + if config.scheduler == 'StepLR': |
| 68 | + return StepLR( |
| 69 | + optimizer, step_size=config.step_size, gamma=config.step_gamma, last_epoch=last_step) |
| 70 | + elif config.scheduler == 'PolyLR': |
| 71 | + return PolyLR(optimizer, max_iter=config.max_iter, power=config.poly_power, last_step=last_step) |
| 72 | + elif config.scheduler == 'SquaredLR': |
| 73 | + return SquaredLR(optimizer, max_iter=config.max_iter, last_step=last_step) |
| 74 | + elif config.scheduler == 'ExpLR': |
| 75 | + return ExpLR( |
| 76 | + optimizer, step_size=config.exp_step_size, gamma=config.exp_gamma, last_step=last_step) |
| 77 | + else: |
| 78 | + logging.error('Scheduler not supported') |
0 commit comments