Skip to content

Commit 4503854

Browse files
fyangftensorflower-gardener
authored andcommitted
Add MEAN_RGB and STDDEV_RGB into preprocess_ops.py which will be the unified location for these constants. This improves consistency and reduces maintenance overhead.
PiperOrigin-RevId: 481246841
1 parent 7aa320c commit 4503854

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

official/vision/ops/preprocess_ops.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
CENTER_CROP_FRACTION = 0.875
2626

27+
# Calculated from the ImageNet training set
28+
MEAN_NORM = (0.485, 0.456, 0.406)
29+
STDDEV_NORM = (0.229, 0.224, 0.225)
30+
MEAN_RGB = tuple(255 * i for i in MEAN_NORM)
31+
STDDEV_RGB = tuple(255 * i for i in STDDEV_NORM)
32+
2733
# Alias for convenience. PLEASE use `box_ops.horizontal_flip_boxes` directly.
2834
horizontal_flip_boxes = box_ops.horizontal_flip_boxes
2935

@@ -66,19 +72,17 @@ def clip_or_pad_to_fixed_size(input_tensor, size, constant_values=0):
6672

6773

6874
def normalize_image(image: tf.Tensor,
69-
offset: Sequence[float] = (0.485, 0.456, 0.406),
70-
scale: Sequence[float] = (0.229, 0.224, 0.225)):
75+
offset: Sequence[float] = MEAN_NORM,
76+
scale: Sequence[float] = STDDEV_NORM):
7177
"""Normalizes the image to zero mean and unit variance."""
7278
with tf.name_scope('normalize_image'):
7379
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
7480
return normalize_scaled_float_image(image, offset, scale)
7581

7682

7783
def normalize_scaled_float_image(image: tf.Tensor,
78-
offset: Sequence[float] = (0.485, 0.456,
79-
0.406),
80-
scale: Sequence[float] = (0.229, 0.224,
81-
0.225)):
84+
offset: Sequence[float] = MEAN_NORM,
85+
scale: Sequence[float] = STDDEV_NORM):
8286
"""Normalizes a scaled float image to zero mean and unit variance.
8387
8488
It assumes the input image is float dtype with values in [0, 1).

0 commit comments

Comments
 (0)