def preprocess_for_train(image,
output_height,
output_width,
mean_vals,
out_dim_scale=1.0):
"""Preprocesses the given image for training.
Note that the actual resizing scale is sampled from
[`resize_size_min`, `resize_size_max`].
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
Returns:
A preprocessed image.
"""
num_channels = image.get_shape().as_list()[-1]
image = tf.image.resize_images(image, [_RESIZE_HT, _RESIZE_WD])
# compute the crop size
base_size = float(min(_RESIZE_HT, _RESIZE_WD))
scale_ratio_h = tf.random_shuffle(tf.constant(_SCALE_RATIOS))[0]
scale_ratio_w = tf.random_shuffle(tf.constant(_SCALE_RATIOS))[0]
image = _random_crop([image],
tf.cast(output_height * scale_ratio_h, tf.int32),
tf.cast(output_width * scale_ratio_w, tf.int32))[0]
image = tf.image.resize_images(
image, [int(output_height * out_dim_scale),
int(output_width * out_dim_scale)])
image = tf.to_float(image)
image = tf.image.random_flip_left_right(image)
image.set_shape([int(output_height * out_dim_scale),
int(output_width * out_dim_scale), num_channels])
image = _mean_image_subtraction(image, mean_vals)
image = tf.expand_dims(image, 0) # 1x... image, to be consistent with eval
# Gets logged multiple times with NetVLAD, so gives an error.
# I'm anyway logging from the train code, so removing it here.
# tf.image_summary('final_distorted_image',
# tf.expand_dims(image / 128.0, 0))
return image
评论列表
文章目录