train_lib_test.py 5.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for the progressive train_lib."""
import os

from absl import flags
from absl.testing import parameterized
Le Hou's avatar
Le Hou committed
20
21
import dataclasses
import orbit
22
23
24
25
26
27
28
29
30
31
import tensorflow as tf

from tensorflow.python.distribute import combinations
from tensorflow.python.distribute import strategy_combinations
from official.common import flags as tfm_flags
# pylint: disable=unused-import
from official.common import registry_imports
# pylint: enable=unused-import
from official.core import config_definitions as cfg
from official.core import task_factory
Le Hou's avatar
Le Hou committed
32
from official.modeling import optimization
33
from official.modeling.hyperparams import params_dict
Le Hou's avatar
Le Hou committed
34
from official.modeling.progressive import policies
35
36
from official.modeling.progressive import train_lib
from official.modeling.progressive import trainer as prog_trainer_lib
Le Hou's avatar
Le Hou committed
37
from official.utils.testing import mock_task
38
39
40
41
42
43

FLAGS = flags.FLAGS

tfm_flags.define_flags()


Le Hou's avatar
Le Hou committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
101
102
103
104
105
106
107
108
@dataclasses.dataclass
class ProgTaskConfig(cfg.TaskConfig):
  pass


@task_factory.register_task_cls(ProgTaskConfig)
class ProgMockTask(policies.ProgressivePolicy, mock_task.MockTask):
  """Progressive task for testing."""

  def __init__(self, params: cfg.TaskConfig, logging_dir: str = None):
    mock_task.MockTask.__init__(
        self, params=params, logging_dir=logging_dir)
    policies.ProgressivePolicy.__init__(self)

  def num_stages(self):
    return 2

  def num_steps(self, stage_id):
    return 2 if stage_id == 0 else 4

  def get_model(self, stage_id, old_model=None):
    del stage_id, old_model
    return self.build_model()

  def get_optimizer(self, stage_id):
    """Build optimizer for each stage."""
    params = optimization.OptimizationConfig({
        'optimizer': {
            'type': 'adamw',
        },
        'learning_rate': {
            'type': 'polynomial',
            'polynomial': {
                'initial_learning_rate': 0.01,
                'end_learning_rate': 0.0,
                'power': 1.0,
                'decay_steps': 10,
            },
        },
        'warmup': {
            'polynomial': {
                'power': 1,
                'warmup_steps': 2,
            },
            'type': 'polynomial',
        }
    })
    opt_factory = optimization.OptimizerFactory(params)
    optimizer = opt_factory.build_optimizer(opt_factory.build_learning_rate())

    return optimizer

  def get_train_dataset(self, stage_id):
    del stage_id
    strategy = tf.distribute.get_strategy()
    return orbit.utils.make_distributed_dataset(
        strategy, self.build_inputs, None)

  def get_eval_dataset(self, stage_id):
    del stage_id
    strategy = tf.distribute.get_strategy()
    return orbit.utils.make_distributed_dataset(
        strategy, self.build_inputs, None)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class TrainTest(tf.test.TestCase, parameterized.TestCase):

  def setUp(self):
    super(TrainTest, self).setUp()
    self._test_config = {
        'trainer': {
            'checkpoint_interval': 10,
            'steps_per_loop': 10,
            'summary_interval': 10,
            'train_steps': 10,
            'validation_steps': 5,
            'validation_interval': 10,
            'continuous_eval_timeout': 1,
            'optimizer_config': {
                'optimizer': {
                    'type': 'sgd',
                },
                'learning_rate': {
                    'type': 'constant'
                }
            }
        },
    }

  @combinations.generate(
      combinations.combine(
          distribution_strategy=[
              strategy_combinations.default_strategy,
              strategy_combinations.cloud_tpu_strategy,
              strategy_combinations.one_device_strategy_gpu,
          ],
          mode='eager',
          flag_mode=['train', 'eval', 'train_and_eval'],
          run_post_eval=[True, False]))
  def test_end_to_end(self, distribution_strategy, flag_mode, run_post_eval):
    model_dir = self.get_temp_dir()
    experiment_config = cfg.ExperimentConfig(
        trainer=prog_trainer_lib.ProgressiveTrainerConfig(),
Le Hou's avatar
Le Hou committed
147
        task=ProgTaskConfig())
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    experiment_config = params_dict.override_params_dict(
        experiment_config, self._test_config, is_strict=False)

    with distribution_strategy.scope():
      task = task_factory.get_task(experiment_config.task,
                                   logging_dir=model_dir)

    _, logs = train_lib.run_experiment(
        distribution_strategy=distribution_strategy,
        task=task,
        mode=flag_mode,
        params=experiment_config,
        model_dir=model_dir,
        run_post_eval=run_post_eval)

    if run_post_eval:
      self.assertNotEmpty(logs)
    else:
      self.assertEmpty(logs)

    if flag_mode == 'eval':
      return
    self.assertNotEmpty(
        tf.io.gfile.glob(os.path.join(model_dir, 'checkpoint')))
    # Tests continuous evaluation.
    _, logs = train_lib.run_experiment(
        distribution_strategy=distribution_strategy,
        task=task,
        mode='continuous_eval',
        params=experiment_config,
        model_dir=model_dir,
        run_post_eval=run_post_eval)
    print(logs)


if __name__ == '__main__':
  tf.test.main()