Skip to content

Commit 451906e

Browse files
pkulzcsguadadreamdragon
authored
Release MobileDet code and model, and require tf_slim installation for OD API. (#8562)
* Merged commit includes the following changes: 311933687 by Sergio Guadarrama: Removes spurios use of tf.compat.v2, which results in spurious tf.compat.v1.compat.v2. Adds basic test to nasnet_utils. Replaces all remaining import tensorflow as tf with import tensorflow.compat.v1 as tf -- 311766063 by Sergio Guadarrama: Removes explicit tf.compat.v1 in all call sites (we already import tf.compat.v1, so this code was doing tf.compat.v1.compat.v1). The existing code worked in latest version of tensorflow, 2.2, (and 1.15) but not in 1.14 or in 2.0.0a, this CL fixes it. -- 311624958 by Sergio Guadarrama: Updates README that doesn't render properly in github documentation -- 310980959 by Sergio Guadarrama: Moves research_models/slim off tf.contrib.slim/layers/framework to tf_slim -- 310263156 by Sergio Guadarrama: Adds model breakdown for MobilenetV3 -- 308640516 by Sergio Guadarrama: Internal change 308244396 by Sergio Guadarrama: GroupNormalization support for MobilenetV3. -- 307475800 by Sergio Guadarrama: Internal change -- 302077708 by Sergio Guadarrama: Remove `disable_tf2` behavior from slim py_library targets -- 301208453 by Sergio Guadarrama: Automated refactoring to make code Python 3 compatible. -- 300816672 by Sergio Guadarrama: Internal change 299433840 by Sergio Guadarrama: Internal change 299221609 by Sergio Guadarrama: Explicitly disable Tensorflow v2 behaviors for all TF1.x binaries and tests -- 299179617 by Sergio Guadarrama: Internal change 299040784 by Sergio Guadarrama: Internal change 299036699 by Sergio Guadarrama: Internal change 298736510 by Sergio Guadarrama: Internal change 298732599 by Sergio Guadarrama: Internal change 298729507 by Sergio Guadarrama: Internal change 298253328 by Sergio Guadarrama: Internal change 297788346 by Sergio Guadarrama: Internal change 297785278 by Sergio Guadarrama: Internal change 297783127 by Sergio Guadarrama: Internal change 297725870 by Sergio Guadarrama: Internal change 297721811 by Sergio Guadarrama: Internal change 297711347 by Sergio Guadarrama: Internal change 297708059 by Sergio Guadarrama: Internal change 297701831 by Sergio Guadarrama: Internal change 297700038 by Sergio Guadarrama: Internal change 297670468 by Sergio Guadarrama: Internal change. -- 297350326 by Sergio Guadarrama: Explicitly replace "import tensorflow" with "tensorflow.compat.v1" for TF2.x migration -- 297201668 by Sergio Guadarrama: Explicitly replace "import tensorflow" with "tensorflow.compat.v1" for TF2.x migration -- 294483372 by Sergio Guadarrama: Internal change PiperOrigin-RevId: 311933687 * Merged commit includes the following changes: 312578615 by Menglong Zhu: Modify the LSTM feature extractors to be python 3 compatible. -- 311264357 by Menglong Zhu: Removes contrib.slim -- 308957207 by Menglong Zhu: Automated refactoring to make code Python 3 compatible. -- 306976470 by yongzhe: Internal change 306777559 by Menglong Zhu: Internal change -- 299232507 by lzyuan: Internal update. -- 299221735 by lzyuan: Add small epsilon on max_range for quantize_op to prevent range collapse. -- PiperOrigin-RevId: 312578615 * Merged commit includes the following changes: 310447280 by lzc: Internal changes. -- PiperOrigin-RevId: 310447280 Co-authored-by: Sergio Guadarrama <[email protected]> Co-authored-by: Menglong Zhu <[email protected]>
1 parent 73b5be6 commit 451906e

File tree

407 files changed

+10483
-5596
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

407 files changed

+10483
-5596
lines changed

research/lstm_object_detection/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen)
2-
![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg)
3-
41
# Tensorflow Mobile Video Object Detection
52

63
Tensorflow mobile video object detection implementation proposed in the
@@ -35,6 +32,7 @@ https://scholar.googleusercontent.com/scholar.bib?q=info:rLqvkztmWYgJ:scholar.go
3532
3633
3734
35+
3836

3937

4038
## Table of Contents

research/lstm_object_detection/inputs/seq_dataset_builder.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
that wraps the build function.
2424
"""
2525
import tensorflow.compat.v1 as tf
26-
from tensorflow.contrib import slim as contrib_slim
26+
import tf_slim as slim
27+
2728
from tensorflow.contrib.training.python.training import sequence_queueing_state_saver as sqss
2829
from lstm_object_detection.inputs import tf_sequence_example_decoder
2930
from lstm_object_detection.protos import input_reader_google_pb2
@@ -33,7 +34,7 @@
3334
from object_detection.protos import input_reader_pb2
3435
from object_detection.utils import ops as util_ops
3536

36-
parallel_reader = contrib_slim.parallel_reader
37+
parallel_reader = slim.parallel_reader
3738
# TODO(yinxiao): Make the following variable into configurable proto.
3839
# Padding size for the labeled objects in each frame. Here we assume each
3940
# frame has a total number of objects less than _PADDING_SIZE.

research/lstm_object_detection/inputs/tf_sequence_example_decoder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
tensorflow.SequenceExample protos.
2020
"""
2121
import tensorflow.compat.v1 as tf
22-
from tensorflow.contrib import slim as contrib_slim
22+
import tf_slim as slim
2323
from object_detection.core import data_decoder
2424
from object_detection.core import standard_fields as fields
2525

26-
tfexample_decoder = contrib_slim.tfexample_decoder
26+
tfexample_decoder = slim.tfexample_decoder
2727

2828

2929
class BoundingBoxSequence(tfexample_decoder.ItemHandler):

research/lstm_object_detection/lstm/lstm_cells.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
"""BottleneckConvLSTMCell implementation."""
1616

1717
import tensorflow.compat.v1 as tf
18-
19-
from tensorflow.contrib import layers as contrib_layers
18+
import tf_slim as slim
2019
from tensorflow.contrib import rnn as contrib_rnn
21-
from tensorflow.contrib import slim
2220
from tensorflow.contrib.framework.python.ops import variables as contrib_variables
2321
import lstm_object_detection.lstm.utils as lstm_utils
2422

@@ -121,7 +119,7 @@ def __call__(self, inputs, state, scope=None):
121119
if self._pre_bottleneck:
122120
bottleneck = inputs
123121
else:
124-
bottleneck = contrib_layers.separable_conv2d(
122+
bottleneck = slim.separable_conv2d(
125123
tf.concat([inputs, h], 3),
126124
self._num_units,
127125
self._filter_size,
@@ -133,7 +131,7 @@ def __call__(self, inputs, state, scope=None):
133131
if self._viz_gates:
134132
slim.summaries.add_histogram_summary(bottleneck, 'bottleneck')
135133

136-
concat = contrib_layers.separable_conv2d(
134+
concat = slim.separable_conv2d(
137135
bottleneck,
138136
4 * self._num_units,
139137
self._filter_size,
@@ -243,7 +241,7 @@ def pre_bottleneck(self, inputs, state, input_index):
243241
state = tf.reshape(state, [batch_size, height, width, -1])
244242
with tf.variable_scope('conv_lstm_cell', reuse=tf.AUTO_REUSE):
245243
scope_name = 'bottleneck_%d' % input_index
246-
inputs = contrib_layers.separable_conv2d(
244+
inputs = slim.separable_conv2d(
247245
tf.concat([inputs, state], 3),
248246
self.output_size[-1],
249247
self._filter_size,

research/lstm_object_detection/lstm/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ def quantize_op(inputs,
217217
# While training, collect EMAs of ranges seen, store in min_var, max_var.
218218
# TFLite requires that 0.0 is always in the [min; max] range.
219219
range_min = tf.minimum(tf.reduce_min(inputs), 0.0, 'SafeQuantRangeMin')
220-
range_max = tf.maximum(tf.reduce_max(inputs), 0.0, 'SafeQuantRangeMax')
220+
# We set the lower_bound of max_range to prevent range collapse.
221+
range_max = tf.maximum(tf.reduce_max(inputs), 1e-5, 'SafeQuantRangeMax')
221222
min_val = moving_averages.assign_moving_average(
222223
min_var, range_min, ema_decay, name='AssignMinEma')
223224
max_val = moving_averages.assign_moving_average(

research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch.py

-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import abc
2626
import re
2727
import tensorflow.compat.v1 as tf
28-
from tensorflow.contrib import slim as contrib_slim
2928

3029
from object_detection.core import box_list_ops
3130
from object_detection.core import matcher
@@ -34,8 +33,6 @@
3433
from object_detection.utils import ops
3534
from object_detection.utils import shape_utils
3635

37-
slim = contrib_slim
38-
3936

4037
class LSTMSSDMetaArch(ssd_meta_arch.SSDMetaArch):
4138
"""LSTM Meta-architecture definition."""

research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch_test.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import numpy as np
2525
import tensorflow.compat.v1 as tf
26-
from tensorflow.contrib import slim as contrib_slim
26+
import tf_slim as slim
2727

2828
from lstm_object_detection.lstm import lstm_cells
2929
from lstm_object_detection.meta_architectures import lstm_ssd_meta_arch
@@ -39,8 +39,6 @@
3939
from object_detection.utils import test_utils
4040

4141

42-
slim = contrib_slim
43-
4442
MAX_TOTAL_NUM_BOXES = 5
4543
NUM_CLASSES = 1
4644

research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""LSTDInterleavedFeatureExtractor which interleaves multiple MobileNet V2."""
1717

1818
import tensorflow.compat.v1 as tf
19-
from tensorflow.contrib import slim
19+
import tf_slim as slim
2020

2121
from tensorflow.python.framework import ops as tf_ops
2222
from lstm_object_detection.lstm import lstm_cells
@@ -134,9 +134,10 @@ def extract_base_features_small(self, preprocessed_inputs):
134134
scope_name = self._base_network_scope + '_2'
135135
with tf.variable_scope(scope_name, reuse=self._reuse_weights) as base_scope:
136136
if self._low_res:
137-
size_small = preprocessed_inputs.get_shape().as_list()[1] / 2
137+
height_small = preprocessed_inputs.get_shape().as_list()[1] // 2
138+
width_small = preprocessed_inputs.get_shape().as_list()[2] // 2
138139
inputs_small = tf.image.resize_images(preprocessed_inputs,
139-
[size_small, size_small])
140+
[height_small, width_small])
140141
# Create end point handle for tflite deployment.
141142
with tf.name_scope(None):
142143
inputs_small = tf.identity(
@@ -152,14 +153,17 @@ def extract_base_features_small(self, preprocessed_inputs):
152153
scope=base_scope)
153154
return net, end_points
154155

155-
def create_lstm_cell(self, batch_size, output_size, state_saver, state_name):
156+
def create_lstm_cell(self, batch_size, output_size, state_saver, state_name,
157+
dtype=tf.float32):
156158
"""Create the LSTM cell, and initialize state if necessary.
157159
158160
Args:
159161
batch_size: input batch size.
160162
output_size: output size of the lstm cell, [width, height].
161163
state_saver: a state saver object with methods `state` and `save_state`.
162164
state_name: string, the name to use with the state_saver.
165+
dtype: dtype to initialize lstm state.
166+
163167
Returns:
164168
lstm_cell: the lstm cell unit.
165169
init_state: initial state representations.
@@ -180,7 +184,7 @@ def create_lstm_cell(self, batch_size, output_size, state_saver, state_name):
180184
visualize_gates=False)
181185

182186
if state_saver is None:
183-
init_state = lstm_cell.init_state('lstm_state', batch_size, tf.float32)
187+
init_state = lstm_cell.init_state('lstm_state', batch_size, dtype)
184188
step = None
185189
else:
186190
step = state_saver.state(state_name + '_step')
@@ -222,7 +226,7 @@ def extract_features(self, preprocessed_inputs, state_saver=None,
222226
33, preprocessed_inputs)
223227
preprocessed_inputs = ops.pad_to_multiple(
224228
preprocessed_inputs, self._pad_to_multiple)
225-
batch_size = preprocessed_inputs.shape[0].value / unroll_length
229+
batch_size = preprocessed_inputs.shape[0].value // unroll_length
226230
batch_axis = 0
227231
nets = []
228232

@@ -250,7 +254,8 @@ def extract_features(self, preprocessed_inputs, state_saver=None,
250254
with tf.variable_scope('LSTM', reuse=self._reuse_weights):
251255
output_size = (large_base_feature_shape[1], large_base_feature_shape[2])
252256
lstm_cell, init_state, step = self.create_lstm_cell(
253-
batch_size, output_size, state_saver, state_name)
257+
batch_size, output_size, state_saver, state_name,
258+
dtype=preprocessed_inputs.dtype)
254259

255260
nets_seq = [
256261
tf.split(net, unroll_length, axis=batch_axis) for net in nets
@@ -269,15 +274,16 @@ def extract_features(self, preprocessed_inputs, state_saver=None,
269274
scope=None)
270275
self._states_out = states_out
271276

272-
batcher_ops = None
277+
image_features = {}
273278
if state_saver is not None:
274279
self._step = state_saver.state(state_name + '_step')
275280
batcher_ops = [
276281
state_saver.save_state(state_name + '_c', states_out[-1][0]),
277282
state_saver.save_state(state_name + '_h', states_out[-1][1]),
278283
state_saver.save_state(state_name + '_step', self._step + 1)]
279-
image_features = {}
280-
with tf_ops.control_dependencies(batcher_ops):
284+
with tf_ops.control_dependencies(batcher_ops):
285+
image_features['layer_19'] = tf.concat(net_seq, 0)
286+
else:
281287
image_features['layer_19'] = tf.concat(net_seq, 0)
282288

283289
# SSD layers.
@@ -289,4 +295,4 @@ def extract_features(self, preprocessed_inputs, state_saver=None,
289295
insert_1x1_conv=True,
290296
image_features=image_features,
291297
pool_residual=True)
292-
return feature_maps.values()
298+
return list(feature_maps.values())

research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor_test.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
"""Tests for lstm_ssd_interleaved_mobilenet_v2_feature_extractor."""
1717

18-
import itertools
1918
import numpy as np
2019
import tensorflow.compat.v1 as tf
21-
from tensorflow.contrib import slim
20+
import tf_slim as slim
2221
from tensorflow.contrib import training as contrib_training
2322

2423
from lstm_object_detection.models import lstm_ssd_interleaved_mobilenet_v2_feature_extractor
@@ -261,16 +260,16 @@ def test_lstm_states(self):
261260
state_channel = 320
262261
init_state1 = {
263262
'lstm_state_c': tf.zeros(
264-
[image_height/32, image_width/32, state_channel]),
263+
[image_height // 32, image_width // 32, state_channel]),
265264
'lstm_state_h': tf.zeros(
266-
[image_height/32, image_width/32, state_channel]),
265+
[image_height // 32, image_width // 32, state_channel]),
267266
'lstm_state_step': tf.zeros([1])
268267
}
269268
init_state2 = {
270269
'lstm_state_c': tf.random_uniform(
271-
[image_height/32, image_width/32, state_channel]),
270+
[image_height // 32, image_width // 32, state_channel]),
272271
'lstm_state_h': tf.random_uniform(
273-
[image_height/32, image_width/32, state_channel]),
272+
[image_height // 32, image_width // 32, state_channel]),
274273
'lstm_state_step': tf.zeros([1])
275274
}
276275
seq = {'dummy': tf.random_uniform([2, 1, 1, 1])}
@@ -326,7 +325,7 @@ def graph_fn(image_tensor):
326325
image_tensor = np.random.rand(batch_size, image_height, image_width,
327326
3).astype(np.float32)
328327
feature_maps = self.execute(graph_fn, [image_tensor])
329-
for feature_map, expected_shape in itertools.izip(
328+
for feature_map, expected_shape in zip(
330329
feature_maps, expected_feature_map_shapes):
331330
self.assertAllEqual(feature_map.shape, expected_shape)
332331

research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""LSTMSSDFeatureExtractor for MobilenetV1 features."""
1717

1818
import tensorflow.compat.v1 as tf
19-
from tensorflow.contrib import slim as contrib_slim
19+
import tf_slim as slim
2020
from tensorflow.python.framework import ops as tf_ops
2121
from lstm_object_detection.lstm import lstm_cells
2222
from lstm_object_detection.lstm import rnn_decoder
@@ -27,8 +27,6 @@
2727
from object_detection.utils import shape_utils
2828
from nets import mobilenet_v1
2929

30-
slim = contrib_slim
31-
3230

3331
class LSTMSSDMobileNetV1FeatureExtractor(
3432
lstm_ssd_meta_arch.LSTMSSDFeatureExtractor):
@@ -85,14 +83,16 @@ def __init__(self,
8583
self._base_network_scope = 'MobilenetV1'
8684
self._lstm_state_depth = lstm_state_depth
8785

88-
def create_lstm_cell(self, batch_size, output_size, state_saver, state_name):
86+
def create_lstm_cell(self, batch_size, output_size, state_saver, state_name,
87+
dtype=tf.float32):
8988
"""Create the LSTM cell, and initialize state if necessary.
9089
9190
Args:
9291
batch_size: input batch size.
9392
output_size: output size of the lstm cell, [width, height].
9493
state_saver: a state saver object with methods `state` and `save_state`.
9594
state_name: string, the name to use with the state_saver.
95+
dtype: dtype to initialize lstm state.
9696
9797
Returns:
9898
lstm_cell: the lstm cell unit.
@@ -107,7 +107,7 @@ def create_lstm_cell(self, batch_size, output_size, state_saver, state_name):
107107
visualize_gates=False)
108108

109109
if state_saver is None:
110-
init_state = lstm_cell.init_state(state_name, batch_size, tf.float32)
110+
init_state = lstm_cell.init_state(state_name, batch_size, dtype)
111111
step = None
112112
else:
113113
step = state_saver.state(state_name + '_step')
@@ -166,11 +166,14 @@ def extract_features(self,
166166
with slim.arg_scope(
167167
[slim.batch_norm], fused=False, is_training=self._is_training):
168168
# ConvLSTM layers.
169-
batch_size = net.shape[0].value / unroll_length
169+
batch_size = net.shape[0].value // unroll_length
170170
with tf.variable_scope('LSTM', reuse=self._reuse_weights) as lstm_scope:
171171
lstm_cell, init_state, _ = self.create_lstm_cell(
172-
batch_size, (net.shape[1].value, net.shape[2].value), state_saver,
173-
state_name)
172+
batch_size,
173+
(net.shape[1].value, net.shape[2].value),
174+
state_saver,
175+
state_name,
176+
dtype=preprocessed_inputs.dtype)
174177
net_seq = list(tf.split(net, unroll_length))
175178

176179
# Identities added for inputing state tensors externally.
@@ -205,4 +208,4 @@ def extract_features(self,
205208
insert_1x1_conv=True,
206209
image_features=image_features)
207210

208-
return feature_maps.values()
211+
return list(feature_maps.values())

research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor_test.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717

1818
import numpy as np
1919
import tensorflow.compat.v1 as tf
20-
from tensorflow.contrib import slim as contrib_slim
20+
import tf_slim as slim
2121
from tensorflow.contrib import training as contrib_training
2222

2323
from lstm_object_detection.models import lstm_ssd_mobilenet_v1_feature_extractor as feature_extractor
2424
from object_detection.models import ssd_feature_extractor_test
2525

26-
slim = contrib_slim
27-
2826

2927
class LstmSsdMobilenetV1FeatureExtractorTest(
3028
ssd_feature_extractor_test.SsdFeatureExtractorTestBase):

research/lstm_object_detection/models/mobilenet_defs.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
"""Definitions for modified MobileNet models used in LSTD."""
1616

1717
import tensorflow.compat.v1 as tf
18-
from tensorflow.contrib import slim as contrib_slim
19-
18+
import tf_slim as slim
2019
from nets import mobilenet_v1
2120
from nets.mobilenet import conv_blocks as mobilenet_convs
2221
from nets.mobilenet import mobilenet
2322

24-
slim = contrib_slim
25-
2623

2724
def mobilenet_v1_lite_def(depth_multiplier, low_res=False):
2825
"""Conv definitions for a lite MobileNet v1 model.

0 commit comments

Comments
 (0)