detr.py 9.58 KB
Newer Older
Frederick Liu's avatar
Frederick Liu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright 2022 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.

"""DETR configurations."""

import dataclasses
Gunho Park's avatar
Gunho Park committed
18
19
20
import os
from typing import List, Optional, Union

Frederick Liu's avatar
Frederick Liu committed
21
22
from official.core import config_definitions as cfg
from official.core import exp_factory
Gunho Park's avatar
Gunho Park committed
23
from official.modeling import hyperparams
Frederick Liu's avatar
Frederick Liu committed
24
25
from official.projects.detr import optimization
from official.projects.detr.dataloaders import coco
26
27
from official.vision.configs import backbones
from official.vision.configs import common
Frederick Liu's avatar
Frederick Liu committed
28
29
30


@dataclasses.dataclass
Gunho Park's avatar
Gunho Park committed
31
32
33
class DataConfig(cfg.DataConfig):
  """Input config for training."""
  input_path: str = ''
34
35
  tfds_name: str = ''
  tfds_split: str = 'train'
Gunho Park's avatar
Gunho Park committed
36
37
38
39
40
41
  global_batch_size: int = 0
  is_training: bool = False
  dtype: str = 'bfloat16'
  decoder: common.DataDecoder = common.DataDecoder()
  shuffle_buffer_size: int = 10000
  file_type: str = 'tfrecord'
42
43
  drop_remainder: bool = True

Gunho Park's avatar
Gunho Park committed
44

Frederick Liu's avatar
Frederick Liu committed
45
@dataclasses.dataclass
Gunho Park's avatar
Gunho Park committed
46
class Losses(hyperparams.Config):
Gunho Park's avatar
Gunho Park committed
47
  class_offset: int = 0
Frederick Liu's avatar
Frederick Liu committed
48
49
50
51
  lambda_cls: float = 1.0
  lambda_box: float = 5.0
  lambda_giou: float = 2.0
  background_cls_weight: float = 0.1
Gunho Park's avatar
Gunho Park committed
52
  l2_weight_decay: float = 1e-4
Frederick Liu's avatar
Frederick Liu committed
53

54

Gunho Park's avatar
Gunho Park committed
55
56
@dataclasses.dataclass
class Detr(hyperparams.Config):
57
  """Detr model definations."""
Gunho Park's avatar
Gunho Park committed
58
59
  num_queries: int = 100
  hidden_size: int = 256
Gunho Park's avatar
Gunho Park committed
60
  num_classes: int = 91  # 0: background
Frederick Liu's avatar
Frederick Liu committed
61
62
  num_encoder_layers: int = 6
  num_decoder_layers: int = 6
Gunho Park's avatar
Gunho Park committed
63
64
  input_size: List[int] = dataclasses.field(default_factory=list)
  backbone: backbones.Backbone = backbones.Backbone(
65
      type='resnet', resnet=backbones.ResNet(model_id=50, bn_trainable=False))
Gunho Park's avatar
Gunho Park committed
66
  norm_activation: common.NormActivation = common.NormActivation()
67
  backbone_endpoint_name: str = '5'
Frederick Liu's avatar
Frederick Liu committed
68

69

Gunho Park's avatar
Gunho Park committed
70
71
72
73
74
75
76
@dataclasses.dataclass
class DetrTask(cfg.TaskConfig):
  model: Detr = Detr()
  train_data: cfg.DataConfig = cfg.DataConfig()
  validation_data: cfg.DataConfig = cfg.DataConfig()
  losses: Losses = Losses()
  init_checkpoint: Optional[str] = None
77
  init_checkpoint_modules: Union[str, List[str]] = 'all'  # all, backbone
Gunho Park's avatar
Gunho Park committed
78
  annotation_file: Optional[str] = None
Frederick Liu's avatar
Frederick Liu committed
79
80
81
  per_category_metrics: bool = False


82
83
84
85
86
COCO_INPUT_PATH_BASE = 'coco'
COCO_TRAIN_EXAMPLES = 118287
COCO_VAL_EXAMPLES = 5000


Frederick Liu's avatar
Frederick Liu committed
87
88
89
90
91
@exp_factory.register_config_factory('detr_coco')
def detr_coco() -> cfg.ExperimentConfig:
  """Config to get results that matches the paper."""
  train_batch_size = 64
  eval_batch_size = 64
92
  num_train_data = COCO_TRAIN_EXAMPLES
Frederick Liu's avatar
Frederick Liu committed
93
94
95
96
  num_steps_per_epoch = num_train_data // train_batch_size
  train_steps = 500 * num_steps_per_epoch  # 500 epochs
  decay_at = train_steps - 100 * num_steps_per_epoch  # 400 epochs
  config = cfg.ExperimentConfig(
97
      task=DetrTask(
98
          init_checkpoint='',
99
100
101
102
          init_checkpoint_modules='backbone',
          model=Detr(
              num_classes=81,
              input_size=[1333, 1333, 3],
Gunho Park's avatar
Gunho Park committed
103
              norm_activation=common.NormActivation()),
104
          losses=Losses(),
Frederick Liu's avatar
Frederick Liu committed
105
106
107
108
109
110
111
112
113
114
115
116
          train_data=coco.COCODataConfig(
              tfds_name='coco/2017',
              tfds_split='train',
              is_training=True,
              global_batch_size=train_batch_size,
              shuffle_buffer_size=1000,
          ),
          validation_data=coco.COCODataConfig(
              tfds_name='coco/2017',
              tfds_split='validation',
              is_training=False,
              global_batch_size=eval_batch_size,
117
              drop_remainder=False)),
Frederick Liu's avatar
Frederick Liu committed
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
      trainer=cfg.TrainerConfig(
          train_steps=train_steps,
          validation_steps=-1,
          steps_per_loop=10000,
          summary_interval=10000,
          checkpoint_interval=10000,
          validation_interval=10000,
          max_to_keep=1,
          best_checkpoint_export_subdir='best_ckpt',
          best_checkpoint_eval_metric='AP',
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'detr_adamw',
                  'detr_adamw': {
                      'weight_decay_rate': 1e-4,
                      'global_clipnorm': 0.1,
                      # Avoid AdamW legacy behavior.
                      'gradient_clip_norm': 0.0
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [decay_at],
                      'values': [0.0001, 1.0e-05]
                  }
              },
145
          })),
146
147
148
149
150
      restrictions=[
          'task.train_data.is_training != None',
      ])
  return config

Frederick Liu's avatar
Frederick Liu committed
151

152
@exp_factory.register_config_factory('detr_coco_tfrecord')
153
def detr_coco_tfrecord() -> cfg.ExperimentConfig:
Frederick Liu's avatar
Frederick Liu committed
154
  """Config to get results that matches the paper."""
Gunho Park's avatar
Gunho Park committed
155
  train_batch_size = 64
Frederick Liu's avatar
Frederick Liu committed
156
  eval_batch_size = 64
Gunho Park's avatar
Gunho Park committed
157
  steps_per_epoch = COCO_TRAIN_EXAMPLES // train_batch_size
Gunho Park's avatar
Gunho Park committed
158
159
  train_steps = 300 * steps_per_epoch  # 300 epochs
  decay_at = train_steps - 100 * steps_per_epoch  # 200 epochs
Frederick Liu's avatar
Frederick Liu committed
160
  config = cfg.ExperimentConfig(
Gunho Park's avatar
Gunho Park committed
161
      task=DetrTask(
162
          init_checkpoint='',
Gunho Park's avatar
Gunho Park committed
163
          init_checkpoint_modules='backbone',
Gunho Park's avatar
Gunho Park committed
164
165
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
Gunho Park's avatar
Gunho Park committed
166
167
          model=Detr(
              input_size=[1333, 1333, 3],
Gunho Park's avatar
Gunho Park committed
168
              norm_activation=common.NormActivation()),
Gunho Park's avatar
Gunho Park committed
169
          losses=Losses(),
Gunho Park's avatar
Gunho Park committed
170
171
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
Frederick Liu's avatar
Frederick Liu committed
172
173
174
              is_training=True,
              global_batch_size=train_batch_size,
              shuffle_buffer_size=1000,
Frederick Liu's avatar
Frederick Liu committed
175
          ),
Gunho Park's avatar
Gunho Park committed
176
177
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
Frederick Liu's avatar
Frederick Liu committed
178
179
              is_training=False,
              global_batch_size=eval_batch_size,
Gunho Park's avatar
Gunho Park committed
180
              drop_remainder=False,
181
          )),
Frederick Liu's avatar
Frederick Liu committed
182
183
      trainer=cfg.TrainerConfig(
          train_steps=train_steps,
Gunho Park's avatar
Gunho Park committed
184
185
186
187
          validation_steps=COCO_VAL_EXAMPLES // eval_batch_size,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
188
          validation_interval=5 * steps_per_epoch,
Frederick Liu's avatar
Frederick Liu committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
          max_to_keep=1,
          best_checkpoint_export_subdir='best_ckpt',
          best_checkpoint_eval_metric='AP',
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'detr_adamw',
                  'detr_adamw': {
                      'weight_decay_rate': 1e-4,
                      'global_clipnorm': 0.1,
                      # Avoid AdamW legacy behavior.
                      'gradient_clip_norm': 0.0
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [decay_at],
                      'values': [0.0001, 1.0e-05]
                  }
              },
209
          })),
Frederick Liu's avatar
Frederick Liu committed
210
211
212
213
      restrictions=[
          'task.train_data.is_training != None',
      ])
  return config
Gunho Park's avatar
Gunho Park committed
214

215

Gunho Park's avatar
Gunho Park committed
216
@exp_factory.register_config_factory('detr_coco_tfds')
217
def detr_coco_tfds() -> cfg.ExperimentConfig:
Gunho Park's avatar
Gunho Park committed
218
219
220
221
222
223
224
225
  """Config to get results that matches the paper."""
  train_batch_size = 64
  eval_batch_size = 64
  steps_per_epoch = COCO_TRAIN_EXAMPLES // train_batch_size
  train_steps = 300 * steps_per_epoch  # 300 epochs
  decay_at = train_steps - 100 * steps_per_epoch  # 200 epochs
  config = cfg.ExperimentConfig(
      task=DetrTask(
226
          init_checkpoint='',
Gunho Park's avatar
Gunho Park committed
227
228
229
230
          init_checkpoint_modules='backbone',
          model=Detr(
              num_classes=81,
              input_size=[1333, 1333, 3],
Gunho Park's avatar
Gunho Park committed
231
              norm_activation=common.NormActivation()),
232
          losses=Losses(class_offset=1),
Gunho Park's avatar
Gunho Park committed
233
234
235
236
237
238
239
240
241
242
243
244
          train_data=DataConfig(
              tfds_name='coco/2017',
              tfds_split='train',
              is_training=True,
              global_batch_size=train_batch_size,
              shuffle_buffer_size=1000,
          ),
          validation_data=DataConfig(
              tfds_name='coco/2017',
              tfds_split='validation',
              is_training=False,
              global_batch_size=eval_batch_size,
245
              drop_remainder=False)),
Gunho Park's avatar
Gunho Park committed
246
247
248
249
250
251
      trainer=cfg.TrainerConfig(
          train_steps=train_steps,
          validation_steps=COCO_VAL_EXAMPLES // eval_batch_size,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
252
          validation_interval=5 * steps_per_epoch,
Gunho Park's avatar
Gunho Park committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
          max_to_keep=1,
          best_checkpoint_export_subdir='best_ckpt',
          best_checkpoint_eval_metric='AP',
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'detr_adamw',
                  'detr_adamw': {
                      'weight_decay_rate': 1e-4,
                      'global_clipnorm': 0.1,
                      # Avoid AdamW legacy behavior.
                      'gradient_clip_norm': 0.0
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [decay_at],
                      'values': [0.0001, 1.0e-05]
                  }
              },
273
          })),
Frederick Liu's avatar
Frederick Liu committed
274
275
276
277
      restrictions=[
          'task.train_data.is_training != None',
      ])
  return config