Skip to content

Commit 1e1333f

Browse files
yeqinglitensorflower-gardener
authored andcommitted
Internal change
PiperOrigin-RevId: 288388231
1 parent fc4ce16 commit 1e1333f

File tree

10 files changed

+226
-147
lines changed

10 files changed

+226
-147
lines changed

official/modeling/training/distributed_executor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,10 @@ def train_step(iterator, num_steps):
248248
_replicated_step, args=(next(iterator),))
249249

250250
# For reporting, we returns the mean of losses.
251-
loss = strategy.reduce(
252-
tf.distribute.ReduceOp.MEAN, per_replica_losses, axis=None)
253-
return loss
251+
losses = tf.nest.map_structure(
252+
lambda x: strategy.reduce(tf.distribute.ReduceOp.MEAN, x, axis=None),
253+
per_replica_losses)
254+
return losses
254255

255256
return train_step
256257

official/vision/detection/configs/maskrcnn_config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
'min_level': 2,
7272
'max_level': 6,
7373
'anchors_per_location': 3,
74+
'num_convs': 2,
75+
'num_filters': 256,
76+
'use_separable_conv': False,
7477
'use_batch_norm': False,
7578
'batch_norm': {
7679
'batch_norm_momentum': 0.997,
@@ -83,7 +86,11 @@
8386
# Note that `num_classes` is the total number of classes including
8487
# one background classes whose index is 0.
8588
'num_classes': 91,
86-
'fast_rcnn_mlp_head_dim': 1024,
89+
'num_convs': 0,
90+
'num_filters': 256,
91+
'use_separable_conv': False,
92+
'num_fcs': 2,
93+
'fc_dims': 1024,
8794
'use_batch_norm': False,
8895
'batch_norm': {
8996
'batch_norm_momentum': 0.997,
@@ -95,6 +102,9 @@
95102
'mrcnn_head': {
96103
'num_classes': 91,
97104
'mask_target_size': 28,
105+
'num_convs': 4,
106+
'num_filters': 256,
107+
'use_separable_conv': False,
98108
'use_batch_norm': False,
99109
'batch_norm': {
100110
'batch_norm_momentum': 0.997,

official/vision/detection/dataloader/maskrcnn_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def _parse_predict_data(self, data):
353353
self._anchor_size,
354354
(image_height, image_width))
355355

356-
labels = {}
356+
labels = {
357+
'image_info': image_info,
358+
}
357359

358360
if self._mode == ModeKeys.PREDICT_WITH_GT:
359361
# Converts boxes from normalized coordinates to pixel coordinates.

official/vision/detection/executor/detection_executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _replicated_step(inputs):
8282

8383
grads = tape.gradient(loss, trainable_variables)
8484
optimizer.apply_gradients(zip(grads, trainable_variables))
85-
return loss
85+
return losses
8686

8787
return _replicated_step
8888

official/vision/detection/modeling/architecture/factory.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,23 @@ def rpn_head_generator(params):
9494
return heads.RpnHead(params.min_level,
9595
params.max_level,
9696
params.anchors_per_location,
97+
params.num_convs,
98+
params.num_filters,
99+
params.use_separable_conv,
100+
params.use_batch_norm,
97101
batch_norm_relu=batch_norm_relu_generator(
98102
params.batch_norm))
99103

100104

101105
def fast_rcnn_head_generator(params):
102106
"""Generator function for Fast R-CNN head architecture."""
103107
return heads.FastrcnnHead(params.num_classes,
104-
params.fast_rcnn_mlp_head_dim,
108+
params.num_convs,
109+
params.num_filters,
110+
params.use_separable_conv,
111+
params.num_fcs,
112+
params.fc_dims,
113+
params.use_batch_norm,
105114
batch_norm_relu=batch_norm_relu_generator(
106115
params.batch_norm))
107116

@@ -110,6 +119,10 @@ def mask_rcnn_head_generator(params):
110119
"""Generator function for Mask R-CNN head architecture."""
111120
return heads.MaskrcnnHead(params.num_classes,
112121
params.mask_target_size,
122+
params.num_convs,
123+
params.num_filters,
124+
params.use_separable_conv,
125+
params.use_batch_norm,
113126
batch_norm_relu=batch_norm_relu_generator(
114127
params.batch_norm))
115128

official/vision/detection/modeling/architecture/fpn.py

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from __future__ import division
2525
from __future__ import print_function
2626

27+
import functools
28+
2729
import tensorflow.compat.v2 as tf
2830

2931
from tensorflow.python.keras import backend
@@ -39,6 +41,7 @@ def __init__(self,
3941
max_level=7,
4042
fpn_feat_dims=256,
4143
use_separable_conv=False,
44+
use_batch_norm=True,
4245
batch_norm_relu=nn_ops.BatchNormRelu):
4346
"""FPN initialization function.
4447
@@ -48,60 +51,46 @@ def __init__(self,
4851
fpn_feat_dims: `int` number of filters in FPN layers.
4952
use_separable_conv: `bool`, if True use separable convolution for
5053
convolution in FPN layers.
54+
use_batch_norm: 'bool', indicating whether batchnorm layers are added.
5155
batch_norm_relu: an operation that includes a batch normalization layer
5256
followed by a relu layer(optional).
5357
"""
5458
self._min_level = min_level
5559
self._max_level = max_level
5660
self._fpn_feat_dims = fpn_feat_dims
61+
if use_separable_conv:
62+
self._conv2d_op = functools.partial(
63+
tf.keras.layers.SeparableConv2D, depth_multiplier=1)
64+
else:
65+
self._conv2d_op = tf.keras.layers.Conv2D
66+
self._use_batch_norm = use_batch_norm
5767
self._batch_norm_relu = batch_norm_relu
5868

5969
self._batch_norm_relus = {}
6070
self._lateral_conv2d_op = {}
6171
self._post_hoc_conv2d_op = {}
6272
self._coarse_conv2d_op = {}
6373
for level in range(self._min_level, self._max_level + 1):
64-
self._batch_norm_relus[level] = batch_norm_relu(
65-
relu=False, name='p%d-bn' % level)
66-
if use_separable_conv:
67-
self._lateral_conv2d_op[level] = tf.keras.layers.SeparableConv2D(
68-
filters=self._fpn_feat_dims,
69-
kernel_size=(1, 1),
70-
padding='same',
71-
depth_multiplier=1,
72-
name='l%d' % level)
73-
self._post_hoc_conv2d_op[level] = tf.keras.layers.SeparableConv2D(
74-
filters=self._fpn_feat_dims,
75-
strides=(1, 1),
76-
kernel_size=(3, 3),
77-
padding='same',
78-
depth_multiplier=1,
79-
name='post_hoc_d%d' % level)
80-
self._coarse_conv2d_op[level] = tf.keras.layers.SeparableConv2D(
81-
filters=self._fpn_feat_dims,
82-
strides=(2, 2),
83-
kernel_size=(3, 3),
84-
padding='same',
85-
depth_multiplier=1,
86-
name='p%d' % level)
87-
else:
88-
self._lateral_conv2d_op[level] = tf.keras.layers.Conv2D(
89-
filters=self._fpn_feat_dims,
90-
kernel_size=(1, 1),
91-
padding='same',
92-
name='l%d' % level)
93-
self._post_hoc_conv2d_op[level] = tf.keras.layers.Conv2D(
94-
filters=self._fpn_feat_dims,
95-
strides=(1, 1),
96-
kernel_size=(3, 3),
97-
padding='same',
98-
name='post_hoc_d%d' % level)
99-
self._coarse_conv2d_op[level] = tf.keras.layers.Conv2D(
100-
filters=self._fpn_feat_dims,
101-
strides=(2, 2),
102-
kernel_size=(3, 3),
103-
padding='same',
104-
name='p%d' % level)
74+
if self._use_batch_norm:
75+
self._batch_norm_relus[level] = batch_norm_relu(
76+
relu=False, name='p%d-bn' % level)
77+
self._lateral_conv2d_op[level] = self._conv2d_op(
78+
filters=self._fpn_feat_dims,
79+
kernel_size=(1, 1),
80+
padding='same',
81+
name='l%d' % level)
82+
self._post_hoc_conv2d_op[level] = self._conv2d_op(
83+
filters=self._fpn_feat_dims,
84+
strides=(1, 1),
85+
kernel_size=(3, 3),
86+
padding='same',
87+
name='post_hoc_d%d' % level)
88+
self._coarse_conv2d_op[level] = self._conv2d_op(
89+
filters=self._fpn_feat_dims,
90+
strides=(2, 2),
91+
kernel_size=(3, 3),
92+
padding='same',
93+
name='p%d' % level)
10594

10695
def __call__(self, multilevel_features, is_training=None):
10796
"""Returns the FPN features for a given multilevel features.
@@ -117,7 +106,7 @@ def __call__(self, multilevel_features, is_training=None):
117106
[min_level, min_level + 1, ..., max_level]. The values are corresponding
118107
FPN features with shape [batch_size, height_l, width_l, fpn_feat_dims].
119108
"""
120-
input_levels = multilevel_features.keys()
109+
input_levels = list(multilevel_features.keys())
121110
if min(input_levels) > self._min_level:
122111
raise ValueError(
123112
'The minimum backbone level %d should be '%(min(input_levels)) +
@@ -146,8 +135,9 @@ def __call__(self, multilevel_features, is_training=None):
146135
if level > backbone_max_level + 1:
147136
feats_in = tf.nn.relu(feats_in)
148137
feats[level] = self._coarse_conv2d_op[level](feats_in)
149-
# Adds batch_norm layer.
150-
for level in range(self._min_level, self._max_level + 1):
151-
feats[level] = self._batch_norm_relus[level](
152-
feats[level], is_training=is_training)
138+
if self._use_batch_norm:
139+
# Adds batch_norm layer.
140+
for level in range(self._min_level, self._max_level + 1):
141+
feats[level] = self._batch_norm_relus[level](
142+
feats[level], is_training=is_training)
153143
return feats

0 commit comments

Comments
 (0)