model_builder.py 54.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Copyright 2017 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.
# ==============================================================================

"""A function to build a DetectionModel from configuration."""
17

18
import functools
19
import sys
Vighnesh Birodkar's avatar
Vighnesh Birodkar committed
20
21
22

from absl import logging

23
24
25
26
27
28
29
30
31
from object_detection.builders import anchor_generator_builder
from object_detection.builders import box_coder_builder
from object_detection.builders import box_predictor_builder
from object_detection.builders import hyperparams_builder
from object_detection.builders import image_resizer_builder
from object_detection.builders import losses_builder
from object_detection.builders import matcher_builder
from object_detection.builders import post_processing_builder
from object_detection.builders import region_similarity_calculator_builder as sim_calc
32
from object_detection.core import balanced_positive_negative_sampler as sampler
33
from object_detection.core import post_processing
34
from object_detection.core import target_assigner
35
36
from object_detection.meta_architectures import center_net_meta_arch
from object_detection.meta_architectures import context_rcnn_meta_arch
37
from object_detection.meta_architectures import deepmac_meta_arch
38
39
40
from object_detection.meta_architectures import faster_rcnn_meta_arch
from object_detection.meta_architectures import rfcn_meta_arch
from object_detection.meta_architectures import ssd_meta_arch
41
from object_detection.predictors.heads import mask_head
42
from object_detection.protos import losses_pb2
43
from object_detection.protos import model_pb2
44
from object_detection.utils import label_map_util
45
from object_detection.utils import ops
46
from object_detection.utils import spatial_transform_ops as spatial_ops
47
48
49
50
51
52
53
54
55
from object_detection.utils import tf_version

## Feature Extractors for TF
## This section conditionally imports different feature extractors based on the
## Tensorflow version.
##
# pylint: disable=g-import-not-at-top
if tf_version.is_tf2():
  from object_detection.models import center_net_hourglass_feature_extractor
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
56
  from object_detection.models import center_net_mobilenet_v2_feature_extractor
57
  from object_detection.models import center_net_mobilenet_v2_fpn_feature_extractor
58
  from object_detection.models import center_net_resnet_feature_extractor
59
  from object_detection.models import center_net_resnet_v1_fpn_feature_extractor
60
61
62
  from object_detection.models import faster_rcnn_inception_resnet_v2_keras_feature_extractor as frcnn_inc_res_keras
  from object_detection.models import faster_rcnn_resnet_keras_feature_extractor as frcnn_resnet_keras
  from object_detection.models import ssd_resnet_v1_fpn_keras_feature_extractor as ssd_resnet_v1_fpn_keras
63
  from object_detection.models import faster_rcnn_resnet_v1_fpn_keras_feature_extractor as frcnn_resnet_fpn_keras
64
65
66
67
68
  from object_detection.models.ssd_mobilenet_v1_fpn_keras_feature_extractor import SSDMobileNetV1FpnKerasFeatureExtractor
  from object_detection.models.ssd_mobilenet_v1_keras_feature_extractor import SSDMobileNetV1KerasFeatureExtractor
  from object_detection.models.ssd_mobilenet_v2_fpn_keras_feature_extractor import SSDMobileNetV2FpnKerasFeatureExtractor
  from object_detection.models.ssd_mobilenet_v2_keras_feature_extractor import SSDMobileNetV2KerasFeatureExtractor
  from object_detection.predictors import rfcn_keras_box_predictor
69
70
  if sys.version_info[0] >= 3:
    from object_detection.models import ssd_efficientnet_bifpn_feature_extractor as ssd_efficientnet_bifpn
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

if tf_version.is_tf1():
  from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
  from object_detection.models import faster_rcnn_inception_v2_feature_extractor as frcnn_inc_v2
  from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_nas
  from object_detection.models import faster_rcnn_pnas_feature_extractor as frcnn_pnas
  from object_detection.models import faster_rcnn_resnet_v1_feature_extractor as frcnn_resnet_v1
  from object_detection.models import ssd_resnet_v1_fpn_feature_extractor as ssd_resnet_v1_fpn
  from object_detection.models import ssd_resnet_v1_ppn_feature_extractor as ssd_resnet_v1_ppn
  from object_detection.models.embedded_ssd_mobilenet_v1_feature_extractor import EmbeddedSSDMobileNetV1FeatureExtractor
  from object_detection.models.ssd_inception_v2_feature_extractor import SSDInceptionV2FeatureExtractor
  from object_detection.models.ssd_mobilenet_v2_fpn_feature_extractor import SSDMobileNetV2FpnFeatureExtractor
  from object_detection.models.ssd_mobilenet_v2_mnasfpn_feature_extractor import SSDMobileNetV2MnasFPNFeatureExtractor
  from object_detection.models.ssd_inception_v3_feature_extractor import SSDInceptionV3FeatureExtractor
  from object_detection.models.ssd_mobilenet_edgetpu_feature_extractor import SSDMobileNetEdgeTPUFeatureExtractor
  from object_detection.models.ssd_mobilenet_v1_feature_extractor import SSDMobileNetV1FeatureExtractor
  from object_detection.models.ssd_mobilenet_v1_fpn_feature_extractor import SSDMobileNetV1FpnFeatureExtractor
  from object_detection.models.ssd_mobilenet_v1_ppn_feature_extractor import SSDMobileNetV1PpnFeatureExtractor
  from object_detection.models.ssd_mobilenet_v2_feature_extractor import SSDMobileNetV2FeatureExtractor
  from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3LargeFeatureExtractor
  from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3SmallFeatureExtractor
92
93
94
  from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetCPUFeatureExtractor
  from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetDSPFeatureExtractor
  from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetEdgeTPUFeatureExtractor
95
  from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetGPUFeatureExtractor
96
  from object_detection.models.ssd_spaghettinet_feature_extractor import SSDSpaghettinetFeatureExtractor
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  from object_detection.models.ssd_pnasnet_feature_extractor import SSDPNASNetFeatureExtractor
  from object_detection.predictors import rfcn_box_predictor
# pylint: enable=g-import-not-at-top

if tf_version.is_tf2():
  SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = {
      'ssd_mobilenet_v1_keras': SSDMobileNetV1KerasFeatureExtractor,
      'ssd_mobilenet_v1_fpn_keras': SSDMobileNetV1FpnKerasFeatureExtractor,
      'ssd_mobilenet_v2_keras': SSDMobileNetV2KerasFeatureExtractor,
      'ssd_mobilenet_v2_fpn_keras': SSDMobileNetV2FpnKerasFeatureExtractor,
      'ssd_resnet50_v1_fpn_keras':
          ssd_resnet_v1_fpn_keras.SSDResNet50V1FpnKerasFeatureExtractor,
      'ssd_resnet101_v1_fpn_keras':
          ssd_resnet_v1_fpn_keras.SSDResNet101V1FpnKerasFeatureExtractor,
      'ssd_resnet152_v1_fpn_keras':
          ssd_resnet_v1_fpn_keras.SSDResNet152V1FpnKerasFeatureExtractor,
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
      'ssd_efficientnet-b0_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB0BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b1_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB1BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b2_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB2BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b3_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB3BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b4_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB4BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b5_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB5BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b6_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB6BiFPNKerasFeatureExtractor,
      'ssd_efficientnet-b7_bifpn_keras':
          ssd_efficientnet_bifpn.SSDEfficientNetB7BiFPNKerasFeatureExtractor,
129
  }
130

131
132
133
134
135
136
137
138
139
  FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = {
      'faster_rcnn_resnet50_keras':
          frcnn_resnet_keras.FasterRCNNResnet50KerasFeatureExtractor,
      'faster_rcnn_resnet101_keras':
          frcnn_resnet_keras.FasterRCNNResnet101KerasFeatureExtractor,
      'faster_rcnn_resnet152_keras':
          frcnn_resnet_keras.FasterRCNNResnet152KerasFeatureExtractor,
      'faster_rcnn_inception_resnet_v2_keras':
      frcnn_inc_res_keras.FasterRCNNInceptionResnetV2KerasFeatureExtractor,
140
      'faster_rcnn_resnet50_fpn_keras':
141
          frcnn_resnet_fpn_keras.FasterRCNNResnet50FpnKerasFeatureExtractor,
142
      'faster_rcnn_resnet101_fpn_keras':
143
          frcnn_resnet_fpn_keras.FasterRCNNResnet101FpnKerasFeatureExtractor,
144
      'faster_rcnn_resnet152_fpn_keras':
145
          frcnn_resnet_fpn_keras.FasterRCNNResnet152FpnKerasFeatureExtractor,
146
  }
147

148
  CENTER_NET_EXTRACTOR_FUNCTION_MAP = {
149
150
151
152
      'resnet_v2_50':
          center_net_resnet_feature_extractor.resnet_v2_50,
      'resnet_v2_101':
          center_net_resnet_feature_extractor.resnet_v2_101,
Yu-hui Chen's avatar
Yu-hui Chen committed
153
154
155
156
      'resnet_v1_18_fpn':
          center_net_resnet_v1_fpn_feature_extractor.resnet_v1_18_fpn,
      'resnet_v1_34_fpn':
          center_net_resnet_v1_fpn_feature_extractor.resnet_v1_34_fpn,
157
158
159
160
      'resnet_v1_50_fpn':
          center_net_resnet_v1_fpn_feature_extractor.resnet_v1_50_fpn,
      'resnet_v1_101_fpn':
          center_net_resnet_v1_fpn_feature_extractor.resnet_v1_101_fpn,
161
162
163
164
165
166
167
168
      'hourglass_10':
          center_net_hourglass_feature_extractor.hourglass_10,
      'hourglass_20':
          center_net_hourglass_feature_extractor.hourglass_20,
      'hourglass_32':
          center_net_hourglass_feature_extractor.hourglass_32,
      'hourglass_52':
          center_net_hourglass_feature_extractor.hourglass_52,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
169
170
171
172
      'hourglass_104':
          center_net_hourglass_feature_extractor.hourglass_104,
      'mobilenet_v2':
          center_net_mobilenet_v2_feature_extractor.mobilenet_v2,
173
174
      'mobilenet_v2_fpn':
          center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn,
175
176
      'mobilenet_v2_fpn_sep_conv':
          center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn,
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  FEATURE_EXTRACTOR_MAPS = [
      CENTER_NET_EXTRACTOR_FUNCTION_MAP,
      FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP,
      SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP
  ]

if tf_version.is_tf1():
  SSD_FEATURE_EXTRACTOR_CLASS_MAP = {
      'ssd_inception_v2':
          SSDInceptionV2FeatureExtractor,
      'ssd_inception_v3':
          SSDInceptionV3FeatureExtractor,
      'ssd_mobilenet_v1':
          SSDMobileNetV1FeatureExtractor,
      'ssd_mobilenet_v1_fpn':
          SSDMobileNetV1FpnFeatureExtractor,
      'ssd_mobilenet_v1_ppn':
          SSDMobileNetV1PpnFeatureExtractor,
      'ssd_mobilenet_v2':
          SSDMobileNetV2FeatureExtractor,
      'ssd_mobilenet_v2_fpn':
          SSDMobileNetV2FpnFeatureExtractor,
      'ssd_mobilenet_v2_mnasfpn':
          SSDMobileNetV2MnasFPNFeatureExtractor,
      'ssd_mobilenet_v3_large':
          SSDMobileNetV3LargeFeatureExtractor,
      'ssd_mobilenet_v3_small':
          SSDMobileNetV3SmallFeatureExtractor,
      'ssd_mobilenet_edgetpu':
          SSDMobileNetEdgeTPUFeatureExtractor,
      'ssd_resnet50_v1_fpn':
          ssd_resnet_v1_fpn.SSDResnet50V1FpnFeatureExtractor,
      'ssd_resnet101_v1_fpn':
          ssd_resnet_v1_fpn.SSDResnet101V1FpnFeatureExtractor,
      'ssd_resnet152_v1_fpn':
          ssd_resnet_v1_fpn.SSDResnet152V1FpnFeatureExtractor,
      'ssd_resnet50_v1_ppn':
          ssd_resnet_v1_ppn.SSDResnet50V1PpnFeatureExtractor,
      'ssd_resnet101_v1_ppn':
          ssd_resnet_v1_ppn.SSDResnet101V1PpnFeatureExtractor,
      'ssd_resnet152_v1_ppn':
          ssd_resnet_v1_ppn.SSDResnet152V1PpnFeatureExtractor,
      'embedded_ssd_mobilenet_v1':
          EmbeddedSSDMobileNetV1FeatureExtractor,
      'ssd_pnasnet':
          SSDPNASNetFeatureExtractor,
225
226
227
228
229
230
231
232
      'ssd_mobiledet_cpu':
          SSDMobileDetCPUFeatureExtractor,
      'ssd_mobiledet_dsp':
          SSDMobileDetDSPFeatureExtractor,
      'ssd_mobiledet_edgetpu':
          SSDMobileDetEdgeTPUFeatureExtractor,
      'ssd_mobiledet_gpu':
          SSDMobileDetGPUFeatureExtractor,
233
234
      'ssd_spaghettinet':
          SSDSpaghettinetFeatureExtractor,
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  }

  FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP = {
      'faster_rcnn_nas':
      frcnn_nas.FasterRCNNNASFeatureExtractor,
      'faster_rcnn_pnas':
      frcnn_pnas.FasterRCNNPNASFeatureExtractor,
      'faster_rcnn_inception_resnet_v2':
      frcnn_inc_res.FasterRCNNInceptionResnetV2FeatureExtractor,
      'faster_rcnn_inception_v2':
      frcnn_inc_v2.FasterRCNNInceptionV2FeatureExtractor,
      'faster_rcnn_resnet50':
      frcnn_resnet_v1.FasterRCNNResnet50FeatureExtractor,
      'faster_rcnn_resnet101':
      frcnn_resnet_v1.FasterRCNNResnet101FeatureExtractor,
      'faster_rcnn_resnet152':
      frcnn_resnet_v1.FasterRCNNResnet152FeatureExtractor,
  }

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
254
255
  CENTER_NET_EXTRACTOR_FUNCTION_MAP = {}

256
257
  FEATURE_EXTRACTOR_MAPS = [
      SSD_FEATURE_EXTRACTOR_CLASS_MAP,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
258
259
      FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP,
      CENTER_NET_EXTRACTOR_FUNCTION_MAP
260
  ]
261

262
263
264
265
266
267
268

def _check_feature_extractor_exists(feature_extractor_type):
  feature_extractors = set().union(*FEATURE_EXTRACTOR_MAPS)
  if feature_extractor_type not in feature_extractors:
    raise ValueError('{} is not supported. See `model_builder.py` for features '
                     'extractors compatible with different versions of '
                     'Tensorflow'.format(feature_extractor_type))
269

270

271
272
273
def _build_ssd_feature_extractor(feature_extractor_config,
                                 is_training,
                                 freeze_batchnorm,
274
                                 reuse_weights=None):
275
276
277
278
279
  """Builds a ssd_meta_arch.SSDFeatureExtractor based on config.

  Args:
    feature_extractor_config: A SSDFeatureExtractor proto config from ssd.proto.
    is_training: True if this feature extractor is being built for training.
280
281
282
283
    freeze_batchnorm: Whether to freeze batch norm parameters during
      training or not. When training with a small batch size (e.g. 1), it is
      desirable to freeze batch norm update and use pretrained batch norm
      params.
284
285
286
287
288
289
290
291
292
293
294
    reuse_weights: if the feature extractor should reuse weights.

  Returns:
    ssd_meta_arch.SSDFeatureExtractor based on config.

  Raises:
    ValueError: On invalid feature extractor type.
  """
  feature_type = feature_extractor_config.type
  depth_multiplier = feature_extractor_config.depth_multiplier
  min_depth = feature_extractor_config.min_depth
295
  pad_to_multiple = feature_extractor_config.pad_to_multiple
296
  use_explicit_padding = feature_extractor_config.use_explicit_padding
297
  use_depthwise = feature_extractor_config.use_depthwise
298

299
300
  is_keras = tf_version.is_tf2()
  if is_keras:
301
302
303
304
305
    conv_hyperparams = hyperparams_builder.KerasLayerHyperparams(
        feature_extractor_config.conv_hyperparams)
  else:
    conv_hyperparams = hyperparams_builder.build(
        feature_extractor_config.conv_hyperparams, is_training)
306
307
  override_base_feature_extractor_hyperparams = (
      feature_extractor_config.override_base_feature_extractor_hyperparams)
308

309
  if not is_keras and feature_type not in SSD_FEATURE_EXTRACTOR_CLASS_MAP:
310
311
    raise ValueError('Unknown ssd feature_extractor: {}'.format(feature_type))

312
  if is_keras:
313
314
315
316
    feature_extractor_class = SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP[
        feature_type]
  else:
    feature_extractor_class = SSD_FEATURE_EXTRACTOR_CLASS_MAP[feature_type]
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  kwargs = {
      'is_training':
          is_training,
      'depth_multiplier':
          depth_multiplier,
      'min_depth':
          min_depth,
      'pad_to_multiple':
          pad_to_multiple,
      'use_explicit_padding':
          use_explicit_padding,
      'use_depthwise':
          use_depthwise,
      'override_base_feature_extractor_hyperparams':
          override_base_feature_extractor_hyperparams
  }

334
335
336
337
338
339
  if feature_extractor_config.HasField('replace_preprocessor_with_placeholder'):
    kwargs.update({
        'replace_preprocessor_with_placeholder':
            feature_extractor_config.replace_preprocessor_with_placeholder
    })

pkulzc's avatar
pkulzc committed
340
341
342
  if feature_extractor_config.HasField('num_layers'):
    kwargs.update({'num_layers': feature_extractor_config.num_layers})

343
  if is_keras:
344
345
346
347
348
349
350
351
352
353
354
    kwargs.update({
        'conv_hyperparams': conv_hyperparams,
        'inplace_batchnorm_update': False,
        'freeze_batchnorm': freeze_batchnorm
    })
  else:
    kwargs.update({
        'conv_hyperparams_fn': conv_hyperparams,
        'reuse_weights': reuse_weights,
    })

355

356
357
358
359
360
361
  if feature_extractor_config.HasField('spaghettinet_arch_name'):
    kwargs.update({
        'spaghettinet_arch_name':
            feature_extractor_config.spaghettinet_arch_name,
    })

362
363
  if feature_extractor_config.HasField('fpn'):
    kwargs.update({
364
365
366
367
368
369
        'fpn_min_level':
            feature_extractor_config.fpn.min_level,
        'fpn_max_level':
            feature_extractor_config.fpn.max_level,
        'additional_layer_depth':
            feature_extractor_config.fpn.additional_layer_depth,
370
371
    })

372
373
374
375
376
377
378
379
  if feature_extractor_config.HasField('bifpn'):
    kwargs.update({
        'bifpn_min_level': feature_extractor_config.bifpn.min_level,
        'bifpn_max_level': feature_extractor_config.bifpn.max_level,
        'bifpn_num_iterations': feature_extractor_config.bifpn.num_iterations,
        'bifpn_num_filters': feature_extractor_config.bifpn.num_filters,
        'bifpn_combine_method': feature_extractor_config.bifpn.combine_method,
    })
380

381
  return feature_extractor_class(**kwargs)
382
383


384
def _build_ssd_model(ssd_config, is_training, add_summaries):
385
386
387
388
389
390
  """Builds an SSD detection model based on the model config.

  Args:
    ssd_config: A ssd.proto object containing the config for the desired
      SSDMetaArch.
    is_training: True if this model is being built for training purposes.
391
    add_summaries: Whether to add tf summaries in the model.
392
393
  Returns:
    SSDMetaArch based on the config.
394

395
396
397
398
399
  Raises:
    ValueError: If ssd_config.type is not recognized (i.e. not registered in
      model_class_map).
  """
  num_classes = ssd_config.num_classes
400
  _check_feature_extractor_exists(ssd_config.feature_extractor.type)
401
402

  # Feature extractor
403
  feature_extractor = _build_ssd_feature_extractor(
404
      feature_extractor_config=ssd_config.feature_extractor,
405
      freeze_batchnorm=ssd_config.freeze_batchnorm,
406
      is_training=is_training)
407
408
409
410
411

  box_coder = box_coder_builder.build(ssd_config.box_coder)
  matcher = matcher_builder.build(ssd_config.matcher)
  region_similarity_calculator = sim_calc.build(
      ssd_config.similarity_calculator)
412
  encode_background_as_zeros = ssd_config.encode_background_as_zeros
413
  negative_class_weight = ssd_config.negative_class_weight
414
415
  anchor_generator = anchor_generator_builder.build(
      ssd_config.anchor_generator)
416
417
  if feature_extractor.is_keras_model:
    ssd_box_predictor = box_predictor_builder.build_keras(
418
        hyperparams_fn=hyperparams_builder.KerasLayerHyperparams,
419
420
421
422
423
424
425
426
427
428
429
430
        freeze_batchnorm=ssd_config.freeze_batchnorm,
        inplace_batchnorm_update=False,
        num_predictions_per_location_list=anchor_generator
        .num_anchors_per_location(),
        box_predictor_config=ssd_config.box_predictor,
        is_training=is_training,
        num_classes=num_classes,
        add_background_class=ssd_config.add_background_class)
  else:
    ssd_box_predictor = box_predictor_builder.build(
        hyperparams_builder.build, ssd_config.box_predictor, is_training,
        num_classes, ssd_config.add_background_class)
431
432
433
434
  image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer)
  non_max_suppression_fn, score_conversion_fn = post_processing_builder.build(
      ssd_config.post_processing)
  (classification_loss, localization_loss, classification_weight,
435
436
   localization_weight, hard_example_miner, random_example_sampler,
   expected_loss_weights_fn) = losses_builder.build(ssd_config.loss)
437
  normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches
438
  normalize_loc_loss_by_codesize = ssd_config.normalize_loc_loss_by_codesize
439
440
441
442

  equalization_loss_config = ops.EqualizationLossConfig(
      weight=ssd_config.loss.equalization_loss.weight,
      exclude_prefixes=ssd_config.loss.equalization_loss.exclude_prefixes)
443
444
445
446
447

  target_assigner_instance = target_assigner.TargetAssigner(
      region_similarity_calculator,
      matcher,
      box_coder,
448
      negative_class_weight=negative_class_weight)
449

450
  ssd_meta_arch_fn = ssd_meta_arch.SSDMetaArch
451
  kwargs = {}
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

  return ssd_meta_arch_fn(
      is_training=is_training,
      anchor_generator=anchor_generator,
      box_predictor=ssd_box_predictor,
      box_coder=box_coder,
      feature_extractor=feature_extractor,
      encode_background_as_zeros=encode_background_as_zeros,
      image_resizer_fn=image_resizer_fn,
      non_max_suppression_fn=non_max_suppression_fn,
      score_conversion_fn=score_conversion_fn,
      classification_loss=classification_loss,
      localization_loss=localization_loss,
      classification_loss_weight=classification_weight,
      localization_loss_weight=localization_weight,
      normalize_loss_by_num_matches=normalize_loss_by_num_matches,
      hard_example_miner=hard_example_miner,
469
      target_assigner_instance=target_assigner_instance,
470
      add_summaries=add_summaries,
471
472
      normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize,
      freeze_batchnorm=ssd_config.freeze_batchnorm,
473
      inplace_batchnorm_update=ssd_config.inplace_batchnorm_update,
474
      add_background_class=ssd_config.add_background_class,
475
      explicit_background_class=ssd_config.explicit_background_class,
476
      random_example_sampler=random_example_sampler,
477
478
479
480
      expected_loss_weights_fn=expected_loss_weights_fn,
      use_confidences_as_targets=ssd_config.use_confidences_as_targets,
      implicit_example_weight=ssd_config.implicit_example_weight,
      equalization_loss_config=equalization_loss_config,
481
482
      return_raw_detections_during_predict=(
          ssd_config.return_raw_detections_during_predict),
483
      **kwargs)
484
485
486


def _build_faster_rcnn_feature_extractor(
487
    feature_extractor_config, is_training, reuse_weights=True,
488
    inplace_batchnorm_update=False):
489
490
491
492
493
494
495
  """Builds a faster_rcnn_meta_arch.FasterRCNNFeatureExtractor based on config.

  Args:
    feature_extractor_config: A FasterRcnnFeatureExtractor proto config from
      faster_rcnn.proto.
    is_training: True if this feature extractor is being built for training.
    reuse_weights: if the feature extractor should reuse weights.
496
497
498
499
500
    inplace_batchnorm_update: Whether to update batch_norm inplace during
      training. This is required for batch norm to work correctly on TPUs. When
      this is false, user must add a control dependency on
      tf.GraphKeys.UPDATE_OPS for train/loss op in order to update the batch
      norm moving average parameters.
501
502
503
504
505
506
507

  Returns:
    faster_rcnn_meta_arch.FasterRCNNFeatureExtractor based on config.

  Raises:
    ValueError: On invalid feature extractor type.
  """
508
509
  if inplace_batchnorm_update:
    raise ValueError('inplace batchnorm updates not supported.')
510
511
512
  feature_type = feature_extractor_config.type
  first_stage_features_stride = (
      feature_extractor_config.first_stage_features_stride)
513
  batch_norm_trainable = feature_extractor_config.batch_norm_trainable
514
515
516
517
518
519
520

  if feature_type not in FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP:
    raise ValueError('Unknown Faster R-CNN feature_extractor: {}'.format(
        feature_type))
  feature_extractor_class = FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP[
      feature_type]
  return feature_extractor_class(
521
      is_training, first_stage_features_stride,
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
      batch_norm_trainable, reuse_weights=reuse_weights)


def _build_faster_rcnn_keras_feature_extractor(
    feature_extractor_config, is_training,
    inplace_batchnorm_update=False):
  """Builds a faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor from config.

  Args:
    feature_extractor_config: A FasterRcnnFeatureExtractor proto config from
      faster_rcnn.proto.
    is_training: True if this feature extractor is being built for training.
    inplace_batchnorm_update: Whether to update batch_norm inplace during
      training. This is required for batch norm to work correctly on TPUs. When
      this is false, user must add a control dependency on
      tf.GraphKeys.UPDATE_OPS for train/loss op in order to update the batch
      norm moving average parameters.

  Returns:
    faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor based on config.

  Raises:
    ValueError: On invalid feature extractor type.
  """
  if inplace_batchnorm_update:
    raise ValueError('inplace batchnorm updates not supported.')
  feature_type = feature_extractor_config.type
  first_stage_features_stride = (
      feature_extractor_config.first_stage_features_stride)
  batch_norm_trainable = feature_extractor_config.batch_norm_trainable

  if feature_type not in FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP:
    raise ValueError('Unknown Faster R-CNN feature_extractor: {}'.format(
        feature_type))
  feature_extractor_class = FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP[
      feature_type]
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579

  kwargs = {}

  if feature_extractor_config.HasField('conv_hyperparams'):
    kwargs.update({
        'conv_hyperparams':
            hyperparams_builder.KerasLayerHyperparams(
                feature_extractor_config.conv_hyperparams),
        'override_base_feature_extractor_hyperparams':
            feature_extractor_config.override_base_feature_extractor_hyperparams
    })

  if feature_extractor_config.HasField('fpn'):
    kwargs.update({
        'fpn_min_level':
            feature_extractor_config.fpn.min_level,
        'fpn_max_level':
            feature_extractor_config.fpn.max_level,
        'additional_layer_depth':
            feature_extractor_config.fpn.additional_layer_depth,
    })

580
581
  return feature_extractor_class(
      is_training, first_stage_features_stride,
582
      batch_norm_trainable, **kwargs)
583
584


585
def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries):
586
587
588
589
590
591
592
  """Builds a Faster R-CNN or R-FCN detection model based on the model config.

  Builds R-FCN model if the second_stage_box_predictor in the config is of type
  `rfcn_box_predictor` else builds a Faster R-CNN model.

  Args:
    frcnn_config: A faster_rcnn.proto object containing the config for the
593
      desired FasterRCNNMetaArch or RFCNMetaArch.
594
    is_training: True if this model is being built for training purposes.
595
    add_summaries: Whether to add tf summaries in the model.
596
597
598

  Returns:
    FasterRCNNMetaArch based on the config.
599

600
601
602
603
604
605
  Raises:
    ValueError: If frcnn_config.type is not recognized (i.e. not registered in
      model_class_map).
  """
  num_classes = frcnn_config.num_classes
  image_resizer_fn = image_resizer_builder.build(frcnn_config.image_resizer)
606
607
  _check_feature_extractor_exists(frcnn_config.feature_extractor.type)
  is_keras = tf_version.is_tf2()
608

syiming's avatar
syiming committed
609
  if is_keras:
610
611
612
613
614
615
616
    feature_extractor = _build_faster_rcnn_keras_feature_extractor(
        frcnn_config.feature_extractor, is_training,
        inplace_batchnorm_update=frcnn_config.inplace_batchnorm_update)
  else:
    feature_extractor = _build_faster_rcnn_feature_extractor(
        frcnn_config.feature_extractor, is_training,
        inplace_batchnorm_update=frcnn_config.inplace_batchnorm_update)
617

618
  number_of_stages = frcnn_config.number_of_stages
619
620
621
  first_stage_anchor_generator = anchor_generator_builder.build(
      frcnn_config.first_stage_anchor_generator)

622
623
624
625
  first_stage_target_assigner = target_assigner.create_target_assigner(
      'FasterRCNN',
      'proposal',
      use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher)
626
  first_stage_atrous_rate = frcnn_config.first_stage_atrous_rate
627
628
629
630
631
632
633
  if is_keras:
    first_stage_box_predictor_arg_scope_fn = (
        hyperparams_builder.KerasLayerHyperparams(
            frcnn_config.first_stage_box_predictor_conv_hyperparams))
  else:
    first_stage_box_predictor_arg_scope_fn = hyperparams_builder.build(
        frcnn_config.first_stage_box_predictor_conv_hyperparams, is_training)
634
635
636
637
  first_stage_box_predictor_kernel_size = (
      frcnn_config.first_stage_box_predictor_kernel_size)
  first_stage_box_predictor_depth = frcnn_config.first_stage_box_predictor_depth
  first_stage_minibatch_size = frcnn_config.first_stage_minibatch_size
638
639
  use_static_shapes = frcnn_config.use_static_shapes and (
      frcnn_config.use_static_shapes_for_eval or is_training)
640
641
  first_stage_sampler = sampler.BalancedPositiveNegativeSampler(
      positive_fraction=frcnn_config.first_stage_positive_balance_fraction,
642
643
      is_static=(frcnn_config.use_static_balanced_label_sampler and
                 use_static_shapes))
644
  first_stage_max_proposals = frcnn_config.first_stage_max_proposals
645
646
647
648
649
650
651
652
653
654
655
656
657
  if (frcnn_config.first_stage_nms_iou_threshold < 0 or
      frcnn_config.first_stage_nms_iou_threshold > 1.0):
    raise ValueError('iou_threshold not in [0, 1.0].')
  if (is_training and frcnn_config.second_stage_batch_size >
      first_stage_max_proposals):
    raise ValueError('second_stage_batch_size should be no greater than '
                     'first_stage_max_proposals.')
  first_stage_non_max_suppression_fn = functools.partial(
      post_processing.batch_multiclass_non_max_suppression,
      score_thresh=frcnn_config.first_stage_nms_score_threshold,
      iou_thresh=frcnn_config.first_stage_nms_iou_threshold,
      max_size_per_class=frcnn_config.first_stage_max_proposals,
      max_total_size=frcnn_config.first_stage_max_proposals,
Pooya Davoodi's avatar
Pooya Davoodi committed
658
      use_static_shapes=use_static_shapes,
659
      use_partitioned_nms=frcnn_config.use_partitioned_nms_in_first_stage,
Pooya Davoodi's avatar
Pooya Davoodi committed
660
      use_combined_nms=frcnn_config.use_combined_nms_in_first_stage)
661
662
663
664
665
666
667
668
  first_stage_loc_loss_weight = (
      frcnn_config.first_stage_localization_loss_weight)
  first_stage_obj_loss_weight = frcnn_config.first_stage_objectness_loss_weight

  initial_crop_size = frcnn_config.initial_crop_size
  maxpool_kernel_size = frcnn_config.maxpool_kernel_size
  maxpool_stride = frcnn_config.maxpool_stride

669
670
671
672
  second_stage_target_assigner = target_assigner.create_target_assigner(
      'FasterRCNN',
      'detection',
      use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher)
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  if is_keras:
    second_stage_box_predictor = box_predictor_builder.build_keras(
        hyperparams_builder.KerasLayerHyperparams,
        freeze_batchnorm=False,
        inplace_batchnorm_update=False,
        num_predictions_per_location_list=[1],
        box_predictor_config=frcnn_config.second_stage_box_predictor,
        is_training=is_training,
        num_classes=num_classes)
  else:
    second_stage_box_predictor = box_predictor_builder.build(
        hyperparams_builder.build,
        frcnn_config.second_stage_box_predictor,
        is_training=is_training,
        num_classes=num_classes)
688
  second_stage_batch_size = frcnn_config.second_stage_batch_size
689
690
  second_stage_sampler = sampler.BalancedPositiveNegativeSampler(
      positive_fraction=frcnn_config.second_stage_balance_fraction,
691
692
      is_static=(frcnn_config.use_static_balanced_label_sampler and
                 use_static_shapes))
693
694
695
696
  (second_stage_non_max_suppression_fn, second_stage_score_conversion_fn
  ) = post_processing_builder.build(frcnn_config.second_stage_post_processing)
  second_stage_localization_loss_weight = (
      frcnn_config.second_stage_localization_loss_weight)
697
698
699
  second_stage_classification_loss = (
      losses_builder.build_faster_rcnn_classification_loss(
          frcnn_config.second_stage_classification_loss))
700
701
  second_stage_classification_loss_weight = (
      frcnn_config.second_stage_classification_loss_weight)
702
703
  second_stage_mask_prediction_loss_weight = (
      frcnn_config.second_stage_mask_prediction_loss_weight)
704
705
706
707
708
709
710
711

  hard_example_miner = None
  if frcnn_config.HasField('hard_example_miner'):
    hard_example_miner = losses_builder.build_hard_example_miner(
        frcnn_config.hard_example_miner,
        second_stage_classification_loss_weight,
        second_stage_localization_loss_weight)

712
  crop_and_resize_fn = (
713
714
715
      spatial_ops.multilevel_matmul_crop_and_resize
      if frcnn_config.use_matmul_crop_and_resize
      else spatial_ops.multilevel_native_crop_and_resize)
716
717
  clip_anchors_to_image = (
      frcnn_config.clip_anchors_to_image)
718

719
  common_kwargs = {
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
      'is_training':
          is_training,
      'num_classes':
          num_classes,
      'image_resizer_fn':
          image_resizer_fn,
      'feature_extractor':
          feature_extractor,
      'number_of_stages':
          number_of_stages,
      'first_stage_anchor_generator':
          first_stage_anchor_generator,
      'first_stage_target_assigner':
          first_stage_target_assigner,
      'first_stage_atrous_rate':
          first_stage_atrous_rate,
736
      'first_stage_box_predictor_arg_scope_fn':
737
          first_stage_box_predictor_arg_scope_fn,
738
      'first_stage_box_predictor_kernel_size':
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
          first_stage_box_predictor_kernel_size,
      'first_stage_box_predictor_depth':
          first_stage_box_predictor_depth,
      'first_stage_minibatch_size':
          first_stage_minibatch_size,
      'first_stage_sampler':
          first_stage_sampler,
      'first_stage_non_max_suppression_fn':
          first_stage_non_max_suppression_fn,
      'first_stage_max_proposals':
          first_stage_max_proposals,
      'first_stage_localization_loss_weight':
          first_stage_loc_loss_weight,
      'first_stage_objectness_loss_weight':
          first_stage_obj_loss_weight,
      'second_stage_target_assigner':
          second_stage_target_assigner,
      'second_stage_batch_size':
          second_stage_batch_size,
      'second_stage_sampler':
          second_stage_sampler,
760
      'second_stage_non_max_suppression_fn':
761
762
763
          second_stage_non_max_suppression_fn,
      'second_stage_score_conversion_fn':
          second_stage_score_conversion_fn,
764
      'second_stage_localization_loss_weight':
765
          second_stage_localization_loss_weight,
766
      'second_stage_classification_loss':
767
          second_stage_classification_loss,
768
      'second_stage_classification_loss_weight':
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
          second_stage_classification_loss_weight,
      'hard_example_miner':
          hard_example_miner,
      'add_summaries':
          add_summaries,
      'crop_and_resize_fn':
          crop_and_resize_fn,
      'clip_anchors_to_image':
          clip_anchors_to_image,
      'use_static_shapes':
          use_static_shapes,
      'resize_masks':
          frcnn_config.resize_masks,
      'return_raw_detections_during_predict':
          frcnn_config.return_raw_detections_during_predict,
      'output_final_box_features':
785
786
787
          frcnn_config.output_final_box_features,
      'output_final_box_rpn_features':
          frcnn_config.output_final_box_rpn_features,
788
  }
789

790
791
792
793
794
  if ((not is_keras and isinstance(second_stage_box_predictor,
                                   rfcn_box_predictor.RfcnBoxPredictor)) or
      (is_keras and
       isinstance(second_stage_box_predictor,
                  rfcn_keras_box_predictor.RfcnKerasBoxPredictor))):
795
796
797
    return rfcn_meta_arch.RFCNMetaArch(
        second_stage_rfcn_box_predictor=second_stage_box_predictor,
        **common_kwargs)
798
799
800
801
802
803
  elif frcnn_config.HasField('context_config'):
    context_config = frcnn_config.context_config
    common_kwargs.update({
        'attention_bottleneck_dimension':
            context_config.attention_bottleneck_dimension,
        'attention_temperature':
804
805
806
807
808
809
810
811
812
813
814
815
816
            context_config.attention_temperature,
        'use_self_attention':
            context_config.use_self_attention,
        'use_long_term_attention':
            context_config.use_long_term_attention,
        'self_attention_in_sequence':
            context_config.self_attention_in_sequence,
        'num_attention_heads':
            context_config.num_attention_heads,
        'num_attention_layers':
            context_config.num_attention_layers,
        'attention_position':
            context_config.attention_position
817
818
819
820
821
822
823
824
825
    })
    return context_rcnn_meta_arch.ContextRCNNMetaArch(
        initial_crop_size=initial_crop_size,
        maxpool_kernel_size=maxpool_kernel_size,
        maxpool_stride=maxpool_stride,
        second_stage_mask_rcnn_box_predictor=second_stage_box_predictor,
        second_stage_mask_prediction_loss_weight=(
            second_stage_mask_prediction_loss_weight),
        **common_kwargs)
826
827
828
829
830
831
  else:
    return faster_rcnn_meta_arch.FasterRCNNMetaArch(
        initial_crop_size=initial_crop_size,
        maxpool_kernel_size=maxpool_kernel_size,
        maxpool_stride=maxpool_stride,
        second_stage_mask_rcnn_box_predictor=second_stage_box_predictor,
832
833
        second_stage_mask_prediction_loss_weight=(
            second_stage_mask_prediction_loss_weight),
834
        **common_kwargs)
835
836
837
838
839
840
841
842
843

EXPERIMENTAL_META_ARCH_BUILDER_MAP = {
}


def _build_experimental_model(config, is_training, add_summaries=True):
  return EXPERIMENTAL_META_ARCH_BUILDER_MAP[config.name](
      is_training, add_summaries)

844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

# The class ID in the groundtruth/model architecture is usually 0-based while
# the ID in the label map is 1-based. The offset is used to convert between the
# the two.
CLASS_ID_OFFSET = 1
KEYPOINT_STD_DEV_DEFAULT = 1.0


def keypoint_proto_to_params(kp_config, keypoint_map_dict):
  """Converts CenterNet.KeypointEstimation proto to parameter namedtuple."""
  label_map_item = keypoint_map_dict[kp_config.keypoint_class_name]

  classification_loss, localization_loss, _, _, _, _, _ = (
      losses_builder.build(kp_config.loss))

  keypoint_indices = [
      keypoint.id for keypoint in label_map_item.keypoints
  ]
  keypoint_labels = [
      keypoint.label for keypoint in label_map_item.keypoints
  ]
  keypoint_std_dev_dict = {
      label: KEYPOINT_STD_DEV_DEFAULT for label in keypoint_labels
  }
  if kp_config.keypoint_label_to_std:
    for label, value in kp_config.keypoint_label_to_std.items():
      keypoint_std_dev_dict[label] = value
  keypoint_std_dev = [keypoint_std_dev_dict[label] for label in keypoint_labels]
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
  if kp_config.HasField('heatmap_head_params'):
    heatmap_head_num_filters = list(kp_config.heatmap_head_params.num_filters)
    heatmap_head_kernel_sizes = list(kp_config.heatmap_head_params.kernel_sizes)
  else:
    heatmap_head_num_filters = [256]
    heatmap_head_kernel_sizes = [3]
  if kp_config.HasField('offset_head_params'):
    offset_head_num_filters = list(kp_config.offset_head_params.num_filters)
    offset_head_kernel_sizes = list(kp_config.offset_head_params.kernel_sizes)
  else:
    offset_head_num_filters = [256]
    offset_head_kernel_sizes = [3]
  if kp_config.HasField('regress_head_params'):
    regress_head_num_filters = list(kp_config.regress_head_params.num_filters)
    regress_head_kernel_sizes = list(
        kp_config.regress_head_params.kernel_sizes)
  else:
    regress_head_num_filters = [256]
    regress_head_kernel_sizes = [3]
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
  return center_net_meta_arch.KeypointEstimationParams(
      task_name=kp_config.task_name,
      class_id=label_map_item.id - CLASS_ID_OFFSET,
      keypoint_indices=keypoint_indices,
      classification_loss=classification_loss,
      localization_loss=localization_loss,
      keypoint_labels=keypoint_labels,
      keypoint_std_dev=keypoint_std_dev,
      task_loss_weight=kp_config.task_loss_weight,
      keypoint_regression_loss_weight=kp_config.keypoint_regression_loss_weight,
      keypoint_heatmap_loss_weight=kp_config.keypoint_heatmap_loss_weight,
      keypoint_offset_loss_weight=kp_config.keypoint_offset_loss_weight,
      heatmap_bias_init=kp_config.heatmap_bias_init,
      keypoint_candidate_score_threshold=(
          kp_config.keypoint_candidate_score_threshold),
      num_candidates_per_keypoint=kp_config.num_candidates_per_keypoint,
      peak_max_pool_kernel_size=kp_config.peak_max_pool_kernel_size,
      unmatched_keypoint_score=kp_config.unmatched_keypoint_score,
      box_scale=kp_config.box_scale,
      candidate_search_scale=kp_config.candidate_search_scale,
911
912
      candidate_ranking_mode=kp_config.candidate_ranking_mode,
      offset_peak_radius=kp_config.offset_peak_radius,
913
914
915
      per_keypoint_offset=kp_config.per_keypoint_offset,
      predict_depth=kp_config.predict_depth,
      per_keypoint_depth=kp_config.per_keypoint_depth,
916
917
918
      keypoint_depth_loss_weight=kp_config.keypoint_depth_loss_weight,
      score_distance_offset=kp_config.score_distance_offset,
      clip_out_of_frame_keypoints=kp_config.clip_out_of_frame_keypoints,
919
920
921
922
923
924
      rescore_instances=kp_config.rescore_instances,
      heatmap_head_num_filters=heatmap_head_num_filters,
      heatmap_head_kernel_sizes=heatmap_head_kernel_sizes,
      offset_head_num_filters=offset_head_num_filters,
      offset_head_kernel_sizes=offset_head_kernel_sizes,
      regress_head_num_filters=regress_head_num_filters,
925
926
927
      regress_head_kernel_sizes=regress_head_kernel_sizes,
      score_distance_multiplier=kp_config.score_distance_multiplier,
      std_dev_multiplier=kp_config.std_dev_multiplier,
928
929
930
      rescoring_threshold=kp_config.rescoring_threshold,
      gaussian_denom_ratio=kp_config.gaussian_denom_ratio,
      argmax_postprocessing=kp_config.argmax_postprocessing)
931
932
933
934
935
936
937
938
939
940
941
942


def object_detection_proto_to_params(od_config):
  """Converts CenterNet.ObjectDetection proto to parameter namedtuple."""
  loss = losses_pb2.Loss()
  # Add dummy classification loss to avoid the loss_builder throwing error.
  # TODO(yuhuic): update the loss builder to take the classification loss
  # directly.
  loss.classification_loss.weighted_sigmoid.CopyFrom(
      losses_pb2.WeightedSigmoidClassificationLoss())
  loss.localization_loss.CopyFrom(od_config.localization_loss)
  _, localization_loss, _, _, _, _, _ = (losses_builder.build(loss))
943
944
945
946
947
948
949
950
951
952
953
954
  if od_config.HasField('scale_head_params'):
    scale_head_num_filters = list(od_config.scale_head_params.num_filters)
    scale_head_kernel_sizes = list(od_config.scale_head_params.kernel_sizes)
  else:
    scale_head_num_filters = [256]
    scale_head_kernel_sizes = [3]
  if od_config.HasField('offset_head_params'):
    offset_head_num_filters = list(od_config.offset_head_params.num_filters)
    offset_head_kernel_sizes = list(od_config.offset_head_params.kernel_sizes)
  else:
    offset_head_num_filters = [256]
    offset_head_kernel_sizes = [3]
955
956
957
958
  return center_net_meta_arch.ObjectDetectionParams(
      localization_loss=localization_loss,
      scale_loss_weight=od_config.scale_loss_weight,
      offset_loss_weight=od_config.offset_loss_weight,
959
960
961
962
963
      task_loss_weight=od_config.task_loss_weight,
      scale_head_num_filters=scale_head_num_filters,
      scale_head_kernel_sizes=scale_head_kernel_sizes,
      offset_head_num_filters=offset_head_num_filters,
      offset_head_kernel_sizes=offset_head_kernel_sizes)
964
965
966
967
968
969
970
971
972
973
974
975


def object_center_proto_to_params(oc_config):
  """Converts CenterNet.ObjectCenter proto to parameter namedtuple."""
  loss = losses_pb2.Loss()
  # Add dummy localization loss to avoid the loss_builder throwing error.
  # TODO(yuhuic): update the loss builder to take the localization loss
  # directly.
  loss.localization_loss.weighted_l2.CopyFrom(
      losses_pb2.WeightedL2LocalizationLoss())
  loss.classification_loss.CopyFrom(oc_config.classification_loss)
  classification_loss, _, _, _, _, _, _ = (losses_builder.build(loss))
976
977
978
  keypoint_weights_for_center = []
  if oc_config.keypoint_weights_for_center:
    keypoint_weights_for_center = list(oc_config.keypoint_weights_for_center)
979

980
  if oc_config.HasField('center_head_params'):
981
982
983
984
985
    center_head_num_filters = list(oc_config.center_head_params.num_filters)
    center_head_kernel_sizes = list(oc_config.center_head_params.kernel_sizes)
  else:
    center_head_num_filters = [256]
    center_head_kernel_sizes = [3]
986
987
988
989
990
  return center_net_meta_arch.ObjectCenterParams(
      classification_loss=classification_loss,
      object_center_loss_weight=oc_config.object_center_loss_weight,
      heatmap_bias_init=oc_config.heatmap_bias_init,
      min_box_overlap_iou=oc_config.min_box_overlap_iou,
991
      max_box_predictions=oc_config.max_box_predictions,
992
      use_labeled_classes=oc_config.use_labeled_classes,
993
994
      keypoint_weights_for_center=keypoint_weights_for_center,
      center_head_num_filters=center_head_num_filters,
995
996
      center_head_kernel_sizes=center_head_kernel_sizes,
      peak_max_pool_kernel_size=oc_config.peak_max_pool_kernel_size)
997
998
999
1000
1001
1002
1003
1004
1005
1006


def mask_proto_to_params(mask_config):
  """Converts CenterNet.MaskEstimation proto to parameter namedtuple."""
  loss = losses_pb2.Loss()
  # Add dummy localization loss to avoid the loss_builder throwing error.
  loss.localization_loss.weighted_l2.CopyFrom(
      losses_pb2.WeightedL2LocalizationLoss())
  loss.classification_loss.CopyFrom(mask_config.classification_loss)
  classification_loss, _, _, _, _, _, _ = (losses_builder.build(loss))
1007
1008
1009
1010
1011
1012
  if mask_config.HasField('mask_head_params'):
    mask_head_num_filters = list(mask_config.mask_head_params.num_filters)
    mask_head_kernel_sizes = list(mask_config.mask_head_params.kernel_sizes)
  else:
    mask_head_num_filters = [256]
    mask_head_kernel_sizes = [3]
1013
1014
1015
1016
1017
1018
  return center_net_meta_arch.MaskParams(
      classification_loss=classification_loss,
      task_loss_weight=mask_config.task_loss_weight,
      mask_height=mask_config.mask_height,
      mask_width=mask_config.mask_width,
      score_threshold=mask_config.score_threshold,
1019
1020
1021
      heatmap_bias_init=mask_config.heatmap_bias_init,
      mask_head_num_filters=mask_head_num_filters,
      mask_head_kernel_sizes=mask_head_kernel_sizes)
1022
1023


1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
def densepose_proto_to_params(densepose_config):
  """Converts CenterNet.DensePoseEstimation proto to parameter namedtuple."""
  classification_loss, localization_loss, _, _, _, _, _ = (
      losses_builder.build(densepose_config.loss))
  return center_net_meta_arch.DensePoseParams(
      class_id=densepose_config.class_id,
      classification_loss=classification_loss,
      localization_loss=localization_loss,
      part_loss_weight=densepose_config.part_loss_weight,
      coordinate_loss_weight=densepose_config.coordinate_loss_weight,
      num_parts=densepose_config.num_parts,
      task_loss_weight=densepose_config.task_loss_weight,
      upsample_to_input_res=densepose_config.upsample_to_input_res,
      heatmap_bias_init=densepose_config.heatmap_bias_init)


1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
def tracking_proto_to_params(tracking_config):
  """Converts CenterNet.TrackEstimation proto to parameter namedtuple."""
  loss = losses_pb2.Loss()
  # Add dummy localization loss to avoid the loss_builder throwing error.
  # TODO(yuhuic): update the loss builder to take the localization loss
  # directly.
  loss.localization_loss.weighted_l2.CopyFrom(
      losses_pb2.WeightedL2LocalizationLoss())
  loss.classification_loss.CopyFrom(tracking_config.classification_loss)
  classification_loss, _, _, _, _, _, _ = losses_builder.build(loss)
  return center_net_meta_arch.TrackParams(
      num_track_ids=tracking_config.num_track_ids,
      reid_embed_size=tracking_config.reid_embed_size,
      classification_loss=classification_loss,
      num_fc_layers=tracking_config.num_fc_layers,
      task_loss_weight=tracking_config.task_loss_weight)


1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
def temporal_offset_proto_to_params(temporal_offset_config):
  """Converts CenterNet.TemporalOffsetEstimation proto to param-tuple."""
  loss = losses_pb2.Loss()
  # Add dummy classification loss to avoid the loss_builder throwing error.
  # TODO(yuhuic): update the loss builder to take the classification loss
  # directly.
  loss.classification_loss.weighted_sigmoid.CopyFrom(
      losses_pb2.WeightedSigmoidClassificationLoss())
  loss.localization_loss.CopyFrom(temporal_offset_config.localization_loss)
  _, localization_loss, _, _, _, _, _ = losses_builder.build(loss)
  return center_net_meta_arch.TemporalOffsetParams(
      localization_loss=localization_loss,
      task_loss_weight=temporal_offset_config.task_loss_weight)


1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
def _build_center_net_model(center_net_config, is_training, add_summaries):
  """Build a CenterNet detection model.

  Args:
    center_net_config: A CenterNet proto object with model configuration.
    is_training: True if this model is being built for training purposes.
    add_summaries: Whether to add tf summaries in the model.

  Returns:
    CenterNetMetaArch based on the config.

  """

  image_resizer_fn = image_resizer_builder.build(
      center_net_config.image_resizer)
  _check_feature_extractor_exists(center_net_config.feature_extractor.type)
  feature_extractor = _build_center_net_feature_extractor(
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1090
      center_net_config.feature_extractor, is_training)
1091
1092
1093
1094
1095
1096
1097
1098
  object_center_params = object_center_proto_to_params(
      center_net_config.object_center_params)

  object_detection_params = None
  if center_net_config.HasField('object_detection_task'):
    object_detection_params = object_detection_proto_to_params(
        center_net_config.object_detection_task)

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
  if center_net_config.HasField('deepmac_mask_estimation'):
    logging.warn(('Building experimental DeepMAC meta-arch.'
                  ' Some features may be omitted.'))
    deepmac_params = deepmac_meta_arch.deepmac_proto_to_params(
        center_net_config.deepmac_mask_estimation)
    return deepmac_meta_arch.DeepMACMetaArch(
        is_training=is_training,
        add_summaries=add_summaries,
        num_classes=center_net_config.num_classes,
        feature_extractor=feature_extractor,
        image_resizer_fn=image_resizer_fn,
        object_center_params=object_center_params,
        object_detection_params=object_detection_params,
        deepmac_params=deepmac_params)

1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
  keypoint_params_dict = None
  if center_net_config.keypoint_estimation_task:
    label_map_proto = label_map_util.load_labelmap(
        center_net_config.keypoint_label_map_path)
    keypoint_map_dict = {
        item.name: item for item in label_map_proto.item if item.keypoints
    }
    keypoint_params_dict = {}
    keypoint_class_id_set = set()
    all_keypoint_indices = []
    for task in center_net_config.keypoint_estimation_task:
      kp_params = keypoint_proto_to_params(task, keypoint_map_dict)
      keypoint_params_dict[task.task_name] = kp_params
      all_keypoint_indices.extend(kp_params.keypoint_indices)
      if kp_params.class_id in keypoint_class_id_set:
        raise ValueError(('Multiple keypoint tasks map to the same class id is '
                          'not allowed: %d' % kp_params.class_id))
      else:
        keypoint_class_id_set.add(kp_params.class_id)
    if len(all_keypoint_indices) > len(set(all_keypoint_indices)):
      raise ValueError('Some keypoint indices are used more than once.')
1135
1136
1137
1138
1139

  mask_params = None
  if center_net_config.HasField('mask_estimation_task'):
    mask_params = mask_proto_to_params(center_net_config.mask_estimation_task)

1140
1141
1142
1143
1144
  densepose_params = None
  if center_net_config.HasField('densepose_estimation_task'):
    densepose_params = densepose_proto_to_params(
        center_net_config.densepose_estimation_task)

1145
1146
1147
1148
1149
  track_params = None
  if center_net_config.HasField('track_estimation_task'):
    track_params = tracking_proto_to_params(
        center_net_config.track_estimation_task)

1150
1151
1152
1153
  temporal_offset_params = None
  if center_net_config.HasField('temporal_offset_task'):
    temporal_offset_params = temporal_offset_proto_to_params(
        center_net_config.temporal_offset_task)
1154
1155
1156
1157
  non_max_suppression_fn = None
  if center_net_config.HasField('post_processing'):
    non_max_suppression_fn, _ = post_processing_builder.build(
        center_net_config.post_processing)
Vighnesh Birodkar's avatar
Vighnesh Birodkar committed
1158

1159
1160
1161
1162
1163
1164
1165
1166
  return center_net_meta_arch.CenterNetMetaArch(
      is_training=is_training,
      add_summaries=add_summaries,
      num_classes=center_net_config.num_classes,
      feature_extractor=feature_extractor,
      image_resizer_fn=image_resizer_fn,
      object_center_params=object_center_params,
      object_detection_params=object_detection_params,
1167
      keypoint_params_dict=keypoint_params_dict,
1168
      mask_params=mask_params,
1169
      densepose_params=densepose_params,
1170
      track_params=track_params,
1171
      temporal_offset_params=temporal_offset_params,
1172
      use_depthwise=center_net_config.use_depthwise,
1173
1174
      compute_heatmap_sparse=center_net_config.compute_heatmap_sparse,
      non_max_suppression_fn=non_max_suppression_fn)
1175
1176


A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1177
def _build_center_net_feature_extractor(feature_extractor_config, is_training):
1178
1179
1180
1181
1182
  """Build a CenterNet feature extractor from the given config."""

  if feature_extractor_config.type not in CENTER_NET_EXTRACTOR_FUNCTION_MAP:
    raise ValueError('\'{}\' is not a known CenterNet feature extractor type'
                     .format(feature_extractor_config.type))
1183
1184
1185
1186
  # For backwards compatibility:
  use_separable_conv = (
      feature_extractor_config.use_separable_conv or
      feature_extractor_config.type == 'mobilenet_v2_fpn_sep_conv')
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1187
  kwargs = {
1188
1189
1190
1191
1192
1193
      'channel_means':
          list(feature_extractor_config.channel_means),
      'channel_stds':
          list(feature_extractor_config.channel_stds),
      'bgr_ordering':
          feature_extractor_config.bgr_ordering,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1194
  }
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
  if feature_extractor_config.HasField('depth_multiplier'):
    kwargs.update({
        'depth_multiplier': feature_extractor_config.depth_multiplier,
    })
  if feature_extractor_config.HasField('use_separable_conv'):
    kwargs.update({
        'use_separable_conv': use_separable_conv,
    })
  if feature_extractor_config.HasField('upsampling_interpolation'):
    kwargs.update({
        'upsampling_interpolation':
            feature_extractor_config.upsampling_interpolation,
    })
  if feature_extractor_config.HasField('use_depthwise'):
    kwargs.update({
        'use_depthwise': feature_extractor_config.use_depthwise,
    })
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1212

1213
1214

  return CENTER_NET_EXTRACTOR_FUNCTION_MAP[feature_extractor_config.type](
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1215
      **kwargs)
1216
1217
1218


META_ARCH_BUILDER_MAP = {
1219
1220
    'ssd': _build_ssd_model,
    'faster_rcnn': _build_faster_rcnn_model,
1221
1222
    'experimental_model': _build_experimental_model,
    'center_net': _build_center_net_model
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
}


def build(model_config, is_training, add_summaries=True):
  """Builds a DetectionModel based on the model config.

  Args:
    model_config: A model.proto object containing the config for the desired
      DetectionModel.
    is_training: True if this model is being built for training purposes.
    add_summaries: Whether to add tensorflow summaries in the model graph.
  Returns:
    DetectionModel based on the config.

  Raises:
    ValueError: On invalid meta architecture or model.
  """
  if not isinstance(model_config, model_pb2.DetectionModel):
    raise ValueError('model_config not of type model_pb2.DetectionModel.')

  meta_architecture = model_config.WhichOneof('model')

1245
  if meta_architecture not in META_ARCH_BUILDER_MAP:
1246
1247
    raise ValueError('Unknown meta architecture: {}'.format(meta_architecture))
  else:
1248
    build_func = META_ARCH_BUILDER_MAP[meta_architecture]
1249
1250
    return build_func(getattr(model_config, meta_architecture), is_training,
                      add_summaries)