def random_crop(images, height, width):
"""Randomly crops an image/images to a given size.
Args:
images: 4-D Tensor of shape `[batch, height, width, channels]` or
3-D Tensor of shape `[height, width, channels]`.
height: `float`. The height to crop to.
width: `float`. The width to crop to.
Returns:
If `images` was 4-D, a 4-D float Tensor of shape
`[batch, new_height, new_width, channels]`.
If `images` was 3-D, a 3-D float Tensor of shape
`[new_height, new_width, channels]`.
"""
images_shape = get_shape(images)
if len(images_shape) > 4:
ValueError("'image' must have either 3 or 4 dimensions, "
"received `{}`.".format(images_shape))
if len(images_shape) == 4:
return tf.map_fn(lambda img: tf.random_crop(img, [height, width, images_shape[-1]]), images)
return tf.random_crop(images, [height, width, images_shape[-1]])
评论列表
文章目录