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

130 lines
4.6 KiB

6 years ago
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  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. """Functions to build DetectionModel training optimizers."""
  16. import tensorflow as tf
  17. from object_detection.utils import learning_schedules
  18. def build(optimizer_config):
  19. """Create optimizer based on config.
  20. Args:
  21. optimizer_config: A Optimizer proto message.
  22. Returns:
  23. An optimizer and a list of variables for summary.
  24. Raises:
  25. ValueError: when using an unsupported input data type.
  26. """
  27. optimizer_type = optimizer_config.WhichOneof('optimizer')
  28. optimizer = None
  29. summary_vars = []
  30. if optimizer_type == 'rms_prop_optimizer':
  31. config = optimizer_config.rms_prop_optimizer
  32. learning_rate = _create_learning_rate(config.learning_rate)
  33. summary_vars.append(learning_rate)
  34. optimizer = tf.train.RMSPropOptimizer(
  35. learning_rate,
  36. decay=config.decay,
  37. momentum=config.momentum_optimizer_value,
  38. epsilon=config.epsilon)
  39. if optimizer_type == 'momentum_optimizer':
  40. config = optimizer_config.momentum_optimizer
  41. learning_rate = _create_learning_rate(config.learning_rate)
  42. summary_vars.append(learning_rate)
  43. optimizer = tf.train.MomentumOptimizer(
  44. learning_rate,
  45. momentum=config.momentum_optimizer_value)
  46. if optimizer_type == 'adam_optimizer':
  47. config = optimizer_config.adam_optimizer
  48. learning_rate = _create_learning_rate(config.learning_rate)
  49. summary_vars.append(learning_rate)
  50. optimizer = tf.train.AdamOptimizer(learning_rate)
  51. if optimizer is None:
  52. raise ValueError('Optimizer %s not supported.' % optimizer_type)
  53. if optimizer_config.use_moving_average:
  54. optimizer = tf.contrib.opt.MovingAverageOptimizer(
  55. optimizer, average_decay=optimizer_config.moving_average_decay)
  56. return optimizer, summary_vars
  57. def _create_learning_rate(learning_rate_config):
  58. """Create optimizer learning rate based on config.
  59. Args:
  60. learning_rate_config: A LearningRate proto message.
  61. Returns:
  62. A learning rate.
  63. Raises:
  64. ValueError: when using an unsupported input data type.
  65. """
  66. learning_rate = None
  67. learning_rate_type = learning_rate_config.WhichOneof('learning_rate')
  68. if learning_rate_type == 'constant_learning_rate':
  69. config = learning_rate_config.constant_learning_rate
  70. learning_rate = tf.constant(config.learning_rate, dtype=tf.float32,
  71. name='learning_rate')
  72. if learning_rate_type == 'exponential_decay_learning_rate':
  73. config = learning_rate_config.exponential_decay_learning_rate
  74. learning_rate = learning_schedules.exponential_decay_with_burnin(
  75. tf.train.get_or_create_global_step(),
  76. config.initial_learning_rate,
  77. config.decay_steps,
  78. config.decay_factor,
  79. burnin_learning_rate=config.burnin_learning_rate,
  80. burnin_steps=config.burnin_steps,
  81. min_learning_rate=config.min_learning_rate,
  82. staircase=config.staircase)
  83. if learning_rate_type == 'manual_step_learning_rate':
  84. config = learning_rate_config.manual_step_learning_rate
  85. if not config.schedule:
  86. raise ValueError('Empty learning rate schedule.')
  87. learning_rate_step_boundaries = [x.step for x in config.schedule]
  88. learning_rate_sequence = [config.initial_learning_rate]
  89. learning_rate_sequence += [x.learning_rate for x in config.schedule]
  90. learning_rate = learning_schedules.manual_stepping(
  91. tf.train.get_or_create_global_step(), learning_rate_step_boundaries,
  92. learning_rate_sequence, config.warmup)
  93. if learning_rate_type == 'cosine_decay_learning_rate':
  94. config = learning_rate_config.cosine_decay_learning_rate
  95. learning_rate = learning_schedules.cosine_decay_with_warmup(
  96. tf.train.get_or_create_global_step(),
  97. config.learning_rate_base,
  98. config.total_steps,
  99. config.warmup_learning_rate,
  100. config.warmup_steps,
  101. config.hold_base_rate_steps)
  102. if learning_rate is None:
  103. raise ValueError('Learning_rate %s not supported.' % learning_rate_type)
  104. return learning_rate