retinanet_config.py 5.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Copyright 2019 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.
# ==============================================================================
"""Config template to train Retinanet."""

# pylint: disable=line-too-long

# For ResNet-50, this freezes the variables of the first conv1 and conv2_x
# layers [1], which leads to higher training speed and slightly better testing
# accuracy. The intuition is that the low-level architecture (e.g., ResNet-50)
# is able to capture low-level features such as edges; therefore, it does not
# need to be fine-tuned for the detection task.
# Note that we need to trailing `/` to avoid the incorrect match.
# [1]: https://github.com/facebookresearch/Detectron/blob/master/detectron/core/config.py#L198
Yeqing Li's avatar
Yeqing Li committed
26
RESNET_FROZEN_VAR_PREFIX = r'(resnet\d+)\/(conv2d(|_([1-9]|10))|batch_normalization(|_([1-9]|10)))\/'
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
27
REGULARIZATION_VAR_REGEX = r'.*(kernel|weight):0$'
28
29
30
31
32
33

# pylint: disable=line-too-long
RETINANET_CFG = {
    'type': 'retinanet',
    'model_dir': '',
    'use_tpu': True,
Yeqing Li's avatar
Yeqing Li committed
34
    'strategy_type': 'tpu',
35
36
37
38
39
40
41
    'train': {
        'batch_size': 64,
        'iterations_per_loop': 500,
        'total_steps': 22500,
        'optimizer': {
            'type': 'momentum',
            'momentum': 0.9,
Yeqing Li's avatar
Yeqing Li committed
42
            'nesterov': True,  # `False` is better for TPU v3-128.
43
44
45
46
47
48
49
50
51
52
53
54
55
        },
        'learning_rate': {
            'type': 'step',
            'warmup_learning_rate': 0.0067,
            'warmup_steps': 500,
            'init_learning_rate': 0.08,
            'learning_rate_levels': [0.008, 0.0008],
            'learning_rate_steps': [15000, 20000],
        },
        'checkpoint': {
            'path': '',
            'prefix': '',
        },
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
56
        'frozen_variable_prefix': RESNET_FROZEN_VAR_PREFIX,
57
58
59
        'train_file_pattern': '',
        # TODO(b/142174042): Support transpose_input option.
        'transpose_input': False,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
60
        'regularization_variable_regex': REGULARIZATION_VAR_REGEX,
61
        'l2_weight_decay': 0.0001,
Yeqing Li's avatar
Yeqing Li committed
62
        'input_sharding': False,
63
64
65
66
67
68
69
70
71
    },
    'eval': {
        'batch_size': 8,
        'min_eval_interval': 180,
        'eval_timeout': None,
        'eval_samples': 5000,
        'type': 'box',
        'val_json_file': '',
        'eval_file_pattern': '',
Yeqing Li's avatar
Yeqing Li committed
72
        'input_sharding': True,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
73
74
75
        # When visualizing images, set evaluation batch size to 40 to avoid
        # potential OOM.
        'num_images_to_visualize': 0,
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
109
110
111
112
113
114
115
116
117
118
119
    },
    'predict': {
        'predict_batch_size': 8,
    },
    'architecture': {
        'parser': 'retinanet_parser',
        'backbone': 'resnet',
        'multilevel_features': 'fpn',
        'use_bfloat16': False,
    },
    'anchor': {
        'min_level': 3,
        'max_level': 7,
        'num_scales': 3,
        'aspect_ratios': [1.0, 2.0, 0.5],
        'anchor_size': 4.0,
    },
    'retinanet_parser': {
        'use_bfloat16': False,
        'output_size': [640, 640],
        'num_channels': 3,
        'match_threshold': 0.5,
        'unmatched_threshold': 0.5,
        'aug_rand_hflip': True,
        'aug_scale_min': 1.0,
        'aug_scale_max': 1.0,
        'use_autoaugment': False,
        'autoaugment_policy_name': 'v0',
        'skip_crowd_during_training': True,
        'max_num_instances': 100,
    },
    'resnet': {
        'resnet_depth': 50,
        'batch_norm': {
            'batch_norm_momentum': 0.997,
            'batch_norm_epsilon': 1e-4,
            'batch_norm_trainable': True,
        },
    },
    'fpn': {
        'min_level': 3,
        'max_level': 7,
        'fpn_feat_dims': 256,
        'use_separable_conv': False,
Yeqing Li's avatar
Yeqing Li committed
120
        'use_batch_norm': True,
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
147
148
149
150
151
152
153
154
155
        'batch_norm': {
            'batch_norm_momentum': 0.997,
            'batch_norm_epsilon': 1e-4,
            'batch_norm_trainable': True,
        },
    },
    'retinanet_head': {
        'min_level': 3,
        'max_level': 7,
        # Note that `num_classes` is the total number of classes including
        # one background classes whose index is 0.
        'num_classes': 91,
        'anchors_per_location': 9,
        'retinanet_head_num_convs': 4,
        'retinanet_head_num_filters': 256,
        'use_separable_conv': False,
        'batch_norm': {
            'batch_norm_momentum': 0.997,
            'batch_norm_epsilon': 1e-4,
            'batch_norm_trainable': True,
        },
    },
    'retinanet_loss': {
        'num_classes': 91,
        'focal_loss_alpha': 0.25,
        'focal_loss_gamma': 1.5,
        'huber_loss_delta': 0.1,
        'box_loss_weight': 50,
    },
    'postprocess': {
        'use_batched_nms': False,
        'min_level': 3,
        'max_level': 7,
        'max_total_size': 100,
        'nms_iou_threshold': 0.5,
Yeqing Li's avatar
Yeqing Li committed
156
157
        'score_threshold': 0.05,
        'pre_nms_num_boxes': 5000,
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    },
    'enable_summary': False,
}

RETINANET_RESTRICTIONS = [
    'architecture.use_bfloat16 == retinanet_parser.use_bfloat16',
    'anchor.min_level == retinanet_head.min_level',
    'anchor.max_level == retinanet_head.max_level',
    'anchor.min_level == postprocess.min_level',
    'anchor.max_level == postprocess.max_level',
    'retinanet_head.num_classes == retinanet_loss.num_classes',
]

# pylint: enable=line-too-long