callbacks.py 9 KB
Newer Older
Yeqing Li's avatar
Yeqing Li committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Allen Wang's avatar
Allen Wang committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Yeqing Li's avatar
Yeqing Li committed
14
15

# Lint as: python3
Allen Wang's avatar
Allen Wang committed
16
17
18
19
20
21
22
"""Common modules for callbacks."""
from __future__ import absolute_import
from __future__ import division
# from __future__ import google_type_annotations
from __future__ import print_function

import os
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
23
from typing import Any, List, MutableMapping, Text
Hongkun Yu's avatar
Hongkun Yu committed
24

Allen Wang's avatar
Allen Wang committed
25
26
from absl import logging
import tensorflow as tf
Allen Wang's avatar
Allen Wang committed
27

Abdullah Rashwan's avatar
Abdullah Rashwan committed
28
from official.modeling import optimization
Allen Wang's avatar
Allen Wang committed
29
from official.utils.misc import keras_utils
Allen Wang's avatar
Allen Wang committed
30
31


32
33
34
35
36
37
38
39
40
41
42
43
def get_callbacks(
    model_checkpoint: bool = True,
    include_tensorboard: bool = True,
    time_history: bool = True,
    track_lr: bool = True,
    write_model_weights: bool = True,
    apply_moving_average: bool = False,
    initial_step: int = 0,
    batch_size: int = 0,
    log_steps: int = 0,
    model_dir: str = None,
    backup_and_restore: bool = False) -> List[tf.keras.callbacks.Callback]:
Allen Wang's avatar
Allen Wang committed
44
45
46
47
48
  """Get all callbacks."""
  model_dir = model_dir or ''
  callbacks = []
  if model_checkpoint:
    ckpt_full_path = os.path.join(model_dir, 'model.ckpt-{epoch:04d}')
Hongkun Yu's avatar
Hongkun Yu committed
49
50
51
    callbacks.append(
        tf.keras.callbacks.ModelCheckpoint(
            ckpt_full_path, save_weights_only=True, verbose=1))
52
53
54
55
  if backup_and_restore:
    backup_dir = os.path.join(model_dir, 'tmp')
    callbacks.append(
        tf.keras.callbacks.experimental.BackupAndRestore(backup_dir))
Allen Wang's avatar
Allen Wang committed
56
  if include_tensorboard:
Hongkun Yu's avatar
Hongkun Yu committed
57
58
59
60
61
62
    callbacks.append(
        CustomTensorBoard(
            log_dir=model_dir,
            track_lr=track_lr,
            initial_step=initial_step,
            write_images=write_model_weights))
Allen Wang's avatar
Allen Wang committed
63
  if time_history:
Hongkun Yu's avatar
Hongkun Yu committed
64
65
66
67
68
    callbacks.append(
        keras_utils.TimeHistory(
            batch_size,
            log_steps,
            logdir=model_dir if include_tensorboard else None))
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
69
70
71
  if apply_moving_average:
    # Save moving average model to a different file so that
    # we can resume training from a checkpoint
Hongkun Yu's avatar
Hongkun Yu committed
72
73
74
75
76
77
78
79
    ckpt_full_path = os.path.join(model_dir, 'average',
                                  'model.ckpt-{epoch:04d}')
    callbacks.append(
        AverageModelCheckpoint(
            update_weights=False,
            filepath=ckpt_full_path,
            save_weights_only=True,
            verbose=1))
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
80
    callbacks.append(MovingAverageCallback())
Allen Wang's avatar
Allen Wang committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  return callbacks


def get_scalar_from_tensor(t: tf.Tensor) -> int:
  """Utility function to convert a Tensor to a scalar."""
  t = tf.keras.backend.get_value(t)
  if callable(t):
    return t()
  else:
    return t


class CustomTensorBoard(tf.keras.callbacks.TensorBoard):
  """A customized TensorBoard callback that tracks additional datapoints.

  Metrics tracked:
  - Global learning rate

  Attributes:
Hongkun Yu's avatar
Hongkun Yu committed
100
101
    log_dir: the path of the directory where to save the log files to be parsed
      by TensorBoard.
Allen Wang's avatar
Allen Wang committed
102
103
    track_lr: `bool`, whether or not to track the global learning rate.
    initial_step: the initial step, used for preemption recovery.
Hongkun Yu's avatar
Hongkun Yu committed
104
105
    **kwargs: Additional arguments for backwards compatibility. Possible key is
      `period`.
Allen Wang's avatar
Allen Wang committed
106
  """
Hongkun Yu's avatar
Hongkun Yu committed
107

Allen Wang's avatar
Allen Wang committed
108
109
110
111
  # TODO(b/146499062): track params, flops, log lr, l2 loss,
  # classification loss

  def __init__(self,
Allen Wang's avatar
Allen Wang committed
112
               log_dir: str,
Allen Wang's avatar
Allen Wang committed
113
114
115
116
117
118
119
120
121
               track_lr: bool = False,
               initial_step: int = 0,
               **kwargs):
    super(CustomTensorBoard, self).__init__(log_dir=log_dir, **kwargs)
    self.step = initial_step
    self._track_lr = track_lr

  def on_batch_begin(self,
                     epoch: int,
Allen Wang's avatar
Allen Wang committed
122
                     logs: MutableMapping[str, Any] = None) -> None:
Allen Wang's avatar
Allen Wang committed
123
124
125
126
127
128
129
130
    self.step += 1
    if logs is None:
      logs = {}
    logs.update(self._calculate_metrics())
    super(CustomTensorBoard, self).on_batch_begin(epoch, logs)

  def on_epoch_begin(self,
                     epoch: int,
Allen Wang's avatar
Allen Wang committed
131
                     logs: MutableMapping[str, Any] = None) -> None:
Allen Wang's avatar
Allen Wang committed
132
133
134
135
136
137
138
139
140
141
    if logs is None:
      logs = {}
    metrics = self._calculate_metrics()
    logs.update(metrics)
    for k, v in metrics.items():
      logging.info('Current %s: %f', k, v)
    super(CustomTensorBoard, self).on_epoch_begin(epoch, logs)

  def on_epoch_end(self,
                   epoch: int,
Allen Wang's avatar
Allen Wang committed
142
                   logs: MutableMapping[str, Any] = None) -> None:
Allen Wang's avatar
Allen Wang committed
143
144
145
146
147
148
    if logs is None:
      logs = {}
    metrics = self._calculate_metrics()
    logs.update(metrics)
    super(CustomTensorBoard, self).on_epoch_end(epoch, logs)

Allen Wang's avatar
Allen Wang committed
149
  def _calculate_metrics(self) -> MutableMapping[str, Any]:
Allen Wang's avatar
Allen Wang committed
150
    logs = {}
151
152
153
    # TODO(b/149030439): disable LR reporting.
    # if self._track_lr:
    #   logs['learning_rate'] = self._calculate_lr()
Allen Wang's avatar
Allen Wang committed
154
155
156
157
    return logs

  def _calculate_lr(self) -> int:
    """Calculates the learning rate given the current step."""
Hongkun Yu's avatar
Hongkun Yu committed
158
    return get_scalar_from_tensor(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
159
        self._get_base_optimizer()._decayed_lr(var_dtype=tf.float32))  # pylint:disable=protected-access
Allen Wang's avatar
Allen Wang committed
160
161
162
163
164
165
166
167
168
169
170

  def _get_base_optimizer(self) -> tf.keras.optimizers.Optimizer:
    """Get the base optimizer used by the current model."""

    optimizer = self.model.optimizer

    # The optimizer might be wrapped by another class, so unwrap it
    while hasattr(optimizer, '_optimizer'):
      optimizer = optimizer._optimizer  # pylint:disable=protected-access

    return optimizer
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
171
172
173


class MovingAverageCallback(tf.keras.callbacks.Callback):
Abdullah Rashwan's avatar
Abdullah Rashwan committed
174
  """A Callback to be used with a `ExponentialMovingAverage` optimizer.
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
175
176
177
178
179
180
181
182
183
184
185
186

  Applies moving average weights to the model during validation time to test
  and predict on the averaged weights rather than the current model weights.
  Once training is complete, the model weights will be overwritten with the
  averaged weights (by default).

  Attributes:
    overwrite_weights_on_train_end: Whether to overwrite the current model
      weights with the averaged weights from the moving average optimizer.
    **kwargs: Any additional callback arguments.
  """

Hongkun Yu's avatar
Hongkun Yu committed
187
  def __init__(self, overwrite_weights_on_train_end: bool = False, **kwargs):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
188
189
190
191
192
    super(MovingAverageCallback, self).__init__(**kwargs)
    self.overwrite_weights_on_train_end = overwrite_weights_on_train_end

  def set_model(self, model: tf.keras.Model):
    super(MovingAverageCallback, self).set_model(model)
Abdullah Rashwan's avatar
Abdullah Rashwan committed
193
194
    assert isinstance(self.model.optimizer,
                      optimization.ExponentialMovingAverage)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    self.model.optimizer.shadow_copy(self.model)

  def on_test_begin(self, logs: MutableMapping[Text, Any] = None):
    self.model.optimizer.swap_weights()

  def on_test_end(self, logs: MutableMapping[Text, Any] = None):
    self.model.optimizer.swap_weights()

  def on_train_end(self, logs: MutableMapping[Text, Any] = None):
    if self.overwrite_weights_on_train_end:
      self.model.optimizer.assign_average_vars(self.model.variables)


class AverageModelCheckpoint(tf.keras.callbacks.ModelCheckpoint):
  """Saves and, optionally, assigns the averaged weights.

  Taken from tfa.callbacks.AverageModelCheckpoint.

  Attributes:
Hongkun Yu's avatar
Hongkun Yu committed
214
215
216
217
    update_weights: If True, assign the moving average weights to the model, and
      save them. If False, keep the old non-averaged weights, but the saved
      model uses the average weights. See `tf.keras.callbacks.ModelCheckpoint`
      for the other args.
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
218
219
  """

Hongkun Yu's avatar
Hongkun Yu committed
220
221
222
223
224
225
226
227
228
229
  def __init__(self,
               update_weights: bool,
               filepath: str,
               monitor: str = 'val_loss',
               verbose: int = 0,
               save_best_only: bool = False,
               save_weights_only: bool = False,
               mode: str = 'auto',
               save_freq: str = 'epoch',
               **kwargs):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
230
    self.update_weights = update_weights
Hongkun Yu's avatar
Hongkun Yu committed
231
232
    super().__init__(filepath, monitor, verbose, save_best_only,
                     save_weights_only, mode, save_freq, **kwargs)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
233
234

  def set_model(self, model):
Abdullah Rashwan's avatar
Abdullah Rashwan committed
235
    if not isinstance(model.optimizer, optimization.ExponentialMovingAverage):
Hongkun Yu's avatar
Hongkun Yu committed
236
237
      raise TypeError('AverageModelCheckpoint is only used when training'
                      'with MovingAverage')
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
238
239
240
    return super().set_model(model)

  def _save_model(self, epoch, logs):
Abdullah Rashwan's avatar
Abdullah Rashwan committed
241
242
    assert isinstance(self.model.optimizer,
                      optimization.ExponentialMovingAverage)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256

    if self.update_weights:
      self.model.optimizer.assign_average_vars(self.model.variables)
      return super()._save_model(epoch, logs)
    else:
      # Note: `model.get_weights()` gives us the weights (non-ref)
      # whereas `model.variables` returns references to the variables.
      non_avg_weights = self.model.get_weights()
      self.model.optimizer.assign_average_vars(self.model.variables)
      # result is currently None, since `super._save_model` doesn't
      # return anything, but this may change in the future.
      result = super()._save_model(epoch, logs)
      self.model.set_weights(non_avg_weights)
      return result