Commit ec4b78f3 authored by A. Unique TensorFlower's avatar A. Unique TensorFlower Committed by TF Object Detection Team
Browse files

Add a preprocessing method to adjust gamma.

PiperOrigin-RevId: 342373365
parent e1d7dafb
......@@ -109,6 +109,8 @@ PREPROCESSING_FUNCTION_MAP = {
preprocessor.subtract_channel_mean,
'convert_class_logits_to_softmax':
preprocessor.convert_class_logits_to_softmax,
'adjust_gamma':
preprocessor.adjust_gamma,
}
......
......@@ -753,6 +753,19 @@ class PreprocessorBuilderTest(tf.test.TestCase):
'max_border': 128
})
def test_adjust_gamma(self):
preprocessor_text_proto = """
adjust_gamma {
gamma: 2.2
gain: 2.0
}
"""
preprocessor_proto = preprocessor_pb2.PreprocessingStep()
text_format.Parse(preprocessor_text_proto, preprocessor_proto)
function, args = preprocessor_builder.build(preprocessor_proto)
self.assertEqual(function, preprocessor.adjust_gamma)
self.assert_dictionary_close(args, {'gamma': 2.2, 'gain': 2.0})
if __name__ == '__main__':
tf.test.main()
......@@ -1049,6 +1049,22 @@ def random_rgb_to_gray(image,
return image
def adjust_gamma(image, gamma=1.0, gain=1.0):
"""Adjusts the gamma.
Args:
image: rank 3 float32 tensor contains 1 image -> [height, width, channels]
with pixel values varying between [0, 255].
gamma: the gamma value. Must be a non-negative real number.
gain: a constant multiplier.
Returns:
image: image which is the same shape as input image.
"""
with tf.name_scope('AdjustGamma', values=[image]):
return tf.image.adjust_gamma(image, gamma, gain)
def random_adjust_brightness(image,
max_delta=0.2,
seed=None,
......@@ -1069,7 +1085,6 @@ def random_adjust_brightness(image,
Returns:
image: image which is the same shape as input image.
boxes: boxes which is the same shape as input boxes.
"""
with tf.name_scope('RandomAdjustBrightness', values=[image]):
generator_func = functools.partial(tf.random_uniform, [],
......@@ -4504,6 +4519,7 @@ def get_default_func_arg_map(include_label_weights=True,
fields.InputDataFields.groundtruth_classes,
groundtruth_label_weights, groundtruth_instance_masks,
groundtruth_keypoints, groundtruth_label_confidences),
adjust_gamma: (fields.InputDataFields.image,),
}
return prep_func_arg_map
......
......@@ -3957,6 +3957,28 @@ class PreprocessorTest(test_case.TestCase, parameterized.TestCase):
self.assertLen(boxes, 2)
self.assertAllEqual(confidences, [-1.0, 1.0])
def testAdjustGamma(self):
def graph_fn():
preprocessing_options = []
preprocessing_options.append((preprocessor.normalize_image, {
'original_minval': 0,
'original_maxval': 255,
'target_minval': 0,
'target_maxval': 1
}))
preprocessing_options.append((preprocessor.adjust_gamma, {}))
images_original = self.createTestImages()
tensor_dict = {fields.InputDataFields.image: images_original}
tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options)
images_gamma = tensor_dict[fields.InputDataFields.image]
image_original_shape = tf.shape(images_original)
image_gamma_shape = tf.shape(images_gamma)
return [image_original_shape, image_gamma_shape]
(image_original_shape_, image_gamma_shape_) = self.execute_cpu(graph_fn, [])
self.assertAllEqual(image_original_shape_, image_gamma_shape_)
if __name__ == '__main__':
tf.test.main()
......@@ -4,7 +4,7 @@ package object_detection.protos;
// Message for defining a preprocessing operation on input data.
// See: //third_party/tensorflow_models/object_detection/core/preprocessor.py
// Next ID: 39
// Next ID: 40
message PreprocessingStep {
oneof preprocessing_step {
NormalizeImage normalize_image = 1;
......@@ -45,6 +45,7 @@ message PreprocessingStep {
RandomPatchGaussian random_patch_gaussian = 36;
RandomSquareCropByScale random_square_crop_by_scale = 37;
RandomScaleCropAndPadToSquare random_scale_crop_and_pad_to_square = 38;
AdjustGamma adjust_gamma = 39;
}
}
......@@ -590,3 +591,9 @@ message RandomScaleCropAndPadToSquare {
optional float scale_min = 2 [default=0.1];
optional float scale_max = 3 [default=2.0];
}
// Adjusts the gamma of the image using the specified gamma and gain values.
message AdjustGamma {
optional float gamma = 1 [default=1.0];
optional float gain = 2 [default=1.0];
}
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