You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

523 lines
23 KiB

6 years ago
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """A function to build a DetectionModel from configuration."""
  16. import functools
  17. from object_detection.builders import anchor_generator_builder
  18. from object_detection.builders import box_coder_builder
  19. from object_detection.builders import box_predictor_builder
  20. from object_detection.builders import hyperparams_builder
  21. from object_detection.builders import image_resizer_builder
  22. from object_detection.builders import losses_builder
  23. from object_detection.builders import matcher_builder
  24. from object_detection.builders import post_processing_builder
  25. from object_detection.builders import region_similarity_calculator_builder as sim_calc
  26. from object_detection.core import balanced_positive_negative_sampler as sampler
  27. from object_detection.core import post_processing
  28. from object_detection.core import target_assigner
  29. from object_detection.meta_architectures import faster_rcnn_meta_arch
  30. from object_detection.meta_architectures import rfcn_meta_arch
  31. from object_detection.meta_architectures import ssd_meta_arch
  32. from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
  33. from object_detection.models import faster_rcnn_inception_v2_feature_extractor as frcnn_inc_v2
  34. from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_nas
  35. from object_detection.models import faster_rcnn_pnas_feature_extractor as frcnn_pnas
  36. from object_detection.models import faster_rcnn_resnet_v1_feature_extractor as frcnn_resnet_v1
  37. from object_detection.models import ssd_resnet_v1_fpn_feature_extractor as ssd_resnet_v1_fpn
  38. from object_detection.models import ssd_resnet_v1_ppn_feature_extractor as ssd_resnet_v1_ppn
  39. from object_detection.models.embedded_ssd_mobilenet_v1_feature_extractor import EmbeddedSSDMobileNetV1FeatureExtractor
  40. from object_detection.models.ssd_inception_v2_feature_extractor import SSDInceptionV2FeatureExtractor
  41. from object_detection.models.ssd_inception_v3_feature_extractor import SSDInceptionV3FeatureExtractor
  42. from object_detection.models.ssd_mobilenet_v1_feature_extractor import SSDMobileNetV1FeatureExtractor
  43. from object_detection.models.ssd_mobilenet_v1_fpn_feature_extractor import SSDMobileNetV1FpnFeatureExtractor
  44. from object_detection.models.ssd_mobilenet_v1_keras_feature_extractor import SSDMobileNetV1KerasFeatureExtractor
  45. from object_detection.models.ssd_mobilenet_v1_ppn_feature_extractor import SSDMobileNetV1PpnFeatureExtractor
  46. from object_detection.models.ssd_mobilenet_v2_feature_extractor import SSDMobileNetV2FeatureExtractor
  47. from object_detection.models.ssd_mobilenet_v2_fpn_feature_extractor import SSDMobileNetV2FpnFeatureExtractor
  48. from object_detection.models.ssd_mobilenet_v2_keras_feature_extractor import SSDMobileNetV2KerasFeatureExtractor
  49. from object_detection.models.ssd_pnasnet_feature_extractor import SSDPNASNetFeatureExtractor
  50. from object_detection.predictors import rfcn_box_predictor
  51. from object_detection.predictors.heads import mask_head
  52. from object_detection.protos import model_pb2
  53. from object_detection.utils import ops
  54. # A map of names to SSD feature extractors.
  55. SSD_FEATURE_EXTRACTOR_CLASS_MAP = {
  56. 'ssd_inception_v2': SSDInceptionV2FeatureExtractor,
  57. 'ssd_inception_v3': SSDInceptionV3FeatureExtractor,
  58. 'ssd_mobilenet_v1': SSDMobileNetV1FeatureExtractor,
  59. 'ssd_mobilenet_v1_fpn': SSDMobileNetV1FpnFeatureExtractor,
  60. 'ssd_mobilenet_v1_ppn': SSDMobileNetV1PpnFeatureExtractor,
  61. 'ssd_mobilenet_v2': SSDMobileNetV2FeatureExtractor,
  62. 'ssd_mobilenet_v2_fpn': SSDMobileNetV2FpnFeatureExtractor,
  63. 'ssd_resnet50_v1_fpn': ssd_resnet_v1_fpn.SSDResnet50V1FpnFeatureExtractor,
  64. 'ssd_resnet101_v1_fpn': ssd_resnet_v1_fpn.SSDResnet101V1FpnFeatureExtractor,
  65. 'ssd_resnet152_v1_fpn': ssd_resnet_v1_fpn.SSDResnet152V1FpnFeatureExtractor,
  66. 'ssd_resnet50_v1_ppn': ssd_resnet_v1_ppn.SSDResnet50V1PpnFeatureExtractor,
  67. 'ssd_resnet101_v1_ppn':
  68. ssd_resnet_v1_ppn.SSDResnet101V1PpnFeatureExtractor,
  69. 'ssd_resnet152_v1_ppn':
  70. ssd_resnet_v1_ppn.SSDResnet152V1PpnFeatureExtractor,
  71. 'embedded_ssd_mobilenet_v1': EmbeddedSSDMobileNetV1FeatureExtractor,
  72. 'ssd_pnasnet': SSDPNASNetFeatureExtractor,
  73. }
  74. SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = {
  75. 'ssd_mobilenet_v1_keras': SSDMobileNetV1KerasFeatureExtractor,
  76. 'ssd_mobilenet_v2_keras': SSDMobileNetV2KerasFeatureExtractor
  77. }
  78. # A map of names to Faster R-CNN feature extractors.
  79. FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP = {
  80. 'faster_rcnn_nas':
  81. frcnn_nas.FasterRCNNNASFeatureExtractor,
  82. 'faster_rcnn_pnas':
  83. frcnn_pnas.FasterRCNNPNASFeatureExtractor,
  84. 'faster_rcnn_inception_resnet_v2':
  85. frcnn_inc_res.FasterRCNNInceptionResnetV2FeatureExtractor,
  86. 'faster_rcnn_inception_v2':
  87. frcnn_inc_v2.FasterRCNNInceptionV2FeatureExtractor,
  88. 'faster_rcnn_resnet50':
  89. frcnn_resnet_v1.FasterRCNNResnet50FeatureExtractor,
  90. 'faster_rcnn_resnet101':
  91. frcnn_resnet_v1.FasterRCNNResnet101FeatureExtractor,
  92. 'faster_rcnn_resnet152':
  93. frcnn_resnet_v1.FasterRCNNResnet152FeatureExtractor,
  94. }
  95. def build(model_config, is_training, add_summaries=True):
  96. """Builds a DetectionModel based on the model config.
  97. Args:
  98. model_config: A model.proto object containing the config for the desired
  99. DetectionModel.
  100. is_training: True if this model is being built for training purposes.
  101. add_summaries: Whether to add tensorflow summaries in the model graph.
  102. Returns:
  103. DetectionModel based on the config.
  104. Raises:
  105. ValueError: On invalid meta architecture or model.
  106. """
  107. if not isinstance(model_config, model_pb2.DetectionModel):
  108. raise ValueError('model_config not of type model_pb2.DetectionModel.')
  109. meta_architecture = model_config.WhichOneof('model')
  110. if meta_architecture == 'ssd':
  111. return _build_ssd_model(model_config.ssd, is_training, add_summaries)
  112. if meta_architecture == 'faster_rcnn':
  113. return _build_faster_rcnn_model(model_config.faster_rcnn, is_training,
  114. add_summaries)
  115. raise ValueError('Unknown meta architecture: {}'.format(meta_architecture))
  116. def _build_ssd_feature_extractor(feature_extractor_config,
  117. is_training,
  118. freeze_batchnorm,
  119. reuse_weights=None):
  120. """Builds a ssd_meta_arch.SSDFeatureExtractor based on config.
  121. Args:
  122. feature_extractor_config: A SSDFeatureExtractor proto config from ssd.proto.
  123. is_training: True if this feature extractor is being built for training.
  124. freeze_batchnorm: Whether to freeze batch norm parameters during
  125. training or not. When training with a small batch size (e.g. 1), it is
  126. desirable to freeze batch norm update and use pretrained batch norm
  127. params.
  128. reuse_weights: if the feature extractor should reuse weights.
  129. Returns:
  130. ssd_meta_arch.SSDFeatureExtractor based on config.
  131. Raises:
  132. ValueError: On invalid feature extractor type.
  133. """
  134. feature_type = feature_extractor_config.type
  135. is_keras_extractor = feature_type in SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP
  136. depth_multiplier = feature_extractor_config.depth_multiplier
  137. min_depth = feature_extractor_config.min_depth
  138. pad_to_multiple = feature_extractor_config.pad_to_multiple
  139. use_explicit_padding = feature_extractor_config.use_explicit_padding
  140. use_depthwise = feature_extractor_config.use_depthwise
  141. if is_keras_extractor:
  142. conv_hyperparams = hyperparams_builder.KerasLayerHyperparams(
  143. feature_extractor_config.conv_hyperparams)
  144. else:
  145. conv_hyperparams = hyperparams_builder.build(
  146. feature_extractor_config.conv_hyperparams, is_training)
  147. override_base_feature_extractor_hyperparams = (
  148. feature_extractor_config.override_base_feature_extractor_hyperparams)
  149. if (feature_type not in SSD_FEATURE_EXTRACTOR_CLASS_MAP) and (
  150. not is_keras_extractor):
  151. raise ValueError('Unknown ssd feature_extractor: {}'.format(feature_type))
  152. if is_keras_extractor:
  153. feature_extractor_class = SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP[
  154. feature_type]
  155. else:
  156. feature_extractor_class = SSD_FEATURE_EXTRACTOR_CLASS_MAP[feature_type]
  157. kwargs = {
  158. 'is_training':
  159. is_training,
  160. 'depth_multiplier':
  161. depth_multiplier,
  162. 'min_depth':
  163. min_depth,
  164. 'pad_to_multiple':
  165. pad_to_multiple,
  166. 'use_explicit_padding':
  167. use_explicit_padding,
  168. 'use_depthwise':
  169. use_depthwise,
  170. 'override_base_feature_extractor_hyperparams':
  171. override_base_feature_extractor_hyperparams
  172. }
  173. if feature_extractor_config.HasField('replace_preprocessor_with_placeholder'):
  174. kwargs.update({
  175. 'replace_preprocessor_with_placeholder':
  176. feature_extractor_config.replace_preprocessor_with_placeholder
  177. })
  178. if is_keras_extractor:
  179. kwargs.update({
  180. 'conv_hyperparams': conv_hyperparams,
  181. 'inplace_batchnorm_update': False,
  182. 'freeze_batchnorm': freeze_batchnorm
  183. })
  184. else:
  185. kwargs.update({
  186. 'conv_hyperparams_fn': conv_hyperparams,
  187. 'reuse_weights': reuse_weights,
  188. })
  189. if feature_extractor_config.HasField('fpn'):
  190. kwargs.update({
  191. 'fpn_min_level':
  192. feature_extractor_config.fpn.min_level,
  193. 'fpn_max_level':
  194. feature_extractor_config.fpn.max_level,
  195. 'additional_layer_depth':
  196. feature_extractor_config.fpn.additional_layer_depth,
  197. })
  198. return feature_extractor_class(**kwargs)
  199. def _build_ssd_model(ssd_config, is_training, add_summaries):
  200. """Builds an SSD detection model based on the model config.
  201. Args:
  202. ssd_config: A ssd.proto object containing the config for the desired
  203. SSDMetaArch.
  204. is_training: True if this model is being built for training purposes.
  205. add_summaries: Whether to add tf summaries in the model.
  206. Returns:
  207. SSDMetaArch based on the config.
  208. Raises:
  209. ValueError: If ssd_config.type is not recognized (i.e. not registered in
  210. model_class_map).
  211. """
  212. num_classes = ssd_config.num_classes
  213. # Feature extractor
  214. feature_extractor = _build_ssd_feature_extractor(
  215. feature_extractor_config=ssd_config.feature_extractor,
  216. freeze_batchnorm=ssd_config.freeze_batchnorm,
  217. is_training=is_training)
  218. box_coder = box_coder_builder.build(ssd_config.box_coder)
  219. matcher = matcher_builder.build(ssd_config.matcher)
  220. region_similarity_calculator = sim_calc.build(
  221. ssd_config.similarity_calculator)
  222. encode_background_as_zeros = ssd_config.encode_background_as_zeros
  223. negative_class_weight = ssd_config.negative_class_weight
  224. anchor_generator = anchor_generator_builder.build(
  225. ssd_config.anchor_generator)
  226. if feature_extractor.is_keras_model:
  227. ssd_box_predictor = box_predictor_builder.build_keras(
  228. conv_hyperparams_fn=hyperparams_builder.KerasLayerHyperparams,
  229. freeze_batchnorm=ssd_config.freeze_batchnorm,
  230. inplace_batchnorm_update=False,
  231. num_predictions_per_location_list=anchor_generator
  232. .num_anchors_per_location(),
  233. box_predictor_config=ssd_config.box_predictor,
  234. is_training=is_training,
  235. num_classes=num_classes,
  236. add_background_class=ssd_config.add_background_class)
  237. else:
  238. ssd_box_predictor = box_predictor_builder.build(
  239. hyperparams_builder.build, ssd_config.box_predictor, is_training,
  240. num_classes, ssd_config.add_background_class)
  241. image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer)
  242. non_max_suppression_fn, score_conversion_fn = post_processing_builder.build(
  243. ssd_config.post_processing)
  244. (classification_loss, localization_loss, classification_weight,
  245. localization_weight, hard_example_miner, random_example_sampler,
  246. expected_loss_weights_fn) = losses_builder.build(ssd_config.loss)
  247. normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches
  248. normalize_loc_loss_by_codesize = ssd_config.normalize_loc_loss_by_codesize
  249. equalization_loss_config = ops.EqualizationLossConfig(
  250. weight=ssd_config.loss.equalization_loss.weight,
  251. exclude_prefixes=ssd_config.loss.equalization_loss.exclude_prefixes)
  252. target_assigner_instance = target_assigner.TargetAssigner(
  253. region_similarity_calculator,
  254. matcher,
  255. box_coder,
  256. negative_class_weight=negative_class_weight)
  257. ssd_meta_arch_fn = ssd_meta_arch.SSDMetaArch
  258. kwargs = {}
  259. return ssd_meta_arch_fn(
  260. is_training=is_training,
  261. anchor_generator=anchor_generator,
  262. box_predictor=ssd_box_predictor,
  263. box_coder=box_coder,
  264. feature_extractor=feature_extractor,
  265. encode_background_as_zeros=encode_background_as_zeros,
  266. image_resizer_fn=image_resizer_fn,
  267. non_max_suppression_fn=non_max_suppression_fn,
  268. score_conversion_fn=score_conversion_fn,
  269. classification_loss=classification_loss,
  270. localization_loss=localization_loss,
  271. classification_loss_weight=classification_weight,
  272. localization_loss_weight=localization_weight,
  273. normalize_loss_by_num_matches=normalize_loss_by_num_matches,
  274. hard_example_miner=hard_example_miner,
  275. target_assigner_instance=target_assigner_instance,
  276. add_summaries=add_summaries,
  277. normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize,
  278. freeze_batchnorm=ssd_config.freeze_batchnorm,
  279. inplace_batchnorm_update=ssd_config.inplace_batchnorm_update,
  280. add_background_class=ssd_config.add_background_class,
  281. explicit_background_class=ssd_config.explicit_background_class,
  282. random_example_sampler=random_example_sampler,
  283. expected_loss_weights_fn=expected_loss_weights_fn,
  284. use_confidences_as_targets=ssd_config.use_confidences_as_targets,
  285. implicit_example_weight=ssd_config.implicit_example_weight,
  286. equalization_loss_config=equalization_loss_config,
  287. **kwargs)
  288. def _build_faster_rcnn_feature_extractor(
  289. feature_extractor_config, is_training, reuse_weights=None,
  290. inplace_batchnorm_update=False):
  291. """Builds a faster_rcnn_meta_arch.FasterRCNNFeatureExtractor based on config.
  292. Args:
  293. feature_extractor_config: A FasterRcnnFeatureExtractor proto config from
  294. faster_rcnn.proto.
  295. is_training: True if this feature extractor is being built for training.
  296. reuse_weights: if the feature extractor should reuse weights.
  297. inplace_batchnorm_update: Whether to update batch_norm inplace during
  298. training. This is required for batch norm to work correctly on TPUs. When
  299. this is false, user must add a control dependency on
  300. tf.GraphKeys.UPDATE_OPS for train/loss op in order to update the batch
  301. norm moving average parameters.
  302. Returns:
  303. faster_rcnn_meta_arch.FasterRCNNFeatureExtractor based on config.
  304. Raises:
  305. ValueError: On invalid feature extractor type.
  306. """
  307. if inplace_batchnorm_update:
  308. raise ValueError('inplace batchnorm updates not supported.')
  309. feature_type = feature_extractor_config.type
  310. first_stage_features_stride = (
  311. feature_extractor_config.first_stage_features_stride)
  312. batch_norm_trainable = feature_extractor_config.batch_norm_trainable
  313. if feature_type not in FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP:
  314. raise ValueError('Unknown Faster R-CNN feature_extractor: {}'.format(
  315. feature_type))
  316. feature_extractor_class = FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP[
  317. feature_type]
  318. return feature_extractor_class(
  319. is_training, first_stage_features_stride,
  320. batch_norm_trainable, reuse_weights)
  321. def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries):
  322. """Builds a Faster R-CNN or R-FCN detection model based on the model config.
  323. Builds R-FCN model if the second_stage_box_predictor in the config is of type
  324. `rfcn_box_predictor` else builds a Faster R-CNN model.
  325. Args:
  326. frcnn_config: A faster_rcnn.proto object containing the config for the
  327. desired FasterRCNNMetaArch or RFCNMetaArch.
  328. is_training: True if this model is being built for training purposes.
  329. add_summaries: Whether to add tf summaries in the model.
  330. Returns:
  331. FasterRCNNMetaArch based on the config.
  332. Raises:
  333. ValueError: If frcnn_config.type is not recognized (i.e. not registered in
  334. model_class_map).
  335. """
  336. num_classes = frcnn_config.num_classes
  337. image_resizer_fn = image_resizer_builder.build(frcnn_config.image_resizer)
  338. feature_extractor = _build_faster_rcnn_feature_extractor(
  339. frcnn_config.feature_extractor, is_training,
  340. inplace_batchnorm_update=frcnn_config.inplace_batchnorm_update)
  341. number_of_stages = frcnn_config.number_of_stages
  342. first_stage_anchor_generator = anchor_generator_builder.build(
  343. frcnn_config.first_stage_anchor_generator)
  344. first_stage_target_assigner = target_assigner.create_target_assigner(
  345. 'FasterRCNN',
  346. 'proposal',
  347. use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher)
  348. first_stage_atrous_rate = frcnn_config.first_stage_atrous_rate
  349. first_stage_box_predictor_arg_scope_fn = hyperparams_builder.build(
  350. frcnn_config.first_stage_box_predictor_conv_hyperparams, is_training)
  351. first_stage_box_predictor_kernel_size = (
  352. frcnn_config.first_stage_box_predictor_kernel_size)
  353. first_stage_box_predictor_depth = frcnn_config.first_stage_box_predictor_depth
  354. first_stage_minibatch_size = frcnn_config.first_stage_minibatch_size
  355. use_static_shapes = frcnn_config.use_static_shapes and (
  356. frcnn_config.use_static_shapes_for_eval or is_training)
  357. first_stage_sampler = sampler.BalancedPositiveNegativeSampler(
  358. positive_fraction=frcnn_config.first_stage_positive_balance_fraction,
  359. is_static=(frcnn_config.use_static_balanced_label_sampler and
  360. use_static_shapes))
  361. first_stage_max_proposals = frcnn_config.first_stage_max_proposals
  362. if (frcnn_config.first_stage_nms_iou_threshold < 0 or
  363. frcnn_config.first_stage_nms_iou_threshold > 1.0):
  364. raise ValueError('iou_threshold not in [0, 1.0].')
  365. if (is_training and frcnn_config.second_stage_batch_size >
  366. first_stage_max_proposals):
  367. raise ValueError('second_stage_batch_size should be no greater than '
  368. 'first_stage_max_proposals.')
  369. first_stage_non_max_suppression_fn = functools.partial(
  370. post_processing.batch_multiclass_non_max_suppression,
  371. score_thresh=frcnn_config.first_stage_nms_score_threshold,
  372. iou_thresh=frcnn_config.first_stage_nms_iou_threshold,
  373. max_size_per_class=frcnn_config.first_stage_max_proposals,
  374. max_total_size=frcnn_config.first_stage_max_proposals,
  375. use_static_shapes=use_static_shapes)
  376. first_stage_loc_loss_weight = (
  377. frcnn_config.first_stage_localization_loss_weight)
  378. first_stage_obj_loss_weight = frcnn_config.first_stage_objectness_loss_weight
  379. initial_crop_size = frcnn_config.initial_crop_size
  380. maxpool_kernel_size = frcnn_config.maxpool_kernel_size
  381. maxpool_stride = frcnn_config.maxpool_stride
  382. second_stage_target_assigner = target_assigner.create_target_assigner(
  383. 'FasterRCNN',
  384. 'detection',
  385. use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher)
  386. second_stage_box_predictor = box_predictor_builder.build(
  387. hyperparams_builder.build,
  388. frcnn_config.second_stage_box_predictor,
  389. is_training=is_training,
  390. num_classes=num_classes)
  391. second_stage_batch_size = frcnn_config.second_stage_batch_size
  392. second_stage_sampler = sampler.BalancedPositiveNegativeSampler(
  393. positive_fraction=frcnn_config.second_stage_balance_fraction,
  394. is_static=(frcnn_config.use_static_balanced_label_sampler and
  395. use_static_shapes))
  396. (second_stage_non_max_suppression_fn, second_stage_score_conversion_fn
  397. ) = post_processing_builder.build(frcnn_config.second_stage_post_processing)
  398. second_stage_localization_loss_weight = (
  399. frcnn_config.second_stage_localization_loss_weight)
  400. second_stage_classification_loss = (
  401. losses_builder.build_faster_rcnn_classification_loss(
  402. frcnn_config.second_stage_classification_loss))
  403. second_stage_classification_loss_weight = (
  404. frcnn_config.second_stage_classification_loss_weight)
  405. second_stage_mask_prediction_loss_weight = (
  406. frcnn_config.second_stage_mask_prediction_loss_weight)
  407. hard_example_miner = None
  408. if frcnn_config.HasField('hard_example_miner'):
  409. hard_example_miner = losses_builder.build_hard_example_miner(
  410. frcnn_config.hard_example_miner,
  411. second_stage_classification_loss_weight,
  412. second_stage_localization_loss_weight)
  413. crop_and_resize_fn = (
  414. ops.matmul_crop_and_resize if frcnn_config.use_matmul_crop_and_resize
  415. else ops.native_crop_and_resize)
  416. clip_anchors_to_image = (
  417. frcnn_config.clip_anchors_to_image)
  418. common_kwargs = {
  419. 'is_training': is_training,
  420. 'num_classes': num_classes,
  421. 'image_resizer_fn': image_resizer_fn,
  422. 'feature_extractor': feature_extractor,
  423. 'number_of_stages': number_of_stages,
  424. 'first_stage_anchor_generator': first_stage_anchor_generator,
  425. 'first_stage_target_assigner': first_stage_target_assigner,
  426. 'first_stage_atrous_rate': first_stage_atrous_rate,
  427. 'first_stage_box_predictor_arg_scope_fn':
  428. first_stage_box_predictor_arg_scope_fn,
  429. 'first_stage_box_predictor_kernel_size':
  430. first_stage_box_predictor_kernel_size,
  431. 'first_stage_box_predictor_depth': first_stage_box_predictor_depth,
  432. 'first_stage_minibatch_size': first_stage_minibatch_size,
  433. 'first_stage_sampler': first_stage_sampler,
  434. 'first_stage_non_max_suppression_fn': first_stage_non_max_suppression_fn,
  435. 'first_stage_max_proposals': first_stage_max_proposals,
  436. 'first_stage_localization_loss_weight': first_stage_loc_loss_weight,
  437. 'first_stage_objectness_loss_weight': first_stage_obj_loss_weight,
  438. 'second_stage_target_assigner': second_stage_target_assigner,
  439. 'second_stage_batch_size': second_stage_batch_size,
  440. 'second_stage_sampler': second_stage_sampler,
  441. 'second_stage_non_max_suppression_fn':
  442. second_stage_non_max_suppression_fn,
  443. 'second_stage_score_conversion_fn': second_stage_score_conversion_fn,
  444. 'second_stage_localization_loss_weight':
  445. second_stage_localization_loss_weight,
  446. 'second_stage_classification_loss':
  447. second_stage_classification_loss,
  448. 'second_stage_classification_loss_weight':
  449. second_stage_classification_loss_weight,
  450. 'hard_example_miner': hard_example_miner,
  451. 'add_summaries': add_summaries,
  452. 'crop_and_resize_fn': crop_and_resize_fn,
  453. 'clip_anchors_to_image': clip_anchors_to_image,
  454. 'use_static_shapes': use_static_shapes,
  455. 'resize_masks': frcnn_config.resize_masks
  456. }
  457. if isinstance(second_stage_box_predictor,
  458. rfcn_box_predictor.RfcnBoxPredictor):
  459. return rfcn_meta_arch.RFCNMetaArch(
  460. second_stage_rfcn_box_predictor=second_stage_box_predictor,
  461. **common_kwargs)
  462. else:
  463. return faster_rcnn_meta_arch.FasterRCNNMetaArch(
  464. initial_crop_size=initial_crop_size,
  465. maxpool_kernel_size=maxpool_kernel_size,
  466. maxpool_stride=maxpool_stride,
  467. second_stage_mask_rcnn_box_predictor=second_stage_box_predictor,
  468. second_stage_mask_prediction_loss_weight=(
  469. second_stage_mask_prediction_loss_weight),
  470. **common_kwargs)