evaluator.py 5.93 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Hongkun Yu's avatar
Hongkun Yu committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# 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.

"""Multitask Evaluator implementation.

The evaluator implements the Orbit `AbstractEvaluator` interface.
"""
19
from typing import Dict, List, Optional, Union
Hongkun Yu's avatar
Hongkun Yu committed
20
21
22
23
import gin
import orbit
import tensorflow as tf

24
from official.core import base_task
25
from official.core import train_utils
Hongkun Yu's avatar
Hongkun Yu committed
26
27
28
29
30
31
32
from official.modeling.multitask import base_model


@gin.configurable
class MultiTaskEvaluator(orbit.AbstractEvaluator):
  """Implements the common trainer shared for TensorFlow models."""

33
34
  def __init__(
      self,
35
      eval_tasks: List[base_task.Task],
36
37
      model: Union[tf.keras.Model, base_model.MultiTaskBaseModel],
      global_step: Optional[tf.Variable] = None,
38
      eval_steps: Optional[Dict[str, int]] = None,
39
      checkpoint_exporter: Optional[train_utils.BestCheckpointExporter] = None):
Hongkun Yu's avatar
Hongkun Yu committed
40
41
42
    """Initialize common trainer for TensorFlow models.

    Args:
43
      eval_tasks: A list of tasks to evaluate.
Hongkun Yu's avatar
Hongkun Yu committed
44
45
      model: tf.keras.Model instance.
      global_step: the global step variable.
46
      eval_steps: a dictionary of steps to run eval keyed by task names.
47
48
      checkpoint_exporter: an object that has the `maybe_export_checkpoint`
        interface.
Hongkun Yu's avatar
Hongkun Yu committed
49
50
51
52
    """
    # Gets the current distribution strategy. If not inside any strategy scope,
    # it gets a single-replica no-op strategy.
    self._strategy = tf.distribute.get_strategy()
53
    self._tasks = eval_tasks
Hongkun Yu's avatar
Hongkun Yu committed
54
55
    self._model = model
    self._global_step = global_step or orbit.utils.create_global_step()
56
    self._checkpoint_exporter = checkpoint_exporter
57
58
59
60
61
    if hasattr(self.model, "checkpoint_items"):
      checkpoint_items = self.model.checkpoint_items
    else:
      checkpoint_items = {}

Hongkun Yu's avatar
Hongkun Yu committed
62
    self._checkpoint = tf.train.Checkpoint(
63
64
65
        model=self.model,
        global_step=self.global_step,
        **checkpoint_items)
Hongkun Yu's avatar
Hongkun Yu committed
66
67
68
69
70
71

    self._validation_losses = None
    self._validation_metrics = None

    # Builds per-task datasets.
    self.eval_datasets = {}
72
73
74
    self.eval_steps = eval_steps or {}
    for task in self.tasks:
      self.eval_datasets[task.name] = orbit.utils.make_distributed_dataset(
Hongkun Yu's avatar
Hongkun Yu committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
          self.strategy, task.build_inputs, task.task_config.validation_data)

    # Builds per-task validation loops.
    def get_function(task_name, task):

      task_metrics = self.validation_metrics[task_name]
      task_loss = self.validation_losses[task_name]
      if isinstance(self.model, base_model.MultiTaskBaseModel):
        model = self.model.sub_tasks[task_name]
      else:
        model = self.model

      def step_fn(inputs):
        logs = task.validation_step(inputs, model=model, metrics=task_metrics)
        task_loss.update_state(logs[task.loss])
        return logs

      @tf.function
      def eval_step_fn(iterator):
        distributed_outputs = self.strategy.run(step_fn, args=(next(iterator),))
        return tf.nest.map_structure(self.strategy.experimental_local_results,
                                     distributed_outputs)

      return orbit.utils.create_loop_fn(eval_step_fn)

    self.task_fns = {
101
        task.name: get_function(task.name, task) for task in self.tasks
Hongkun Yu's avatar
Hongkun Yu committed
102
103
104
105
106
107
108
    }

  @property
  def strategy(self):
    return self._strategy

  @property
109
110
  def tasks(self):
    return self._tasks
Hongkun Yu's avatar
Hongkun Yu committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

  @property
  def model(self):
    return self._model

  @property
  def global_step(self):
    return self._global_step

  @property
  def validation_losses(self):
    """Accesses the validation loss metric object."""
    if self._validation_losses is None:
      # Builds the per-task metrics and losses.
      self._validation_losses = {}
126
127
      for task in self.tasks:
        self._validation_losses[task.name] = tf.keras.metrics.Mean(
Hongkun Yu's avatar
Hongkun Yu committed
128
129
130
131
132
133
134
135
136
            "validation_loss", dtype=tf.float32)
    return self._validation_losses

  @property
  def validation_metrics(self):
    """Accesses all validation metric metric objects."""
    if self._validation_metrics is None:
      # Builds the per-task metrics and losses.
      self._validation_metrics = {}
137
138
      for task in self.tasks:
        self._validation_metrics[task.name] = task.build_metrics(training=False)
Hongkun Yu's avatar
Hongkun Yu committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    return self._validation_metrics

  @property
  def checkpoint(self):
    """Accesses the training checkpoint."""
    return self._checkpoint

  def evaluate(self, num_steps: tf.Tensor):
    """Performs evaluation for each `EvalTask`."""
    for metric in self.validation_losses.values():
      metric.reset_states()
    for metrics in self.validation_metrics.values():
      for metric in metrics:
        metric.reset_states()
    results = {}
    eval_iters = tf.nest.map_structure(iter, self.eval_datasets)

156
    for task in self.tasks:
Hongkun Yu's avatar
Hongkun Yu committed
157
      outputs = None
158
      name = task.name
Hongkun Yu's avatar
Hongkun Yu committed
159
      eval_iter = eval_iters[name]
160
161
      task_eval_steps = self.eval_steps.get(name, None) or num_steps
      outputs = self.task_fns[name](
Hongkun Yu's avatar
Hongkun Yu committed
162
163
164
165
166
167
168
169
170
171
          eval_iter,
          task_eval_steps,
          state=outputs,
          reduce_fn=task.aggregate_logs)
      task_metrics = self.validation_metrics[name]
      task_loss = self.validation_losses[name]
      logs = {}
      for metric in task_metrics + [task_loss]:
        logs[metric.name] = metric.result()
      if outputs:
172
173
        metrics = task.reduce_aggregated_logs(
            outputs, global_step=self.global_step)
Hongkun Yu's avatar
Hongkun Yu committed
174
175
        logs.update(metrics)
      results[name] = logs
176
177
178
179

    if self._checkpoint_exporter:
      self._checkpoint_exporter.maybe_export_checkpoint(
          self.checkpoint, results, self.global_step.numpy())
Hongkun Yu's avatar
Hongkun Yu committed
180
    return results