Commit 0ba83cf0 authored by pkulzc's avatar pkulzc Committed by Sergio Guadarrama
Browse files

Release MobileNet V3 models and SSDLite models with MobileNet V3 backbone. (#7678)

* Merged commit includes the following changes:
275131829  by Sergio Guadarrama:

    updates mobilenet/README.md to be github compatible adds V2+ reference to mobilenet_v1.md file and fixes invalid markdown

--
274908068  by Sergio Guadarrama:

    Opensource MobilenetV3 detection models.

--
274697808  by Sergio Guadarrama:

    Fixed cases where tf.TensorShape was constructed with float dimensions

    This is a prerequisite for making TensorShape and Dimension more strict
    about the types of their arguments.

--
273577462  by Sergio Guadarrama:

    Fixing `conv_defs['defaults']` override issue.

--
272801298  by Sergio Guadarrama:

    Adds links to trained models for Moblienet V3, adds a version of minimalistic mobilenet-v3 to the definitions.

--
268928503  by Sergio Guadarrama:

    Mobilenet v2 with group normalization.

--
263492735  by Sergio Guadarrama:

    Internal change

260037126  by Sergio Guadarrama:

    Adds an option of using a custom depthwise operation in `expanded_conv`.

--
259997001  by Sergio Guadarrama:

    Explicitly mark Python binaries/tests with python_version = "PY2".

--
252697685  by Sergio Guadarrama:

    Internal change

251918746  by Sergio Guadarrama:

    Internal change

251909704  by Sergio Guadarrama:

    Mobilenet V3 backbone implementation.

--
247510236  by Sergio Guadarrama:

    Internal change

246196802  by Sergio Guadarrama:

    Internal change

246014539  by Sergio Guadarrama:

    Internal change

245891435  by Sergio Guadarrama:

    Internal change

245834925  by Sergio Guadarrama:

    n/a

--

PiperOrigin-RevId: 275131829

* Merged commit includes the following changes:
274959989  by Zhichao Lu:

    Update detection model zoo with MobilenetV3 SSD candidates.

--
274908068  by Zhichao Lu:

    Opensource MobilenetV3 detection models.

--
274695889  by richardmunoz:

    RandomPatchGaussian preprocessing step

    This step can be used during model training to randomly apply gaussian noise to a random image patch. Example addition to an Object Detection API pipeline config:

    train_config {
      ...
      data_augmentation_options {
        random_patch_gaussian {
          random_coef: 0.5
          min_patch_size: 1
          max_patch_size: 250
          min_gaussian_stddev: 0.0
          max_gaussian_stddev: 1.0
        }
      }
      ...
    }

--
274257872  by lzc:

    Internal change.

--
274114689  by Zhichao Lu:

    Pass native_resize flag to other FPN variants.

--
274112308  by lzc:

    Internal change.

--
274090763  by richardmunoz:

    Util function for getting a patch mask on an image for use with the Object Detection API

--
274069806  by Zhichao Lu:

    Adding functions which will help compute predictions and losses for CenterNet.

--
273860828  by lzc:

    Internal change.

--
273380069  by richardmunoz:

    RandomImageDownscaleToTargetPixels preprocessing step

    This step can be used during model training to randomly downscale an image to a random target number of pixels. If the image does not contain more than the target number of pixels, then downscaling is skipped. Example addition to an Object Detection API pipeline config:

    train_config {
      ...
      data_augmentation_options {
        random_downscale_to_target_pixels {
          random_coef: 0.5
          min_target_pixels: 300000
          max_target_pixels: 500000
        }
      }
      ...
    }

--
272987602  by Zhichao Lu:

    Avoid -inf when empty box list is passed.

--
272525836  by Zhichao Lu:

    Cleanup repeated resizing code in meta archs.

--
272458667  by richardmunoz:

    RandomJpegQuality preprocessing step

    This step can be used during model training to randomly encode the image into a jpeg with a random quality level. Example addition to an Object Detection API pipeline config:

    train_config {
      ...
      data_augmentation_options {
        random_jpeg_quality {
          random_coef: 0.5
          min_jpeg_quality: 80
          max_jpeg_quality: 100
        }
      }
      ...
    }

--
271412717  by Zhichao Lu:

    Enables TPU training with the V2 eager + tf.function Object Detection training loops.

--
270744153  by Zhichao Lu:

    Adding the offset and size target assigners for CenterNet.

--
269916081  by Zhichao Lu:

    Include basic installation in Object Detection API tutorial.
    Also:
     - Use TF2.0
     - Use saved_model

--
269376056  by Zhichao Lu:

    Fix to variable loading in RetinaNet w/ custom loops. (makes the code rely on the exact name scopes that are generated a little bit less)

--
269256251  by lzc:

    Add use_partitioned_nms field to config and update post_prossing_builder to honor that flag when building nms function.

--
268865295  by Zhichao Lu:

    Adding functionality for importing and merging back internal state of the metric.

--
268640984  by Zhichao Lu:

    Fix computation of gaussian sigma value to create CenterNet heatmap target.

--
267475576  by Zhichao Lu:

    Fix for exporter trying to export non-existent exponential moving averages.

--
267286768  by Zhichao Lu:

    Update mixed-precision policy.

--
266166879  by Zhichao Lu:

    Internal change

265860884  by Zhichao Lu:

    Apply floor function to center coordinates when creating heatmap for CenterNet target.

--
265702749  by Zhichao Lu:

    Internal change

--
264241949  by ronnyvotel:

    Updating Faster R-CNN 'final_anchors' to be in normalized coordinates.

--
264175192  by lzc:

    Update model_fn to only read hparams if it is not None.

--
264159328  by Zhichao Lu:

    Modify nearest neighbor upsampling to eliminate a multiply operation. For quantized models, the multiply operation gets unnecessarily quantized and reduces accuracy (simple stacking would work in place of the broadcast op which doesn't require quantization). Also removes an unnecessary reshape op.

--
263668306  by Zhichao Lu:

    Add the option to use dynamic map_fn for batch NMS

--
263031163  by Zhichao Lu:

    Mark outside compilation for NMS as optional.

--
263024916  by Zhichao Lu:

    Add an ExperimentalModel meta arch for experimenting with new model types.

--
262655894  by Zhichao Lu:

    Add the center heatmap target assigner for CenterNet

--
262431036  by Zhichao Lu:

    Adding add_eval_dict to allow for evaluation on model_v2

--
262035351  by ronnyvotel:

    Removing any non-Tensor predictions from the third stage of Mask R-CNN.

--
261953416  by Zhichao Lu:

    Internal change.

--
261834966  by Zhichao Lu:

    Fix the NMS OOM issue on TPU by forcing NMS to run outside of TPU.

--
261775941  by Zhichao Lu:

    Make Keras InputLayer compatible with both TF 1.x and TF 2.0.

--
261775633  by Zhichao Lu:

    Visualize additional channels with ground-truth bounding boxes.

--
261768117  by lzc:

    Internal change.

--
261766773  by ronnyvotel:

    Exposing `return_raw_detections_during_predict` in Faster R-CNN Proto.

--
260975089  by ronnyvotel:

    Moving calculation of batched prediction tensor names after all tensors in prediction dictionary are created.

--
259816913  by ronnyvotel:

    Adding raw detection boxes and feature map indices to SSD

--
259791955  by Zhichao Lu:

    Added a flag to control the use partitioned_non_max_suppression.

--
259580475  by Zhichao Lu:

    Tweak quantization-aware training re-writer to support NasFpn model architecture.

--
259579943  by rathodv:

    Add a meta target assigner proto and builders in OD API.

--
259577741  by Zhichao Lu:

    Internal change.

--
259366315  by lzc:

    Internal change.

--
259344310  by ronnyvotel:

    Updating faster rcnn so that raw_detection_boxes from predict() are in normalized coordinates.

--
259338670  by Zhichao Lu:

    Add support for use_native_resize_op to more feature extractors. Use dynamic shapes when static shapes are not available.

--
259083543  by ronnyvotel:

    Updating/fixing documentation.

--
259078937  by rathodv:

    Add prediction fields for tensors returned from detection_model.predict.

--
259044601  by Zhichao Lu:

    Add protocol buffer and builders for temperature scaling calibration.

--
259036770  by lzc:

    Internal changes.

--
259006223  by ronnyvotel:

    Adding detection anchor indices to Faster R-CNN Config. This is useful when one wishes to associate final detections and the anchors (or pre-nms boxes) from which they originated.

--
258872501  by Zhichao Lu:

    Run the training pipeline of ssd + resnet_v1_50 + fpn with a checkpoint.

--
258840686  by ronnyvotel:

    Adding standard outputs to DetectionModel.predict(). This CL only updates Faster R-CNN. Other meta architectures will be updated in future CLs.

--
258672969  by lzc:

    Internal change.

--
258649494  by lzc:

    Internal changes.

--
258630321  by ronnyvotel:

    Fixing documentation in shape_utils.flatten_dimensions().

--
258468145  by Zhichao Lu:

    Add additional output tensors parameter to Postprocess op.

--
258099219  by Zhichao Lu:

    Internal changes

--

PiperOrigin-RevId: 274959989
parent 9aed0ffb
......@@ -101,6 +101,21 @@ reporting an issue.
## Release information
### Oct 15th, 2019
We have released two MobileNet V3 SSDLite models (presented in
[Searching for MobileNetV3](https://arxiv.org/abs/1905.02244)).
* SSDLite with MobileNet-V3-Large backbone, which is 27% faster than Mobilenet
V2 SSDLite (119ms vs 162ms) on a Google Pixel phone CPU at the same mAP.
* SSDLite with MobileNet-V3-Small backbone, which is 37% faster than MnasNet
SSDLite reduced with depth-multiplier (43ms vs 68ms) at the same mAP.
Along with the model definition, we are also releasing model checkpoints
trained on the COCO dataset.
<b>Thanks to contributors</b>: Bo Chen, Zhichao Lu, Vivek Rathod, Jonathan Huang
### July 1st, 2019
We have released an updated set of utils and an updated
......@@ -265,8 +280,7 @@ release includes:
distributed training and evaluation pipelines via
[Google Cloud](g3doc/running_on_cloud.md).
<b>Thanks to contributors</b>: Jonathan Huang, Vivek Rathod, Derek Chow,
Chen Sun, Menglong Zhu, Matthew Tang, Anoop Korattikara, Alireza Fathi, Ian Fischer, Zbigniew Wojna, Yang Song, Sergio Guadarrama, Jasper Uijlings,
Viacheslav Kovalevskyi, Kevin Murphy
<b>Thanks to contributors</b>: Jonathan Huang, Vivek Rathod, Derek Chow, Chen
Sun, Menglong Zhu, Matthew Tang, Anoop Korattikara, Alireza Fathi, Ian Fischer,
Zbigniew Wojna, Yang Song, Sergio Guadarrama, Jasper Uijlings, Viacheslav
Kovalevskyi, Kevin Murphy
......@@ -245,7 +245,8 @@ def build_weight_shared_convolutional_box_predictor(
apply_batch_norm=True,
use_depthwise=False,
score_converter_fn=tf.identity,
box_encodings_clip_range=None):
box_encodings_clip_range=None,
keyword_args=None):
"""Builds and returns a WeightSharedConvolutionalBoxPredictor class.
Args:
......@@ -274,6 +275,7 @@ def build_weight_shared_convolutional_box_predictor(
score_converter_fn: Callable score converter to perform elementwise op on
class scores.
box_encodings_clip_range: Min and max values for clipping the box_encodings.
keyword_args: A dictionary with additional args.
Returns:
A WeightSharedConvolutionalBoxPredictor class.
......@@ -329,7 +331,8 @@ def build_weight_shared_convolutional_keras_box_predictor(
use_depthwise=False,
score_converter_fn=tf.identity,
box_encodings_clip_range=None,
name='WeightSharedConvolutionalBoxPredictor'):
name='WeightSharedConvolutionalBoxPredictor',
keyword_args=None):
"""Builds the Keras WeightSharedConvolutionalBoxPredictor from the arguments.
Args:
......@@ -371,6 +374,7 @@ def build_weight_shared_convolutional_keras_box_predictor(
box_encodings_clip_range: Min and max values for clipping the box_encodings.
name: A string name scope to assign to the box predictor. If `None`, Keras
will auto-generate one from the class name.
keyword_args: A dictionary with additional args.
Returns:
A Keras WeightSharedConvolutionalBoxPredictor class.
......@@ -728,6 +732,8 @@ def build(argscope_fn, box_predictor_config, is_training, num_classes,
box_encodings_clip_range = BoxEncodingsClipRange(
min=config_box_predictor.box_encodings_clip_range.min,
max=config_box_predictor.box_encodings_clip_range.max)
keyword_args = None
return build_weight_shared_convolutional_box_predictor(
is_training=is_training,
num_classes=num_classes,
......@@ -746,7 +752,8 @@ def build(argscope_fn, box_predictor_config, is_training, num_classes,
apply_batch_norm=apply_batch_norm,
use_depthwise=config_box_predictor.use_depthwise,
score_converter_fn=score_converter_fn,
box_encodings_clip_range=box_encodings_clip_range)
box_encodings_clip_range=box_encodings_clip_range,
keyword_args=keyword_args)
if box_predictor_oneof == 'mask_rcnn_box_predictor':
......@@ -891,6 +898,7 @@ def build_keras(hyperparams_fn, freeze_batchnorm, inplace_batchnorm_update,
box_encodings_clip_range = BoxEncodingsClipRange(
min=config_box_predictor.box_encodings_clip_range.min,
max=config_box_predictor.box_encodings_clip_range.max)
keyword_args = None
return build_weight_shared_convolutional_keras_box_predictor(
is_training=is_training,
......@@ -913,7 +921,8 @@ def build_keras(hyperparams_fn, freeze_batchnorm, inplace_batchnorm_update,
apply_batch_norm=apply_batch_norm,
use_depthwise=config_box_predictor.use_depthwise,
score_converter_fn=score_converter_fn,
box_encodings_clip_range=box_encodings_clip_range)
box_encodings_clip_range=box_encodings_clip_range,
keyword_args=keyword_args)
if box_predictor_oneof == 'mask_rcnn_box_predictor':
config_box_predictor = box_predictor_config.mask_rcnn_box_predictor
......
......@@ -355,6 +355,7 @@ class WeightSharedConvolutionalBoxPredictorBuilderTest(tf.test.TestCase):
class MaskRCNNBoxPredictorBuilderTest(tf.test.TestCase):
def test_box_predictor_builder_calls_fc_argscope_fn(self):
......
......@@ -213,6 +213,35 @@ def build(calibration_config):
name='calibrate_scores')
return calibrated_class_predictions_with_background
elif (calibration_config.WhichOneof('calibrator') ==
'temperature_scaling_calibration'):
def calibration_fn(class_predictions_with_background):
"""Calibrate predictions via temperature scaling.
Predictions logits scores are scaled by the temperature scaler. Note that
the 0-indexed background class is also transformed.
Args:
class_predictions_with_background: tf.float32 tensor of shape
[batch_size, num_anchors, num_classes + 1] containing logits scores.
This is usually produced before a sigmoid or softmax layer.
Returns:
tf.float32 tensor of the same shape as the input.
Raises:
ValueError: If temperature scaler is of incorrect value.
"""
scaler = calibration_config.temperature_scaling_calibration.scaler
if scaler <= 0:
raise ValueError('The scaler in temperature scaling must be positive.')
calibrated_class_predictions_with_background = tf.math.divide(
class_predictions_with_background,
scaler,
name='calibrate_score')
return calibrated_class_predictions_with_background
# TODO(zbeaver): Add sigmoid calibration.
else:
raise ValueError('No calibration builder defined for "Oneof" in '
......
......@@ -169,6 +169,35 @@ class CalibrationBuilderTest(tf.test.TestCase):
self.assertAllClose(calibrated_scores_np, [[[0.5, 0.6], [0.5, 0.3]],
[[0.5, 0.7], [0.5, 0.96]]])
def test_temperature_scaling(self):
"""Tests that calibration produces correct temperature scaling values."""
calibration_config = calibration_pb2.CalibrationConfig()
calibration_config.temperature_scaling_calibration.scaler = 2.0
od_graph = tf.Graph()
with self.test_session(graph=od_graph) as sess:
calibration_fn = calibration_builder.build(calibration_config)
# batch_size = 2, num_classes = 2, num_anchors = 2.
class_predictions_with_background = tf.constant(
[[[0.1, 0.2, 0.3], [0.4, 0.5, 0.0]],
[[0.6, 0.7, 0.8], [0.9, 1.0, 1.0]]],
dtype=tf.float32)
calibrated_scores = calibration_fn(class_predictions_with_background)
calibrated_scores_np = sess.run(calibrated_scores)
self.assertAllClose(calibrated_scores_np,
[[[0.05, 0.1, 0.15], [0.2, 0.25, 0.0]],
[[0.3, 0.35, 0.4], [0.45, 0.5, 0.5]]])
def test_temperature_scaling_incorrect_value_error(self):
calibration_config = calibration_pb2.CalibrationConfig()
calibration_config.temperature_scaling_calibration.scaler = 0
calibration_fn = calibration_builder.build(calibration_config)
class_predictions_with_background = tf.constant(
[[[0.1, 0.2, 0.3]]], dtype=tf.float32)
with self.assertRaises(ValueError):
calibration_fn(class_predictions_with_background)
def test_skips_class_when_calibration_parameters_not_present(self):
"""Tests that graph fails when parameters not present for all classes."""
# Only adding calibration parameters for class id = 0, even though class id
......
......@@ -49,8 +49,8 @@ def read_dataset(file_read_func, input_files, config):
"""Reads a dataset, and handles repetition and shuffling.
Args:
file_read_func: Function to use in tf.data.experimental.parallel_interleave,
to read every individual file into a tf.data.Dataset.
file_read_func: Function to use in tf.contrib.data.parallel_interleave, to
read every individual file into a tf.data.Dataset.
input_files: A list of file paths to read.
config: A input_reader_builder.InputReader object.
......@@ -79,7 +79,7 @@ def read_dataset(file_read_func, input_files, config):
'still slightly shuffled since `num_readers` > 1.')
filename_dataset = filename_dataset.repeat(config.num_epochs or None)
records_dataset = filename_dataset.apply(
tf.data.experimental.parallel_interleave(
tf.contrib.data.parallel_interleave(
file_read_func,
cycle_length=num_readers,
block_length=config.read_block_length,
......@@ -154,7 +154,8 @@ def build(input_reader_config, batch_size=None, transform_input_data_fn=None):
data_map_fn = dataset.map
dataset = data_map_fn(process_fn, num_parallel_calls=num_parallel_calls)
if batch_size:
dataset = dataset.batch(batch_size, drop_remainder=True)
dataset = dataset.apply(
tf.contrib.data.batch_and_drop_remainder(batch_size))
dataset = dataset.prefetch(input_reader_config.num_prefetch_batches)
return dataset
......
......@@ -39,6 +39,7 @@ from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_n
from object_detection.models import faster_rcnn_pnas_feature_extractor as frcnn_pnas
from object_detection.models import faster_rcnn_resnet_v1_feature_extractor as frcnn_resnet_v1
from object_detection.models import ssd_resnet_v1_fpn_feature_extractor as ssd_resnet_v1_fpn
from object_detection.models import ssd_resnet_v1_fpn_keras_feature_extractor as ssd_resnet_v1_fpn_keras
from object_detection.models import ssd_resnet_v1_ppn_feature_extractor as ssd_resnet_v1_ppn
from object_detection.models.embedded_ssd_mobilenet_v1_feature_extractor import EmbeddedSSDMobileNetV1FeatureExtractor
from object_detection.models.ssd_inception_v2_feature_extractor import SSDInceptionV2FeatureExtractor
......@@ -52,6 +53,8 @@ from object_detection.models.ssd_mobilenet_v2_feature_extractor import SSDMobile
from object_detection.models.ssd_mobilenet_v2_fpn_feature_extractor import SSDMobileNetV2FpnFeatureExtractor
from object_detection.models.ssd_mobilenet_v2_fpn_keras_feature_extractor import SSDMobileNetV2FpnKerasFeatureExtractor
from object_detection.models.ssd_mobilenet_v2_keras_feature_extractor import SSDMobileNetV2KerasFeatureExtractor
from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3LargeFeatureExtractor
from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3SmallFeatureExtractor
from object_detection.models.ssd_pnasnet_feature_extractor import SSDPNASNetFeatureExtractor
from object_detection.predictors import rfcn_box_predictor
from object_detection.predictors import rfcn_keras_box_predictor
......@@ -68,6 +71,8 @@ SSD_FEATURE_EXTRACTOR_CLASS_MAP = {
'ssd_mobilenet_v1_ppn': SSDMobileNetV1PpnFeatureExtractor,
'ssd_mobilenet_v2': SSDMobileNetV2FeatureExtractor,
'ssd_mobilenet_v2_fpn': SSDMobileNetV2FpnFeatureExtractor,
'ssd_mobilenet_v3_large': SSDMobileNetV3LargeFeatureExtractor,
'ssd_mobilenet_v3_small': SSDMobileNetV3SmallFeatureExtractor,
'ssd_resnet50_v1_fpn': ssd_resnet_v1_fpn.SSDResnet50V1FpnFeatureExtractor,
'ssd_resnet101_v1_fpn': ssd_resnet_v1_fpn.SSDResnet101V1FpnFeatureExtractor,
'ssd_resnet152_v1_fpn': ssd_resnet_v1_fpn.SSDResnet152V1FpnFeatureExtractor,
......@@ -85,6 +90,12 @@ SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = {
'ssd_mobilenet_v1_fpn_keras': SSDMobileNetV1FpnKerasFeatureExtractor,
'ssd_mobilenet_v2_keras': SSDMobileNetV2KerasFeatureExtractor,
'ssd_mobilenet_v2_fpn_keras': SSDMobileNetV2FpnKerasFeatureExtractor,
'ssd_resnet50_v1_fpn_keras':
ssd_resnet_v1_fpn_keras.SSDResNet50V1FpnKerasFeatureExtractor,
'ssd_resnet101_v1_fpn_keras':
ssd_resnet_v1_fpn_keras.SSDResNet101V1FpnKerasFeatureExtractor,
'ssd_resnet152_v1_fpn_keras':
ssd_resnet_v1_fpn_keras.SSDResNet152V1FpnKerasFeatureExtractor,
}
# A map of names to Faster R-CNN feature extractors.
......@@ -111,31 +122,6 @@ FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = {
}
def build(model_config, is_training, add_summaries=True):
"""Builds a DetectionModel based on the model config.
Args:
model_config: A model.proto object containing the config for the desired
DetectionModel.
is_training: True if this model is being built for training purposes.
add_summaries: Whether to add tensorflow summaries in the model graph.
Returns:
DetectionModel based on the config.
Raises:
ValueError: On invalid meta architecture or model.
"""
if not isinstance(model_config, model_pb2.DetectionModel):
raise ValueError('model_config not of type model_pb2.DetectionModel.')
meta_architecture = model_config.WhichOneof('model')
if meta_architecture == 'ssd':
return _build_ssd_model(model_config.ssd, is_training, add_summaries)
if meta_architecture == 'faster_rcnn':
return _build_faster_rcnn_model(model_config.faster_rcnn, is_training,
add_summaries)
raise ValueError('Unknown meta architecture: {}'.format(meta_architecture))
def _build_ssd_feature_extractor(feature_extractor_config,
is_training,
freeze_batchnorm,
......@@ -231,6 +217,7 @@ def _build_ssd_feature_extractor(feature_extractor_config,
feature_extractor_config.fpn.additional_layer_depth,
})
return feature_extractor_class(**kwargs)
......@@ -330,6 +317,8 @@ def _build_ssd_model(ssd_config, is_training, add_summaries):
use_confidences_as_targets=ssd_config.use_confidences_as_targets,
implicit_example_weight=ssd_config.implicit_example_weight,
equalization_loss_config=equalization_loss_config,
return_raw_detections_during_predict=(
ssd_config.return_raw_detections_during_predict),
**kwargs)
......@@ -485,6 +474,7 @@ def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries):
max_size_per_class=frcnn_config.first_stage_max_proposals,
max_total_size=frcnn_config.first_stage_max_proposals,
use_static_shapes=use_static_shapes,
use_partitioned_nms=frcnn_config.use_partitioned_nms_in_first_stage,
use_combined_nms=frcnn_config.use_combined_nms_in_first_stage)
first_stage_loc_loss_weight = (
frcnn_config.first_stage_localization_loss_weight)
......@@ -580,7 +570,9 @@ def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries):
'crop_and_resize_fn': crop_and_resize_fn,
'clip_anchors_to_image': clip_anchors_to_image,
'use_static_shapes': use_static_shapes,
'resize_masks': frcnn_config.resize_masks
'resize_masks': frcnn_config.resize_masks,
'return_raw_detections_during_predict': (
frcnn_config.return_raw_detections_during_predict)
}
if (isinstance(second_stage_box_predictor,
......@@ -599,3 +591,44 @@ def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries):
second_stage_mask_prediction_loss_weight=(
second_stage_mask_prediction_loss_weight),
**common_kwargs)
EXPERIMENTAL_META_ARCH_BUILDER_MAP = {
}
def _build_experimental_model(config, is_training, add_summaries=True):
return EXPERIMENTAL_META_ARCH_BUILDER_MAP[config.name](
is_training, add_summaries)
META_ARCHITECURE_BUILDER_MAP = {
'ssd': _build_ssd_model,
'faster_rcnn': _build_faster_rcnn_model,
'experimental_model': _build_experimental_model
}
def build(model_config, is_training, add_summaries=True):
"""Builds a DetectionModel based on the model config.
Args:
model_config: A model.proto object containing the config for the desired
DetectionModel.
is_training: True if this model is being built for training purposes.
add_summaries: Whether to add tensorflow summaries in the model graph.
Returns:
DetectionModel based on the config.
Raises:
ValueError: On invalid meta architecture or model.
"""
if not isinstance(model_config, model_pb2.DetectionModel):
raise ValueError('model_config not of type model_pb2.DetectionModel.')
meta_architecture = model_config.WhichOneof('model')
if meta_architecture not in META_ARCHITECURE_BUILDER_MAP:
raise ValueError('Unknown meta architecture: {}'.format(meta_architecture))
else:
build_func = META_ARCHITECURE_BUILDER_MAP[meta_architecture]
return build_func(getattr(model_config, meta_architecture), is_training,
add_summaries)
......@@ -327,6 +327,20 @@ class ModelBuilderTest(tf.test.TestCase, parameterized.TestCase):
'inplace batchnorm updates not supported'):
model_builder.build(model_proto, is_training=True)
def test_create_experimental_model(self):
model_text_proto = """
experimental_model {
name: 'model42'
}"""
build_func = lambda *args: 42
model_builder.EXPERIMENTAL_META_ARCH_BUILDER_MAP['model42'] = build_func
model_proto = model_pb2.DetectionModel()
text_format.Merge(model_text_proto, model_proto)
self.assertEqual(model_builder.build(model_proto, is_training=True), 42)
if __name__ == '__main__':
tf.test.main()
......@@ -89,8 +89,7 @@ def _build_non_max_suppressor(nms_config):
if nms_config.soft_nms_sigma < 0.0:
raise ValueError('soft_nms_sigma should be non-negative.')
if nms_config.use_combined_nms and nms_config.use_class_agnostic_nms:
raise ValueError('combined_nms does not support class_agnostic_nms')
raise ValueError('combined_nms does not support class_agnostic_nms.')
non_max_suppressor_fn = functools.partial(
post_processing.batch_multiclass_non_max_suppression,
score_thresh=nms_config.score_threshold,
......@@ -101,6 +100,7 @@ def _build_non_max_suppressor(nms_config):
use_class_agnostic_nms=nms_config.use_class_agnostic_nms,
max_classes_per_detection=nms_config.max_classes_per_detection,
soft_nms_sigma=nms_config.soft_nms_sigma,
use_partitioned_nms=nms_config.use_partitioned_nms,
use_combined_nms=nms_config.use_combined_nms)
return non_max_suppressor_fn
......@@ -143,8 +143,12 @@ def _build_score_converter(score_converter_config, logit_scale):
def _build_calibrated_score_converter(score_converter_fn, calibration_config):
"""Wraps a score_converter_fn, adding a calibration step.
Builds a score converter function witha calibration transformation according
to calibration_builder.py. Calibration applies positive monotonic
Builds a score converter function with a calibration transformation according
to calibration_builder.py. The score conversion function may be applied before
or after the calibration transformation, depending on the calibration method.
If the method is temperature scaling, the score conversion is
after the calibration transformation. Otherwise, the score conversion is
before the calibration transformation. Calibration applies positive monotonic
transformations to inputs (i.e. score ordering is strictly preserved or
adjacent scores are mapped to the same score). When calibration is
class-agnostic, the highest-scoring class remains unchanged, unless two
......@@ -162,8 +166,14 @@ def _build_calibrated_score_converter(score_converter_fn, calibration_config):
"""
calibration_fn = calibration_builder.build(calibration_config)
def calibrated_score_converter_fn(logits):
converted_logits = score_converter_fn(logits)
return calibration_fn(converted_logits)
if (calibration_config.WhichOneof('calibrator') ==
'temperature_scaling_calibration'):
calibrated_logits = calibration_fn(logits)
return score_converter_fn(calibrated_logits)
else:
converted_logits = score_converter_fn(logits)
return calibration_fn(converted_logits)
calibrated_score_converter_fn.__name__ = (
'calibrate_with_%s' % calibration_config.WhichOneof('calibrator'))
return calibrated_score_converter_fn
......@@ -160,6 +160,26 @@ class PostProcessingBuilderTest(tf.test.TestCase):
expected_calibrated_scores = sess.run(tf.constant([0.5, 0.5], tf.float32))
self.assertAllClose(calibrated_scores, expected_calibrated_scores)
def test_build_temperature_scaling_calibrator(self):
post_processing_text_proto = """
score_converter: SOFTMAX
calibration_config {
temperature_scaling_calibration {
scaler: 2.0
}}"""
post_processing_config = post_processing_pb2.PostProcessing()
text_format.Merge(post_processing_text_proto, post_processing_config)
_, calibrated_score_conversion_fn = post_processing_builder.build(
post_processing_config)
self.assertEqual(calibrated_score_conversion_fn.__name__,
'calibrate_with_temperature_scaling_calibration')
input_scores = tf.constant([1, 1], tf.float32)
outputs = calibrated_score_conversion_fn(input_scores)
with self.test_session() as sess:
calibrated_scores = sess.run(outputs)
expected_calibrated_scores = sess.run(tf.constant([0.5, 0.5], tf.float32))
self.assertAllClose(calibrated_scores, expected_calibrated_scores)
if __name__ == '__main__':
tf.test.main()
......@@ -95,6 +95,12 @@ PREPROCESSING_FUNCTION_MAP = {
preprocessor.random_crop_to_aspect_ratio,
'random_black_patches':
preprocessor.random_black_patches,
'random_jpeg_quality':
preprocessor.random_jpeg_quality,
'random_downscale_to_target_pixels':
preprocessor.random_downscale_to_target_pixels,
'random_patch_gaussian':
preprocessor.random_patch_gaussian,
'rgb_to_gray':
preprocessor.rgb_to_gray,
'scale_boxes_to_pixel_coordinates': (
......
......@@ -363,6 +363,62 @@ class PreprocessorBuilderTest(tf.test.TestCase):
'probability': 0.95,
'size_to_image_ratio': 0.12})
def test_build_random_jpeg_quality(self):
preprocessor_text_proto = """
random_jpeg_quality {
random_coef: 0.5
min_jpeg_quality: 40
max_jpeg_quality: 90
}
"""
preprocessor_proto = preprocessor_pb2.PreprocessingStep()
text_format.Parse(preprocessor_text_proto, preprocessor_proto)
function, args = preprocessor_builder.build(preprocessor_proto)
self.assertEqual(function, preprocessor.random_jpeg_quality)
self.assert_dictionary_close(args, {'random_coef': 0.5,
'min_jpeg_quality': 40,
'max_jpeg_quality': 90})
def test_build_random_downscale_to_target_pixels(self):
preprocessor_text_proto = """
random_downscale_to_target_pixels {
random_coef: 0.5
min_target_pixels: 200
max_target_pixels: 900
}
"""
preprocessor_proto = preprocessor_pb2.PreprocessingStep()
text_format.Parse(preprocessor_text_proto, preprocessor_proto)
function, args = preprocessor_builder.build(preprocessor_proto)
self.assertEqual(function, preprocessor.random_downscale_to_target_pixels)
self.assert_dictionary_close(args, {
'random_coef': 0.5,
'min_target_pixels': 200,
'max_target_pixels': 900
})
def test_build_random_patch_gaussian(self):
preprocessor_text_proto = """
random_patch_gaussian {
random_coef: 0.5
min_patch_size: 10
max_patch_size: 300
min_gaussian_stddev: 0.2
max_gaussian_stddev: 1.5
}
"""
preprocessor_proto = preprocessor_pb2.PreprocessingStep()
text_format.Parse(preprocessor_text_proto, preprocessor_proto)
function, args = preprocessor_builder.build(preprocessor_proto)
self.assertEqual(function, preprocessor.random_patch_gaussian)
self.assert_dictionary_close(args, {
'random_coef': 0.5,
'min_patch_size': 10,
'max_patch_size': 300,
'min_gaussian_stddev': 0.2,
'max_gaussian_stddev': 1.5
})
def test_auto_augment_image(self):
preprocessor_text_proto = """
autoaugment_image {
......
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""A function to build an object detection box coder from configuration."""
from object_detection.builders import box_coder_builder
from object_detection.builders import matcher_builder
from object_detection.builders import region_similarity_calculator_builder
from object_detection.core import target_assigner
def build(target_assigner_config):
"""Builds a TargetAssigner object based on the config.
Args:
target_assigner_config: A target_assigner proto message containing config
for the desired target assigner.
Returns:
TargetAssigner object based on the config.
"""
matcher_instance = matcher_builder.build(target_assigner_config.matcher)
similarity_calc_instance = region_similarity_calculator_builder.build(
target_assigner_config.similarity_calculator)
box_coder = box_coder_builder.build(target_assigner_config.box_coder)
return target_assigner.TargetAssigner(
matcher=matcher_instance,
similarity_calc=similarity_calc_instance,
box_coder_instance=box_coder)
"""Tests for google3.third_party.tensorflow_models.object_detection.builders.target_assigner_builder."""
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import tensorflow as tf
from google.protobuf import text_format
from object_detection.builders import target_assigner_builder
from object_detection.core import target_assigner
from object_detection.protos import target_assigner_pb2
class TargetAssignerBuilderTest(tf.test.TestCase):
def test_build_a_target_assigner(self):
target_assigner_text_proto = """
matcher {
argmax_matcher {matched_threshold: 0.5}
}
similarity_calculator {
iou_similarity {}
}
box_coder {
faster_rcnn_box_coder {}
}
"""
target_assigner_proto = target_assigner_pb2.TargetAssigner()
text_format.Merge(target_assigner_text_proto, target_assigner_proto)
target_assigner_instance = target_assigner_builder.build(
target_assigner_proto)
self.assertIsInstance(target_assigner_instance,
target_assigner.TargetAssigner)
if __name__ == '__main__':
tf.test.main()
......@@ -130,6 +130,23 @@ class AnchorGenerator(six.with_metaclass(ABCMeta, object)):
"""
pass
def anchor_index_to_feature_map_index(self, boxlist_list):
"""Returns a 1-D array of feature map indices for each anchor.
Args:
boxlist_list: a list of Boxlist, each holding a collection of N anchor
boxes. This list is produced in self.generate().
Returns:
A [num_anchors] integer array, where each element indicates which feature
map index the anchor belongs to.
"""
feature_map_indices_list = []
for i, boxes in enumerate(boxlist_list):
feature_map_indices_list.append(
i * tf.ones([boxes.num_boxes()], dtype=tf.int32))
return tf.concat(feature_map_indices_list, axis=0)
def _assert_correct_number_of_anchors(self, anchors_list,
feature_map_shape_list):
"""Assert that correct number of anchors was generated.
......
......@@ -87,7 +87,8 @@ class BatchMulticlassNonMaxSuppressionTest(test_case.TestCase,
iou = sess.run(iou)
self.assertAllClose(iou, expected_iou)
def test_batch_multiclass_nms_with_batch_size_2(self):
@parameterized.parameters(False, True)
def test_batch_multiclass_nms_with_batch_size_2(self, use_dynamic_map_fn):
boxes = tf.constant([[[[0, 0, 1, 1], [0, 0, 4, 5]],
[[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]],
[[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]],
......@@ -122,7 +123,8 @@ class BatchMulticlassNonMaxSuppressionTest(test_case.TestCase,
nmsed_additional_fields, num_detections
) = post_processing.batch_multiclass_non_max_suppression(
boxes, scores, score_thresh, iou_thresh,
max_size_per_class=max_output_size, max_total_size=max_output_size)
max_size_per_class=max_output_size, max_total_size=max_output_size,
use_dynamic_map_fn=use_dynamic_map_fn)
self.assertIsNone(nmsed_masks)
self.assertIsNone(nmsed_additional_fields)
......@@ -713,5 +715,7 @@ class BatchMulticlassNonMaxSuppressionTest(test_case.TestCase,
self.assertAllClose(nmsed_classes, exp_nms_classes)
self.assertListEqual(num_detections.tolist(), [3, 3])
# TODO(bhattad): Remove conditional after CMLE moves to TF 1.9
if __name__ == '__main__':
tf.test.main()
......@@ -53,7 +53,8 @@ class BoxList(object):
float32 format.
"""
if len(boxes.get_shape()) != 2 or boxes.get_shape()[-1] != 4:
raise ValueError('Invalid dimensions for box data.')
raise ValueError('Invalid dimensions for box data: {}'.format(
boxes.shape))
if boxes.dtype != tf.float32:
raise ValueError('Invalid tensor type: should be tf.float32')
self.data = {'boxes': boxes}
......
......@@ -393,6 +393,7 @@ def multiclass_non_max_suppression(boxes,
masks=None,
boundaries=None,
pad_to_max_output_size=False,
use_partitioned_nms=False,
additional_fields=None,
soft_nms_sigma=0.0,
scope=None):
......@@ -438,6 +439,8 @@ def multiclass_non_max_suppression(boxes,
depending on whether a separate boundary is predicted per class.
pad_to_max_output_size: If true, the output nmsed boxes are padded to be of
length `max_size_per_class`. Defaults to false.
use_partitioned_nms: If true, use partitioned version of
non_max_suppression.
additional_fields: (optional) If not None, a dictionary that maps keys to
tensors whose first dimensions are all of size `k`. After non-maximum
suppression, all tensors corresponding to the selected boxes will be
......@@ -506,15 +509,26 @@ def multiclass_non_max_suppression(boxes,
selected_scores = None
if pad_to_max_output_size:
max_selection_size = max_size_per_class
(selected_indices, num_valid_nms_boxes,
boxlist_and_class_scores.data['boxes'],
boxlist_and_class_scores.data['scores'],
_) = partitioned_non_max_suppression_padded(
boxlist_and_class_scores.get(),
boxlist_and_class_scores.get_field(fields.BoxListFields.scores),
max_selection_size,
iou_threshold=iou_thresh,
score_threshold=score_thresh)
if use_partitioned_nms:
(selected_indices, num_valid_nms_boxes,
boxlist_and_class_scores.data['boxes'],
boxlist_and_class_scores.data['scores'],
_) = partitioned_non_max_suppression_padded(
boxlist_and_class_scores.get(),
boxlist_and_class_scores.get_field(fields.BoxListFields.scores),
max_selection_size,
iou_threshold=iou_thresh,
score_threshold=score_thresh)
else:
selected_indices, num_valid_nms_boxes = (
tf.image.non_max_suppression_padded(
boxlist_and_class_scores.get(),
boxlist_and_class_scores.get_field(
fields.BoxListFields.scores),
max_selection_size,
iou_threshold=iou_thresh,
score_threshold=score_thresh,
pad_to_max_output_size=True))
nms_result = box_list_ops.gather(boxlist_and_class_scores,
selected_indices)
selected_scores = nms_result.get_field(fields.BoxListFields.scores)
......@@ -606,6 +620,7 @@ def class_agnostic_non_max_suppression(boxes,
masks=None,
boundaries=None,
pad_to_max_output_size=False,
use_partitioned_nms=False,
additional_fields=None,
soft_nms_sigma=0.0,
scope=None):
......@@ -653,6 +668,8 @@ def class_agnostic_non_max_suppression(boxes,
depending on whether a separate boundary is predicted per class.
pad_to_max_output_size: If true, the output nmsed boxes are padded to be of
length `max_size_per_class`. Defaults to false.
use_partitioned_nms: If true, use partitioned version of
non_max_suppression.
additional_fields: (optional) If not None, a dictionary that maps keys to
tensors whose first dimensions are all of size `k`. After non-maximum
suppression, all tensors corresponding to the selected boxes will be added
......@@ -719,19 +736,30 @@ def class_agnostic_non_max_suppression(boxes,
selected_scores = None
if pad_to_max_output_size:
max_selection_size = max_total_size
(selected_indices, num_valid_nms_boxes,
boxlist_and_class_scores.data['boxes'],
boxlist_and_class_scores.data['scores'],
argsort_ids) = partitioned_non_max_suppression_padded(
boxlist_and_class_scores.get(),
boxlist_and_class_scores.get_field(fields.BoxListFields.scores),
max_selection_size,
iou_threshold=iou_thresh,
score_threshold=score_thresh)
if use_partitioned_nms:
(selected_indices, num_valid_nms_boxes,
boxlist_and_class_scores.data['boxes'],
boxlist_and_class_scores.data['scores'],
argsort_ids) = partitioned_non_max_suppression_padded(
boxlist_and_class_scores.get(),
boxlist_and_class_scores.get_field(fields.BoxListFields.scores),
max_selection_size,
iou_threshold=iou_thresh,
score_threshold=score_thresh)
classes_with_max_scores = tf.gather(classes_with_max_scores,
argsort_ids)
else:
selected_indices, num_valid_nms_boxes = (
tf.image.non_max_suppression_padded(
boxlist_and_class_scores.get(),
boxlist_and_class_scores.get_field(fields.BoxListFields.scores),
max_selection_size,
iou_threshold=iou_thresh,
score_threshold=score_thresh,
pad_to_max_output_size=True))
nms_result = box_list_ops.gather(boxlist_and_class_scores,
selected_indices)
selected_scores = nms_result.get_field(fields.BoxListFields.scores)
classes_with_max_scores = tf.gather(classes_with_max_scores, argsort_ids)
else:
max_selection_size = tf.minimum(max_total_size,
boxlist_and_class_scores.num_boxes())
......@@ -779,6 +807,7 @@ def class_agnostic_non_max_suppression(boxes,
selected_scores, -1*tf.ones(max_selection_size)))
selected_classes = tf.gather(classes_with_max_scores, selected_indices)
selected_classes = tf.cast(selected_classes, tf.float32)
nms_result.add_field(fields.BoxListFields.classes, selected_classes)
selected_boxes = nms_result
sorted_boxes = box_list_ops.sort_by_field(selected_boxes,
......@@ -818,9 +847,11 @@ def batch_multiclass_non_max_suppression(boxes,
soft_nms_sigma=0.0,
scope=None,
use_static_shapes=False,
use_partitioned_nms=False,
parallel_iterations=32,
use_class_agnostic_nms=False,
max_classes_per_detection=1,
use_dynamic_map_fn=False,
use_combined_nms=False):
"""Multi-class version of non maximum suppression that operates on a batch.
......@@ -867,15 +898,18 @@ def batch_multiclass_non_max_suppression(boxes,
False.
scope: tf scope name.
use_static_shapes: If true, the output nmsed boxes are padded to be of
length `minimum(max_total_size, max_size_per_class*num_classes)`.
If false, they are padded to be of length `max_total_size`.
length `max_size_per_class` and it doesn't clip boxes to max_total_size.
Defaults to false.
use_partitioned_nms: If true, use partitioned version of
non_max_suppression.
parallel_iterations: (optional) number of batch items to process in
parallel.
use_class_agnostic_nms: If true, this uses class-agnostic non max
suppression
max_classes_per_detection: Maximum number of retained classes per detection
box in class-agnostic NMS.
use_dynamic_map_fn: If true, images in the batch will be processed within a
dynamic loop. Otherwise, a static loop will be used if possible.
use_combined_nms: If true, it uses tf.image.combined_non_max_suppression (
multi-class version of NMS that operates on a batch).
It greedily selects a subset of detection bounding boxes, pruning away
......@@ -1108,6 +1142,7 @@ def batch_multiclass_non_max_suppression(boxes,
change_coordinate_frame=change_coordinate_frame,
masks=per_image_masks,
pad_to_max_output_size=use_static_shapes,
use_partitioned_nms=use_partitioned_nms,
additional_fields=per_image_additional_fields,
soft_nms_sigma=soft_nms_sigma)
else:
......@@ -1122,6 +1157,7 @@ def batch_multiclass_non_max_suppression(boxes,
change_coordinate_frame=change_coordinate_frame,
masks=per_image_masks,
pad_to_max_output_size=use_static_shapes,
use_partitioned_nms=use_partitioned_nms,
additional_fields=per_image_additional_fields,
soft_nms_sigma=soft_nms_sigma)
......@@ -1147,7 +1183,12 @@ def batch_multiclass_non_max_suppression(boxes,
num_additional_fields = len(ordered_additional_fields)
num_nmsed_outputs = 4 + num_additional_fields
batch_outputs = shape_utils.static_or_dynamic_map_fn(
if use_dynamic_map_fn:
map_fn = tf.map_fn
else:
map_fn = shape_utils.static_or_dynamic_map_fn
batch_outputs = map_fn(
_single_image_nms_fn,
elems=([boxes, scores, masks, clip_window] +
list(ordered_additional_fields.values()) + [num_valid_boxes]),
......
......@@ -70,19 +70,20 @@ from __future__ import print_function
import functools
import inspect
import sys
import six
from six.moves import range
from six.moves import zip
import tensorflow as tf
from tensorflow.python.ops import control_flow_ops
from object_detection.core import box_list
from object_detection.core import box_list_ops
from object_detection.core import keypoint_ops
from object_detection.core import preprocessor_cache
from object_detection.core import standard_fields as fields
from object_detection.utils import autoaugment_utils
from object_detection.utils import patch_ops
from object_detection.utils import shape_utils
......@@ -2284,6 +2285,282 @@ def random_black_patches(image,
return image
def random_jpeg_quality(image,
min_jpeg_quality=0,
max_jpeg_quality=100,
random_coef=0.0,
seed=None,
preprocess_vars_cache=None):
"""Randomly encode the image to a random JPEG quality level.
Args:
image: rank 3 float32 tensor with shape [height, width, channels] and
values in the range [0, 255].
min_jpeg_quality: An int for the lower bound for selecting a random jpeg
quality level.
max_jpeg_quality: An int for the upper bound for selecting a random jpeg
quality level.
random_coef: a random coefficient that defines the chance of getting the
original image. If random_coef is 0, we will always get the encoded image,
and if it is 1.0, we will always get the original image.
seed: random seed.
preprocess_vars_cache: PreprocessorCache object that records previously
performed augmentations. Updated in-place. If this function is called
multiple times with the same non-null cache, it will perform
deterministically.
Returns:
image: image which is the same shape as input image.
"""
def _adjust_jpeg_quality():
"""Encodes the image as jpeg with a random quality and then decodes."""
generator_func = functools.partial(
tf.random_uniform, [],
minval=min_jpeg_quality,
maxval=max_jpeg_quality,
dtype=tf.int32,
seed=seed)
quality = _get_or_create_preprocess_rand_vars(
generator_func, preprocessor_cache.PreprocessorCache.JPEG_QUALITY,
preprocess_vars_cache, key='quality')
# Need to convert to uint8 before calling adjust_jpeg_quality since it
# assumes that float features are in the range [0, 1], where herein the
# range is [0, 255].
image_uint8 = tf.cast(image, tf.uint8)
adjusted_image = tf.image.adjust_jpeg_quality(image_uint8, quality)
return tf.cast(adjusted_image, tf.float32)
with tf.name_scope('RandomJpegQuality', values=[image]):
generator_func = functools.partial(tf.random_uniform, [], seed=seed)
do_encoding_random = _get_or_create_preprocess_rand_vars(
generator_func, preprocessor_cache.PreprocessorCache.JPEG_QUALITY,
preprocess_vars_cache)
do_encoding_random = tf.greater_equal(do_encoding_random, random_coef)
image = tf.cond(do_encoding_random, _adjust_jpeg_quality,
lambda: tf.cast(image, tf.float32))
return image
def random_downscale_to_target_pixels(image,
masks=None,
min_target_pixels=300000,
max_target_pixels=800000,
random_coef=0.0,
seed=None,
preprocess_vars_cache=None):
"""Randomly downscales the image to a target number of pixels.
If the image contains less than the chosen target number of pixels, it will
not be downscaled.
Args:
image: Rank 3 float32 tensor with shape [height, width, channels] and
values in the range [0, 255].
masks: (optional) Rank 3 float32 tensor with shape
[num_instances, height, width] containing instance masks. The masks are of
the same height, width as the input `image`.
min_target_pixels: Integer. An inclusive lower bound for for the target
number of pixels.
max_target_pixels: Integer. An exclusive upper bound for for the target
number of pixels.
random_coef: Float. Random coefficient that defines the chance of getting
the original image. If random_coef is 0, we will always apply downscaling,
and if it is 1.0, we will always get the original image.
seed: (optional) Integer. Random seed.
preprocess_vars_cache: (optional) PreprocessorCache object that records
previously performed augmentations. Updated in-place. If this function is
called multiple times with the same non-null cache, it will perform
deterministically.
Returns:
Tuple with elements:
image: Resized image which is the same rank as input image.
masks: If masks is not None, resized masks which are the same rank as
the input masks.
Raises:
ValueError: If min_target_pixels or max_target_pixels are not positive.
"""
if min_target_pixels <= 0:
raise ValueError('Minimum target pixels must be positive')
if max_target_pixels <= 0:
raise ValueError('Maximum target pixels must be positive')
def _resize_image_to_target(target_height, target_width):
# pylint: disable=unbalanced-tuple-unpacking
new_image, _ = resize_image(image, None, target_height, target_width)
return (new_image,)
def _resize_image_and_masks_to_target(target_height, target_width):
# pylint: disable=unbalanced-tuple-unpacking
new_image, new_masks, _ = resize_image(image, masks, target_height,
target_width)
return new_image, new_masks
with tf.name_scope('RandomDownscaleToTargetPixels', values=[image]):
generator_fn = functools.partial(tf.random_uniform, [], seed=seed)
do_downscale_random = _get_or_create_preprocess_rand_vars(
generator_fn,
preprocessor_cache.PreprocessorCache.DOWNSCALE_TO_TARGET_PIXELS,
preprocess_vars_cache)
do_downscale_random = tf.greater_equal(do_downscale_random, random_coef)
generator_fn = functools.partial(
tf.random_uniform, [],
minval=min_target_pixels,
maxval=max_target_pixels,
dtype=tf.int32,
seed=seed)
target_pixels = _get_or_create_preprocess_rand_vars(
generator_fn,
preprocessor_cache.PreprocessorCache.DOWNSCALE_TO_TARGET_PIXELS,
preprocess_vars_cache,
key='target_pixels')
image_shape = tf.shape(image)
image_height = image_shape[0]
image_width = image_shape[1]
image_pixels = image_height * image_width
scale_factor = tf.sqrt(
tf.cast(target_pixels, dtype=tf.float32) /
tf.cast(image_pixels, dtype=tf.float32))
target_height = tf.cast(
scale_factor * tf.cast(image_height, dtype=tf.float32), dtype=tf.int32)
target_width = tf.cast(
scale_factor * tf.cast(image_width, dtype=tf.float32), dtype=tf.int32)
image_larger_than_target = tf.greater(image_pixels, target_pixels)
should_apply_resize = tf.logical_and(do_downscale_random,
image_larger_than_target)
if masks is not None:
resize_fn = functools.partial(_resize_image_and_masks_to_target,
target_height, target_width)
return tf.cond(should_apply_resize, resize_fn,
lambda: (tf.cast(image, dtype=tf.float32), masks))
else:
resize_fn = lambda: _resize_image_to_target(target_height, target_width)
return tf.cond(should_apply_resize, resize_fn,
lambda: (tf.cast(image, dtype=tf.float32),))
def random_patch_gaussian(image,
min_patch_size=1,
max_patch_size=250,
min_gaussian_stddev=0.0,
max_gaussian_stddev=1.0,
random_coef=0.0,
seed=None,
preprocess_vars_cache=None):
"""Randomly applies gaussian noise to a random patch on the image.
The gaussian noise is applied to the image with values scaled to the range
[0.0, 1.0]. The result of applying gaussian noise to the scaled image is
clipped to be within the range [0.0, 1.0], equivalent to the range
[0.0, 255.0] after rescaling the image back.
See "Improving Robustness Without Sacrificing Accuracy with Patch Gaussian
Augmentation " by Lopes et al., 2019, for further details.
https://arxiv.org/abs/1906.02611
Args:
image: Rank 3 float32 tensor with shape [height, width, channels] and
values in the range [0.0, 255.0].
min_patch_size: Integer. An inclusive lower bound for the patch size.
max_patch_size: Integer. An exclusive upper bound for the patch size.
min_gaussian_stddev: Float. An inclusive lower bound for the standard
deviation of the gaussian noise.
max_gaussian_stddev: Float. An exclusive upper bound for the standard
deviation of the gaussian noise.
random_coef: Float. Random coefficient that defines the chance of getting
the original image. If random_coef is 0.0, we will always apply
downscaling, and if it is 1.0, we will always get the original image.
seed: (optional) Integer. Random seed.
preprocess_vars_cache: (optional) PreprocessorCache object that records
previously performed augmentations. Updated in-place. If this function is
called multiple times with the same non-null cache, it will perform
deterministically.
Returns:
Rank 3 float32 tensor with same shape as the input image and with gaussian
noise applied within a random patch.
Raises:
ValueError: If min_patch_size is < 1.
"""
if min_patch_size < 1:
raise ValueError('Minimum patch size must be >= 1.')
get_or_create_rand_vars_fn = functools.partial(
_get_or_create_preprocess_rand_vars,
function_id=preprocessor_cache.PreprocessorCache.PATCH_GAUSSIAN,
preprocess_vars_cache=preprocess_vars_cache)
def _apply_patch_gaussian(image):
"""Applies a patch gaussian with random size, location, and stddev."""
patch_size = get_or_create_rand_vars_fn(
functools.partial(
tf.random_uniform, [],
minval=min_patch_size,
maxval=max_patch_size,
dtype=tf.int32,
seed=seed),
key='patch_size')
gaussian_stddev = get_or_create_rand_vars_fn(
functools.partial(
tf.random_uniform, [],
minval=min_gaussian_stddev,
maxval=max_gaussian_stddev,
dtype=tf.float32,
seed=seed),
key='gaussian_stddev')
image_shape = tf.shape(image)
y = get_or_create_rand_vars_fn(
functools.partial(
tf.random_uniform, [],
minval=0,
maxval=image_shape[0],
dtype=tf.int32,
seed=seed),
key='y')
x = get_or_create_rand_vars_fn(
functools.partial(
tf.random_uniform, [],
minval=0,
maxval=image_shape[1],
dtype=tf.int32,
seed=seed),
key='x')
gaussian = get_or_create_rand_vars_fn(
functools.partial(
tf.random.normal,
image_shape,
stddev=gaussian_stddev,
dtype=tf.float32,
seed=seed),
key='gaussian')
scaled_image = image / 255.0
image_plus_gaussian = tf.clip_by_value(scaled_image + gaussian, 0.0, 1.0)
patch_mask = patch_ops.get_patch_mask(y, x, patch_size, image_shape)
patch_mask = tf.expand_dims(patch_mask, -1)
patch_mask = tf.tile(patch_mask, [1, 1, image_shape[2]])
patched_image = tf.where(patch_mask, image_plus_gaussian, scaled_image)
return patched_image * 255.0
with tf.name_scope('RandomPatchGaussian', values=[image]):
image = tf.cast(image, tf.float32)
patch_gaussian_random = get_or_create_rand_vars_fn(
functools.partial(tf.random_uniform, [], seed=seed))
do_patch_gaussian = tf.greater_equal(patch_gaussian_random, random_coef)
image = tf.cond(do_patch_gaussian,
lambda: _apply_patch_gaussian(image),
lambda: image)
return image
# TODO(barretzoph): Put in AutoAugment Paper link when paper is live.
def autoaugment_image(image, boxes, policy_name='v0'):
"""Apply an autoaugment policy to the image and boxes.
......@@ -3538,6 +3815,12 @@ def get_default_func_arg_map(include_label_weights=True,
groundtruth_keypoints,
),
random_black_patches: (fields.InputDataFields.image,),
random_jpeg_quality: (fields.InputDataFields.image,),
random_downscale_to_target_pixels: (
fields.InputDataFields.image,
groundtruth_instance_masks,
),
random_patch_gaussian: (fields.InputDataFields.image,),
autoaugment_image: (fields.InputDataFields.image,
fields.InputDataFields.groundtruth_boxes,),
retain_boxes_above_threshold: (
......
......@@ -54,15 +54,19 @@ class PreprocessorCache(object):
SELF_CONCAT_IMAGE = 'self_concat_image'
SSD_CROP_SELECTOR_ID = 'ssd_crop_selector_id'
SSD_CROP_PAD_SELECTOR_ID = 'ssd_crop_pad_selector_id'
JPEG_QUALITY = 'jpeg_quality'
DOWNSCALE_TO_TARGET_PIXELS = 'downscale_to_target_pixels'
PATCH_GAUSSIAN = 'patch_gaussian'
# 23 permitted function ids
# 27 permitted function ids
_VALID_FNS = [ROTATION90, HORIZONTAL_FLIP, VERTICAL_FLIP, PIXEL_VALUE_SCALE,
IMAGE_SCALE, RGB_TO_GRAY, ADJUST_BRIGHTNESS, ADJUST_CONTRAST,
ADJUST_HUE, ADJUST_SATURATION, DISTORT_COLOR, STRICT_CROP_IMAGE,
CROP_IMAGE, PAD_IMAGE, CROP_TO_ASPECT_RATIO, RESIZE_METHOD,
PAD_TO_ASPECT_RATIO, BLACK_PATCHES, ADD_BLACK_PATCH, SELECTOR,
SELECTOR_TUPLES, SELF_CONCAT_IMAGE, SSD_CROP_SELECTOR_ID,
SSD_CROP_PAD_SELECTOR_ID]
SSD_CROP_PAD_SELECTOR_ID, JPEG_QUALITY,
DOWNSCALE_TO_TARGET_PIXELS, PATCH_GAUSSIAN]
def __init__(self):
self._history = defaultdict(dict)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment