main.py 8.86 KB
Newer Older
Yeqing Li's avatar
Yeqing Li committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
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
16
17
18
"""Main function to train various object detection models."""

import functools
import pprint
19
20
21
22

from absl import app
from absl import flags
from absl import logging
23
import tensorflow as tf
24

25
from official.common import distribute_utils
26
from official.modeling.hyperparams import params_dict
Allen Wang's avatar
Allen Wang committed
27
from official.utils import hyperparams_flags
28
29
from official.utils.flags import core as flags_core
from official.utils.misc import keras_utils
30
31
32
from official.vision.detection.configs import factory as config_factory
from official.vision.detection.dataloader import input_reader
from official.vision.detection.dataloader import mode_keys as ModeKeys
Hongkun Yu's avatar
Hongkun Yu committed
33
from official.vision.detection.executor import distributed_executor as executor
34
35
36
from official.vision.detection.executor.detection_executor import DetectionDistributedExecutor
from official.vision.detection.modeling import factory as model_factory

Allen Wang's avatar
Allen Wang committed
37
hyperparams_flags.initialize_common_flags()
Will Cromar's avatar
Will Cromar committed
38
flags_core.define_log_steps()
39

Yeqing Li's avatar
Yeqing Li committed
40
flags.DEFINE_bool('enable_xla', default=False, help='Enable XLA for GPU')
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
41

42
flags.DEFINE_string(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
43
44
45
    'mode',
    default='train',
    help='Mode to run: `train`, `eval` or `eval_once`.')
46
47
48

flags.DEFINE_string(
    'model', default='retinanet',
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
49
    help='Model to run: `retinanet`, `mask_rcnn` or `shapemask`.')
50
51
52
53
54
55

flags.DEFINE_string('training_file_pattern', None,
                    'Location of the train data.')

flags.DEFINE_string('eval_file_pattern', None, 'Location of ther eval data')

Yeqing Li's avatar
Yeqing Li committed
56
57
58
flags.DEFINE_string(
    'checkpoint_path', None,
    'The checkpoint path to eval. Only used in eval_once mode.')
59
60
61
62

FLAGS = flags.FLAGS


63
def run_executor(params,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
64
65
                 mode,
                 checkpoint_path=None,
66
67
                 train_input_fn=None,
                 eval_input_fn=None,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
68
                 callbacks=None,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
69
                 prebuilt_strategy=None):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
70
  """Runs the object detection model on distribution strategy defined by the user."""
71

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
72
73
74
75
76
  if params.architecture.use_bfloat16:
    policy = tf.compat.v2.keras.mixed_precision.experimental.Policy(
        'mixed_bfloat16')
    tf.compat.v2.keras.mixed_precision.experimental.set_policy(policy)

77
78
  model_builder = model_factory.model_generator(params)

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
79
80
81
  if prebuilt_strategy is not None:
    strategy = prebuilt_strategy
  else:
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
82
    strategy_config = params.strategy_config
83
84
85
    distribute_utils.configure_cluster(strategy_config.worker_hosts,
                                       strategy_config.task_index)
    strategy = distribute_utils.get_distribution_strategy(
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
86
87
88
89
90
91
92
93
94
        distribution_strategy=params.strategy_type,
        num_gpus=strategy_config.num_gpus,
        all_reduce_alg=strategy_config.all_reduce_alg,
        num_packs=strategy_config.num_packs,
        tpu_address=strategy_config.tpu)

  num_workers = int(strategy.num_replicas_in_sync + 7) // 8
  is_multi_host = (int(num_workers) >= 2)

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
95
  if mode == 'train':
96
97
98
99

    def _model_fn(params):
      return model_builder.build_model(params, mode=ModeKeys.TRAIN)

Yeqing Li's avatar
Yeqing Li committed
100
101
    logging.info(
        'Train num_replicas_in_sync %d num_workers %d is_multi_host %s',
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
102
        strategy.num_replicas_in_sync, num_workers, is_multi_host)
103

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
104
105
    dist_executor = DetectionDistributedExecutor(
        strategy=strategy,
106
107
108
        params=params,
        model_fn=_model_fn,
        loss_fn=model_builder.build_loss_fn,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
109
        is_multi_host=is_multi_host,
110
111
112
113
        predict_post_process_fn=model_builder.post_processing,
        trainable_variables_filter=model_builder
        .make_filter_trainable_variables_fn())

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
114
115
116
117
118
    if is_multi_host:
      train_input_fn = functools.partial(
          train_input_fn,
          batch_size=params.train.batch_size // strategy.num_replicas_in_sync)

119
120
121
122
123
124
    return dist_executor.train(
        train_input_fn=train_input_fn,
        model_dir=params.model_dir,
        iterations_per_loop=params.train.iterations_per_loop,
        total_steps=params.train.total_steps,
        init_checkpoint=model_builder.make_restore_checkpoint_fn(),
125
        custom_callbacks=callbacks,
126
        save_config=True)
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
127
  elif mode == 'eval' or mode == 'eval_once':
128
129
130
131

    def _model_fn(params):
      return model_builder.build_model(params, mode=ModeKeys.PREDICT_WITH_GT)

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
132
133
134
    logging.info('Eval num_replicas_in_sync %d num_workers %d is_multi_host %s',
                 strategy.num_replicas_in_sync, num_workers, is_multi_host)

Yeqing Li's avatar
Yeqing Li committed
135
136
137
    if is_multi_host:
      eval_input_fn = functools.partial(
          eval_input_fn,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
138
139
140
141
          batch_size=params.eval.batch_size // strategy.num_replicas_in_sync)

    dist_executor = DetectionDistributedExecutor(
        strategy=strategy,
142
143
144
        params=params,
        model_fn=_model_fn,
        loss_fn=model_builder.build_loss_fn,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
145
        is_multi_host=is_multi_host,
146
147
148
149
        predict_post_process_fn=model_builder.post_processing,
        trainable_variables_filter=model_builder
        .make_filter_trainable_variables_fn())

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
150
    if mode == 'eval':
Yeqing Li's avatar
Yeqing Li committed
151
152
153
154
155
156
157
158
159
      results = dist_executor.evaluate_from_model_dir(
          model_dir=params.model_dir,
          eval_input_fn=eval_input_fn,
          eval_metric_fn=model_builder.eval_metrics,
          eval_timeout=params.eval.eval_timeout,
          min_eval_interval=params.eval.min_eval_interval,
          total_steps=params.train.total_steps)
    else:
      # Run evaluation once for a single checkpoint.
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
160
161
      if not checkpoint_path:
        raise ValueError('checkpoint_path cannot be empty.')
Yeqing Li's avatar
Yeqing Li committed
162
163
164
165
166
167
168
169
      if tf.io.gfile.isdir(checkpoint_path):
        checkpoint_path = tf.train.latest_checkpoint(checkpoint_path)
      summary_writer = executor.SummaryWriter(params.model_dir, 'eval')
      results, _ = dist_executor.evaluate_checkpoint(
          checkpoint_path=checkpoint_path,
          eval_input_fn=eval_input_fn,
          eval_metric_fn=model_builder.eval_metrics,
          summary_writer=summary_writer)
170
171
172
173
    for k, v in results.items():
      logging.info('Final eval metric %s: %f', k, v)
    return results
  else:
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
174
    raise ValueError('Mode not found: %s.' % mode)
175
176


177
def run(callbacks=None):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
178
179
  keras_utils.set_session_config(enable_xla=FLAGS.enable_xla)

180
181
182
183
184
185
186
187
188
189
190
191
192
193
  params = config_factory.config_generator(FLAGS.model)

  params = params_dict.override_params_dict(
      params, FLAGS.config_file, is_strict=True)

  params = params_dict.override_params_dict(
      params, FLAGS.params_override, is_strict=True)
  params.override(
      {
          'strategy_type': FLAGS.strategy_type,
          'model_dir': FLAGS.model_dir,
          'strategy_config': executor.strategy_flags_dict(),
      },
      is_strict=False)
194
195
196
197
198
199
200
201
202
203
204
205
206
207

  # Make sure use_tpu and strategy_type are in sync.
  params.use_tpu = (params.strategy_type == 'tpu')

  if not params.use_tpu:
    params.override({
        'architecture': {
            'use_bfloat16': False,
        },
        'norm_activation': {
            'use_sync_bn': False,
        },
    }, is_strict=True)

208
209
210
211
  params.validate()
  params.lock()
  pp = pprint.PrettyPrinter()
  params_str = pp.pformat(params.as_dict())
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
212
  logging.info('Model Parameters: %s', params_str)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

  train_input_fn = None
  eval_input_fn = None
  training_file_pattern = FLAGS.training_file_pattern or params.train.train_file_pattern
  eval_file_pattern = FLAGS.eval_file_pattern or params.eval.eval_file_pattern
  if not training_file_pattern and not eval_file_pattern:
    raise ValueError('Must provide at least one of training_file_pattern and '
                     'eval_file_pattern.')

  if training_file_pattern:
    # Use global batch size for single host.
    train_input_fn = input_reader.InputFn(
        file_pattern=training_file_pattern,
        params=params,
        mode=input_reader.ModeKeys.TRAIN,
        batch_size=params.train.batch_size)

  if eval_file_pattern:
    eval_input_fn = input_reader.InputFn(
        file_pattern=eval_file_pattern,
        params=params,
        mode=input_reader.ModeKeys.PREDICT_WITH_GT,
        batch_size=params.eval.batch_size,
        num_examples=params.eval.eval_samples)
Will Cromar's avatar
Will Cromar committed
237
238
239
240
241
242
243
244
245
246
247

  if callbacks is None:
    callbacks = []

  if FLAGS.log_steps:
    callbacks.append(
        keras_utils.TimeHistory(
            batch_size=params.train.batch_size,
            log_steps=FLAGS.log_steps,
        ))

248
  return run_executor(
249
      params,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
250
251
      FLAGS.mode,
      checkpoint_path=FLAGS.checkpoint_path,
252
253
254
255
256
257
258
259
      train_input_fn=train_input_fn,
      eval_input_fn=eval_input_fn,
      callbacks=callbacks)


def main(argv):
  del argv  # Unused.

Yeqing Li's avatar
Yeqing Li committed
260
  run()
261
262
263


if __name__ == '__main__':
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
264
  tf.config.set_soft_device_placement(True)
265
  app.run(main)