Skip to content

Commit 031e806

Browse files
authored
Merge pull request #393 from google/sync
Sync github with internal code.
2 parents 2da2c33 + 58d5d64 commit 031e806

30 files changed

+674
-347
lines changed

efficientdet/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Then you will get:
8181
- frozen graph with name savedmodeldir/efficientdet-d0_frozen.pb
8282
- tflite file with name efficientdet-d0.tflite
8383

84-
Notably, --tflite_path is optional, and it only works with tensorflow >= 2.2.0-rc4.
84+
Notably, --tflite_path does not work for now. It requires some extra fixes in future TensorFlow rerlease (> 2.2.0-rc4).
8585

8686

8787
## 4. Benchmark model latency.
@@ -304,7 +304,7 @@ If you want to do inference for custom data, you can run
304304
--model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 \
305305
--hparams=voc_config.yaml \
306306
--input_image=img.png --output_image_dir=/tmp/
307-
307+
308308
You should check more details of runmode which is written in caption-4.
309309

310310
## 10. Training EfficientDets on TPUs.

efficientdet/anchors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ def _generate_detections_tf(cls_outputs,
291291
image_scale: a float tensor representing the scale between original image
292292
and input image for the detector. It is used to rescale detections for
293293
evaluating with the original groundtruth annotations.
294+
image_size: a tuple (height, width) or an integer for image size.
294295
min_score_thresh: A float representing the threshold for deciding when to
295296
remove boxes based on score.
296297
max_boxes_to_draw: Max number of boxes to draw.
@@ -307,7 +308,7 @@ def _generate_detections_tf(cls_outputs,
307308
[image_id, ymin, xmin, ymax, xmax, score, class]
308309
"""
309310
if not image_size:
310-
raise ValueError('image_size cannot be empty.')
311+
raise ValueError('tf version generate_detection needs non-empty image_size')
311312

312313
logging.info('Using tf version of post-processing.')
313314
anchor_boxes = tf.gather(anchor_boxes, indices)

efficientdet/aug/autoaugment.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020

2121
from __future__ import absolute_import
2222
from __future__ import division
23+
# gtype import
2324
from __future__ import print_function
2425

2526
import inspect
2627
import math
2728
from absl import logging
29+
import numpy as np
2830
import tensorflow.compat.v1 as tf
2931
import tensorflow_probability as tfp
32+
3033
import hparams_config
31-
import numpy as np
3234

3335
try:
3436
# addon image_ops are simpler, but they have some issues on GPU and TPU.
@@ -1547,7 +1549,13 @@ def select_and_apply_random_policy(policies, image, bboxes):
15471549
lambda: (image, bboxes))
15481550
return (image, bboxes)
15491551

1550-
def select_and_apply_random_policy_augmix(policies, image, bboxes, mixture_width=3, mixture_depth=-1, alpha=1):
1552+
1553+
def select_and_apply_random_policy_augmix(policies,
1554+
image,
1555+
bboxes,
1556+
mixture_width=3,
1557+
mixture_depth=-1,
1558+
alpha=1):
15511559
"""Select a random policy from `policies` and apply it to `image`."""
15521560
policy_to_select = tf.random_uniform([], maxval=len(policies), dtype=tf.int32)
15531561
# Note that using tf.case instead of tf.conds would result in significantly
@@ -1562,12 +1570,13 @@ def select_and_apply_random_policy_augmix(policies, image, bboxes, mixture_width
15621570
for (i, policy) in enumerate(policies):
15631571
aug_image, bboxes = tf.cond(
15641572
tf.equal(i, policy_to_select),
1565-
lambda selected_policy=policy: selected_policy(aug_image, bboxes),
1566-
lambda: (aug_image, bboxes))
1573+
lambda policy_fn=policy, img=aug_image: policy_fn(img, bboxes),
1574+
lambda img=aug_image: (img, bboxes))
15671575
mix += ws[j] * tf.cast(aug_image, tf.float32)
15681576
mixed = tf.cast((1 - m) * tf.cast(image, tf.float32) + m * mix, tf.uint8)
15691577
return (mixed, bboxes)
15701578

1579+
15711580
def build_and_apply_nas_policy(policies, image, bboxes,
15721581
augmentation_hparams, use_augmix=False,
15731582
mixture_width=3, mixture_depth=-1, alpha=1):
@@ -1583,9 +1592,9 @@ def build_and_apply_nas_policy(policies, image, bboxes,
15831592
normalized between [0, 1].
15841593
augmentation_hparams: Hparams associated with the NAS learned policy.
15851594
use_augmix: whether use augmix[https://arxiv.org/pdf/1912.02781.pdf]
1586-
width: Width of augmentation chain
1587-
depth: Depth of augmentation chain. -1 enables stochastic depth uniformly
1588-
from [1, 3]
1595+
mixture_width: Width of augmentation chain
1596+
mixture_depth: Depth of augmentation chain. -1 enables stochastic depth
1597+
uniformly from [1, 3].
15891598
alpha: Probability coefficient for Beta and Dirichlet distributions.
15901599
15911600
Returns:
@@ -1631,8 +1640,13 @@ def final_policy(image_, bboxes_):
16311640
return (augmented_images, augmented_bboxes)
16321641

16331642

1634-
def distort_image_with_autoaugment(image, bboxes, augmentation_name, use_augmix=False,
1635-
mixture_width=3, mixture_depth=-1, alpha=1):
1643+
def distort_image_with_autoaugment(image,
1644+
bboxes,
1645+
augmentation_name,
1646+
use_augmix=False,
1647+
mixture_width=3,
1648+
mixture_depth=-1,
1649+
alpha=1):
16361650
"""Applies the AutoAugment policy to `image` and `bboxes`.
16371651
16381652
Args:
@@ -1646,6 +1660,11 @@ def distort_image_with_autoaugment(image, bboxes, augmentation_name, use_augmix=
16461660
found on the COCO dataset that have slight variation in what operations
16471661
were used during the search procedure along with how many operations are
16481662
applied in parallel to a single image (2 vs 3).
1663+
use_augmix: whether use augmix[https://arxiv.org/pdf/1912.02781.pdf]
1664+
mixture_width: Width of augmentation chain
1665+
mixture_depth: Depth of augmentation chain. -1 enables stochastic depth
1666+
uniformly from [1, 3].
1667+
alpha: Probability coefficient for Beta and Dirichlet distributions.
16491668
16501669
Returns:
16511670
A tuple containing the augmented versions of `image` and `bboxes`.
@@ -1668,4 +1687,5 @@ def distort_image_with_autoaugment(image, bboxes, augmentation_name, use_augmix=
16681687

16691688
with tf.device('/cpu:0'):
16701689
return build_and_apply_nas_policy(policy, image, bboxes,
1671-
augmentation_hparams, use_augmix, mixture_width, mixture_depth, alpha)
1690+
augmentation_hparams, use_augmix,
1691+
mixture_width, mixture_depth, alpha)

efficientdet/aug/autoaugment_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import division
1919
from __future__ import print_function
2020

21+
from absl import logging
2122
import tensorflow.compat.v1 as tf
2223

2324
from aug import autoaugment
@@ -30,10 +31,12 @@ def test_autoaugment_policy(self):
3031
image = tf.placeholder(tf.uint8, shape=[640, 640, 3])
3132
bboxes = tf.placeholder(tf.float32, shape=[4, 4])
3233
autoaugment.distort_image_with_autoaugment(image, bboxes, 'test')
33-
autoaugment.distort_image_with_autoaugment(image, bboxes, 'test', use_augmix=True)
34+
autoaugment.distort_image_with_autoaugment(
35+
image, bboxes, 'test', use_augmix=True)
3436

3537

3638
if __name__ == '__main__':
39+
logging.set_verbosity(logging.WARNING)
3740
tf.disable_eager_execution()
3841
tf.test.main()
3942

efficientdet/backbone/efficientnet_builder_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import division
1919
from __future__ import print_function
2020

21+
from absl import logging
2122
import numpy as np
2223
import tensorflow.compat.v1 as tf
2324

@@ -109,6 +110,7 @@ def test_efficientnet_b0_base(self):
109110

110111

111112
if __name__ == '__main__':
113+
logging.set_verbosity(logging.WARNING)
112114
# Disable eager to allow tf.profile works for #params/#flops.
113115
tf.disable_eager_execution()
114116
tf.test.main()

efficientdet/backbone/efficientnet_lite_builder_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import division
1919
from __future__ import print_function
2020

21+
from absl import logging
2122
import numpy as np
2223
import tensorflow.compat.v1 as tf
2324

@@ -67,6 +68,7 @@ def test_efficientnet_b4(self):
6768

6869

6970
if __name__ == '__main__':
71+
logging.set_verbosity(logging.WARNING)
7072
# Disable eager to allow tf.profile works for #params/#flops.
7173
tf.disable_eager_execution()
7274
tf.test.main()

efficientdet/backbone/efficientnet_model_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import division
1919
from __future__ import print_function
2020

21+
from absl import logging
2122
import tensorflow.compat.v1 as tf
2223

2324
import utils
@@ -252,4 +253,5 @@ def test_reduction_endpoint_with_single_block_without_sp(self):
252253
self.assertNotIn('reduction_2', model.endpoints)
253254

254255
if __name__ == '__main__':
256+
logging.set_verbosity(logging.WARNING)
255257
tf.test.main()

efficientdet/dataloader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def _dataset_parser(value):
277277
if params.get('autoaugment_policy', None) and self._is_training:
278278
from aug import autoaugment # pylint: disable=g-import-not-at-top
279279
image, boxes = autoaugment.distort_image_with_autoaugment(
280-
image, boxes, params['autoaugment_policy'], params['use_augmix'], *params['augmix_params'])
280+
image, boxes, params['autoaugment_policy'], params['use_augmix'],
281+
*params['augmix_params'])
281282

282283
input_processor = DetectionInputProcessor(
283284
image, params['image_size'], boxes, classes)

efficientdet/dataset/create_coco_tfrecord.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
import hashlib
3232
import io
3333
import json
34-
import logging
3534
import multiprocessing
3635
import os
36+
37+
from absl import app
3738
from absl import flags
39+
from absl import logging
3840
import numpy as np
3941
import PIL.Image
4042

@@ -43,7 +45,6 @@
4345
from dataset import label_map_util
4446
from dataset import tfrecord_util
4547

46-
4748
flags.DEFINE_boolean(
4849
'include_masks', False, 'Whether to include instance segmentations masks '
4950
'(PNG encoded) in the result. default: False.')
@@ -61,6 +62,7 @@
6162
'captions.')
6263
flags.DEFINE_string('output_file_prefix', '/tmp/train', 'Path to output file')
6364
flags.DEFINE_integer('num_shards', 32, 'Number of shards for output file.')
65+
flags.DEFINE_integer('num_threads', None, 'Number of threads to run.')
6466
FLAGS = flags.FLAGS
6567

6668

@@ -285,9 +287,8 @@ def _create_tf_record_from_coco_annotations(images_info_file,
285287

286288
logging.info('writing to output path: %s', output_path)
287289
writers = [
288-
tf.python_io.TFRecordWriter(
289-
output_path + '-%05d-of-%05d.tfrecord' % (i, num_shards))
290-
for i in range(num_shards)
290+
tf.python_io.TFRecordWriter(output_path + '-%05d-of-%05d.tfrecord' %
291+
(i, num_shards)) for i in range(num_shards)
291292
]
292293
images = _load_images_info(images_info_file)
293294

@@ -313,13 +314,14 @@ def _get_caption_annotation(image_id):
313314
else:
314315
return None
315316

316-
pool = multiprocessing.Pool()
317+
pool = multiprocessing.Pool(FLAGS.num_threads)
317318
total_num_annotations_skipped = 0
318319
for idx, (_, tf_example, num_annotations_skipped) in enumerate(
319-
pool.imap(_pool_create_tf_example,
320-
[(image, image_dir, _get_object_annotation(image['id']),
321-
category_index, _get_caption_annotation(image['id']),
322-
include_masks) for image in images])):
320+
pool.imap(
321+
_pool_create_tf_example,
322+
[(image, image_dir, _get_object_annotation(image['id']),
323+
category_index, _get_caption_annotation(image['id']), include_masks)
324+
for image in images])):
323325
if idx % 100 == 0:
324326
logging.info('On image %d of %d', idx, len(images))
325327

@@ -361,4 +363,4 @@ def main(_):
361363

362364

363365
if __name__ == '__main__':
364-
tf.app.run(main)
366+
app.run(main)

efficientdet/dataset/create_coco_tfrecord_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import json
1919
import os
2020

21+
from absl import flags
22+
from absl import logging
2123
import numpy as np
2224
import PIL.Image
2325
import six
@@ -28,6 +30,10 @@
2830

2931
class CreateCocoTFRecordTest(tf.test.TestCase):
3032

33+
def setUp(self):
34+
super(CreateCocoTFRecordTest, self).setUp()
35+
flags.FLAGS.num_threads = 1
36+
3137
def _assertProtoEqual(self, proto_field, expectation):
3238
"""Helper function to assert if a proto field equals some value.
3339
@@ -251,4 +257,5 @@ def test_create_sharded_tf_record(self):
251257

252258

253259
if __name__ == '__main__':
260+
logging.set_verbosity(logging.WARNING)
254261
tf.test.main()

0 commit comments

Comments
 (0)