def tf_random_aspect_resize(image, label, low_val=1.0, upper_val=1.5):
shape = tf.shape(image)
height = shape[0]
width = shape[1]
# 1~1.5
which_side = tf.to_float(tf.random_uniform([1]))[0]
multi_val = tf.to_float(tf.random_uniform([1]))[0] * (upper_val - low_val) + low_val
new_height = tf.cond(which_side > 0.5, lambda: tf.to_float(height), lambda: tf.to_float(height) * multi_val)
new_width = tf.cond(which_side <= 0.5, lambda: tf.to_float(width), lambda: tf.to_float(width) * multi_val)
new_height = tf.to_int32(new_height)
new_width = tf.to_int32(new_width)
image = tf.expand_dims(image, 0)
label = tf.expand_dims(label, 0)
resized_image = tf.image.resize_bilinear(image, [new_height, new_width], align_corners=False)
resized_image = tf.cast(resized_image, tf.uint8)
resized_label = tf.image.resize_nearest_neighbor(label, [new_height, new_width], align_corners=False)
resized_label = tf.cast(resized_label, tf.uint8)
resized_image = tf.squeeze(resized_image, 0)
resized_label = tf.squeeze(resized_label, 0)
return resized_image, resized_label
评论列表
文章目录