Skip to content

Commit 7074952

Browse files
committed
Code cleanup to allow TF V2 behavior but still disable eager execution.
Also resolved some simple warnings.
1 parent 25ebe40 commit 7074952

14 files changed

+59
-48
lines changed

efficientdet/aug/autoaugment_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ def test_autoaugment_policy(self):
3333

3434

3535
if __name__ == '__main__':
36-
tf.disable_v2_behavior()
36+
tf.disable_eager_execution()
3737
tf.test.main()
3838

efficientdet/backbone/efficientnet_builder_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,6 @@ def test_efficientnet_b0_base(self):
109109

110110

111111
if __name__ == '__main__':
112-
tf.disable_v2_behavior()
112+
# Disable eager to allow tf.profile works for #params/#flops.
113+
tf.disable_eager_execution()
113114
tf.test.main()

efficientdet/backbone/efficientnet_lite_builder_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,6 @@ def test_efficientnet_b4(self):
6767

6868

6969
if __name__ == '__main__':
70-
tf.disable_v2_behavior()
70+
# Disable eager to allow tf.profile works for #params/#flops.
71+
tf.disable_eager_execution()
7172
tf.test.main()

efficientdet/backbone/efficientnet_model.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ def superpixel_kernel_initializer(shape, dtype='float32', partition_info=None):
136136

137137
def round_filters(filters, global_params, skip=False):
138138
"""Round number of filters based on depth multiplier."""
139-
orig_f = filters
140139
multiplier = global_params.width_coefficient
141140
divisor = global_params.depth_divisor
142141
min_depth = global_params.min_depth
@@ -149,7 +148,6 @@ def round_filters(filters, global_params, skip=False):
149148
# Make sure that round down does not go down by more than 10%.
150149
if new_filters < 0.9 * filters:
151150
new_filters += divisor
152-
logging.info('round_filter input=%s output=%s', orig_f, new_filters)
153151
return int(new_filters)
154152

155153

@@ -342,7 +340,7 @@ def call(self, inputs, training=True, survival_prob=None):
342340
Returns:
343341
A output tensor.
344342
"""
345-
logging.info('Block input: %s shape: %s', inputs.name, inputs.shape)
343+
logging.info('Block %s input shape: %s', self.name, inputs.shape)
346344
x = inputs
347345

348346
fused_conv_fn = self._fused_conv
@@ -355,21 +353,20 @@ def call(self, inputs, training=True, survival_prob=None):
355353
with tf.name_scope('super_pixel'):
356354
x = self._relu_fn(
357355
self._bnsp(self._superpixel(x), training=training))
358-
logging.info(
359-
'Block start with SuperPixel: %s shape: %s', x.name, x.shape)
356+
logging.info('Block start with SuperPixel shape: %s', x.shape)
360357

361358
if self._block_args.fused_conv:
362359
# If use fused mbconv, skip expansion and use regular conv.
363360
x = self._relu_fn(self._bn1(fused_conv_fn(x), training=training))
364-
logging.info('Conv2D: %s shape: %s', x.name, x.shape)
361+
logging.info('Conv2D shape: %s', x.shape)
365362
else:
366363
# Otherwise, first apply expansion and then apply depthwise conv.
367364
if self._block_args.expand_ratio != 1:
368365
x = self._relu_fn(self._bn0(expand_conv_fn(x), training=training))
369-
logging.info('Expand: %s shape: %s', x.name, x.shape)
366+
logging.info('Expand shape: %s', x.shape)
370367

371368
x = self._relu_fn(self._bn1(depthwise_conv_fn(x), training=training))
372-
logging.info('DWConv: %s shape: %s', x.name, x.shape)
369+
logging.info('DWConv shape: %s', x.shape)
373370

374371
if self._has_se:
375372
with tf.name_scope('se'):
@@ -391,7 +388,7 @@ def call(self, inputs, training=True, survival_prob=None):
391388
if survival_prob:
392389
x = utils.drop_connect(x, training, survival_prob)
393390
x = tf.add(x, inputs)
394-
logging.info('Project: %s shape: %s', x.name, x.shape)
391+
logging.info('Project shape: %s', x.shape)
395392
return x
396393

397394

@@ -440,12 +437,12 @@ def call(self, inputs, training=True, survival_prob=None):
440437
Returns:
441438
A output tensor.
442439
"""
443-
logging.info('Block input: %s shape: %s', inputs.name, inputs.shape)
440+
logging.info('Block %s input shape: %s', self.name, inputs.shape)
444441
if self._block_args.expand_ratio != 1:
445442
x = self._relu_fn(self._bn0(self._expand_conv(inputs), training=training))
446443
else:
447444
x = inputs
448-
logging.info('Expand: %s shape: %s', x.name, x.shape)
445+
logging.info('Expand shape: %s', x.shape)
449446

450447
self.endpoints = {'expansion_output': x}
451448

@@ -464,7 +461,7 @@ def call(self, inputs, training=True, survival_prob=None):
464461
if survival_prob:
465462
x = utils.drop_connect(x, training, survival_prob)
466463
x = tf.add(x, inputs)
467-
logging.info('Project: %s shape: %s', x.name, x.shape)
464+
logging.info('Project shape: %s', x.shape)
468465
return x
469466

470467

efficientdet/backbone/efficientnet_model_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,4 @@ def test_reduction_endpoint_with_single_block_without_sp(self):
252252
self.assertNotIn('reduction_2', model.endpoints)
253253

254254
if __name__ == '__main__':
255-
tf.disable_v2_behavior()
256255
tf.test.main()

efficientdet/dataloader.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,26 @@ def set_training_random_scale_factors(self, scale_min, scale_max):
7171
"""Set the parameters for multiscale training."""
7272
# Select a random scale factor.
7373
random_scale_factor = tf.random_uniform([], scale_min, scale_max)
74-
scaled_size_y = tf.to_int32(random_scale_factor * self._output_size[0])
75-
scaled_size_x = tf.to_int32(random_scale_factor * self._output_size[1])
74+
scaled_y = tf.cast(random_scale_factor * self._output_size[0], tf.int32)
75+
scaled_x = tf.cast(random_scale_factor * self._output_size[1], tf.int32)
7676

7777
# Recompute the accurate scale_factor using rounded scaled image size.
78-
height = tf.shape(self._image)[0]
79-
width = tf.shape(self._image)[1]
80-
image_scale_y = tf.to_float(scaled_size_y) / tf.to_float(height)
81-
image_scale_x = tf.to_float(scaled_size_x) / tf.to_float(width)
78+
height = tf.cast(tf.shape(self._image)[0], tf.float32)
79+
width = tf.cast(tf.shape(self._image)[1], tf.float32)
80+
image_scale_y = tf.cast(scaled_y, tf.float32) / height
81+
image_scale_x = tf.cast(scaled_x, tf.float32) / width
8282
image_scale = tf.minimum(image_scale_x, image_scale_y)
8383

8484
# Select non-zero random offset (x, y) if scaled image is larger than
8585
# self._output_size.
86-
scaled_height = tf.to_int32(tf.to_float(height) * image_scale)
87-
scaled_width = tf.to_int32(tf.to_float(width) * image_scale)
88-
offset_y = tf.to_float(scaled_height - self._output_size[0])
89-
offset_x = tf.to_float(scaled_width - self._output_size[1])
86+
scaled_height = tf.cast(height * image_scale, tf.int32)
87+
scaled_width = tf.cast(width * image_scale, tf.int32)
88+
offset_y = tf.cast(scaled_height - self._output_size[0], tf.float32)
89+
offset_x = tf.cast(scaled_width - self._output_size[1], tf.float32)
9090
offset_y = tf.maximum(0.0, offset_y) * tf.random_uniform([], 0, 1)
9191
offset_x = tf.maximum(0.0, offset_x) * tf.random_uniform([], 0, 1)
92-
offset_y = tf.to_int32(offset_y)
93-
offset_x = tf.to_int32(offset_x)
92+
offset_y = tf.cast(offset_y, tf.int32)
93+
offset_x = tf.cast(offset_x, tf.int32)
9494
self._image_scale = image_scale
9595
self._scaled_height = scaled_height
9696
self._scaled_width = scaled_width
@@ -100,13 +100,13 @@ def set_training_random_scale_factors(self, scale_min, scale_max):
100100
def set_scale_factors_to_output_size(self):
101101
"""Set the parameters to resize input image to self._output_size."""
102102
# Compute the scale_factor using rounded scaled image size.
103-
height = tf.shape(self._image)[0]
104-
width = tf.shape(self._image)[1]
105-
image_scale_y = tf.to_float(self._output_size[0]) / tf.to_float(height)
106-
image_scale_x = tf.to_float(self._output_size[1]) / tf.to_float(width)
103+
height = tf.cast(tf.shape(self._image)[0], tf.float32)
104+
width = tf.cast(tf.shape(self._image)[1], tf.float32)
105+
image_scale_y = tf.cast(self._output_size[0], tf.float32) / height
106+
image_scale_x = tf.cast(self._output_size[1], tf.float32) / width
107107
image_scale = tf.minimum(image_scale_x, image_scale_y)
108-
scaled_height = tf.to_int32(tf.to_float(height) * image_scale)
109-
scaled_width = tf.to_int32(tf.to_float(width) * image_scale)
108+
scaled_height = tf.cast(height * image_scale, tf.int32)
109+
scaled_width = tf.cast(width * image_scale, tf.int32)
110110
self._image_scale = image_scale
111111
self._scaled_height = scaled_height
112112
self._scaled_width = scaled_width
@@ -151,7 +151,7 @@ def resize_and_crop_boxes(self):
151151
# Adjust box coordinates based on the offset.
152152
box_offset = tf.stack([self._crop_offset_y, self._crop_offset_x,
153153
self._crop_offset_y, self._crop_offset_x,])
154-
boxes -= tf.to_float(tf.reshape(box_offset, [1, 4]))
154+
boxes -= tf.cast(tf.reshape(box_offset, [1, 4]), tf.float32)
155155
# Clip the boxes.
156156
boxes = self.clip_boxes(boxes)
157157
# Filter out ground truth boxes that are all zeros.

efficientdet/efficientdet_arch_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def build_model(self,
3838
inputs_shape = [1, 3, isize[0], isize[1]]
3939
else:
4040
inputs_shape = [1, isize[0], isize[1], 3]
41-
inputs = tf.placeholder(tf.float32, name='input', shape=inputs_shape)
41+
inputs = tf.ones(shape=inputs_shape, name='input', dtype=tf.float32)
4242
efficientdet_arch.efficientdet(
4343
inputs,
4444
model_name=model_name,
@@ -107,7 +107,7 @@ def _model_fn(inputs):
107107
return utils.build_model_with_precision(precision, _model_fn, features)
108108

109109
def test_float16(self):
110-
inputs = tf.placeholder(tf.float32, name='input', shape=[1, 512, 512, 3])
110+
inputs = tf.ones(shape=[1, 512, 512, 3], name='input', dtype=tf.float32)
111111
cls_out, _ = self.build_model(inputs, True, 'mixed_float16')
112112
for v in tf.global_variables():
113113
# All variables should be float32.
@@ -117,7 +117,7 @@ def test_float16(self):
117117
self.assertIs(v.dtype, tf.float16)
118118

119119
def test_bfloat16(self):
120-
inputs = tf.placeholder(tf.float32, name='input', shape=[1, 512, 512, 3])
120+
inputs = tf.ones(shape=[1, 512, 512, 3], name='input', dtype=tf.float32)
121121
cls_out, _ = self.build_model(inputs, True, 'mixed_bfloat16')
122122
for v in tf.global_variables():
123123
# All variables should be float32.
@@ -126,7 +126,7 @@ def test_bfloat16(self):
126126
self.assertEqual(v.dtype, tf.bfloat16)
127127

128128
def test_float32(self):
129-
inputs = tf.placeholder(tf.float32, name='input', shape=[1, 512, 512, 3])
129+
inputs = tf.ones(shape=[1, 512, 512, 3], name='input', dtype=tf.float32)
130130
cls_out, _ = self.build_model(inputs, True, 'float32')
131131
for v in tf.global_variables():
132132
# All variables should be float32.
@@ -189,4 +189,5 @@ def test_bifpn_dynamic_l2l7(self):
189189

190190

191191
if __name__ == '__main__':
192+
tf.disable_eager_execution()
192193
tf.test.main()

efficientdet/inference.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ def det_post_process_combined(params, cls_outputs, box_outputs, scales,
244244
clip_boxes=False))
245245
del valid_detections # to be used in futue.
246246

247-
image_ids = tf.tile(
248-
tf.expand_dims(tf.range(batch_size, dtype=tf.float32), axis=1),
249-
[1, max_boxes_to_draw])
247+
image_ids = tf.cast(
248+
tf.tile(
249+
tf.expand_dims(tf.range(batch_size), axis=1), [1, max_boxes_to_draw]),
250+
dtype=tf.float32)
250251
y = nmsed_boxes[..., 0] * scales
251252
x = nmsed_boxes[..., 1] * scales
252253
height = nmsed_boxes[..., 2] * scales - y

efficientdet/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,5 +394,5 @@ def terminate_eval():
394394

395395

396396
if __name__ == '__main__':
397-
tf.disable_v2_behavior()
397+
tf.disable_eager_execution()
398398
tf.app.run(main)

efficientdet/model_inspect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def saved_model_inference(self, image_path_pattern, output_dir, **kwargs):
160160
# Serving time batch size should be fixed.
161161
batch_size = self.batch_size or 1
162162
all_files = list(tf.io.gfile.glob(image_path_pattern))
163+
print('all_files=', all_files)
163164
num_batches = len(all_files) // batch_size
164165

165166
for i in range(num_batches):
@@ -465,5 +466,5 @@ def main(_):
465466

466467
if __name__ == '__main__':
467468
logging.set_verbosity(logging.WARNING)
468-
tf.disable_v2_behavior()
469+
tf.disable_eager_execution()
469470
tf.app.run(main)

0 commit comments

Comments
 (0)