maskrcnn.py 18.2 KB
Newer Older
Yeqing Li's avatar
Yeqing Li committed
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Abdullah Rashwan's avatar
Abdullah Rashwan 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
Xianzhi Du's avatar
Xianzhi Du committed
16
"""R-CNN(-RS) configuration definition."""
Abdullah Rashwan's avatar
Abdullah Rashwan committed
17

Abdullah Rashwan's avatar
Abdullah Rashwan committed
18
import dataclasses
Abdullah Rashwan's avatar
Abdullah Rashwan committed
19
import os
Xianzhi Du's avatar
Xianzhi Du committed
20
from typing import List, Optional, Union
Abdullah Rashwan's avatar
Abdullah Rashwan committed
21

22
from official.core import config_definitions as cfg
Abdullah Rashwan's avatar
Abdullah Rashwan committed
23
24
25
26
27
from official.core import exp_factory
from official.modeling import hyperparams
from official.modeling import optimization
from official.vision.beta.configs import common
from official.vision.beta.configs import decoders
Abdullah Rashwan's avatar
Abdullah Rashwan committed
28
from official.vision.beta.configs import backbones
Abdullah Rashwan's avatar
Abdullah Rashwan committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


# pylint: disable=missing-class-docstring
@dataclasses.dataclass
class Parser(hyperparams.Config):
  num_channels: int = 3
  match_threshold: float = 0.5
  unmatched_threshold: float = 0.5
  aug_rand_hflip: bool = False
  aug_scale_min: float = 1.0
  aug_scale_max: float = 1.0
  skip_crowd_during_training: bool = True
  max_num_instances: int = 100
  rpn_match_threshold: float = 0.7
  rpn_unmatched_threshold: float = 0.3
  rpn_batch_size_per_im: int = 256
  rpn_fg_fraction: float = 0.5
  mask_crop_size: int = 112


@dataclasses.dataclass
class DataConfig(cfg.DataConfig):
  """Input config for training."""
  input_path: str = ''
  global_batch_size: int = 0
  is_training: bool = False
  dtype: str = 'bfloat16'
56
  decoder: common.DataDecoder = common.DataDecoder()
Abdullah Rashwan's avatar
Abdullah Rashwan committed
57
58
  parser: Parser = Parser()
  shuffle_buffer_size: int = 10000
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
59
  file_type: str = 'tfrecord'
Abdullah Rashwan's avatar
Abdullah Rashwan committed
60
  drop_remainder: bool = True
Abdullah Rashwan's avatar
Abdullah Rashwan committed
61
62
  # Number of examples in the data set, it's used to create the annotation file.
  num_examples: int = -1
Abdullah Rashwan's avatar
Abdullah Rashwan committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86


@dataclasses.dataclass
class Anchor(hyperparams.Config):
  num_scales: int = 1
  aspect_ratios: List[float] = dataclasses.field(
      default_factory=lambda: [0.5, 1.0, 2.0])
  anchor_size: float = 8.0


@dataclasses.dataclass
class RPNHead(hyperparams.Config):
  num_convs: int = 1
  num_filters: int = 256
  use_separable_conv: bool = False


@dataclasses.dataclass
class DetectionHead(hyperparams.Config):
  num_convs: int = 4
  num_filters: int = 256
  use_separable_conv: bool = False
  num_fcs: int = 1
  fc_dims: int = 1024
Xianzhi Du's avatar
Xianzhi Du committed
87
88
89
90
  class_agnostic_bbox_pred: bool = False  # Has to be True for Cascade RCNN.
  # If additional IoUs are passed in 'cascade_iou_thresholds'
  # then ensemble the class probabilities from all heads.
  cascade_class_ensemble: bool = False
Abdullah Rashwan's avatar
Abdullah Rashwan committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115


@dataclasses.dataclass
class ROIGenerator(hyperparams.Config):
  pre_nms_top_k: int = 2000
  pre_nms_score_threshold: float = 0.0
  pre_nms_min_size_threshold: float = 0.0
  nms_iou_threshold: float = 0.7
  num_proposals: int = 1000
  test_pre_nms_top_k: int = 1000
  test_pre_nms_score_threshold: float = 0.0
  test_pre_nms_min_size_threshold: float = 0.0
  test_nms_iou_threshold: float = 0.7
  test_num_proposals: int = 1000
  use_batched_nms: bool = False


@dataclasses.dataclass
class ROISampler(hyperparams.Config):
  mix_gt_boxes: bool = True
  num_sampled_rois: int = 512
  foreground_fraction: float = 0.25
  foreground_iou_threshold: float = 0.5
  background_iou_high_threshold: float = 0.5
  background_iou_low_threshold: float = 0.0
Xianzhi Du's avatar
Xianzhi Du committed
116
117
118
  # IoU thresholds for additional FRCNN heads in Cascade mode.
  # `foreground_iou_threshold` is the first threshold.
  cascade_iou_thresholds: Optional[List[float]] = None
Abdullah Rashwan's avatar
Abdullah Rashwan committed
119
120
121
122
123
124
125
126
127
128


@dataclasses.dataclass
class ROIAligner(hyperparams.Config):
  crop_size: int = 7
  sample_offset: float = 0.5


@dataclasses.dataclass
class DetectionGenerator(hyperparams.Config):
Fan Yang's avatar
Fan Yang committed
129
  apply_nms: bool = True
Abdullah Rashwan's avatar
Abdullah Rashwan committed
130
131
132
133
  pre_nms_top_k: int = 5000
  pre_nms_score_threshold: float = 0.05
  nms_iou_threshold: float = 0.5
  max_num_detections: int = 100
Xianzhi Du's avatar
Xianzhi Du committed
134
  nms_version: str = 'v2'  # `v2`, `v1`, `batched`
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
135
  use_cpu_nms: bool = False
Xianzhi Du's avatar
Xianzhi Du committed
136
  soft_nms_sigma: Optional[float] = None  # Only works when nms_version='v1'.
Abdullah Rashwan's avatar
Abdullah Rashwan committed
137
138
139
140
141
142
143
144


@dataclasses.dataclass
class MaskHead(hyperparams.Config):
  upsample_factor: int = 2
  num_convs: int = 4
  num_filters: int = 256
  use_separable_conv: bool = False
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
145
  class_agnostic: bool = False
Abdullah Rashwan's avatar
Abdullah Rashwan committed
146
147
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201


@dataclasses.dataclass
class MaskSampler(hyperparams.Config):
  num_sampled_masks: int = 128


@dataclasses.dataclass
class MaskROIAligner(hyperparams.Config):
  crop_size: int = 14
  sample_offset: float = 0.5


@dataclasses.dataclass
class MaskRCNN(hyperparams.Config):
  num_classes: int = 0
  input_size: List[int] = dataclasses.field(default_factory=list)
  min_level: int = 2
  max_level: int = 6
  anchor: Anchor = Anchor()
  include_mask: bool = True
  backbone: backbones.Backbone = backbones.Backbone(
      type='resnet', resnet=backbones.ResNet())
  decoder: decoders.Decoder = decoders.Decoder(
      type='fpn', fpn=decoders.FPN())
  rpn_head: RPNHead = RPNHead()
  detection_head: DetectionHead = DetectionHead()
  roi_generator: ROIGenerator = ROIGenerator()
  roi_sampler: ROISampler = ROISampler()
  roi_aligner: ROIAligner = ROIAligner()
  detection_generator: DetectionGenerator = DetectionGenerator()
  mask_head: Optional[MaskHead] = MaskHead()
  mask_sampler: Optional[MaskSampler] = MaskSampler()
  mask_roi_aligner: Optional[MaskROIAligner] = MaskROIAligner()
  norm_activation: common.NormActivation = common.NormActivation(
      norm_momentum=0.997,
      norm_epsilon=0.0001,
      use_sync_bn=True)


@dataclasses.dataclass
class Losses(hyperparams.Config):
  rpn_huber_loss_delta: float = 1. / 9.
  frcnn_huber_loss_delta: float = 1.
  l2_weight_decay: float = 0.0
  rpn_score_weight: float = 1.0
  rpn_box_weight: float = 1.0
  frcnn_class_weight: float = 1.0
  frcnn_box_weight: float = 1.0
  mask_weight: float = 1.0


@dataclasses.dataclass
class MaskRCNNTask(cfg.TaskConfig):
  model: MaskRCNN = MaskRCNN()
  train_data: DataConfig = DataConfig(is_training=True)
Abdullah Rashwan's avatar
Abdullah Rashwan committed
202
203
  validation_data: DataConfig = DataConfig(is_training=False,
                                           drop_remainder=False)
Abdullah Rashwan's avatar
Abdullah Rashwan committed
204
205
  losses: Losses = Losses()
  init_checkpoint: Optional[str] = None
Xianzhi Du's avatar
Xianzhi Du committed
206
207
  init_checkpoint_modules: Union[
      str, List[str]] = 'all'  # all, backbone, and/or decoder
Abdullah Rashwan's avatar
Abdullah Rashwan committed
208
  annotation_file: Optional[str] = None
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
209
  per_category_metrics: bool = False
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
210
211
  # If set, we only use masks for the specified class IDs.
  allowed_mask_class_ids: Optional[List[int]] = None
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
212
213
214
215
  # If set, the COCO metrics will be computed.
  use_coco_metrics: bool = True
  # If set, the Waymo Open Dataset evaluator would be used.
  use_wod_metrics: bool = False
Abdullah Rashwan's avatar
Abdullah Rashwan committed
216
217
218
219
220
221
222
223
224
225


COCO_INPUT_PATH_BASE = 'coco'


@exp_factory.register_config_factory('fasterrcnn_resnetfpn_coco')
def fasterrcnn_resnetfpn_coco() -> cfg.ExperimentConfig:
  """COCO object detection with Faster R-CNN."""
  steps_per_epoch = 500
  coco_val_samples = 5000
Xianzhi Du's avatar
Xianzhi Du committed
226
227
  train_batch_size = 64
  eval_batch_size = 8
Abdullah Rashwan's avatar
Abdullah Rashwan committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

  config = cfg.ExperimentConfig(
      runtime=cfg.RuntimeConfig(mixed_precision_dtype='bfloat16'),
      task=MaskRCNNTask(
          init_checkpoint='gs://cloud-tpu-checkpoints/vision-2.0/resnet50_imagenet/ckpt-28080',
          init_checkpoint_modules='backbone',
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
          model=MaskRCNN(
              num_classes=91,
              input_size=[1024, 1024, 3],
              include_mask=False,
              mask_head=None,
              mask_sampler=None,
              mask_roi_aligner=None),
          losses=Losses(l2_weight_decay=0.00004),
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
              is_training=True,
Xianzhi Du's avatar
Xianzhi Du committed
247
              global_batch_size=train_batch_size,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
248
249
250
251
252
              parser=Parser(
                  aug_rand_hflip=True, aug_scale_min=0.8, aug_scale_max=1.25)),
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
              is_training=False,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
253
254
              global_batch_size=eval_batch_size,
              drop_remainder=False)),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
255
256
      trainer=cfg.TrainerConfig(
          train_steps=22500,
Xianzhi Du's avatar
Xianzhi Du committed
257
          validation_steps=coco_val_samples // eval_batch_size,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
          validation_interval=steps_per_epoch,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'sgd',
                  'sgd': {
                      'momentum': 0.9
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [15000, 20000],
                      'values': [0.12, 0.012, 0.0012],
                  }
              },
              'warmup': {
                  'type': 'linear',
                  'linear': {
                      'warmup_steps': 500,
                      'warmup_learning_rate': 0.0067
                  }
              }
          })),
      restrictions=[
          'task.train_data.is_training != None',
          'task.validation_data.is_training != None'
      ])
  return config


@exp_factory.register_config_factory('maskrcnn_resnetfpn_coco')
def maskrcnn_resnetfpn_coco() -> cfg.ExperimentConfig:
  """COCO object detection with Mask R-CNN."""
  steps_per_epoch = 500
  coco_val_samples = 5000
Xianzhi Du's avatar
Xianzhi Du committed
296
297
  train_batch_size = 64
  eval_batch_size = 8
Abdullah Rashwan's avatar
Abdullah Rashwan committed
298

Xianzhi Du's avatar
Xianzhi Du committed
299
  config = cfg.ExperimentConfig(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
300
301
      runtime=cfg.RuntimeConfig(
          mixed_precision_dtype='bfloat16', enable_xla=True),
Xianzhi Du's avatar
Xianzhi Du committed
302
303
304
305
306
307
308
309
310
311
312
      task=MaskRCNNTask(
          init_checkpoint='gs://cloud-tpu-checkpoints/vision-2.0/resnet50_imagenet/ckpt-28080',
          init_checkpoint_modules='backbone',
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
          model=MaskRCNN(
              num_classes=91, input_size=[1024, 1024, 3], include_mask=True),
          losses=Losses(l2_weight_decay=0.00004),
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
              is_training=True,
Xianzhi Du's avatar
Xianzhi Du committed
313
              global_batch_size=train_batch_size,
Xianzhi Du's avatar
Xianzhi Du committed
314
315
316
317
318
              parser=Parser(
                  aug_rand_hflip=True, aug_scale_min=0.8, aug_scale_max=1.25)),
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
              is_training=False,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
319
320
              global_batch_size=eval_batch_size,
              drop_remainder=False)),
Xianzhi Du's avatar
Xianzhi Du committed
321
322
      trainer=cfg.TrainerConfig(
          train_steps=22500,
Xianzhi Du's avatar
Xianzhi Du committed
323
          validation_steps=coco_val_samples // eval_batch_size,
Xianzhi Du's avatar
Xianzhi Du committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
          validation_interval=steps_per_epoch,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'sgd',
                  'sgd': {
                      'momentum': 0.9
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [15000, 20000],
                      'values': [0.12, 0.012, 0.0012],
                  }
              },
              'warmup': {
                  'type': 'linear',
                  'linear': {
                      'warmup_steps': 500,
                      'warmup_learning_rate': 0.0067
                  }
              }
          })),
      restrictions=[
          'task.train_data.is_training != None',
          'task.validation_data.is_training != None'
      ])
  return config


Xianzhi Du's avatar
Xianzhi Du committed
357
358
359
360
@exp_factory.register_config_factory('maskrcnn_spinenet_coco')
def maskrcnn_spinenet_coco() -> cfg.ExperimentConfig:
  """COCO object detection with Mask R-CNN with SpineNet backbone."""
  steps_per_epoch = 463
Xianzhi Du's avatar
Xianzhi Du committed
361
  coco_val_samples = 5000
Xianzhi Du's avatar
Xianzhi Du committed
362
363
  train_batch_size = 256
  eval_batch_size = 8
Xianzhi Du's avatar
Xianzhi Du committed
364

Abdullah Rashwan's avatar
Abdullah Rashwan committed
365
366
367
368
369
370
  config = cfg.ExperimentConfig(
      runtime=cfg.RuntimeConfig(mixed_precision_dtype='bfloat16'),
      task=MaskRCNNTask(
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
          model=MaskRCNN(
Xianzhi Du's avatar
Xianzhi Du committed
371
372
373
374
375
376
377
378
379
380
381
              backbone=backbones.Backbone(
                  type='spinenet',
                  spinenet=backbones.SpineNet(
                      model_id='49',
                      min_level=3,
                      max_level=7,
                  )),
              decoder=decoders.Decoder(
                  type='identity', identity=decoders.Identity()),
              anchor=Anchor(anchor_size=3),
              norm_activation=common.NormActivation(use_sync_bn=True),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
382
              num_classes=91,
Xianzhi Du's avatar
Xianzhi Du committed
383
384
385
386
              input_size=[640, 640, 3],
              min_level=3,
              max_level=7,
              include_mask=True),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
387
388
389
390
          losses=Losses(l2_weight_decay=0.00004),
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
              is_training=True,
Xianzhi Du's avatar
Xianzhi Du committed
391
              global_batch_size=train_batch_size,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
392
              parser=Parser(
Xianzhi Du's avatar
Xianzhi Du committed
393
                  aug_rand_hflip=True, aug_scale_min=0.5, aug_scale_max=2.0)),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
394
395
396
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
              is_training=False,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
397
398
              global_batch_size=eval_batch_size,
              drop_remainder=False)),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
399
      trainer=cfg.TrainerConfig(
Xianzhi Du's avatar
Xianzhi Du committed
400
401
          train_steps=steps_per_epoch * 350,
          validation_steps=coco_val_samples // eval_batch_size,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
          validation_interval=steps_per_epoch,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'sgd',
                  'sgd': {
                      'momentum': 0.9
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
Xianzhi Du's avatar
Xianzhi Du committed
416
417
418
419
                      'boundaries': [
                          steps_per_epoch * 320, steps_per_epoch * 340
                      ],
                      'values': [0.32, 0.032, 0.0032],
Abdullah Rashwan's avatar
Abdullah Rashwan committed
420
421
422
423
424
                  }
              },
              'warmup': {
                  'type': 'linear',
                  'linear': {
Xianzhi Du's avatar
Xianzhi Du committed
425
                      'warmup_steps': 2000,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
426
427
428
429
430
431
                      'warmup_learning_rate': 0.0067
                  }
              }
          })),
      restrictions=[
          'task.train_data.is_training != None',
Xianzhi Du's avatar
Xianzhi Du committed
432
433
434
          'task.validation_data.is_training != None',
          'task.model.min_level == task.model.backbone.spinenet.min_level',
          'task.model.max_level == task.model.backbone.spinenet.max_level',
Abdullah Rashwan's avatar
Abdullah Rashwan committed
435
436
437
438
      ])
  return config


Xianzhi Du's avatar
Xianzhi Du committed
439
440
@exp_factory.register_config_factory('cascadercnn_spinenet_coco')
def cascadercnn_spinenet_coco() -> cfg.ExperimentConfig:
Xianzhi Du's avatar
Xianzhi Du committed
441
  """COCO object detection with Cascade RCNN-RS with SpineNet backbone."""
Abdullah Rashwan's avatar
Abdullah Rashwan committed
442
443
  steps_per_epoch = 463
  coco_val_samples = 5000
Xianzhi Du's avatar
Xianzhi Du committed
444
445
  train_batch_size = 256
  eval_batch_size = 8
Abdullah Rashwan's avatar
Abdullah Rashwan committed
446
447
448
449
450
451
452
453

  config = cfg.ExperimentConfig(
      runtime=cfg.RuntimeConfig(mixed_precision_dtype='bfloat16'),
      task=MaskRCNNTask(
          annotation_file=os.path.join(COCO_INPUT_PATH_BASE,
                                       'instances_val2017.json'),
          model=MaskRCNN(
              backbone=backbones.Backbone(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
454
455
456
457
458
459
                  type='spinenet',
                  spinenet=backbones.SpineNet(
                      model_id='49',
                      min_level=3,
                      max_level=7,
                  )),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
460
461
              decoder=decoders.Decoder(
                  type='identity', identity=decoders.Identity()),
Xianzhi Du's avatar
Xianzhi Du committed
462
463
464
              roi_sampler=ROISampler(cascade_iou_thresholds=[0.6, 0.7]),
              detection_head=DetectionHead(
                  class_agnostic_bbox_pred=True, cascade_class_ensemble=True),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
465
              anchor=Anchor(anchor_size=3),
Xianzhi Du's avatar
Xianzhi Du committed
466
467
              norm_activation=common.NormActivation(
                  use_sync_bn=True, activation='swish'),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
468
469
470
471
472
473
474
475
476
              num_classes=91,
              input_size=[640, 640, 3],
              min_level=3,
              max_level=7,
              include_mask=True),
          losses=Losses(l2_weight_decay=0.00004),
          train_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'train*'),
              is_training=True,
Xianzhi Du's avatar
Xianzhi Du committed
477
              global_batch_size=train_batch_size,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
478
              parser=Parser(
Xianzhi Du's avatar
Xianzhi Du committed
479
                  aug_rand_hflip=True, aug_scale_min=0.1, aug_scale_max=2.5)),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
480
481
482
          validation_data=DataConfig(
              input_path=os.path.join(COCO_INPUT_PATH_BASE, 'val*'),
              is_training=False,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
483
484
              global_batch_size=eval_batch_size,
              drop_remainder=False)),
Abdullah Rashwan's avatar
Abdullah Rashwan committed
485
      trainer=cfg.TrainerConfig(
Xianzhi Du's avatar
Xianzhi Du committed
486
487
          train_steps=steps_per_epoch * 500,
          validation_steps=coco_val_samples // eval_batch_size,
Abdullah Rashwan's avatar
Abdullah Rashwan committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
          validation_interval=steps_per_epoch,
          steps_per_loop=steps_per_epoch,
          summary_interval=steps_per_epoch,
          checkpoint_interval=steps_per_epoch,
          optimizer_config=optimization.OptimizationConfig({
              'optimizer': {
                  'type': 'sgd',
                  'sgd': {
                      'momentum': 0.9
                  }
              },
              'learning_rate': {
                  'type': 'stepwise',
                  'stepwise': {
                      'boundaries': [
Xianzhi Du's avatar
Xianzhi Du committed
503
                          steps_per_epoch * 475, steps_per_epoch * 490
Abdullah Rashwan's avatar
Abdullah Rashwan committed
504
                      ],
Xianzhi Du's avatar
Xianzhi Du committed
505
                      'values': [0.32, 0.032, 0.0032],
Abdullah Rashwan's avatar
Abdullah Rashwan committed
506
507
508
509
510
511
512
513
514
515
516
517
                  }
              },
              'warmup': {
                  'type': 'linear',
                  'linear': {
                      'warmup_steps': 2000,
                      'warmup_learning_rate': 0.0067
                  }
              }
          })),
      restrictions=[
          'task.train_data.is_training != None',
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
518
          'task.validation_data.is_training != None',
Xianzhi Du's avatar
Xianzhi Du committed
519
520
          'task.model.min_level == task.model.backbone.spinenet.min_level',
          'task.model.max_level == task.model.backbone.spinenet.max_level',
Abdullah Rashwan's avatar
Abdullah Rashwan committed
521
522
      ])
  return config