def random_crop_and_pad_image_and_labels(self, image, labels, size):
"""Randomly crops `image` together with `labels`.
Args:
image: A Tensor with shape [D_1, ..., D_K, N]
labels: A Tensor with shape [D_1, ..., D_K, M]
size: A Tensor with shape [K] indicating the crop size.
Returns:
A tuple of (cropped_image, cropped_label).
"""
combined = tf.concat([image, labels], axis=2)
image_shape = tf.shape(image)
combined_pad = tf.image.pad_to_bounding_box(
combined, 0, 0,
tf.maximum(size[0], image_shape[0]),
tf.maximum(size[1], image_shape[1]))
last_label_dim = tf.shape(labels)[-1]
last_image_dim = tf.shape(image)[-1]
combined_crop = tf.random_crop(
combined_pad,
size=tf.concat([size, [last_label_dim + last_image_dim]],
axis=0))
return (combined_crop[:, :, :last_image_dim],
combined_crop[:, :, last_image_dim:])
评论列表
文章目录