Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
a3a379a8
Commit
a3a379a8
authored
Jan 11, 2021
by
Yu-hui Chen
Committed by
TF Object Detection Team
Jan 11, 2021
Browse files
Added the option to build CenterNet MobileNetV2 model with separable convolution in the
FPN network. PiperOrigin-RevId: 351255218
parent
6e01e1cd
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
6 deletions
+65
-6
research/object_detection/builders/model_builder.py
research/object_detection/builders/model_builder.py
+3
-0
research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor.py
...n/models/center_net_mobilenet_v2_fpn_feature_extractor.py
+32
-6
research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor_tf2_test.py
...center_net_mobilenet_v2_fpn_feature_extractor_tf2_test.py
+30
-0
No files found.
research/object_detection/builders/model_builder.py
View file @
a3a379a8
...
@@ -159,6 +159,9 @@ if tf_version.is_tf2():
...
@@ -159,6 +159,9 @@ if tf_version.is_tf2():
center_net_mobilenet_v2_feature_extractor
.
mobilenet_v2
,
center_net_mobilenet_v2_feature_extractor
.
mobilenet_v2
,
'mobilenet_v2_fpn'
:
'mobilenet_v2_fpn'
:
center_net_mobilenet_v2_fpn_feature_extractor
.
mobilenet_v2_fpn
,
center_net_mobilenet_v2_fpn_feature_extractor
.
mobilenet_v2_fpn
,
'mobilenet_v2_fpn_sep_conv'
:
center_net_mobilenet_v2_fpn_feature_extractor
.
mobilenet_v2_fpn_sep_conv
,
}
}
FEATURE_EXTRACTOR_MAPS
=
[
FEATURE_EXTRACTOR_MAPS
=
[
...
...
research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor.py
View file @
a3a379a8
...
@@ -38,7 +38,8 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
...
@@ -38,7 +38,8 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
mobilenet_v2_net
,
mobilenet_v2_net
,
channel_means
=
(
0.
,
0.
,
0.
),
channel_means
=
(
0.
,
0.
,
0.
),
channel_stds
=
(
1.
,
1.
,
1.
),
channel_stds
=
(
1.
,
1.
,
1.
),
bgr_ordering
=
False
):
bgr_ordering
=
False
,
fpn_separable_conv
=
False
):
"""Intializes the feature extractor.
"""Intializes the feature extractor.
Args:
Args:
...
@@ -49,6 +50,8 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
...
@@ -49,6 +50,8 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
channel. Each channel will be divided by its standard deviation value.
channel. Each channel will be divided by its standard deviation value.
bgr_ordering: bool, if set will change the channel ordering to be in the
bgr_ordering: bool, if set will change the channel ordering to be in the
[blue, red, green] order.
[blue, red, green] order.
fpn_separable_conv: If set to True, all convolutional layers in the FPN
network will be replaced by separable convolutions.
"""
"""
super
(
CenterNetMobileNetV2FPNFeatureExtractor
,
self
).
__init__
(
super
(
CenterNetMobileNetV2FPNFeatureExtractor
,
self
).
__init__
(
...
@@ -72,6 +75,7 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
...
@@ -72,6 +75,7 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
# 7x7x1280, which we continually upsample, apply a residual on and merge.
# 7x7x1280, which we continually upsample, apply a residual on and merge.
# This results in a 56x56x24 output volume.
# This results in a 56x56x24 output volume.
top_layer
=
fpn_outputs
[
-
1
]
top_layer
=
fpn_outputs
[
-
1
]
# Use normal convolutional layer since the kernel_size is 1.
residual_op
=
tf
.
keras
.
layers
.
Conv2D
(
residual_op
=
tf
.
keras
.
layers
.
Conv2D
(
filters
=
64
,
kernel_size
=
1
,
strides
=
1
,
padding
=
'same'
)
filters
=
64
,
kernel_size
=
1
,
strides
=
1
,
padding
=
'same'
)
top_down
=
residual_op
(
top_layer
)
top_down
=
residual_op
(
top_layer
)
...
@@ -84,6 +88,7 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
...
@@ -84,6 +88,7 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
top_down
=
upsample_op
(
top_down
)
top_down
=
upsample_op
(
top_down
)
# Residual (skip-connection) from bottom-up pathway.
# Residual (skip-connection) from bottom-up pathway.
# Use normal convolutional layer since the kernel_size is 1.
residual_op
=
tf
.
keras
.
layers
.
Conv2D
(
residual_op
=
tf
.
keras
.
layers
.
Conv2D
(
filters
=
num_filters
,
kernel_size
=
1
,
strides
=
1
,
padding
=
'same'
)
filters
=
num_filters
,
kernel_size
=
1
,
strides
=
1
,
padding
=
'same'
)
residual
=
residual_op
(
fpn_outputs
[
level_ind
])
residual
=
residual_op
(
fpn_outputs
[
level_ind
])
...
@@ -91,6 +96,10 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
...
@@ -91,6 +96,10 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
# Merge.
# Merge.
top_down
=
top_down
+
residual
top_down
=
top_down
+
residual
next_num_filters
=
num_filters_list
[
i
+
1
]
if
i
+
1
<=
2
else
24
next_num_filters
=
num_filters_list
[
i
+
1
]
if
i
+
1
<=
2
else
24
if
fpn_separable_conv
:
conv
=
tf
.
keras
.
layers
.
SeparableConv2D
(
filters
=
next_num_filters
,
kernel_size
=
3
,
strides
=
1
,
padding
=
'same'
)
else
:
conv
=
tf
.
keras
.
layers
.
Conv2D
(
conv
=
tf
.
keras
.
layers
.
Conv2D
(
filters
=
next_num_filters
,
kernel_size
=
3
,
strides
=
1
,
padding
=
'same'
)
filters
=
next_num_filters
,
kernel_size
=
3
,
strides
=
1
,
padding
=
'same'
)
top_down
=
conv
(
top_down
)
top_down
=
conv
(
top_down
)
...
@@ -133,10 +142,27 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
...
@@ -133,10 +142,27 @@ class CenterNetMobileNetV2FPNFeatureExtractor(
def
mobilenet_v2_fpn
(
channel_means
,
channel_stds
,
bgr_ordering
):
def
mobilenet_v2_fpn
(
channel_means
,
channel_stds
,
bgr_ordering
):
"""The MobileNetV2+FPN backbone for CenterNet."""
"""The MobileNetV2+FPN backbone for CenterNet."""
# Set to
is
_training to True for now.
# Set to
batchnorm
_training to True for now.
network
=
mobilenetv2
.
mobilenet_v2
(
True
,
include_top
=
False
)
network
=
mobilenetv2
.
mobilenet_v2
(
batchnorm_training
=
True
,
include_top
=
False
)
return
CenterNetMobileNetV2FPNFeatureExtractor
(
return
CenterNetMobileNetV2FPNFeatureExtractor
(
network
,
network
,
channel_means
=
channel_means
,
channel_means
=
channel_means
,
channel_stds
=
channel_stds
,
channel_stds
=
channel_stds
,
bgr_ordering
=
bgr_ordering
)
bgr_ordering
=
bgr_ordering
,
fpn_separable_conv
=
False
)
def
mobilenet_v2_fpn_sep_conv
(
channel_means
,
channel_stds
,
bgr_ordering
):
"""Same as mobilenet_v2_fpn except with separable convolution in FPN."""
# Setting batchnorm_training to True, which will use the correct
# BatchNormalization layer strategy based on the current Keras learning phase.
# TODO(yuhuic): expriment with True vs. False to understand it's effect in
# practice.
network
=
mobilenetv2
.
mobilenet_v2
(
batchnorm_training
=
True
,
include_top
=
False
)
return
CenterNetMobileNetV2FPNFeatureExtractor
(
network
,
channel_means
=
channel_means
,
channel_stds
=
channel_stds
,
bgr_ordering
=
bgr_ordering
,
fpn_separable_conv
=
True
)
research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor_tf2_test.py
View file @
a3a379a8
...
@@ -41,6 +41,36 @@ class CenterNetMobileNetV2FPNFeatureExtractorTest(test_case.TestCase):
...
@@ -41,6 +41,36 @@ class CenterNetMobileNetV2FPNFeatureExtractorTest(test_case.TestCase):
outputs
=
self
.
execute
(
graph_fn
,
[])
outputs
=
self
.
execute
(
graph_fn
,
[])
self
.
assertEqual
(
outputs
.
shape
,
(
8
,
56
,
56
,
24
))
self
.
assertEqual
(
outputs
.
shape
,
(
8
,
56
,
56
,
24
))
# Pull out the FPN network.
output
=
model
.
get_layer
(
'model_1'
)
for
layer
in
output
.
layers
:
# All convolution layers should be normal 2D convolutions.
if
'conv'
in
layer
.
name
:
self
.
assertIsInstance
(
layer
,
tf
.
keras
.
layers
.
Conv2D
)
def
test_center_net_mobilenet_v2_fpn_feature_extractor_sep_conv
(
self
):
net
=
mobilenet_v2
.
mobilenet_v2
(
True
,
include_top
=
False
)
model
=
center_net_mobilenet_v2_fpn_feature_extractor
.
CenterNetMobileNetV2FPNFeatureExtractor
(
net
,
fpn_separable_conv
=
True
)
def
graph_fn
():
img
=
np
.
zeros
((
8
,
224
,
224
,
3
),
dtype
=
np
.
float32
)
processed_img
=
model
.
preprocess
(
img
)
return
model
(
processed_img
)
outputs
=
self
.
execute
(
graph_fn
,
[])
self
.
assertEqual
(
outputs
.
shape
,
(
8
,
56
,
56
,
24
))
# Pull out the FPN network.
output
=
model
.
get_layer
(
'model_1'
)
for
layer
in
output
.
layers
:
# Convolution layers with kernel size not equal to (1, 1) should be
# separable 2D convolutions.
if
'conv'
in
layer
.
name
and
layer
.
kernel_size
!=
(
1
,
1
):
self
.
assertIsInstance
(
layer
,
tf
.
keras
.
layers
.
SeparableConv2D
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
tf
.
test
.
main
()
tf
.
test
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment